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

Tekkotsu v3.0
Generated Wed Oct 4 00:03:47 2006 by Doxygen 1.4.7