BatteryInterface.cpp

00001
00002 /***************************************************************************
00003  *  BatteryInterface.cpp - Fawkes BlackBoard Interface - BatteryInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008  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/BatteryInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class BatteryInterface <interfaces/BatteryInterface.h>
00034  * BatteryInterface Fawkes BlackBoard Interface.
00035  * This interface contains status information about the
00036     battery. In addition to this it allows to send messages which
00037     turn the battery on/off
00038  * @ingroup FawkesInterfaces
00039  */
00040
00041
00042 
00043 /** Constructor */
00044 BatteryInterface::BatteryInterface() : Interface()
00045 {
00046   data_size = sizeof(BatteryInterface_data_t);
00047   data_ptr  = malloc(data_size);
00048   data      = (BatteryInterface_data_t *)data_ptr;
00049   memset(data_ptr, 0, data_size);
00050   add_fieldinfo(IFT_UINT, "current", 1, &data->current);
00051   add_fieldinfo(IFT_UINT, "voltage", 1, &data->voltage);
00052   add_fieldinfo(IFT_UINT, "temperature", 1, &data->temperature);
00053   add_fieldinfo(IFT_FLOAT, "absolute_soc", 1, &data->absolute_soc);
00054   add_fieldinfo(IFT_FLOAT, "relative_soc", 1, &data->relative_soc);
00055   add_messageinfo("PushButtonMessage");
00056   add_messageinfo("SleepMessage");
00057   unsigned char tmp_hash[] = {0xaf, 0x87, 0xbb, 0x32, 0x19, 0x6b, 0x9, 0x3d, 0x7a, 0x6c, 0xf0, 0x4a, 0xb0, 0xd8, 0xa, 0x1d};
00058   set_hash(tmp_hash);
00059 }
00060 
00061 /** Destructor */
00062 BatteryInterface::~BatteryInterface()
00063 {
00064   free(data_ptr);
00065 }
00066 /* Methods */
00067 /** Get current value.
00068  * Battery Current [mA]
00069  * @return current value
00070  */
00071 unsigned int
00072 BatteryInterface::current() const
00073 {
00074   return data->current;
00075 }
00076 
00077 /** Get maximum length of current value.
00078  * @return length of current value, can be length of the array or number of 
00079  * maximum number of characters for a string
00080  */
00081 size_t
00082 BatteryInterface::maxlenof_current() const
00083 {
00084   return 1;
00085 }
00086 
00087 /** Set current value.
00088  * Battery Current [mA]
00089  * @param new_current new current value
00090  */
00091 void
00092 BatteryInterface::set_current(const unsigned int new_current)
00093 {
00094   data->current = new_current;
00095 }
00096 
00097 /** Get voltage value.
00098  * Battery Voltage [mV]
00099  * @return voltage value
00100  */
00101 unsigned int
00102 BatteryInterface::voltage() const
00103 {
00104   return data->voltage;
00105 }
00106 
00107 /** Get maximum length of voltage value.
00108  * @return length of voltage value, can be length of the array or number of 
00109  * maximum number of characters for a string
00110  */
00111 size_t
00112 BatteryInterface::maxlenof_voltage() const
00113 {
00114   return 1;
00115 }
00116 
00117 /** Set voltage value.
00118  * Battery Voltage [mV]
00119  * @param new_voltage new voltage value
00120  */
00121 void
00122 BatteryInterface::set_voltage(const unsigned int new_voltage)
00123 {
00124   data->voltage = new_voltage;
00125 }
00126 
00127 /** Get temperature value.
00128  * Battery Temperature [°C]
00129  * @return temperature value
00130  */
00131 unsigned int
00132 BatteryInterface::temperature() const
00133 {
00134   return data->temperature;
00135 }
00136 
00137 /** Get maximum length of temperature value.
00138  * @return length of temperature value, can be length of the array or number of 
00139  * maximum number of characters for a string
00140  */
00141 size_t
00142 BatteryInterface::maxlenof_temperature() const
00143 {
00144   return 1;
00145 }
00146 
00147 /** Set temperature value.
00148  * Battery Temperature [°C]
00149  * @param new_temperature new temperature value
00150  */
00151 void
00152 BatteryInterface::set_temperature(const unsigned int new_temperature)
00153 {
00154   data->temperature = new_temperature;
00155 }
00156 
00157 /** Get absolute_soc value.
00158  * Absolute state of charge [%]
00159  * @return absolute_soc value
00160  */
00161 float
00162 BatteryInterface::absolute_soc() const
00163 {
00164   return data->absolute_soc;
00165 }
00166 
00167 /** Get maximum length of absolute_soc value.
00168  * @return length of absolute_soc value, can be length of the array or number of 
00169  * maximum number of characters for a string
00170  */
00171 size_t
00172 BatteryInterface::maxlenof_absolute_soc() const
00173 {
00174   return 1;
00175 }
00176 
00177 /** Set absolute_soc value.
00178  * Absolute state of charge [%]
00179  * @param new_absolute_soc new absolute_soc value
00180  */
00181 void
00182 BatteryInterface::set_absolute_soc(const float new_absolute_soc)
00183 {
00184   data->absolute_soc = new_absolute_soc;
00185 }
00186 
00187 /** Get relative_soc value.
00188  * Relative state of charge [%]
00189  * @return relative_soc value
00190  */
00191 float
00192 BatteryInterface::relative_soc() const
00193 {
00194   return data->relative_soc;
00195 }
00196 
00197 /** Get maximum length of relative_soc value.
00198  * @return length of relative_soc value, can be length of the array or number of 
00199  * maximum number of characters for a string
00200  */
00201 size_t
00202 BatteryInterface::maxlenof_relative_soc() const
00203 {
00204   return 1;
00205 }
00206 
00207 /** Set relative_soc value.
00208  * Relative state of charge [%]
00209  * @param new_relative_soc new relative_soc value
00210  */
00211 void
00212 BatteryInterface::set_relative_soc(const float new_relative_soc)
00213 {
00214   data->relative_soc = new_relative_soc;
00215 }
00216
00217 /* =========== message create =========== */
00218 Message *
00219 BatteryInterface::create_message(const char *type) const
00220 {
00221   if ( strncmp("PushButtonMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00222     return new PushButtonMessage();
00223   } else if ( strncmp("SleepMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00224     return new SleepMessage();
00225   } else {
00226     throw UnknownTypeException("The given type '%s' does not match any known "
00227                                "message type for this interface type.", type);
00228   }
00229 }
00230
00231 
00232 /** Copy values from other interface.
00233  * @param other other interface to copy values from
00234  */
00235 void
00236 BatteryInterface::copy_values(const Interface *other)
00237 {
00238   const BatteryInterface *oi = dynamic_cast<const BatteryInterface *>(other);
00239   if (oi == NULL) {
00240     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00241                                 type(), other->type());
00242   }
00243   memcpy(data, oi->data, sizeof(BatteryInterface_data_t));
00244 }
00245
00246 /* =========== messages =========== */
00247 /** @class BatteryInterface::PushButtonMessage <interfaces/BatteryInterface.h>
00248  * PushButtonMessage Fawkes BlackBoard Interface Message.
00249  * 
00250     
00251  */
00252
00253 
00254 /** Constructor */
00255 BatteryInterface::PushButtonMessage::PushButtonMessage() : Message("PushButtonMessage")
00256 {
00257   data_size = 0;
00258   data_ptr  = NULL;
00259 }
00260 
00261 /** Destructor */
00262 BatteryInterface::PushButtonMessage::~PushButtonMessage()
00263 {
00264 }
00265 
00266 /** Copy constructor.
00267  * @param m message to copy from
00268  */
00269 BatteryInterface::PushButtonMessage::PushButtonMessage(const PushButtonMessage *m) : Message("PushButtonMessage")
00270 {
00271   data_size = 0;
00272   data_ptr  = NULL;
00273 }
00274
00275 /* Methods */
00276 /** Clone this message.
00277  * Produces a message of the same type as this message and copies the
00278  * data to the new message.
00279  * @return clone of this message
00280  */
00281 Message *
00282 BatteryInterface::PushButtonMessage::clone() const
00283 {
00284   return new BatteryInterface::PushButtonMessage(this);
00285 }
00286 /** @class BatteryInterface::SleepMessage <interfaces/BatteryInterface.h>
00287  * SleepMessage Fawkes BlackBoard Interface Message.
00288  * 
00289     
00290  */
00291
00292 
00293 /** Constructor */
00294 BatteryInterface::SleepMessage::SleepMessage() : Message("SleepMessage")
00295 {
00296   data_size = 0;
00297   data_ptr  = NULL;
00298 }
00299 
00300 /** Destructor */
00301 BatteryInterface::SleepMessage::~SleepMessage()
00302 {
00303 }
00304 
00305 /** Copy constructor.
00306  * @param m message to copy from
00307  */
00308 BatteryInterface::SleepMessage::SleepMessage(const SleepMessage *m) : Message("SleepMessage")
00309 {
00310   data_size = 0;
00311   data_ptr  = NULL;
00312 }
00313
00314 /* Methods */
00315 /** Clone this message.
00316  * Produces a message of the same type as this message and copies the
00317  * data to the new message.
00318  * @return clone of this message
00319  */
00320 Message *
00321 BatteryInterface::SleepMessage::clone() const
00322 {
00323   return new BatteryInterface::SleepMessage(this);
00324 }
00325 /** Check if message is valid and can be enqueued.
00326  * @param message Message to check
00327  */
00328 bool
00329 BatteryInterface::message_valid(const Message *message) const
00330 {
00331   const PushButtonMessage *m0 = dynamic_cast<const PushButtonMessage *>(message);
00332   if ( m0 != NULL ) {
00333     return true;
00334   }
00335   const SleepMessage *m1 = dynamic_cast<const SleepMessage *>(message);
00336   if ( m1 != NULL ) {
00337     return true;
00338   }
00339   return false;
00340 }
00341 
00342 /// @cond INTERNALS
00343 EXPORT_INTERFACE(BatteryInterface)
00344 /// @endcond
00345 
00346
00347 } // end namespace fawkes