Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

LocomotionEvent.cc

Go to the documentation of this file.
00001 #include "LocomotionEvent.h"
00002 #include <sstream>
00003 #include <libxml/tree.h>
00004 #include <iostream>
00005 
00006 using namespace std;
00007 
00008 const EventBase::classTypeID_t LocomotionEvent::autoRegisterLocomotionEvent=getTypeRegistry().registerType<LocomotionEvent>(makeClassTypeID("LOCO"));
00009 
00010 std::string
00011 LocomotionEvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00012   if(!showTypeSpecific)
00013     return EventBase::getDescription(showTypeSpecific,verbosity);
00014   std::ostringstream logdata;
00015   logdata << EventBase::getDescription(showTypeSpecific,verbosity) << '\t' << x << '\t' << y << '\t' << a;
00016   return logdata.str();
00017 }
00018   
00019 unsigned int
00020 LocomotionEvent::getBinSize() const {
00021   unsigned int used=EventBase::getBinSize();
00022   if(saveFormat==XML)
00023     return used; //if using XML, the XMLLoadSave::getBinSize (called by EventBase::getBinSize) is all we need
00024   //otherwise need to add our own fields
00025   used+=creatorSize("EventBase::LocomotionEvent");
00026   used+=getSerializedSize(x);
00027   used+=getSerializedSize(y);
00028   used+=getSerializedSize(a);
00029   return used;
00030 }
00031 
00032 unsigned int
00033 LocomotionEvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00034   unsigned int origlen=len;
00035   if(!checkInc(EventBase::loadBinaryBuffer(buf,len),buf,len)) return 0;
00036   if(!checkCreatorInc("EventBase::LocomotionEvent",buf,len,true)) return 0;
00037   if(!decodeInc(x,buf,len)) return 0;
00038   if(!decodeInc(y,buf,len)) return 0;
00039   if(!decodeInc(a,buf,len)) return 0;
00040   return origlen-len; 
00041 }
00042 
00043 unsigned int
00044 LocomotionEvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00045   unsigned int origlen=len;
00046   if(!checkInc(EventBase::saveBinaryBuffer(buf,len),buf,len)) return 0;
00047   if(!saveCreatorInc("EventBase::LocomotionEvent",buf,len)) return 0;
00048   if(!encodeInc(x,buf,len)) return 0;
00049   if(!encodeInc(y,buf,len)) return 0;
00050   if(!encodeInc(a,buf,len)) return 0;
00051   return origlen-len;
00052 }
00053 
00054 void
00055 LocomotionEvent::loadXML(xmlNode* node) {
00056   if(node==NULL)
00057     return;
00058   
00059   EventBase::loadXML(node);
00060   
00061   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00062     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00063       continue;
00064     
00065     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00066     if(name==NULL)
00067       throw bad_format(cur,"property missing name");
00068     
00069     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00070     if(val==NULL)
00071       throw bad_format(cur,"property missing value");
00072     
00073     //cout << "loadXML: " << name << "=" << val << endl;
00074     
00075     if(xmlStrcmp(name, (const xmlChar *)"x")==0)
00076       x=(float)atof((const char*)val);
00077     else if(xmlStrcmp(name, (const xmlChar *)"y")==0)
00078       y=(float)atof((const char*)val);
00079     else if(xmlStrcmp(name, (const xmlChar *)"a")==0)
00080       a=(float)atof((const char*)val);
00081     
00082     xmlFree(val);
00083     xmlFree(name);
00084   }
00085 }
00086 
00087 //! a little local macro to make saving fields easier
00088 #define SAVE_PARAM(name) { \
00089 xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL); \
00090 if(cur==NULL) \
00091 throw bad_format(node,"Error: LocomotionEvent xml error on saving param"); \
00092 xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)#name); \
00093 char valbuf[20]; \
00094 snprintf(valbuf,20,"%g",name); \
00095 xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)valbuf); }
00096 
00097 void
00098 LocomotionEvent::saveXML(xmlNode * node) const {
00099   if(node==NULL)
00100     return;
00101   EventBase::saveXML(node);
00102   
00103   //clear old params first
00104   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00105     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00106       xmlUnlinkNode(cur);
00107       xmlFreeNode(cur);
00108       cur = skipToElement(node->children); //restart the search (boo)
00109     } else
00110       cur = skipToElement(cur->next);
00111   }
00112   
00113   //cout << "saveXML: " << x << ' ' << y << ' ' << a << endl;
00114 
00115   SAVE_PARAM(x);
00116   SAVE_PARAM(y);
00117   SAVE_PARAM(a);
00118 }
00119 
00120 /*! @file
00121  * @brief Implements LocomotionEvent, which gives updates regarding the current movement of the robot through the world
00122  * @author ejt (Creator)
00123  */

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