Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WorldStateSerializerBehavior.cc

Go to the documentation of this file.
00001 #include "WorldStateSerializerBehavior.h"
00002 #include "Shared/WorldState.h"
00003 #include "Wireless/Wireless.h"
00004 #include "Shared/Config.h"
00005 #include "Behaviors/Controller.h"
00006 #include "Events/EventRouter.h"
00007 #include "Shared/LoadSave.h"
00008 
00009 WorldStateSerializerBehavior::WorldStateSerializerBehavior()
00010 : BehaviorBase("WorldStateSerializerBehavior"), wsJoints(NULL), wsPIDs(NULL), lastProcessedTime(0)
00011 {
00012 }
00013 
00014 void WorldStateSerializerBehavior::DoStart() {
00015   BehaviorBase::DoStart(); // do this first
00016   
00017   const unsigned int transmitPIDSize=(NumPIDJoints*3)*LoadSave::getSerializedSize<float>()+2*LoadSave::getSerializedSize<unsigned int>();
00018   wsPIDs=wireless->socket(SocketNS::SOCK_STREAM, 1024, transmitPIDSize*4); // enough for 4 packets queued up
00019   wireless->setDaemon(wsPIDs);
00020   wireless->listen(wsPIDs, config->main.wspids_port);
00021 
00022   const size_t robotNameLen=strlen(RobotName)+1; //note the +1 to include '\0' at the end
00023   const size_t transmitJointsSize=(NumOutputs+NumSensors+NumButtons+NumPIDJoints)*LoadSave::getSerializedSize<float>()+6*LoadSave::getSerializedSize<unsigned int>()+robotNameLen;
00024   wsJoints=wireless->socket(SocketNS::SOCK_STREAM, 1024, transmitJointsSize*4); // enough for 4 packets queued up
00025   wireless->setDaemon(wsJoints);
00026   wireless->listen(wsJoints, config->main.wsjoints_port);
00027     
00028   Controller::loadGUI(getGUIType(),getGUIType(),getPort());
00029   
00030   erouter->addListener(this,EventBase::sensorEGID);
00031 }
00032 
00033 void WorldStateSerializerBehavior::DoStop() {
00034   erouter->removeListener(this);
00035   
00036   Controller::closeGUI(getGUIType());
00037   
00038   wireless->setDaemon(wsJoints,false);
00039   wireless->close(wsJoints);
00040   wireless->setDaemon(wsPIDs,false);
00041   wireless->close(wsPIDs);
00042   
00043   BehaviorBase::DoStop(); // do this last
00044 }
00045 
00046 void WorldStateSerializerBehavior::processEvent(const EventBase& e) {
00047   const size_t transmitPIDSize=(NumPIDJoints*3)*LoadSave::getSerializedSize<float>()+2*LoadSave::getSerializedSize<unsigned int>();
00048   if ((e.getTimeStamp() - lastProcessedTime) < config->main.worldState_interval) // not enough time has gone by
00049     return;
00050   lastProcessedTime = e.getTimeStamp();
00051   char *buf=(char*)wsPIDs->getWriteBuffer(transmitPIDSize);
00052   if(buf==NULL) {
00053     if(wireless->isConnected(wsPIDs->sock))
00054       std::cout << "WorldStateSerializer dropped pid at " << e.getTimeStamp() << std::endl;
00055   } else {
00056 #if LOADSAVE_SWAPBYTES
00057     // if we need to swap bytes, need to use this slightly slower method
00058     unsigned int remain=transmitPIDSize;
00059     LoadSave::encodeInc(state->lastSensorUpdateTime,buf,remain);
00060     LoadSave::encodeInc(NumPIDJoints,buf,remain);
00061     for(unsigned int i=0; i<NumPIDJoints; ++i)
00062       for(unsigned int j=0; j<3; ++j)
00063         LoadSave::encodeInc(state->pids[i][j],buf,remain);
00064     ASSERT(remain==0,"buffer remains");
00065     wsPIDs->write(transmitPIDSize-remain);
00066 #else
00067     // this is the original version, doesn't handle byte swapping, but faster
00068     copy(&buf,state->lastSensorUpdateTime);
00069     copy(&buf,NumPIDJoints);
00070     copy(&buf,state->pids,NumPIDJoints*3);
00071     wsPIDs->write(transmitPIDSize);
00072 #endif
00073   }
00074   
00075   const size_t robotNameLen=strlen(RobotName)+1; //note the +1 to include '\0' at the end
00076   const size_t transmitJointsSize=(NumOutputs+NumSensors+NumButtons+NumPIDJoints)*LoadSave::getSerializedSize<float>()+6*LoadSave::getSerializedSize<unsigned int>()+robotNameLen;
00077   //std::cout << "transmitting at " << e.getTimeStamp() << " with " << (wsJoints->getAvailableSendSize() / float(transmitJointsSize)) << std::endl;
00078   buf=(char*)wsJoints->getWriteBuffer(transmitJointsSize);
00079   if(buf==NULL) {
00080     if(wireless->isConnected(wsJoints->sock))
00081       std::cout << "WorldStateSerializer dropped state at " << e.getTimeStamp() << std::endl;
00082   } else {
00083 #if LOADSAVE_SWAPBYTES
00084     // if we need to swap bytes, need to use this slightly slower method
00085     unsigned int remain=transmitJointsSize;
00086     memcpy(buf,RobotName,robotNameLen);
00087     buf+=robotNameLen; remain-=robotNameLen;
00088     LoadSave::encodeInc(state->lastSensorUpdateTime,buf,remain);
00089     LoadSave::encodeInc(state->frameNumber,buf,remain);
00090     LoadSave::encodeInc(NumOutputs,buf,remain);
00091     for(unsigned int i=0; i<NumOutputs; ++i)
00092       LoadSave::encodeInc(state->outputs[i],buf,remain);
00093     LoadSave::encodeInc(NumSensors,buf,remain);
00094     for(unsigned int i=0; i<NumSensors; ++i)
00095       LoadSave::encodeInc(state->sensors[i],buf,remain);
00096     LoadSave::encodeInc(NumButtons,buf,remain);
00097     for(unsigned int i=0; i<NumButtons; ++i)
00098       LoadSave::encodeInc(state->buttons[i],buf,remain);
00099     LoadSave::encodeInc(NumPIDJoints,buf,remain);
00100     for(unsigned int i=0; i<NumPIDJoints; ++i)
00101       LoadSave::encodeInc(state->pidduties[i],buf,remain);
00102     ASSERT(remain==0,"buffer remains");
00103     wsJoints->write(transmitJointsSize-remain);
00104 #else
00105     // this is the original version, doesn't handle byte swapping, but faster
00106     copy(&buf,RobotName,robotNameLen);
00107     copy(&buf,state->lastSensorUpdateTime);
00108     copy(&buf,state->frameNumber);
00109     copy(&buf,NumOutputs);
00110     copy(&buf,state->outputs,NumOutputs);
00111     copy(&buf,NumSensors);
00112     copy(&buf,state->sensors,NumSensors);
00113     copy(&buf,NumButtons);
00114     copy(&buf,state->buttons,NumButtons);
00115     copy(&buf,NumPIDJoints);
00116     copy(&buf,state->pidduties,NumPIDJoints);
00117     wsJoints->write(transmitJointsSize);
00118 #endif
00119   }
00120 }
00121 
00122 
00123 /*! @file
00124 * @brief Implements WorldStateSerializer, which copies WorldState into a buffer for transmission over the network
00125 * @author alokl (Creator)
00126 *
00127 * $Author: ejt $
00128 * $Name: tekkotsu-3_0 $
00129 * $Revision: 1.10 $
00130 * $State: Exp $
00131 * $Date: 2006/09/19 22:51:38 $
00132 */

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