Fawkes API  Fawkes Development Version
LaserClusterInterface.cpp
1 
2 /***************************************************************************
3  * LaserClusterInterface.cpp - Fawkes BlackBoard Interface - LaserClusterInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2013 Tim Niemueller
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/LaserClusterInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class LaserClusterInterface <interfaces/LaserClusterInterface.h>
36  * LaserClusterInterface Fawkes BlackBoard Interface.
37  * Laser cluster parameterization.
38  * @ingroup FawkesInterfaces
39  */
40 
41 
42 
43 /** Constructor */
44 LaserClusterInterface::LaserClusterInterface() : Interface()
45 {
46  data_size = sizeof(LaserClusterInterface_data_t);
47  data_ptr = malloc(data_size);
48  data = (LaserClusterInterface_data_t *)data_ptr;
49  data_ts = (interface_data_ts_t *)data_ptr;
50  memset(data_ptr, 0, data_size);
51  enum_map_SelectionMode[(int)SELMODE_MIN_ANGLE] = "SELMODE_MIN_ANGLE";
52  enum_map_SelectionMode[(int)SELMODE_MIN_DIST] = "SELMODE_MIN_DIST";
53  add_fieldinfo(IFT_FLOAT, "max_x", 1, &data->max_x);
54  add_fieldinfo(IFT_ENUM, "selection_mode", 1, &data->selection_mode, "SelectionMode", &enum_map_SelectionMode);
55  add_messageinfo("SetMaxXMessage");
56  add_messageinfo("SetSelectionModeMessage");
57  unsigned char tmp_hash[] = {0xad, 0xf8, 0x6e, 0xe7, 0x17, 0x56, 0x8a, 0xfb, 0xf9, 0xad, 0x3e, 0xba, 0xd, 0x15, 0xce, 0xde};
58  set_hash(tmp_hash);
59 }
60 
61 /** Destructor */
62 LaserClusterInterface::~LaserClusterInterface()
63 {
64  free(data_ptr);
65 }
66 /** Convert SelectionMode constant to string.
67  * @param value value to convert to string
68  * @return constant value as string.
69  */
70 const char *
71 LaserClusterInterface::tostring_SelectionMode(SelectionMode value) const
72 {
73  switch (value) {
74  case SELMODE_MIN_ANGLE: return "SELMODE_MIN_ANGLE";
75  case SELMODE_MIN_DIST: return "SELMODE_MIN_DIST";
76  default: return "UNKNOWN";
77  }
78 }
79 /* Methods */
80 /** Get max_x value.
81  * Maximum distance in X coordinate
82  of sensor frame.
83  * @return max_x value
84  */
85 float
86 LaserClusterInterface::max_x() const
87 {
88  return data->max_x;
89 }
90 
91 /** Get maximum length of max_x value.
92  * @return length of max_x value, can be length of the array or number of
93  * maximum number of characters for a string
94  */
95 size_t
96 LaserClusterInterface::maxlenof_max_x() const
97 {
98  return 1;
99 }
100 
101 /** Set max_x value.
102  * Maximum distance in X coordinate
103  of sensor frame.
104  * @param new_max_x new max_x value
105  */
106 void
107 LaserClusterInterface::set_max_x(const float new_max_x)
108 {
109  set_field(data->max_x, new_max_x);
110 }
111 
112 /** Get selection_mode value.
113  *
114  Current cluster selection mode.
115 
116  * @return selection_mode value
117  */
119 LaserClusterInterface::selection_mode() const
120 {
121  return (LaserClusterInterface::SelectionMode)data->selection_mode;
122 }
123 
124 /** Get maximum length of selection_mode value.
125  * @return length of selection_mode value, can be length of the array or number of
126  * maximum number of characters for a string
127  */
128 size_t
129 LaserClusterInterface::maxlenof_selection_mode() const
130 {
131  return 1;
132 }
133 
134 /** Set selection_mode value.
135  *
136  Current cluster selection mode.
137 
138  * @param new_selection_mode new selection_mode value
139  */
140 void
141 LaserClusterInterface::set_selection_mode(const SelectionMode new_selection_mode)
142 {
143  set_field(data->selection_mode, new_selection_mode);
144 }
145 
146 /* =========== message create =========== */
147 Message *
148 LaserClusterInterface::create_message(const char *type) const
149 {
150  if ( strncmp("SetMaxXMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
151  return new SetMaxXMessage();
152  } else if ( strncmp("SetSelectionModeMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
153  return new SetSelectionModeMessage();
154  } else {
155  throw UnknownTypeException("The given type '%s' does not match any known "
156  "message type for this interface type.", type);
157  }
158 }
159 
160 
161 /** Copy values from other interface.
162  * @param other other interface to copy values from
163  */
164 void
165 LaserClusterInterface::copy_values(const Interface *other)
166 {
167  const LaserClusterInterface *oi = dynamic_cast<const LaserClusterInterface *>(other);
168  if (oi == NULL) {
169  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
170  type(), other->type());
171  }
172  memcpy(data, oi->data, sizeof(LaserClusterInterface_data_t));
173 }
174 
175 const char *
176 LaserClusterInterface::enum_tostring(const char *enumtype, int val) const
177 {
178  if (strcmp(enumtype, "SelectionMode") == 0) {
179  return tostring_SelectionMode((SelectionMode)val);
180  }
181  throw UnknownTypeException("Unknown enum type %s", enumtype);
182 }
183 
184 /* =========== messages =========== */
185 /** @class LaserClusterInterface::SetMaxXMessage <interfaces/LaserClusterInterface.h>
186  * SetMaxXMessage Fawkes BlackBoard Interface Message.
187  *
188 
189  */
190 
191 
192 /** Constructor with initial values.
193  * @param ini_max_x initial value for max_x
194  */
195 LaserClusterInterface::SetMaxXMessage::SetMaxXMessage(const float ini_max_x) : Message("SetMaxXMessage")
196 {
197  data_size = sizeof(SetMaxXMessage_data_t);
198  data_ptr = malloc(data_size);
199  memset(data_ptr, 0, data_size);
200  data = (SetMaxXMessage_data_t *)data_ptr;
202  data->max_x = ini_max_x;
203  enum_map_SelectionMode[(int)SELMODE_MIN_ANGLE] = "SELMODE_MIN_ANGLE";
204  enum_map_SelectionMode[(int)SELMODE_MIN_DIST] = "SELMODE_MIN_DIST";
205  add_fieldinfo(IFT_FLOAT, "max_x", 1, &data->max_x);
206 }
207 /** Constructor */
209 {
210  data_size = sizeof(SetMaxXMessage_data_t);
211  data_ptr = malloc(data_size);
212  memset(data_ptr, 0, data_size);
213  data = (SetMaxXMessage_data_t *)data_ptr;
215  enum_map_SelectionMode[(int)SELMODE_MIN_ANGLE] = "SELMODE_MIN_ANGLE";
216  enum_map_SelectionMode[(int)SELMODE_MIN_DIST] = "SELMODE_MIN_DIST";
217  add_fieldinfo(IFT_FLOAT, "max_x", 1, &data->max_x);
218 }
219 
220 /** Destructor */
222 {
223  free(data_ptr);
224 }
225 
226 /** Copy constructor.
227  * @param m message to copy from
228  */
230 {
231  data_size = m->data_size;
232  data_ptr = malloc(data_size);
233  memcpy(data_ptr, m->data_ptr, data_size);
234  data = (SetMaxXMessage_data_t *)data_ptr;
236 }
237 
238 /* Methods */
239 /** Get max_x value.
240  * Maximum distance in X coordinate
241  of sensor frame.
242  * @return max_x value
243  */
244 float
246 {
247  return data->max_x;
248 }
249 
250 /** Get maximum length of max_x value.
251  * @return length of max_x value, can be length of the array or number of
252  * maximum number of characters for a string
253  */
254 size_t
256 {
257  return 1;
258 }
259 
260 /** Set max_x value.
261  * Maximum distance in X coordinate
262  of sensor frame.
263  * @param new_max_x new max_x value
264  */
265 void
267 {
268  set_field(data->max_x, new_max_x);
269 }
270 
271 /** Clone this message.
272  * Produces a message of the same type as this message and copies the
273  * data to the new message.
274  * @return clone of this message
275  */
276 Message *
278 {
279  return new LaserClusterInterface::SetMaxXMessage(this);
280 }
281 /** @class LaserClusterInterface::SetSelectionModeMessage <interfaces/LaserClusterInterface.h>
282  * SetSelectionModeMessage Fawkes BlackBoard Interface Message.
283  *
284 
285  */
286 
287 
288 /** Constructor with initial values.
289  * @param ini_selection_mode initial value for selection_mode
290  */
292 {
293  data_size = sizeof(SetSelectionModeMessage_data_t);
294  data_ptr = malloc(data_size);
295  memset(data_ptr, 0, data_size);
296  data = (SetSelectionModeMessage_data_t *)data_ptr;
298  data->selection_mode = ini_selection_mode;
299  enum_map_SelectionMode[(int)SELMODE_MIN_ANGLE] = "SELMODE_MIN_ANGLE";
300  enum_map_SelectionMode[(int)SELMODE_MIN_DIST] = "SELMODE_MIN_DIST";
301  add_fieldinfo(IFT_ENUM, "selection_mode", 1, &data->selection_mode, "SelectionMode", &enum_map_SelectionMode);
302 }
303 /** Constructor */
305 {
306  data_size = sizeof(SetSelectionModeMessage_data_t);
307  data_ptr = malloc(data_size);
308  memset(data_ptr, 0, data_size);
309  data = (SetSelectionModeMessage_data_t *)data_ptr;
311  enum_map_SelectionMode[(int)SELMODE_MIN_ANGLE] = "SELMODE_MIN_ANGLE";
312  enum_map_SelectionMode[(int)SELMODE_MIN_DIST] = "SELMODE_MIN_DIST";
313  add_fieldinfo(IFT_ENUM, "selection_mode", 1, &data->selection_mode, "SelectionMode", &enum_map_SelectionMode);
314 }
315 
316 /** Destructor */
318 {
319  free(data_ptr);
320 }
321 
322 /** Copy constructor.
323  * @param m message to copy from
324  */
326 {
327  data_size = m->data_size;
328  data_ptr = malloc(data_size);
329  memcpy(data_ptr, m->data_ptr, data_size);
330  data = (SetSelectionModeMessage_data_t *)data_ptr;
332 }
333 
334 /* Methods */
335 /** Get selection_mode value.
336  *
337  Current cluster selection mode.
338 
339  * @return selection_mode value
340  */
343 {
344  return (LaserClusterInterface::SelectionMode)data->selection_mode;
345 }
346 
347 /** Get maximum length of selection_mode value.
348  * @return length of selection_mode value, can be length of the array or number of
349  * maximum number of characters for a string
350  */
351 size_t
353 {
354  return 1;
355 }
356 
357 /** Set selection_mode value.
358  *
359  Current cluster selection mode.
360 
361  * @param new_selection_mode new selection_mode value
362  */
363 void
365 {
366  set_field(data->selection_mode, new_selection_mode);
367 }
368 
369 /** Clone this message.
370  * Produces a message of the same type as this message and copies the
371  * data to the new message.
372  * @return clone of this message
373  */
374 Message *
376 {
378 }
379 /** Check if message is valid and can be enqueued.
380  * @param message Message to check
381  * @return true if the message is valid, false otherwise.
382  */
383 bool
385 {
386  const SetMaxXMessage *m0 = dynamic_cast<const SetMaxXMessage *>(message);
387  if ( m0 != NULL ) {
388  return true;
389  }
390  const SetSelectionModeMessage *m1 = dynamic_cast<const SetSelectionModeMessage *>(message);
391  if ( m1 != NULL ) {
392  return true;
393  }
394  return false;
395 }
396 
397 /// @cond INTERNALS
398 EXPORT_INTERFACE(LaserClusterInterface)
399 /// @endcond
400 
401 
402 } // end namespace fawkes
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * type() const
Get type of interface.
Definition: interface.cpp:652
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:244
void set_field(FieldT &field, DataT &data)
Set a field, set data_changed to true and update data_changed accordingly.
Definition: interface.h:304
SetMaxXMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_max_x() const
Get maximum length of max_x value.
void set_max_x(const float new_max_x)
Set max_x value.
virtual Message * clone() const
Clone this message.
SetSelectionModeMessage Fawkes BlackBoard Interface Message.
void set_selection_mode(const SelectionMode new_selection_mode)
Set selection_mode value.
virtual Message * clone() const
Clone this message.
SelectionMode selection_mode() const
Get selection_mode value.
size_t maxlenof_selection_mode() const
Get maximum length of selection_mode value.
LaserClusterInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
SelectionMode
Enumeration defining the possible cluster selection modes.
@ SELMODE_MIN_ANGLE
Choose the cluster with the minimum angle difference from 0 degrees.
@ SELMODE_MIN_DIST
Choose the cluster with the minimum distance in X direction of the reference frame (typically forward...
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:435
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:146
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:156
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:147
Fawkes library namespace.
@ IFT_FLOAT
float field
Definition: types.h:46
@ IFT_ENUM
field with interface specific enum type
Definition: types.h:50
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152