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
93 SoccerPenaltyInterface::penalty() const
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
103 SoccerPenaltyInterface::maxlenof_penalty() const
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_changed |= change_field(data->penalty, new_penalty);
116 }
117 
118 /** Get remaining value.
119  * Estimated time in seconds until the robot is unpenalized.
120  * @return remaining value
121  */
122 uint16_t
123 SoccerPenaltyInterface::remaining() const
124 {
125  return data->remaining;
126 }
127 
128 /** Get maximum length of remaining value.
129  * @return length of remaining value, can be length of the array or number of
130  * maximum number of characters for a string
131  */
132 size_t
133 SoccerPenaltyInterface::maxlenof_remaining() const
134 {
135  return 1;
136 }
137 
138 /** Set remaining value.
139  * Estimated time in seconds until the robot is unpenalized.
140  * @param new_remaining new remaining value
141  */
142 void
143 SoccerPenaltyInterface::set_remaining(const uint16_t new_remaining)
144 {
145  data_changed |= change_field(data->remaining, new_remaining);
146 }
147 
148 /* =========== message create =========== */
149 Message *
150 SoccerPenaltyInterface::create_message(const char *type) const
151 {
152  if ( strncmp("SetPenaltyMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
153  return new SetPenaltyMessage();
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 SoccerPenaltyInterface::copy_values(const Interface *other)
166 {
167  const SoccerPenaltyInterface *oi = dynamic_cast<const SoccerPenaltyInterface *>(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(SoccerPenaltyInterface_data_t));
173 }
174 
175 const char *
176 SoccerPenaltyInterface::enum_tostring(const char *enumtype, int val) const
177 {
178  throw UnknownTypeException("Unknown enum type %s", enumtype);
179 }
180 
181 /* =========== messages =========== */
182 /** @class SoccerPenaltyInterface::SetPenaltyMessage <interfaces/SoccerPenaltyInterface.h>
183  * SetPenaltyMessage Fawkes BlackBoard Interface Message.
184  *
185 
186  */
187 
188 
189 /** Constructor with initial values.
190  * @param ini_penalty initial value for penalty
191  */
192 SoccerPenaltyInterface::SetPenaltyMessage::SetPenaltyMessage(const uint16_t ini_penalty) : Message("SetPenaltyMessage")
193 {
194  data_size = sizeof(SetPenaltyMessage_data_t);
195  data_ptr = malloc(data_size);
196  memset(data_ptr, 0, data_size);
197  data = (SetPenaltyMessage_data_t *)data_ptr;
199  data->penalty = ini_penalty;
200  add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
201 }
202 /** Constructor */
204 {
205  data_size = sizeof(SetPenaltyMessage_data_t);
206  data_ptr = malloc(data_size);
207  memset(data_ptr, 0, data_size);
208  data = (SetPenaltyMessage_data_t *)data_ptr;
210  add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
211 }
212 
213 /** Destructor */
215 {
216  free(data_ptr);
217 }
218 
219 /** Copy constructor.
220  * @param m message to copy from
221  */
223 {
224  data_size = m->data_size;
225  data_ptr = malloc(data_size);
226  memcpy(data_ptr, m->data_ptr, data_size);
227  data = (SetPenaltyMessage_data_t *)data_ptr;
229 }
230 
231 /* Methods */
232 /** Get penalty value.
233  * Current penalty code.
234  * @return penalty value
235  */
236 uint16_t
238 {
239  return data->penalty;
240 }
241 
242 /** Get maximum length of penalty value.
243  * @return length of penalty value, can be length of the array or number of
244  * maximum number of characters for a string
245  */
246 size_t
248 {
249  return 1;
250 }
251 
252 /** Set penalty value.
253  * Current penalty code.
254  * @param new_penalty new penalty value
255  */
256 void
258 {
259  change_field(data->penalty, new_penalty);
260 }
261 
262 /** Clone this message.
263  * Produces a message of the same type as this message and copies the
264  * data to the new message.
265  * @return clone of this message
266  */
267 Message *
269 {
271 }
272 /** Check if message is valid and can be enqueued.
273  * @param message Message to check
274  * @return true if the message is valid, false otherwise.
275  */
276 bool
278 {
279  const SetPenaltyMessage *m0 = dynamic_cast<const SetPenaltyMessage *>(message);
280  if ( m0 != NULL ) {
281  return true;
282  }
283  return false;
284 }
285 
286 /// @cond INTERNALS
287 EXPORT_INTERFACE(SoccerPenaltyInterface)
288 /// @endcond
289 
290 
291 } // end namespace fawkes
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
const char * type() const
Get type of interface.
Definition: interface.cpp:643
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
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:400
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:128
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:138
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:129
SetPenaltyMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_penalty() const
Get maximum length of penalty value.
void set_penalty(const uint16_t new_penalty)
Set penalty value.
virtual Message * clone() const
Clone this message.
SoccerPenaltyInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
static const uint16_t SPL_PENALTY_BALL_HOLDING
SPL_PENALTY_BALL_HOLDING constant.
static const uint16_t SPL_PENALTY_NONE
SPL_PENALTY_NONE constant.
static const uint16_t SPL_PENALTY_PLAYER_PUSHING
SPL_PENALTY_PLAYER_PUSHING constant.
static const uint16_t SPL_PENALTY_MANUAL
SPL_PENALTY_MANUAL constant.
static const uint16_t SPL_PENALTY_PLAYING_WITH_HANDS
SPL_PENALTY_PLAYING_WITH_HANDS constant.
static const uint16_t SPL_PENALTY_LEAVING_THE_FIELD
SPL_PENALTY_LEAVING_THE_FIELD constant.
static const uint16_t SPL_PENALTY_INACTIVE_PLAYER
SPL_PENALTY_INACTIVE_PLAYER constant.
static const uint16_t SPL_PENALTY_ILLEGAL_DEFENDER
SPL_PENALTY_ILLEGAL_DEFENDER constant.
static const uint16_t SPL_PENALTY_REQ_FOR_PICKUP
SPL_PENALTY_REQ_FOR_PICKUP constant.
static const uint16_t SPL_PENALTY_OBSTRUCTION
SPL_PENALTY_OBSTRUCTION constant.
Fawkes library namespace.
@ IFT_UINT16
16 bit unsigned integer field
Definition: types.h:41
bool change_field(FieldT &field, const DataT &value)
Set a field and return whether it changed.
Definition: message.h:167
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:134