Fawkes API  Fawkes Development Version
Laser360Interface.cpp
00001 
00002 /***************************************************************************
00003  *  Laser360Interface.cpp - Fawkes BlackBoard Interface - Laser360Interface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008-2009  Tim Niemueller
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <interfaces/Laser360Interface.h>
00025 
00026 #include <core/exceptions/software.h>
00027 
00028 #include <cstring>
00029 #include <cstdlib>
00030 
00031 namespace fawkes {
00032 
00033 /** @class Laser360Interface <interfaces/Laser360Interface.h>
00034  * Laser360Interface Fawkes BlackBoard Interface.
00035  * 
00036       This interface provides access to data of a laser scanner that produces
00037       360 beams per scan. The inter-beam distance is 1 deg, 0 deg is
00038       "forward", i.e. in the Fawkes coordinate system pointing towards
00039       the cartesian point (1,0). The direction in which the angle
00040       grows is indicated by the clockwise_angle field.
00041     
00042  * @ingroup FawkesInterfaces
00043  */
00044 
00045 
00046 
00047 /** Constructor */
00048 Laser360Interface::Laser360Interface() : Interface()
00049 {
00050   data_size = sizeof(Laser360Interface_data_t);
00051   data_ptr  = malloc(data_size);
00052   data      = (Laser360Interface_data_t *)data_ptr;
00053   data_ts   = (interface_data_ts_t *)data_ptr;
00054   memset(data_ptr, 0, data_size);
00055   add_fieldinfo(IFT_FLOAT, "distances", 360, &data->distances);
00056   add_fieldinfo(IFT_BOOL, "clockwise_angle", 1, &data->clockwise_angle);
00057   unsigned char tmp_hash[] = {0xf6, 0x3a, 0x26, 0x7b, 0x46, 0x96, 0x74, 0xad, 0x48, 0x1c, 0x32, 0x66, 0x2b, 0xfe, 0x41, 0x43};
00058   set_hash(tmp_hash);
00059 }
00060 
00061 /** Destructor */
00062 Laser360Interface::~Laser360Interface()
00063 {
00064   free(data_ptr);
00065 }
00066 /* Methods */
00067 /** Get distances value.
00068  * 
00069       The distances in meter of the beams.
00070     
00071  * @return distances value
00072  */
00073 float *
00074 Laser360Interface::distances() const
00075 {
00076   return data->distances;
00077 }
00078 
00079 /** Get distances value at given index.
00080  * 
00081       The distances in meter of the beams.
00082     
00083  * @param index index of value
00084  * @return distances value
00085  * @exception Exception thrown if index is out of bounds
00086  */
00087 float
00088 Laser360Interface::distances(unsigned int index) const
00089 {
00090   if (index > 360) {
00091     throw Exception("Index value %u out of bounds (0..360)", index);
00092   }
00093   return data->distances[index];
00094 }
00095 
00096 /** Get maximum length of distances value.
00097  * @return length of distances value, can be length of the array or number of 
00098  * maximum number of characters for a string
00099  */
00100 size_t
00101 Laser360Interface::maxlenof_distances() const
00102 {
00103   return 360;
00104 }
00105 
00106 /** Set distances value.
00107  * 
00108       The distances in meter of the beams.
00109     
00110  * @param new_distances new distances value
00111  */
00112 void
00113 Laser360Interface::set_distances(const float * new_distances)
00114 {
00115   memcpy(data->distances, new_distances, sizeof(float) * 360);
00116   data_changed = true;
00117 }
00118 
00119 /** Set distances value at given index.
00120  * 
00121       The distances in meter of the beams.
00122     
00123  * @param new_distances new distances value
00124  * @param index index for of the value
00125  */
00126 void
00127 Laser360Interface::set_distances(unsigned int index, const float new_distances)
00128 {
00129   if (index > 360) {
00130     throw Exception("Index value %u out of bounds (0..360)", index);
00131   }
00132   data->distances[index] = new_distances;
00133 }
00134 /** Get clockwise_angle value.
00135  * 
00136       True if the angle grows clockwise.
00137     
00138  * @return clockwise_angle value
00139  */
00140 bool
00141 Laser360Interface::is_clockwise_angle() const
00142 {
00143   return data->clockwise_angle;
00144 }
00145 
00146 /** Get maximum length of clockwise_angle value.
00147  * @return length of clockwise_angle value, can be length of the array or number of 
00148  * maximum number of characters for a string
00149  */
00150 size_t
00151 Laser360Interface::maxlenof_clockwise_angle() const
00152 {
00153   return 1;
00154 }
00155 
00156 /** Set clockwise_angle value.
00157  * 
00158       True if the angle grows clockwise.
00159     
00160  * @param new_clockwise_angle new clockwise_angle value
00161  */
00162 void
00163 Laser360Interface::set_clockwise_angle(const bool new_clockwise_angle)
00164 {
00165   data->clockwise_angle = new_clockwise_angle;
00166   data_changed = true;
00167 }
00168 
00169 /* =========== message create =========== */
00170 Message *
00171 Laser360Interface::create_message(const char *type) const
00172 {
00173   throw UnknownTypeException("The given type '%s' does not match any known "
00174                              "message type for this interface type.", type);
00175 }
00176 
00177 
00178 /** Copy values from other interface.
00179  * @param other other interface to copy values from
00180  */
00181 void
00182 Laser360Interface::copy_values(const Interface *other)
00183 {
00184   const Laser360Interface *oi = dynamic_cast<const Laser360Interface *>(other);
00185   if (oi == NULL) {
00186     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00187                                 type(), other->type());
00188   }
00189   memcpy(data, oi->data, sizeof(Laser360Interface_data_t));
00190 }
00191 
00192 const char *
00193 Laser360Interface::enum_tostring(const char *enumtype, int val) const
00194 {
00195   throw UnknownTypeException("Unknown enum type %s", enumtype);
00196 }
00197 
00198 /* =========== messages =========== */
00199 /** Check if message is valid and can be enqueued.
00200  * @param message Message to check
00201  * @return true if the message is valid, false otherwise.
00202  */
00203 bool
00204 Laser360Interface::message_valid(const Message *message) const
00205 {
00206   return false;
00207 }
00208 
00209 /// @cond INTERNALS
00210 EXPORT_INTERFACE(Laser360Interface)
00211 /// @endcond
00212 
00213 
00214 } // end namespace fawkes