Fawkes API  Fawkes Development Version
KickerInterface.cpp
1 
2 /***************************************************************************
3  * KickerInterface.cpp - Fawkes BlackBoard Interface - KickerInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2007 Daniel Beck
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/KickerInterface.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 KickerInterface <interfaces/KickerInterface.h>
36  * KickerInterface Fawkes BlackBoard Interface.
37  *
38  In these variables it is stored how often the right, center or
39  left kick have been triggered.
40 
41  * @ingroup FawkesInterfaces
42  */
43 
44 
45 
46 /** Constructor */
47 KickerInterface::KickerInterface() : Interface()
48 {
49  data_size = sizeof(KickerInterface_data_t);
50  data_ptr = malloc(data_size);
51  data = (KickerInterface_data_t *)data_ptr;
52  data_ts = (interface_data_ts_t *)data_ptr;
53  memset(data_ptr, 0, data_size);
54  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
55  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
56  add_fieldinfo(IFT_INT32, "num_kicks_left", 1, &data->num_kicks_left);
57  add_fieldinfo(IFT_INT32, "num_kicks_center", 1, &data->num_kicks_center);
58  add_fieldinfo(IFT_INT32, "num_kicks_right", 1, &data->num_kicks_right);
59  add_fieldinfo(IFT_ENUM, "guide_ball_side", 1, &data->guide_ball_side, "GuideBallSideEnum", &enum_map_GuideBallSideEnum);
60  add_fieldinfo(IFT_UINT32, "current_intensity", 1, &data->current_intensity);
61  add_messageinfo("KickMessage");
62  add_messageinfo("ResetCounterMessage");
63  add_messageinfo("GuideBallMessage");
64  unsigned char tmp_hash[] = {0x96, 0x3d, 0x55, 0x60, 0xfd, 0x65, 0xf2, 0xfa, 0xa8, 0xfa, 0xfc, 0xaa, 0xb6, 0xfc, 0xc2, 0x81};
65  set_hash(tmp_hash);
66 }
67 
68 /** Destructor */
69 KickerInterface::~KickerInterface()
70 {
71  free(data_ptr);
72 }
73 /** Convert GuideBallSideEnum constant to string.
74  * @param value value to convert to string
75  * @return constant value as string.
76  */
77 const char *
79 {
80  switch (value) {
81  case GUIDE_BALL_LEFT: return "GUIDE_BALL_LEFT";
82  case GUIDE_BALL_RIGHT: return "GUIDE_BALL_RIGHT";
83  default: return "UNKNOWN";
84  }
85 }
86 /* Methods */
87 /** Get num_kicks_left value.
88  *
89  Number of Left-Kicks
90 
91  * @return num_kicks_left value
92  */
93 int32_t
95 {
96  return data->num_kicks_left;
97 }
98 
99 /** Get maximum length of num_kicks_left value.
100  * @return length of num_kicks_left value, can be length of the array or number of
101  * maximum number of characters for a string
102  */
103 size_t
105 {
106  return 1;
107 }
108 
109 /** Set num_kicks_left value.
110  *
111  Number of Left-Kicks
112 
113  * @param new_num_kicks_left new num_kicks_left value
114  */
115 void
116 KickerInterface::set_num_kicks_left(const int32_t new_num_kicks_left)
117 {
118  data->num_kicks_left = new_num_kicks_left;
119  data_changed = true;
120 }
121 
122 /** Get num_kicks_center value.
123  *
124  Number of Center-Kicks
125 
126  * @return num_kicks_center value
127  */
128 int32_t
130 {
131  return data->num_kicks_center;
132 }
133 
134 /** Get maximum length of num_kicks_center value.
135  * @return length of num_kicks_center value, can be length of the array or number of
136  * maximum number of characters for a string
137  */
138 size_t
140 {
141  return 1;
142 }
143 
144 /** Set num_kicks_center value.
145  *
146  Number of Center-Kicks
147 
148  * @param new_num_kicks_center new num_kicks_center value
149  */
150 void
151 KickerInterface::set_num_kicks_center(const int32_t new_num_kicks_center)
152 {
153  data->num_kicks_center = new_num_kicks_center;
154  data_changed = true;
155 }
156 
157 /** Get num_kicks_right value.
158  *
159  Number of Right-Kicks
160 
161  * @return num_kicks_right value
162  */
163 int32_t
165 {
166  return data->num_kicks_right;
167 }
168 
169 /** Get maximum length of num_kicks_right value.
170  * @return length of num_kicks_right value, can be length of the array or number of
171  * maximum number of characters for a string
172  */
173 size_t
175 {
176  return 1;
177 }
178 
179 /** Set num_kicks_right value.
180  *
181  Number of Right-Kicks
182 
183  * @param new_num_kicks_right new num_kicks_right value
184  */
185 void
186 KickerInterface::set_num_kicks_right(const int32_t new_num_kicks_right)
187 {
188  data->num_kicks_right = new_num_kicks_right;
189  data_changed = true;
190 }
191 
192 /** Get guide_ball_side value.
193  * Side where the ball
194  guidance arm is currently erected.
195  * @return guide_ball_side value
196  */
199 {
200  return (KickerInterface::GuideBallSideEnum)data->guide_ball_side;
201 }
202 
203 /** Get maximum length of guide_ball_side value.
204  * @return length of guide_ball_side value, can be length of the array or number of
205  * maximum number of characters for a string
206  */
207 size_t
209 {
210  return 1;
211 }
212 
213 /** Set guide_ball_side value.
214  * Side where the ball
215  guidance arm is currently erected.
216  * @param new_guide_ball_side new guide_ball_side value
217  */
218 void
220 {
221  data->guide_ball_side = new_guide_ball_side;
222  data_changed = true;
223 }
224 
225 /** Get current_intensity value.
226  *
227  The currently set intensity.
228 
229  * @return current_intensity value
230  */
231 uint32_t
233 {
234  return data->current_intensity;
235 }
236 
237 /** Get maximum length of current_intensity value.
238  * @return length of current_intensity value, can be length of the array or number of
239  * maximum number of characters for a string
240  */
241 size_t
243 {
244  return 1;
245 }
246 
247 /** Set current_intensity value.
248  *
249  The currently set intensity.
250 
251  * @param new_current_intensity new current_intensity value
252  */
253 void
254 KickerInterface::set_current_intensity(const uint32_t new_current_intensity)
255 {
256  data->current_intensity = new_current_intensity;
257  data_changed = true;
258 }
259 
260 /* =========== message create =========== */
261 Message *
263 {
264  if ( strncmp("KickMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
265  return new KickMessage();
266  } else if ( strncmp("ResetCounterMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
267  return new ResetCounterMessage();
268  } else if ( strncmp("GuideBallMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
269  return new GuideBallMessage();
270  } else {
271  throw UnknownTypeException("The given type '%s' does not match any known "
272  "message type for this interface type.", type);
273  }
274 }
275 
276 
277 /** Copy values from other interface.
278  * @param other other interface to copy values from
279  */
280 void
282 {
283  const KickerInterface *oi = dynamic_cast<const KickerInterface *>(other);
284  if (oi == NULL) {
285  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
286  type(), other->type());
287  }
288  memcpy(data, oi->data, sizeof(KickerInterface_data_t));
289 }
290 
291 const char *
292 KickerInterface::enum_tostring(const char *enumtype, int val) const
293 {
294  if (strcmp(enumtype, "GuideBallSideEnum") == 0) {
295  return tostring_GuideBallSideEnum((GuideBallSideEnum)val);
296  }
297  throw UnknownTypeException("Unknown enum type %s", enumtype);
298 }
299 
300 /* =========== messages =========== */
301 /** @class KickerInterface::KickMessage <interfaces/KickerInterface.h>
302  * KickMessage Fawkes BlackBoard Interface Message.
303  *
304 
305  */
306 
307 
308 /** Constructor with initial values.
309  * @param ini_left initial value for left
310  * @param ini_center initial value for center
311  * @param ini_right initial value for right
312  * @param ini_intensity initial value for intensity
313  */
314 KickerInterface::KickMessage::KickMessage(const bool ini_left, const bool ini_center, const bool ini_right, const uint32_t ini_intensity) : Message("KickMessage")
315 {
316  data_size = sizeof(KickMessage_data_t);
317  data_ptr = malloc(data_size);
318  memset(data_ptr, 0, data_size);
319  data = (KickMessage_data_t *)data_ptr;
321  data->left = ini_left;
322  data->center = ini_center;
323  data->right = ini_right;
324  data->intensity = ini_intensity;
325  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
326  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
327  add_fieldinfo(IFT_BOOL, "left", 1, &data->left);
328  add_fieldinfo(IFT_BOOL, "center", 1, &data->center);
329  add_fieldinfo(IFT_BOOL, "right", 1, &data->right);
330  add_fieldinfo(IFT_UINT32, "intensity", 1, &data->intensity);
331 }
332 /** Constructor */
334 {
335  data_size = sizeof(KickMessage_data_t);
336  data_ptr = malloc(data_size);
337  memset(data_ptr, 0, data_size);
338  data = (KickMessage_data_t *)data_ptr;
340  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
341  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
342  add_fieldinfo(IFT_BOOL, "left", 1, &data->left);
343  add_fieldinfo(IFT_BOOL, "center", 1, &data->center);
344  add_fieldinfo(IFT_BOOL, "right", 1, &data->right);
345  add_fieldinfo(IFT_UINT32, "intensity", 1, &data->intensity);
346 }
347 
348 /** Destructor */
350 {
351  free(data_ptr);
352 }
353 
354 /** Copy constructor.
355  * @param m message to copy from
356  */
358 {
359  data_size = m->data_size;
360  data_ptr = malloc(data_size);
361  memcpy(data_ptr, m->data_ptr, data_size);
362  data = (KickMessage_data_t *)data_ptr;
364 }
365 
366 /* Methods */
367 /** Get left value.
368  * True to kick with left kicker.
369  * @return left value
370  */
371 bool
373 {
374  return data->left;
375 }
376 
377 /** Get maximum length of left value.
378  * @return length of left value, can be length of the array or number of
379  * maximum number of characters for a string
380  */
381 size_t
383 {
384  return 1;
385 }
386 
387 /** Set left value.
388  * True to kick with left kicker.
389  * @param new_left new left value
390  */
391 void
393 {
394  data->left = new_left;
395 }
396 
397 /** Get center value.
398  * True to kick with central kicker.
399  * @return center value
400  */
401 bool
403 {
404  return data->center;
405 }
406 
407 /** Get maximum length of center value.
408  * @return length of center value, can be length of the array or number of
409  * maximum number of characters for a string
410  */
411 size_t
413 {
414  return 1;
415 }
416 
417 /** Set center value.
418  * True to kick with central kicker.
419  * @param new_center new center value
420  */
421 void
423 {
424  data->center = new_center;
425 }
426 
427 /** Get right value.
428  * True to kick with right kicker.
429  * @return right value
430  */
431 bool
433 {
434  return data->right;
435 }
436 
437 /** Get maximum length of right value.
438  * @return length of right value, can be length of the array or number of
439  * maximum number of characters for a string
440  */
441 size_t
443 {
444  return 1;
445 }
446 
447 /** Set right value.
448  * True to kick with right kicker.
449  * @param new_right new right value
450  */
451 void
453 {
454  data->right = new_right;
455 }
456 
457 /** Get intensity value.
458  * Intensity in the range [0..255].
459  * @return intensity value
460  */
461 uint32_t
463 {
464  return data->intensity;
465 }
466 
467 /** Get maximum length of intensity value.
468  * @return length of intensity value, can be length of the array or number of
469  * maximum number of characters for a string
470  */
471 size_t
473 {
474  return 1;
475 }
476 
477 /** Set intensity value.
478  * Intensity in the range [0..255].
479  * @param new_intensity new intensity value
480  */
481 void
482 KickerInterface::KickMessage::set_intensity(const uint32_t new_intensity)
483 {
484  data->intensity = new_intensity;
485 }
486 
487 /** Clone this message.
488  * Produces a message of the same type as this message and copies the
489  * data to the new message.
490  * @return clone of this message
491  */
492 Message *
494 {
495  return new KickerInterface::KickMessage(this);
496 }
497 /** @class KickerInterface::ResetCounterMessage <interfaces/KickerInterface.h>
498  * ResetCounterMessage Fawkes BlackBoard Interface Message.
499  *
500 
501  */
502 
503 
504 /** Constructor */
506 {
507  data_size = sizeof(ResetCounterMessage_data_t);
508  data_ptr = malloc(data_size);
509  memset(data_ptr, 0, data_size);
510  data = (ResetCounterMessage_data_t *)data_ptr;
512  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
513  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
514 }
515 
516 /** Destructor */
518 {
519  free(data_ptr);
520 }
521 
522 /** Copy constructor.
523  * @param m message to copy from
524  */
526 {
527  data_size = m->data_size;
528  data_ptr = malloc(data_size);
529  memcpy(data_ptr, m->data_ptr, data_size);
530  data = (ResetCounterMessage_data_t *)data_ptr;
532 }
533 
534 /* Methods */
535 /** Clone this message.
536  * Produces a message of the same type as this message and copies the
537  * data to the new message.
538  * @return clone of this message
539  */
540 Message *
542 {
543  return new KickerInterface::ResetCounterMessage(this);
544 }
545 /** @class KickerInterface::GuideBallMessage <interfaces/KickerInterface.h>
546  * GuideBallMessage Fawkes BlackBoard Interface Message.
547  *
548 
549  */
550 
551 
552 /** Constructor with initial values.
553  * @param ini_guide_ball_side initial value for guide_ball_side
554  */
555 KickerInterface::GuideBallMessage::GuideBallMessage(const GuideBallSideEnum ini_guide_ball_side) : Message("GuideBallMessage")
556 {
557  data_size = sizeof(GuideBallMessage_data_t);
558  data_ptr = malloc(data_size);
559  memset(data_ptr, 0, data_size);
560  data = (GuideBallMessage_data_t *)data_ptr;
562  data->guide_ball_side = ini_guide_ball_side;
563  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
564  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
565  add_fieldinfo(IFT_ENUM, "guide_ball_side", 1, &data->guide_ball_side, "GuideBallSideEnum", &enum_map_GuideBallSideEnum);
566 }
567 /** Constructor */
569 {
570  data_size = sizeof(GuideBallMessage_data_t);
571  data_ptr = malloc(data_size);
572  memset(data_ptr, 0, data_size);
573  data = (GuideBallMessage_data_t *)data_ptr;
575  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
576  enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
577  add_fieldinfo(IFT_ENUM, "guide_ball_side", 1, &data->guide_ball_side, "GuideBallSideEnum", &enum_map_GuideBallSideEnum);
578 }
579 
580 /** Destructor */
582 {
583  free(data_ptr);
584 }
585 
586 /** Copy constructor.
587  * @param m message to copy from
588  */
590 {
591  data_size = m->data_size;
592  data_ptr = malloc(data_size);
593  memcpy(data_ptr, m->data_ptr, data_size);
594  data = (GuideBallMessage_data_t *)data_ptr;
596 }
597 
598 /* Methods */
599 /** Get guide_ball_side value.
600  * Side where to guide the ball and erect the arm.
601  * @return guide_ball_side value
602  */
605 {
606  return (KickerInterface::GuideBallSideEnum)data->guide_ball_side;
607 }
608 
609 /** Get maximum length of guide_ball_side value.
610  * @return length of guide_ball_side value, can be length of the array or number of
611  * maximum number of characters for a string
612  */
613 size_t
615 {
616  return 1;
617 }
618 
619 /** Set guide_ball_side value.
620  * Side where to guide the ball and erect the arm.
621  * @param new_guide_ball_side new guide_ball_side value
622  */
623 void
625 {
626  data->guide_ball_side = new_guide_ball_side;
627 }
628 
629 /** Clone this message.
630  * Produces a message of the same type as this message and copies the
631  * data to the new message.
632  * @return clone of this message
633  */
634 Message *
636 {
637  return new KickerInterface::GuideBallMessage(this);
638 }
639 /** Check if message is valid and can be enqueued.
640  * @param message Message to check
641  * @return true if the message is valid, false otherwise.
642  */
643 bool
645 {
646  const KickMessage *m0 = dynamic_cast<const KickMessage *>(message);
647  if ( m0 != NULL ) {
648  return true;
649  }
650  const ResetCounterMessage *m1 = dynamic_cast<const ResetCounterMessage *>(message);
651  if ( m1 != NULL ) {
652  return true;
653  }
654  const GuideBallMessage *m2 = dynamic_cast<const GuideBallMessage *>(message);
655  if ( m2 != NULL ) {
656  return true;
657  }
658  return false;
659 }
660 
661 /// @cond INTERNALS
662 EXPORT_INTERFACE(KickerInterface)
663 /// @endcond
664 
665 
666 } // end namespace fawkes
const char * tostring_GuideBallSideEnum(GuideBallSideEnum value) const
Convert GuideBallSideEnum constant to string.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
virtual Message * clone() const
Clone this message.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
Fawkes library namespace.
void set_num_kicks_left(const int32_t new_num_kicks_left)
Set num_kicks_left value.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
unsigned int data_size
Minimal data size to hold data storage.
Definition: interface.h:221
void set_intensity(const uint32_t new_intensity)
Set intensity value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
void set_center(const bool new_center)
Set center value.
bool is_left() const
Get left value.
void set_guide_ball_side(const GuideBallSideEnum new_guide_ball_side)
Set guide_ball_side value.
GuideBallMessage Fawkes BlackBoard Interface Message.
void set_right(const bool new_right)
Set right value.
size_t maxlenof_intensity() const
Get maximum length of intensity value.
GuideBallSideEnum guide_ball_side() const
Get guide_ball_side value.
size_t maxlenof_left() const
Get maximum length of left value.
size_t maxlenof_right() const
Get maximum length of right value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
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 field info list.
Definition: interface.cpp:335
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
void set_current_intensity(const uint32_t new_current_intensity)
Set current_intensity value.
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:373
void set_num_kicks_right(const int32_t new_num_kicks_right)
Set num_kicks_right value.
void set_num_kicks_center(const int32_t new_num_kicks_center)
Set num_kicks_center value.
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
int32_t num_kicks_right() const
Get num_kicks_right value.
const char * type() const
Get type of interface.
Definition: interface.cpp:651
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:220
size_t maxlenof_num_kicks_right() const
Get maximum length of num_kicks_right value.
bool is_right() const
Get right value.
KickMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_guide_ball_side() const
Get maximum length of guide_ball_side value.
size_t maxlenof_num_kicks_center() const
Get maximum length of num_kicks_center value.
void set_left(const bool new_left)
Set left value.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
virtual Message * clone() const
Clone this message.
ResetCounterMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_current_intensity() const
Get maximum length of current_intensity value.
uint32_t intensity() const
Get intensity value.
KickerInterface Fawkes BlackBoard Interface.
Constant defining that the kicker shall activate the ball guidance device in such a way that the left...
size_t maxlenof_guide_ball_side() const
Get maximum length of guide_ball_side value.
GuideBallSideEnum guide_ball_side() const
Get guide_ball_side value.
virtual Message * clone() const
Clone this message.
32 bit integer field
Definition: types.h:41
GuideBallSideEnum
Enumeration defining on which side of the robot the ball shall be guided (and thus on which side the ...
bool is_center() const
Get center value.
virtual Message * create_message(const char *type) const
Create message based on type name.
size_t maxlenof_center() const
Get maximum length of center value.
virtual void copy_values(const Interface *other)
Copy values from other interface.
void set_guide_ball_side(const GuideBallSideEnum new_guide_ball_side)
Set guide_ball_side value.
interface_data_ts_t * data_ts
Pointer to data casted to timestamp struct.
Definition: interface.h:224
Constant defining that the kicker shall activate the ball guidance device in such a way that the righ...
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:436
boolean field
Definition: types.h:36
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
size_t maxlenof_num_kicks_left() const
Get maximum length of num_kicks_left value.
uint32_t current_intensity() const
Get current_intensity value.
int32_t num_kicks_left() const
Get num_kicks_left value.
32 bit unsigned integer field
Definition: types.h:42
field with interface specific enum type
Definition: types.h:49
int32_t num_kicks_center() const
Get num_kicks_center value.