KickerInterface.cpp

00001
00002 /***************************************************************************
00003  *  KickerInterface.cpp - Fawkes BlackBoard Interface - KickerInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2007  Daniel Beck
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/KickerInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class KickerInterface <interfaces/KickerInterface.h>
00034  * KickerInterface Fawkes BlackBoard Interface.
00035  * 
00036       In these variables it is stored how often the right, center or 
00037       left kick have been triggered.
00038     
00039  * @ingroup FawkesInterfaces
00040  */
00041
00042
00043 
00044 /** Constructor */
00045 KickerInterface::KickerInterface() : Interface()
00046 {
00047   data_size = sizeof(KickerInterface_data_t);
00048   data_ptr  = malloc(data_size);
00049   data      = (KickerInterface_data_t *)data_ptr;
00050   memset(data_ptr, 0, data_size);
00051   add_fieldinfo(IFT_INT, "num_kicks_left", 1, &data->num_kicks_left);
00052   add_fieldinfo(IFT_INT, "num_kicks_center", 1, &data->num_kicks_center);
00053   add_fieldinfo(IFT_INT, "num_kicks_right", 1, &data->num_kicks_right);
00054   add_fieldinfo(IFT_UINT, "current_intensity", 1, &data->current_intensity);
00055   add_messageinfo("KickMessage");
00056   add_messageinfo("ResetCounterMessage");
00057   add_messageinfo("GuideBallMessage");
00058   unsigned char tmp_hash[] = {0xdc, 0xe9, 0x59, 0xc4, 0xc2, 0xd9, 0x46, 0x62, 0xd7, 0x78, 0x52, 0xb0, 0x6f, 0xb, 0x2c, 0x76};
00059   set_hash(tmp_hash);
00060 }
00061 
00062 /** Destructor */
00063 KickerInterface::~KickerInterface()
00064 {
00065   free(data_ptr);
00066 }
00067 /* Methods */
00068 /** Get num_kicks_left value.
00069  * 
00070       Number of Left-Kicks
00071     
00072  * @return num_kicks_left value
00073  */
00074 int
00075 KickerInterface::num_kicks_left() const
00076 {
00077   return data->num_kicks_left;
00078 }
00079 
00080 /** Get maximum length of num_kicks_left value.
00081  * @return length of num_kicks_left value, can be length of the array or number of 
00082  * maximum number of characters for a string
00083  */
00084 size_t
00085 KickerInterface::maxlenof_num_kicks_left() const
00086 {
00087   return 1;
00088 }
00089 
00090 /** Set num_kicks_left value.
00091  * 
00092       Number of Left-Kicks
00093     
00094  * @param new_num_kicks_left new num_kicks_left value
00095  */
00096 void
00097 KickerInterface::set_num_kicks_left(const int new_num_kicks_left)
00098 {
00099   data->num_kicks_left = new_num_kicks_left;
00100 }
00101 
00102 /** Get num_kicks_center value.
00103  * 
00104       Number of Center-Kicks
00105     
00106  * @return num_kicks_center value
00107  */
00108 int
00109 KickerInterface::num_kicks_center() const
00110 {
00111   return data->num_kicks_center;
00112 }
00113 
00114 /** Get maximum length of num_kicks_center value.
00115  * @return length of num_kicks_center value, can be length of the array or number of 
00116  * maximum number of characters for a string
00117  */
00118 size_t
00119 KickerInterface::maxlenof_num_kicks_center() const
00120 {
00121   return 1;
00122 }
00123 
00124 /** Set num_kicks_center value.
00125  * 
00126       Number of Center-Kicks
00127     
00128  * @param new_num_kicks_center new num_kicks_center value
00129  */
00130 void
00131 KickerInterface::set_num_kicks_center(const int new_num_kicks_center)
00132 {
00133   data->num_kicks_center = new_num_kicks_center;
00134 }
00135 
00136 /** Get num_kicks_right value.
00137  * 
00138       Number of Right-Kicks
00139     
00140  * @return num_kicks_right value
00141  */
00142 int
00143 KickerInterface::num_kicks_right() const
00144 {
00145   return data->num_kicks_right;
00146 }
00147 
00148 /** Get maximum length of num_kicks_right value.
00149  * @return length of num_kicks_right value, can be length of the array or number of 
00150  * maximum number of characters for a string
00151  */
00152 size_t
00153 KickerInterface::maxlenof_num_kicks_right() const
00154 {
00155   return 1;
00156 }
00157 
00158 /** Set num_kicks_right value.
00159  * 
00160       Number of Right-Kicks
00161     
00162  * @param new_num_kicks_right new num_kicks_right value
00163  */
00164 void
00165 KickerInterface::set_num_kicks_right(const int new_num_kicks_right)
00166 {
00167   data->num_kicks_right = new_num_kicks_right;
00168 }
00169 
00170 /** Get guide_ball_side value.
00171  * Side where the ball
00172       guidance arm is currently erected.
00173  * @return guide_ball_side value
00174  */
00175 KickerInterface::GuideBallSideEnum
00176 KickerInterface::guide_ball_side() const
00177 {
00178   return data->guide_ball_side;
00179 }
00180 
00181 /** Get maximum length of guide_ball_side value.
00182  * @return length of guide_ball_side value, can be length of the array or number of 
00183  * maximum number of characters for a string
00184  */
00185 size_t
00186 KickerInterface::maxlenof_guide_ball_side() const
00187 {
00188   return 1;
00189 }
00190 
00191 /** Set guide_ball_side value.
00192  * Side where the ball
00193       guidance arm is currently erected.
00194  * @param new_guide_ball_side new guide_ball_side value
00195  */
00196 void
00197 KickerInterface::set_guide_ball_side(const GuideBallSideEnum new_guide_ball_side)
00198 {
00199   data->guide_ball_side = new_guide_ball_side;
00200 }
00201 
00202 /** Get current_intensity value.
00203  * 
00204       The currently set intensity.
00205     
00206  * @return current_intensity value
00207  */
00208 unsigned int
00209 KickerInterface::current_intensity() const
00210 {
00211   return data->current_intensity;
00212 }
00213 
00214 /** Get maximum length of current_intensity value.
00215  * @return length of current_intensity value, can be length of the array or number of 
00216  * maximum number of characters for a string
00217  */
00218 size_t
00219 KickerInterface::maxlenof_current_intensity() const
00220 {
00221   return 1;
00222 }
00223 
00224 /** Set current_intensity value.
00225  * 
00226       The currently set intensity.
00227     
00228  * @param new_current_intensity new current_intensity value
00229  */
00230 void
00231 KickerInterface::set_current_intensity(const unsigned int new_current_intensity)
00232 {
00233   data->current_intensity = new_current_intensity;
00234 }
00235
00236 /* =========== message create =========== */
00237 Message *
00238 KickerInterface::create_message(const char *type) const
00239 {
00240   if ( strncmp("KickMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00241     return new KickMessage();
00242   } else if ( strncmp("ResetCounterMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00243     return new ResetCounterMessage();
00244   } else if ( strncmp("GuideBallMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00245     return new GuideBallMessage();
00246   } else {
00247     throw UnknownTypeException("The given type '%s' does not match any known "
00248                                "message type for this interface type.", type);
00249   }
00250 }
00251
00252 
00253 /** Copy values from other interface.
00254  * @param other other interface to copy values from
00255  */
00256 void
00257 KickerInterface::copy_values(const Interface *other)
00258 {
00259   const KickerInterface *oi = dynamic_cast<const KickerInterface *>(other);
00260   if (oi == NULL) {
00261     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00262                                 type(), other->type());
00263   }
00264   memcpy(data, oi->data, sizeof(KickerInterface_data_t));
00265 }
00266
00267 /* =========== messages =========== */
00268 /** @class KickerInterface::KickMessage <interfaces/KickerInterface.h>
00269  * KickMessage Fawkes BlackBoard Interface Message.
00270  * 
00271     
00272  */
00273
00274 
00275 /** Constructor with initial values.
00276  * @param ini_left initial value for left
00277  * @param ini_center initial value for center
00278  * @param ini_right initial value for right
00279  * @param ini_intensity initial value for intensity
00280  */
00281 KickerInterface::KickMessage::KickMessage(const bool ini_left, const bool ini_center, const bool ini_right, const unsigned int ini_intensity) : Message("KickMessage")
00282 {
00283   data_size = sizeof(KickMessage_data_t);
00284   data_ptr  = malloc(data_size);
00285   memset(data_ptr, 0, data_size);
00286   data      = (KickMessage_data_t *)data_ptr;
00287   data->left = ini_left;
00288   data->center = ini_center;
00289   data->right = ini_right;
00290   data->intensity = ini_intensity;
00291   add_fieldinfo(IFT_BOOL, "left", 1, &data->left);
00292   add_fieldinfo(IFT_BOOL, "center", 1, &data->center);
00293   add_fieldinfo(IFT_BOOL, "right", 1, &data->right);
00294   add_fieldinfo(IFT_UINT, "intensity", 1, &data->intensity);
00295 }
00296 /** Constructor */
00297 KickerInterface::KickMessage::KickMessage() : Message("KickMessage")
00298 {
00299   data_size = sizeof(KickMessage_data_t);
00300   data_ptr  = malloc(data_size);
00301   memset(data_ptr, 0, data_size);
00302   data      = (KickMessage_data_t *)data_ptr;
00303   add_fieldinfo(IFT_BOOL, "left", 1, &data->left);
00304   add_fieldinfo(IFT_BOOL, "center", 1, &data->center);
00305   add_fieldinfo(IFT_BOOL, "right", 1, &data->right);
00306   add_fieldinfo(IFT_UINT, "intensity", 1, &data->intensity);
00307 }
00308 
00309 /** Destructor */
00310 KickerInterface::KickMessage::~KickMessage()
00311 {
00312   free(data_ptr);
00313 }
00314 
00315 /** Copy constructor.
00316  * @param m message to copy from
00317  */
00318 KickerInterface::KickMessage::KickMessage(const KickMessage *m) : Message("KickMessage")
00319 {
00320   data_size = m->data_size;
00321   data_ptr  = malloc(data_size);
00322   memcpy(data_ptr, m->data_ptr, data_size);
00323   data      = (KickMessage_data_t *)data_ptr;
00324 }
00325
00326 /* Methods */
00327 /** Get left value.
00328  * True to kick with left kicker.
00329  * @return left value
00330  */
00331 bool
00332 KickerInterface::KickMessage::is_left() const
00333 {
00334   return data->left;
00335 }
00336 
00337 /** Get maximum length of left value.
00338  * @return length of left value, can be length of the array or number of 
00339  * maximum number of characters for a string
00340  */
00341 size_t
00342 KickerInterface::KickMessage::maxlenof_left() const
00343 {
00344   return 1;
00345 }
00346 
00347 /** Set left value.
00348  * True to kick with left kicker.
00349  * @param new_left new left value
00350  */
00351 void
00352 KickerInterface::KickMessage::set_left(const bool new_left)
00353 {
00354   data->left = new_left;
00355 }
00356 
00357 /** Get center value.
00358  * True to kick with central kicker.
00359  * @return center value
00360  */
00361 bool
00362 KickerInterface::KickMessage::is_center() const
00363 {
00364   return data->center;
00365 }
00366 
00367 /** Get maximum length of center value.
00368  * @return length of center value, can be length of the array or number of 
00369  * maximum number of characters for a string
00370  */
00371 size_t
00372 KickerInterface::KickMessage::maxlenof_center() const
00373 {
00374   return 1;
00375 }
00376 
00377 /** Set center value.
00378  * True to kick with central kicker.
00379  * @param new_center new center value
00380  */
00381 void
00382 KickerInterface::KickMessage::set_center(const bool new_center)
00383 {
00384   data->center = new_center;
00385 }
00386 
00387 /** Get right value.
00388  * True to kick with right kicker.
00389  * @return right value
00390  */
00391 bool
00392 KickerInterface::KickMessage::is_right() const
00393 {
00394   return data->right;
00395 }
00396 
00397 /** Get maximum length of right value.
00398  * @return length of right value, can be length of the array or number of 
00399  * maximum number of characters for a string
00400  */
00401 size_t
00402 KickerInterface::KickMessage::maxlenof_right() const
00403 {
00404   return 1;
00405 }
00406 
00407 /** Set right value.
00408  * True to kick with right kicker.
00409  * @param new_right new right value
00410  */
00411 void
00412 KickerInterface::KickMessage::set_right(const bool new_right)
00413 {
00414   data->right = new_right;
00415 }
00416 
00417 /** Get intensity value.
00418  * Intensity in the range [0..255].
00419  * @return intensity value
00420  */
00421 unsigned int
00422 KickerInterface::KickMessage::intensity() const
00423 {
00424   return data->intensity;
00425 }
00426 
00427 /** Get maximum length of intensity value.
00428  * @return length of intensity value, can be length of the array or number of 
00429  * maximum number of characters for a string
00430  */
00431 size_t
00432 KickerInterface::KickMessage::maxlenof_intensity() const
00433 {
00434   return 1;
00435 }
00436 
00437 /** Set intensity value.
00438  * Intensity in the range [0..255].
00439  * @param new_intensity new intensity value
00440  */
00441 void
00442 KickerInterface::KickMessage::set_intensity(const unsigned int new_intensity)
00443 {
00444   data->intensity = new_intensity;
00445 }
00446 
00447 /** Clone this message.
00448  * Produces a message of the same type as this message and copies the
00449  * data to the new message.
00450  * @return clone of this message
00451  */
00452 Message *
00453 KickerInterface::KickMessage::clone() const
00454 {
00455   return new KickerInterface::KickMessage(this);
00456 }
00457 /** @class KickerInterface::ResetCounterMessage <interfaces/KickerInterface.h>
00458  * ResetCounterMessage Fawkes BlackBoard Interface Message.
00459  * 
00460     
00461  */
00462
00463 
00464 /** Constructor */
00465 KickerInterface::ResetCounterMessage::ResetCounterMessage() : Message("ResetCounterMessage")
00466 {
00467   data_size = 0;
00468   data_ptr  = NULL;
00469 }
00470 
00471 /** Destructor */
00472 KickerInterface::ResetCounterMessage::~ResetCounterMessage()
00473 {
00474 }
00475 
00476 /** Copy constructor.
00477  * @param m message to copy from
00478  */
00479 KickerInterface::ResetCounterMessage::ResetCounterMessage(const ResetCounterMessage *m) : Message("ResetCounterMessage")
00480 {
00481   data_size = 0;
00482   data_ptr  = NULL;
00483 }
00484
00485 /* Methods */
00486 /** Clone this message.
00487  * Produces a message of the same type as this message and copies the
00488  * data to the new message.
00489  * @return clone of this message
00490  */
00491 Message *
00492 KickerInterface::ResetCounterMessage::clone() const
00493 {
00494   return new KickerInterface::ResetCounterMessage(this);
00495 }
00496 /** @class KickerInterface::GuideBallMessage <interfaces/KickerInterface.h>
00497  * GuideBallMessage Fawkes BlackBoard Interface Message.
00498  * 
00499     
00500  */
00501
00502 
00503 /** Constructor with initial values.
00504  * @param ini_guide_ball_side initial value for guide_ball_side
00505  */
00506 KickerInterface::GuideBallMessage::GuideBallMessage(const GuideBallSideEnum ini_guide_ball_side) : Message("GuideBallMessage")
00507 {
00508   data_size = sizeof(GuideBallMessage_data_t);
00509   data_ptr  = malloc(data_size);
00510   memset(data_ptr, 0, data_size);
00511   data      = (GuideBallMessage_data_t *)data_ptr;
00512   data->guide_ball_side = ini_guide_ball_side;
00513 }
00514 /** Constructor */
00515 KickerInterface::GuideBallMessage::GuideBallMessage() : Message("GuideBallMessage")
00516 {
00517   data_size = sizeof(GuideBallMessage_data_t);
00518   data_ptr  = malloc(data_size);
00519   memset(data_ptr, 0, data_size);
00520   data      = (GuideBallMessage_data_t *)data_ptr;
00521 }
00522 
00523 /** Destructor */
00524 KickerInterface::GuideBallMessage::~GuideBallMessage()
00525 {
00526   free(data_ptr);
00527 }
00528 
00529 /** Copy constructor.
00530  * @param m message to copy from
00531  */
00532 KickerInterface::GuideBallMessage::GuideBallMessage(const GuideBallMessage *m) : Message("GuideBallMessage")
00533 {
00534   data_size = m->data_size;
00535   data_ptr  = malloc(data_size);
00536   memcpy(data_ptr, m->data_ptr, data_size);
00537   data      = (GuideBallMessage_data_t *)data_ptr;
00538 }
00539
00540 /* Methods */
00541 /** Get guide_ball_side value.
00542  * Side where to guide the ball and erect the arm.
00543  * @return guide_ball_side value
00544  */
00545 KickerInterface::GuideBallSideEnum
00546 KickerInterface::GuideBallMessage::guide_ball_side() const
00547 {
00548   return data->guide_ball_side;
00549 }
00550 
00551 /** Get maximum length of guide_ball_side value.
00552  * @return length of guide_ball_side value, can be length of the array or number of 
00553  * maximum number of characters for a string
00554  */
00555 size_t
00556 KickerInterface::GuideBallMessage::maxlenof_guide_ball_side() const
00557 {
00558   return 1;
00559 }
00560 
00561 /** Set guide_ball_side value.
00562  * Side where to guide the ball and erect the arm.
00563  * @param new_guide_ball_side new guide_ball_side value
00564  */
00565 void
00566 KickerInterface::GuideBallMessage::set_guide_ball_side(const GuideBallSideEnum new_guide_ball_side)
00567 {
00568   data->guide_ball_side = new_guide_ball_side;
00569 }
00570 
00571 /** Clone this message.
00572  * Produces a message of the same type as this message and copies the
00573  * data to the new message.
00574  * @return clone of this message
00575  */
00576 Message *
00577 KickerInterface::GuideBallMessage::clone() const
00578 {
00579   return new KickerInterface::GuideBallMessage(this);
00580 }
00581 /** Check if message is valid and can be enqueued.
00582  * @param message Message to check
00583  */
00584 bool
00585 KickerInterface::message_valid(const Message *message) const
00586 {
00587   const KickMessage *m0 = dynamic_cast<const KickMessage *>(message);
00588   if ( m0 != NULL ) {
00589     return true;
00590   }
00591   const ResetCounterMessage *m1 = dynamic_cast<const ResetCounterMessage *>(message);
00592   if ( m1 != NULL ) {
00593     return true;
00594   }
00595   const GuideBallMessage *m2 = dynamic_cast<const GuideBallMessage *>(message);
00596   if ( m2 != NULL ) {
00597     return true;
00598   }
00599   return false;
00600 }
00601 
00602 /// @cond INTERNALS
00603 EXPORT_INTERFACE(KickerInterface)
00604 /// @endcond
00605 
00606
00607 } // end namespace fawkes