Fawkes API  Fawkes Development Version
SoccerPenaltyInterface.cpp
1 
2 /***************************************************************************
3  * SoccerPenaltyInterface.cpp - Fawkes BlackBoard Interface - SoccerPenaltyInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008-2010 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/SoccerPenaltyInterface.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 SoccerPenaltyInterface <interfaces/SoccerPenaltyInterface.h>
36  * SoccerPenaltyInterface Fawkes BlackBoard Interface.
37  *
38  This interface stores penalization information for soccer robots.
39  Currently it contains constants used in the RoboCup Standard Platform
40  League (SPL).
41 
42  * @ingroup FawkesInterfaces
43  */
44 
45 
46 /** SPL_PENALTY_NONE constant */
48 /** SPL_PENALTY_BALL_HOLDING constant */
50 /** SPL_PENALTY_PLAYER_PUSHING constant */
52 /** SPL_PENALTY_OBSTRUCTION constant */
54 /** SPL_PENALTY_INACTIVE_PLAYER constant */
56 /** SPL_PENALTY_ILLEGAL_DEFENDER constant */
58 /** SPL_PENALTY_LEAVING_THE_FIELD constant */
60 /** SPL_PENALTY_PLAYING_WITH_HANDS constant */
62 /** SPL_PENALTY_REQ_FOR_PICKUP constant */
64 /** SPL_PENALTY_MANUAL constant */
66 
67 /** Constructor */
68 SoccerPenaltyInterface::SoccerPenaltyInterface() : Interface()
69 {
70  data_size = sizeof(SoccerPenaltyInterface_data_t);
71  data_ptr = malloc(data_size);
72  data = (SoccerPenaltyInterface_data_t *)data_ptr;
73  data_ts = (interface_data_ts_t *)data_ptr;
74  memset(data_ptr, 0, data_size);
75  add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
76  add_fieldinfo(IFT_UINT16, "remaining", 1, &data->remaining);
77  add_messageinfo("SetPenaltyMessage");
78  unsigned char tmp_hash[] = {0xa0, 0xa1, 0xf0, 0xc2, 0x4e, 0x8c, 0xd1, 0xe1, 0xaf, 0x46, 0x11, 0xe9, 0xa0, 0xc8, 0xaf, 0x5d};
79  set_hash(tmp_hash);
80 }
81 
82 /** Destructor */
83 SoccerPenaltyInterface::~SoccerPenaltyInterface()
84 {
85  free(data_ptr);
86 }
87 /* Methods */
88 /** Get penalty value.
89  * Current penalty code.
90  * @return penalty value
91  */
92 uint16_t
94 {
95  return data->penalty;
96 }
97 
98 /** Get maximum length of penalty value.
99  * @return length of penalty value, can be length of the array or number of
100  * maximum number of characters for a string
101  */
102 size_t
104 {
105  return 1;
106 }
107 
108 /** Set penalty value.
109  * Current penalty code.
110  * @param new_penalty new penalty value
111  */
112 void
113 SoccerPenaltyInterface::set_penalty(const uint16_t new_penalty)
114 {
115  data->penalty = new_penalty;
116  data_changed = true;
117 }
118 
119 /** Get remaining value.
120  * Estimated time in seconds until the robot is unpenalized.
121  * @return remaining value
122  */
123 uint16_t
125 {
126  return data->remaining;
127 }
128 
129 /** Get maximum length of remaining value.
130  * @return length of remaining value, can be length of the array or number of
131  * maximum number of characters for a string
132  */
133 size_t
135 {
136  return 1;
137 }
138 
139 /** Set remaining value.
140  * Estimated time in seconds until the robot is unpenalized.
141  * @param new_remaining new remaining value
142  */
143 void
144 SoccerPenaltyInterface::set_remaining(const uint16_t new_remaining)
145 {
146  data->remaining = new_remaining;
147  data_changed = true;
148 }
149 
150 /* =========== message create =========== */
151 Message *
153 {
154  if ( strncmp("SetPenaltyMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
155  return new SetPenaltyMessage();
156  } else {
157  throw UnknownTypeException("The given type '%s' does not match any known "
158  "message type for this interface type.", type);
159  }
160 }
161 
162 
163 /** Copy values from other interface.
164  * @param other other interface to copy values from
165  */
166 void
168 {
169  const SoccerPenaltyInterface *oi = dynamic_cast<const SoccerPenaltyInterface *>(other);
170  if (oi == NULL) {
171  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
172  type(), other->type());
173  }
174  memcpy(data, oi->data, sizeof(SoccerPenaltyInterface_data_t));
175 }
176 
177 const char *
178 SoccerPenaltyInterface::enum_tostring(const char *enumtype, int val) const
179 {
180  throw UnknownTypeException("Unknown enum type %s", enumtype);
181 }
182 
183 /* =========== messages =========== */
184 /** @class SoccerPenaltyInterface::SetPenaltyMessage <interfaces/SoccerPenaltyInterface.h>
185  * SetPenaltyMessage Fawkes BlackBoard Interface Message.
186  *
187 
188  */
189 
190 
191 /** Constructor with initial values.
192  * @param ini_penalty initial value for penalty
193  */
194 SoccerPenaltyInterface::SetPenaltyMessage::SetPenaltyMessage(const uint16_t ini_penalty) : Message("SetPenaltyMessage")
195 {
196  data_size = sizeof(SetPenaltyMessage_data_t);
197  data_ptr = malloc(data_size);
198  memset(data_ptr, 0, data_size);
199  data = (SetPenaltyMessage_data_t *)data_ptr;
201  data->penalty = ini_penalty;
202  add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
203 }
204 /** Constructor */
206 {
207  data_size = sizeof(SetPenaltyMessage_data_t);
208  data_ptr = malloc(data_size);
209  memset(data_ptr, 0, data_size);
210  data = (SetPenaltyMessage_data_t *)data_ptr;
212  add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
213 }
214 
215 /** Destructor */
217 {
218  free(data_ptr);
219 }
220 
221 /** Copy constructor.
222  * @param m message to copy from
223  */
225 {
226  data_size = m->data_size;
227  data_ptr = malloc(data_size);
228  memcpy(data_ptr, m->data_ptr, data_size);
229  data = (SetPenaltyMessage_data_t *)data_ptr;
231 }
232 
233 /* Methods */
234 /** Get penalty value.
235  * Current penalty code.
236  * @return penalty value
237  */
238 uint16_t
240 {
241  return data->penalty;
242 }
243 
244 /** Get maximum length of penalty value.
245  * @return length of penalty value, can be length of the array or number of
246  * maximum number of characters for a string
247  */
248 size_t
250 {
251  return 1;
252 }
253 
254 /** Set penalty value.
255  * Current penalty code.
256  * @param new_penalty new penalty value
257  */
258 void
260 {
261  data->penalty = new_penalty;
262 }
263 
264 /** Clone this message.
265  * Produces a message of the same type as this message and copies the
266  * data to the new message.
267  * @return clone of this message
268  */
269 Message *
271 {
273 }
274 /** Check if message is valid and can be enqueued.
275  * @param message Message to check
276  * @return true if the message is valid, false otherwise.
277  */
278 bool
280 {
281  const SetPenaltyMessage *m0 = dynamic_cast<const SetPenaltyMessage *>(message);
282  if ( m0 != NULL ) {
283  return true;
284  }
285  return false;
286 }
287 
288 /// @cond INTERNALS
289 EXPORT_INTERFACE(SoccerPenaltyInterface)
290 /// @endcond
291 
292 
293 } // end namespace fawkes
virtual void copy_values(const Interface *other)
Copy values from other interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
static const uint16_t SPL_PENALTY_ILLEGAL_DEFENDER
SPL_PENALTY_ILLEGAL_DEFENDER constant.
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
uint16_t remaining() const
Get remaining value.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
uint16_t penalty() const
Get penalty value.
Fawkes library namespace.
void set_penalty(const uint16_t new_penalty)
Set penalty value.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
static const uint16_t SPL_PENALTY_PLAYING_WITH_HANDS
SPL_PENALTY_PLAYING_WITH_HANDS constant.
16 bit unsigned integer field
Definition: types.h:40
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
static const uint16_t SPL_PENALTY_NONE
SPL_PENALTY_NONE constant.
size_t maxlenof_remaining() const
Get maximum length of remaining value.
SoccerPenaltyInterface Fawkes BlackBoard Interface.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
SetPenaltyMessage Fawkes BlackBoard Interface Message.
void set_penalty(const uint16_t new_penalty)
Set penalty value.
static const uint16_t SPL_PENALTY_INACTIVE_PLAYER
SPL_PENALTY_INACTIVE_PLAYER constant.
virtual Message * create_message(const char *type) const
Create message based on type name.
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:373
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
static const uint16_t SPL_PENALTY_LEAVING_THE_FIELD
SPL_PENALTY_LEAVING_THE_FIELD constant.
const char * type() const
Get type of interface.
Definition: interface.cpp:651
static const uint16_t SPL_PENALTY_REQ_FOR_PICKUP
SPL_PENALTY_REQ_FOR_PICKUP constant.
size_t maxlenof_penalty() const
Get maximum length of penalty value.
static const uint16_t SPL_PENALTY_MANUAL
SPL_PENALTY_MANUAL constant.
void set_remaining(const uint16_t new_remaining)
Set remaining value.
static const uint16_t SPL_PENALTY_PLAYER_PUSHING
SPL_PENALTY_PLAYER_PUSHING constant.
virtual Message * clone() const
Clone this message.
static const uint16_t SPL_PENALTY_BALL_HOLDING
SPL_PENALTY_BALL_HOLDING constant.
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
static const uint16_t SPL_PENALTY_OBSTRUCTION
SPL_PENALTY_OBSTRUCTION constant.
const char * type() const
Get message type.
Definition: message.cpp:378
size_t maxlenof_penalty() const
Get maximum length of penalty value.