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
33namespace 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 */
47KickerInterface::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 */
69KickerInterface::~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 */
77const char *
78KickerInterface::tostring_GuideBallSideEnum(GuideBallSideEnum value) const
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 */
93int32_t
94KickerInterface::num_kicks_left() const
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 */
103size_t
104KickerInterface::maxlenof_num_kicks_left() const
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 */
115void
116KickerInterface::set_num_kicks_left(const int32_t new_num_kicks_left)
117{
118 set_field(data->num_kicks_left, new_num_kicks_left);
119}
120
121/** Get num_kicks_center value.
122 *
123 Number of Center-Kicks
124
125 * @return num_kicks_center value
126 */
127int32_t
128KickerInterface::num_kicks_center() const
129{
130 return data->num_kicks_center;
131}
132
133/** Get maximum length of num_kicks_center value.
134 * @return length of num_kicks_center value, can be length of the array or number of
135 * maximum number of characters for a string
136 */
137size_t
138KickerInterface::maxlenof_num_kicks_center() const
139{
140 return 1;
141}
142
143/** Set num_kicks_center value.
144 *
145 Number of Center-Kicks
146
147 * @param new_num_kicks_center new num_kicks_center value
148 */
149void
150KickerInterface::set_num_kicks_center(const int32_t new_num_kicks_center)
151{
152 set_field(data->num_kicks_center, new_num_kicks_center);
153}
154
155/** Get num_kicks_right value.
156 *
157 Number of Right-Kicks
158
159 * @return num_kicks_right value
160 */
161int32_t
162KickerInterface::num_kicks_right() const
163{
164 return data->num_kicks_right;
165}
166
167/** Get maximum length of num_kicks_right value.
168 * @return length of num_kicks_right value, can be length of the array or number of
169 * maximum number of characters for a string
170 */
171size_t
172KickerInterface::maxlenof_num_kicks_right() const
173{
174 return 1;
175}
176
177/** Set num_kicks_right value.
178 *
179 Number of Right-Kicks
180
181 * @param new_num_kicks_right new num_kicks_right value
182 */
183void
184KickerInterface::set_num_kicks_right(const int32_t new_num_kicks_right)
185{
186 set_field(data->num_kicks_right, new_num_kicks_right);
187}
188
189/** Get guide_ball_side value.
190 * Side where the ball
191 guidance arm is currently erected.
192 * @return guide_ball_side value
193 */
195KickerInterface::guide_ball_side() const
196{
197 return (KickerInterface::GuideBallSideEnum)data->guide_ball_side;
198}
199
200/** Get maximum length of guide_ball_side value.
201 * @return length of guide_ball_side value, can be length of the array or number of
202 * maximum number of characters for a string
203 */
204size_t
205KickerInterface::maxlenof_guide_ball_side() const
206{
207 return 1;
208}
209
210/** Set guide_ball_side value.
211 * Side where the ball
212 guidance arm is currently erected.
213 * @param new_guide_ball_side new guide_ball_side value
214 */
215void
216KickerInterface::set_guide_ball_side(const GuideBallSideEnum new_guide_ball_side)
217{
218 set_field(data->guide_ball_side, new_guide_ball_side);
219}
220
221/** Get current_intensity value.
222 *
223 The currently set intensity.
224
225 * @return current_intensity value
226 */
227uint32_t
228KickerInterface::current_intensity() const
229{
230 return data->current_intensity;
231}
232
233/** Get maximum length of current_intensity value.
234 * @return length of current_intensity value, can be length of the array or number of
235 * maximum number of characters for a string
236 */
237size_t
238KickerInterface::maxlenof_current_intensity() const
239{
240 return 1;
241}
242
243/** Set current_intensity value.
244 *
245 The currently set intensity.
246
247 * @param new_current_intensity new current_intensity value
248 */
249void
250KickerInterface::set_current_intensity(const uint32_t new_current_intensity)
251{
252 set_field(data->current_intensity, new_current_intensity);
253}
254
255/* =========== message create =========== */
256Message *
257KickerInterface::create_message(const char *type) const
258{
259 if ( strncmp("KickMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
260 return new KickMessage();
261 } else if ( strncmp("ResetCounterMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
262 return new ResetCounterMessage();
263 } else if ( strncmp("GuideBallMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
264 return new GuideBallMessage();
265 } else {
266 throw UnknownTypeException("The given type '%s' does not match any known "
267 "message type for this interface type.", type);
268 }
269}
270
271
272/** Copy values from other interface.
273 * @param other other interface to copy values from
274 */
275void
276KickerInterface::copy_values(const Interface *other)
277{
278 const KickerInterface *oi = dynamic_cast<const KickerInterface *>(other);
279 if (oi == NULL) {
280 throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
281 type(), other->type());
282 }
283 memcpy(data, oi->data, sizeof(KickerInterface_data_t));
284}
285
286const char *
287KickerInterface::enum_tostring(const char *enumtype, int val) const
288{
289 if (strcmp(enumtype, "GuideBallSideEnum") == 0) {
290 return tostring_GuideBallSideEnum((GuideBallSideEnum)val);
291 }
292 throw UnknownTypeException("Unknown enum type %s", enumtype);
293}
294
295/* =========== messages =========== */
296/** @class KickerInterface::KickMessage <interfaces/KickerInterface.h>
297 * KickMessage Fawkes BlackBoard Interface Message.
298 *
299
300 */
301
302
303/** Constructor with initial values.
304 * @param ini_left initial value for left
305 * @param ini_center initial value for center
306 * @param ini_right initial value for right
307 * @param ini_intensity initial value for intensity
308 */
309KickerInterface::KickMessage::KickMessage(const bool ini_left, const bool ini_center, const bool ini_right, const uint32_t ini_intensity) : Message("KickMessage")
310{
311 data_size = sizeof(KickMessage_data_t);
312 data_ptr = malloc(data_size);
313 memset(data_ptr, 0, data_size);
314 data = (KickMessage_data_t *)data_ptr;
316 data->left = ini_left;
317 data->center = ini_center;
318 data->right = ini_right;
319 data->intensity = ini_intensity;
320 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
321 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
322 add_fieldinfo(IFT_BOOL, "left", 1, &data->left);
323 add_fieldinfo(IFT_BOOL, "center", 1, &data->center);
324 add_fieldinfo(IFT_BOOL, "right", 1, &data->right);
325 add_fieldinfo(IFT_UINT32, "intensity", 1, &data->intensity);
326}
327/** Constructor */
329{
330 data_size = sizeof(KickMessage_data_t);
331 data_ptr = malloc(data_size);
332 memset(data_ptr, 0, data_size);
333 data = (KickMessage_data_t *)data_ptr;
335 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
336 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
337 add_fieldinfo(IFT_BOOL, "left", 1, &data->left);
338 add_fieldinfo(IFT_BOOL, "center", 1, &data->center);
339 add_fieldinfo(IFT_BOOL, "right", 1, &data->right);
340 add_fieldinfo(IFT_UINT32, "intensity", 1, &data->intensity);
341}
342
343/** Destructor */
345{
346 free(data_ptr);
347}
348
349/** Copy constructor.
350 * @param m message to copy from
351 */
353{
354 data_size = m->data_size;
355 data_ptr = malloc(data_size);
356 memcpy(data_ptr, m->data_ptr, data_size);
357 data = (KickMessage_data_t *)data_ptr;
359}
360
361/* Methods */
362/** Get left value.
363 * True to kick with left kicker.
364 * @return left value
365 */
366bool
368{
369 return data->left;
370}
371
372/** Get maximum length of left value.
373 * @return length of left value, can be length of the array or number of
374 * maximum number of characters for a string
375 */
376size_t
378{
379 return 1;
380}
381
382/** Set left value.
383 * True to kick with left kicker.
384 * @param new_left new left value
385 */
386void
388{
389 set_field(data->left, new_left);
390}
391
392/** Get center value.
393 * True to kick with central kicker.
394 * @return center value
395 */
396bool
398{
399 return data->center;
400}
401
402/** Get maximum length of center value.
403 * @return length of center value, can be length of the array or number of
404 * maximum number of characters for a string
405 */
406size_t
408{
409 return 1;
410}
411
412/** Set center value.
413 * True to kick with central kicker.
414 * @param new_center new center value
415 */
416void
418{
419 set_field(data->center, new_center);
420}
421
422/** Get right value.
423 * True to kick with right kicker.
424 * @return right value
425 */
426bool
428{
429 return data->right;
430}
431
432/** Get maximum length of right value.
433 * @return length of right value, can be length of the array or number of
434 * maximum number of characters for a string
435 */
436size_t
438{
439 return 1;
440}
441
442/** Set right value.
443 * True to kick with right kicker.
444 * @param new_right new right value
445 */
446void
448{
449 set_field(data->right, new_right);
450}
451
452/** Get intensity value.
453 * Intensity in the range [0..255].
454 * @return intensity value
455 */
456uint32_t
458{
459 return data->intensity;
460}
461
462/** Get maximum length of intensity value.
463 * @return length of intensity value, can be length of the array or number of
464 * maximum number of characters for a string
465 */
466size_t
468{
469 return 1;
470}
471
472/** Set intensity value.
473 * Intensity in the range [0..255].
474 * @param new_intensity new intensity value
475 */
476void
478{
479 set_field(data->intensity, new_intensity);
480}
481
482/** Clone this message.
483 * Produces a message of the same type as this message and copies the
484 * data to the new message.
485 * @return clone of this message
486 */
487Message *
489{
490 return new KickerInterface::KickMessage(this);
491}
492/** @class KickerInterface::ResetCounterMessage <interfaces/KickerInterface.h>
493 * ResetCounterMessage Fawkes BlackBoard Interface Message.
494 *
495
496 */
497
498
499/** Constructor */
501{
502 data_size = sizeof(ResetCounterMessage_data_t);
503 data_ptr = malloc(data_size);
504 memset(data_ptr, 0, data_size);
505 data = (ResetCounterMessage_data_t *)data_ptr;
507 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
508 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
509}
510
511/** Destructor */
513{
514 free(data_ptr);
515}
516
517/** Copy constructor.
518 * @param m message to copy from
519 */
521{
522 data_size = m->data_size;
523 data_ptr = malloc(data_size);
524 memcpy(data_ptr, m->data_ptr, data_size);
525 data = (ResetCounterMessage_data_t *)data_ptr;
527}
528
529/* Methods */
530/** Clone this message.
531 * Produces a message of the same type as this message and copies the
532 * data to the new message.
533 * @return clone of this message
534 */
535Message *
537{
539}
540/** @class KickerInterface::GuideBallMessage <interfaces/KickerInterface.h>
541 * GuideBallMessage Fawkes BlackBoard Interface Message.
542 *
543
544 */
545
546
547/** Constructor with initial values.
548 * @param ini_guide_ball_side initial value for guide_ball_side
549 */
551{
552 data_size = sizeof(GuideBallMessage_data_t);
553 data_ptr = malloc(data_size);
554 memset(data_ptr, 0, data_size);
555 data = (GuideBallMessage_data_t *)data_ptr;
557 data->guide_ball_side = ini_guide_ball_side;
558 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
559 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
560 add_fieldinfo(IFT_ENUM, "guide_ball_side", 1, &data->guide_ball_side, "GuideBallSideEnum", &enum_map_GuideBallSideEnum);
561}
562/** Constructor */
564{
565 data_size = sizeof(GuideBallMessage_data_t);
566 data_ptr = malloc(data_size);
567 memset(data_ptr, 0, data_size);
568 data = (GuideBallMessage_data_t *)data_ptr;
570 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_LEFT] = "GUIDE_BALL_LEFT";
571 enum_map_GuideBallSideEnum[(int)GUIDE_BALL_RIGHT] = "GUIDE_BALL_RIGHT";
572 add_fieldinfo(IFT_ENUM, "guide_ball_side", 1, &data->guide_ball_side, "GuideBallSideEnum", &enum_map_GuideBallSideEnum);
573}
574
575/** Destructor */
577{
578 free(data_ptr);
579}
580
581/** Copy constructor.
582 * @param m message to copy from
583 */
585{
586 data_size = m->data_size;
587 data_ptr = malloc(data_size);
588 memcpy(data_ptr, m->data_ptr, data_size);
589 data = (GuideBallMessage_data_t *)data_ptr;
591}
592
593/* Methods */
594/** Get guide_ball_side value.
595 * Side where to guide the ball and erect the arm.
596 * @return guide_ball_side value
597 */
600{
601 return (KickerInterface::GuideBallSideEnum)data->guide_ball_side;
602}
603
604/** Get maximum length of guide_ball_side value.
605 * @return length of guide_ball_side value, can be length of the array or number of
606 * maximum number of characters for a string
607 */
608size_t
610{
611 return 1;
612}
613
614/** Set guide_ball_side value.
615 * Side where to guide the ball and erect the arm.
616 * @param new_guide_ball_side new guide_ball_side value
617 */
618void
620{
621 set_field(data->guide_ball_side, new_guide_ball_side);
622}
623
624/** Clone this message.
625 * Produces a message of the same type as this message and copies the
626 * data to the new message.
627 * @return clone of this message
628 */
629Message *
631{
632 return new KickerInterface::GuideBallMessage(this);
633}
634/** Check if message is valid and can be enqueued.
635 * @param message Message to check
636 * @return true if the message is valid, false otherwise.
637 */
638bool
640{
641 const KickMessage *m0 = dynamic_cast<const KickMessage *>(message);
642 if ( m0 != NULL ) {
643 return true;
644 }
645 const ResetCounterMessage *m1 = dynamic_cast<const ResetCounterMessage *>(message);
646 if ( m1 != NULL ) {
647 return true;
648 }
649 const GuideBallMessage *m2 = dynamic_cast<const GuideBallMessage *>(message);
650 if ( m2 != NULL ) {
651 return true;
652 }
653 return false;
654}
655
656/// @cond INTERNALS
657EXPORT_INTERFACE(KickerInterface)
658/// @endcond
659
660
661} // 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
GuideBallMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_guide_ball_side() const
Get maximum length of guide_ball_side value.
virtual Message * clone() const
Clone this message.
GuideBallSideEnum guide_ball_side() const
Get guide_ball_side value.
void set_guide_ball_side(const GuideBallSideEnum new_guide_ball_side)
Set guide_ball_side value.
KickMessage Fawkes BlackBoard Interface Message.
void set_left(const bool new_left)
Set left value.
bool is_left() const
Get left value.
size_t maxlenof_right() const
Get maximum length of right value.
void set_intensity(const uint32_t new_intensity)
Set intensity value.
void set_right(const bool new_right)
Set right value.
bool is_right() const
Get right value.
uint32_t intensity() const
Get intensity value.
void set_center(const bool new_center)
Set center value.
size_t maxlenof_intensity() const
Get maximum length of intensity value.
size_t maxlenof_left() const
Get maximum length of left value.
bool is_center() const
Get center value.
virtual Message * clone() const
Clone this message.
size_t maxlenof_center() const
Get maximum length of center value.
ResetCounterMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
KickerInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
GuideBallSideEnum
Enumeration defining on which side of the robot the ball shall be guided (and thus on which side the ...
@ GUIDE_BALL_RIGHT
Constant defining that the kicker shall activate the ball guidance device in such a way that the righ...
@ GUIDE_BALL_LEFT
Constant defining that the kicker shall activate the ball guidance device in such a way that the left...
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_UINT32
32 bit unsigned integer field
Definition: types.h:43
@ IFT_BOOL
boolean field
Definition: types.h:37
@ 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