Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WorldStateSerializerBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_WorldStateSerializer_h
00003 #define INCLUDED_WorldStateSerializer_h
00004 
00005 #include "Behaviors/BehaviorBase.h"
00006 #include "Shared/Config.h"
00007 #include <string>
00008 
00009 class Socket;
00010 
00011 //! Copies WorldState into a buffer for transmission over the network
00012 /*! To determine the communication protocol, just look in the
00013  *  doEvent() function - it's pretty straightforward binary copy
00014  *  of values.
00015  *
00016  *  Protocol:
00017  *  - <@c char[]: modelName> (null terminated character array)
00018  *  - <@c unsigned @c int: timestamp>
00019  *  - <@c unsigned @c int: framenumber>
00020  *  - <@c unsigned @c int: ::NumOutputs>
00021  *  - for each <i>i</i> of ::NumOutputs:
00022  *    - <@c float: position of output <i>i</i>>
00023  *  - <@c unsigned @c int: ::NumSensors>
00024  *  - for each <i>i</i> of ::NumSensors:
00025  *    - <@c float: value of sensor <i>i</i>>
00026  *  - <@c unsigned @c int: ::NumButtons>
00027  *  - for each <i>i</i> of ::NumButtons:
00028  *    - <@c float: value of button <i>i</i>>
00029  *  - <@c unsigned @c int: ::NumPIDJoints>
00030  *  - for each <i>i</i> of ::NumPIDJoints:
00031  *    - <@c float: duty cycle of joint <i>i</i>>
00032  * */
00033 class WorldStateSerializerBehavior : public BehaviorBase {
00034 public:
00035   WorldStateSerializerBehavior(); //!< constructor
00036   
00037   virtual void doStart(); //!< starts listening for sensor update events
00038   virtual void doStop(); //!< stops listening for events
00039   virtual void doEvent(); //!< core functionality - performs serialization, sends to sockets
00040   static std::string getClassDescription() {
00041     char tmp[80];
00042     sprintf(tmp,"Sends sensor information to port %d and current pid values to port %d",*config->main.wsjoints_port,*config->main.wspids_port);
00043     return tmp;
00044   }
00045   virtual std::string getDescription() const { return getClassDescription(); }
00046   
00047   //! returns string corresponding to the Java GUI which should be launched
00048   virtual std::string getGUIType() const { return "org.tekkotsu.mon.WorldStateRecordGUI"; }
00049   //! returns port number the Java GUI should connect to
00050   virtual unsigned int getPort() const { return config->main.wsjoints_port; }
00051   
00052 protected:
00053   //! writes @a value to @a dst and advances @a dst by sizeof(T)
00054   /*! doesn't do any byte swapping, so this is only used if LoadSave indicates no byte swapping is needed */
00055   template<class T>
00056   inline static void copy(char **dst, const T& value) {
00057     memcpy(*dst, &value, sizeof(T));
00058     (*dst) += sizeof(T);
00059   }
00060 
00061   //! writes @a num copies of T from @a src to @a dst and advances @a dst by @a num * sizeof(T)
00062   /*! doesn't do any byte swapping, so this is only used if LoadSave indicates no byte swapping is needed */
00063   template<class T>
00064   inline static void copy(char **dst, const T * src, size_t num) {
00065     memcpy(*dst, src, num*sizeof(T));
00066     (*dst) += num*sizeof(T);
00067   }
00068   
00069   //! writes @a num characters from @a src to @a dst and advances @a dst by @a num * sizeof(T)
00070   inline static void copy(char **dst, const std::string& src, size_t num) {
00071     memcpy(*dst, src.c_str(), num*sizeof(std::string::value_type));
00072     (*dst) += num*sizeof(std::string::value_type);
00073   }
00074 
00075   Socket *wsJoints; //!< socket for sending current joint data
00076   Socket *wsPIDs; //!< socket for sending current PID info
00077   unsigned int lastProcessedTime; //!< the time that the last event was processed
00078   
00079 private:
00080   WorldStateSerializerBehavior(const WorldStateSerializerBehavior&); //!< don't call
00081   WorldStateSerializerBehavior& operator= (const WorldStateSerializerBehavior&); //!< don't call
00082 };
00083 
00084 /*! @file
00085  * @brief Describes WorldStateSerializerBehavior, which copies WorldState into a buffer for transmission over the network
00086  * @author alokl (Creator)
00087  */
00088 
00089 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:53 2016 by Doxygen 1.6.3