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 unsigned @c int: timestamp>
00017  *  - <@c unsigned @c int: ::NumPIDJoints>
00018  *  - for each <i>i</i> of ::NumPIDJoints:
00019  *    - <@c float: position of joint <i>i</i>>
00020  *  - <@c unsigned @c int: ::NumSensors>
00021  *  - for each <i>i</i> of ::NumSensors:
00022  *    - <@c float: value of sensor <i>i</i>>
00023  *  - <@c unsigned @c int: ::NumButtons>
00024  *  - for each <i>i</i> of ::NumButtons:
00025  *    - <@c float: value of button <i>i</i>>
00026  *  - for each <i>i</i> of ::NumPIDJoints:
00027  *    - <@c float: duty cycle of joint <i>i</i>>
00028  * */
00029 class WorldStateSerializerBehavior : public BehaviorBase {
00030 public:
00031   WorldStateSerializerBehavior(); //!< constructor
00032 
00033   virtual void DoStart(); //!< starts listening for sensor update events
00034   virtual void DoStop(); //!< stops listening for events
00035   virtual void processEvent(const EventBase& e); //!< core functionality - performs serialization, sends to sockets
00036   static std::string getClassDescription() {
00037     char tmp[80];
00038     sprintf(tmp,"Sends sensor information to port %d and current pid values to port %d",config->main.wsjoints_port,config->main.wspids_port);
00039     return tmp;
00040   }
00041   virtual std::string getDescription() const { return getClassDescription(); }
00042 
00043 protected:
00044   //! writes @a value to @a dst and advances @a dst
00045   template<class T>
00046   inline static void encode(char **dst, const T& value) {
00047     memcpy(*dst, &value, sizeof(T));
00048     // it'd be nice to use network byte order, but we'll save the aibo extra work
00049     //hostToNetwork(*dst, (char *)&value, sizeof(T));
00050     (*dst) += sizeof(T);
00051   }
00052 
00053   //! writes @a length bytes from @a src to @a dst
00054   template<class T>
00055   inline static void encode(char **dst, const T * src, int num) {
00056     memcpy(*dst, src, num*sizeof(T));
00057     (*dst) += num*sizeof(T);
00058   }
00059 
00060   Socket *wsJoints; //!< socket for sending current joint data
00061   Socket *wsPIDs; //!< socket for sending current PID info
00062   unsigned int lastProcessedTime; //!< the time that the last event was processed
00063 
00064 private:
00065   WorldStateSerializerBehavior(const WorldStateSerializerBehavior&); //!< don't call
00066   WorldStateSerializerBehavior& operator= (const WorldStateSerializerBehavior&); //!< don't call
00067 };
00068 
00069 /*! @file
00070  * @brief Describes WorldStateSerializerBehavior, which copies WorldState into a buffer for transmission over the network
00071  * @author alokl (Creator)
00072  *
00073  * $Author: ejt $
00074  * $Name: tekkotsu-2_4_1 $
00075  * $Revision: 1.6 $
00076  * $State: Exp $
00077  * $Date: 2005/02/21 08:05:57 $
00078  */
00079 
00080 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:50 2005 by Doxygen 1.4.4