SoccerPenaltyInterface.cpp

00001 
00002 /***************************************************************************
00003  *  SoccerPenaltyInterface.cpp - Fawkes BlackBoard Interface - SoccerPenaltyInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008-2010  Tim Niemueller
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <interfaces/SoccerPenaltyInterface.h>
00025 
00026 #include <core/exceptions/software.h>
00027 
00028 #include <cstring>
00029 #include <cstdlib>
00030 
00031 namespace fawkes {
00032 
00033 /** @class SoccerPenaltyInterface <interfaces/SoccerPenaltyInterface.h>
00034  * SoccerPenaltyInterface Fawkes BlackBoard Interface.
00035  * 
00036       This interface stores penalization information for soccer robots.
00037       Currently it contains constants used in the RoboCup Standard Platform
00038       League (SPL).
00039     
00040  * @ingroup FawkesInterfaces
00041  */
00042 
00043 
00044 /** SPL_PENALTY_NONE constant */
00045 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_NONE = 0;
00046 /** SPL_PENALTY_BALL_HOLDING constant */
00047 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_BALL_HOLDING = 1;
00048 /** SPL_PENALTY_PLAYER_PUSHING constant */
00049 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_PLAYER_PUSHING = 2;
00050 /** SPL_PENALTY_OBSTRUCTION constant */
00051 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_OBSTRUCTION = 3;
00052 /** SPL_PENALTY_INACTIVE_PLAYER constant */
00053 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_INACTIVE_PLAYER = 4;
00054 /** SPL_PENALTY_ILLEGAL_DEFENDER constant */
00055 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_ILLEGAL_DEFENDER = 5;
00056 /** SPL_PENALTY_LEAVING_THE_FIELD constant */
00057 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_LEAVING_THE_FIELD = 6;
00058 /** SPL_PENALTY_PLAYING_WITH_HANDS constant */
00059 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_PLAYING_WITH_HANDS = 7;
00060 /** SPL_PENALTY_REQ_FOR_PICKUP constant */
00061 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_REQ_FOR_PICKUP = 8;
00062 /** SPL_PENALTY_MANUAL constant */
00063 const uint16_t SoccerPenaltyInterface::SPL_PENALTY_MANUAL = 15;
00064 
00065 /** Constructor */
00066 SoccerPenaltyInterface::SoccerPenaltyInterface() : Interface()
00067 {
00068   data_size = sizeof(SoccerPenaltyInterface_data_t);
00069   data_ptr  = malloc(data_size);
00070   data      = (SoccerPenaltyInterface_data_t *)data_ptr;
00071   data_ts   = (interface_data_ts_t *)data_ptr;
00072   memset(data_ptr, 0, data_size);
00073   add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
00074   add_fieldinfo(IFT_UINT16, "remaining", 1, &data->remaining);
00075   add_messageinfo("SetPenaltyMessage");
00076   unsigned char tmp_hash[] = {0xa0, 0xa1, 0xf0, 0xc2, 0x4e, 0x8c, 0xd1, 0xe1, 0xaf, 0x46, 0x11, 0xe9, 0xa0, 0xc8, 0xaf, 0x5d};
00077   set_hash(tmp_hash);
00078 }
00079 
00080 /** Destructor */
00081 SoccerPenaltyInterface::~SoccerPenaltyInterface()
00082 {
00083   free(data_ptr);
00084 }
00085 /* Methods */
00086 /** Get penalty value.
00087  * Current penalty code.
00088  * @return penalty value
00089  */
00090 uint16_t
00091 SoccerPenaltyInterface::penalty() const
00092 {
00093   return data->penalty;
00094 }
00095 
00096 /** Get maximum length of penalty value.
00097  * @return length of penalty value, can be length of the array or number of 
00098  * maximum number of characters for a string
00099  */
00100 size_t
00101 SoccerPenaltyInterface::maxlenof_penalty() const
00102 {
00103   return 1;
00104 }
00105 
00106 /** Set penalty value.
00107  * Current penalty code.
00108  * @param new_penalty new penalty value
00109  */
00110 void
00111 SoccerPenaltyInterface::set_penalty(const uint16_t new_penalty)
00112 {
00113   data->penalty = new_penalty;
00114   data_changed = true;
00115 }
00116 
00117 /** Get remaining value.
00118  * Estimated time in seconds until the robot is unpenalized.
00119  * @return remaining value
00120  */
00121 uint16_t
00122 SoccerPenaltyInterface::remaining() const
00123 {
00124   return data->remaining;
00125 }
00126 
00127 /** Get maximum length of remaining value.
00128  * @return length of remaining value, can be length of the array or number of 
00129  * maximum number of characters for a string
00130  */
00131 size_t
00132 SoccerPenaltyInterface::maxlenof_remaining() const
00133 {
00134   return 1;
00135 }
00136 
00137 /** Set remaining value.
00138  * Estimated time in seconds until the robot is unpenalized.
00139  * @param new_remaining new remaining value
00140  */
00141 void
00142 SoccerPenaltyInterface::set_remaining(const uint16_t new_remaining)
00143 {
00144   data->remaining = new_remaining;
00145   data_changed = true;
00146 }
00147 
00148 /* =========== message create =========== */
00149 Message *
00150 SoccerPenaltyInterface::create_message(const char *type) const
00151 {
00152   if ( strncmp("SetPenaltyMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00153     return new SetPenaltyMessage();
00154   } else {
00155     throw UnknownTypeException("The given type '%s' does not match any known "
00156                                "message type for this interface type.", type);
00157   }
00158 }
00159 
00160 
00161 /** Copy values from other interface.
00162  * @param other other interface to copy values from
00163  */
00164 void
00165 SoccerPenaltyInterface::copy_values(const Interface *other)
00166 {
00167   const SoccerPenaltyInterface *oi = dynamic_cast<const SoccerPenaltyInterface *>(other);
00168   if (oi == NULL) {
00169     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00170                                 type(), other->type());
00171   }
00172   memcpy(data, oi->data, sizeof(SoccerPenaltyInterface_data_t));
00173 }
00174 
00175 const char *
00176 SoccerPenaltyInterface::enum_tostring(const char *enumtype, int val) const
00177 {
00178   throw UnknownTypeException("Unknown enum type %s", enumtype);
00179 }
00180 
00181 /* =========== messages =========== */
00182 /** @class SoccerPenaltyInterface::SetPenaltyMessage <interfaces/SoccerPenaltyInterface.h>
00183  * SetPenaltyMessage Fawkes BlackBoard Interface Message.
00184  * 
00185     
00186  */
00187 
00188 
00189 /** Constructor with initial values.
00190  * @param ini_penalty initial value for penalty
00191  */
00192 SoccerPenaltyInterface::SetPenaltyMessage::SetPenaltyMessage(const uint16_t ini_penalty) : Message("SetPenaltyMessage")
00193 {
00194   data_size = sizeof(SetPenaltyMessage_data_t);
00195   data_ptr  = malloc(data_size);
00196   memset(data_ptr, 0, data_size);
00197   data      = (SetPenaltyMessage_data_t *)data_ptr;
00198   data_ts   = (message_data_ts_t *)data_ptr;
00199   data->penalty = ini_penalty;
00200   add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
00201 }
00202 /** Constructor */
00203 SoccerPenaltyInterface::SetPenaltyMessage::SetPenaltyMessage() : Message("SetPenaltyMessage")
00204 {
00205   data_size = sizeof(SetPenaltyMessage_data_t);
00206   data_ptr  = malloc(data_size);
00207   memset(data_ptr, 0, data_size);
00208   data      = (SetPenaltyMessage_data_t *)data_ptr;
00209   data_ts   = (message_data_ts_t *)data_ptr;
00210   add_fieldinfo(IFT_UINT16, "penalty", 1, &data->penalty);
00211 }
00212 
00213 /** Destructor */
00214 SoccerPenaltyInterface::SetPenaltyMessage::~SetPenaltyMessage()
00215 {
00216   free(data_ptr);
00217 }
00218 
00219 /** Copy constructor.
00220  * @param m message to copy from
00221  */
00222 SoccerPenaltyInterface::SetPenaltyMessage::SetPenaltyMessage(const SetPenaltyMessage *m) : Message("SetPenaltyMessage")
00223 {
00224   data_size = m->data_size;
00225   data_ptr  = malloc(data_size);
00226   memcpy(data_ptr, m->data_ptr, data_size);
00227   data      = (SetPenaltyMessage_data_t *)data_ptr;
00228   data_ts   = (message_data_ts_t *)data_ptr;
00229 }
00230 
00231 /* Methods */
00232 /** Get penalty value.
00233  * Current penalty code.
00234  * @return penalty value
00235  */
00236 uint16_t
00237 SoccerPenaltyInterface::SetPenaltyMessage::penalty() const
00238 {
00239   return data->penalty;
00240 }
00241 
00242 /** Get maximum length of penalty value.
00243  * @return length of penalty value, can be length of the array or number of 
00244  * maximum number of characters for a string
00245  */
00246 size_t
00247 SoccerPenaltyInterface::SetPenaltyMessage::maxlenof_penalty() const
00248 {
00249   return 1;
00250 }
00251 
00252 /** Set penalty value.
00253  * Current penalty code.
00254  * @param new_penalty new penalty value
00255  */
00256 void
00257 SoccerPenaltyInterface::SetPenaltyMessage::set_penalty(const uint16_t new_penalty)
00258 {
00259   data->penalty = new_penalty;
00260 }
00261 
00262 /** Clone this message.
00263  * Produces a message of the same type as this message and copies the
00264  * data to the new message.
00265  * @return clone of this message
00266  */
00267 Message *
00268 SoccerPenaltyInterface::SetPenaltyMessage::clone() const
00269 {
00270   return new SoccerPenaltyInterface::SetPenaltyMessage(this);
00271 }
00272 /** Check if message is valid and can be enqueued.
00273  * @param message Message to check
00274  * @return true if the message is valid, false otherwise.
00275  */
00276 bool
00277 SoccerPenaltyInterface::message_valid(const Message *message) const
00278 {
00279   const SetPenaltyMessage *m0 = dynamic_cast<const SetPenaltyMessage *>(message);
00280   if ( m0 != NULL ) {
00281     return true;
00282   }
00283   return false;
00284 }
00285 
00286 /// @cond INTERNALS
00287 EXPORT_INTERFACE(SoccerPenaltyInterface)
00288 /// @endcond
00289 
00290 
00291 } // end namespace fawkes