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 
00005 #include <iostream>
00006 using namespace std;
00007 
00008 std::string
00009 LocomotionEvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00010   if(!showTypeSpecific)
00011     return EventBase::getDescription(showTypeSpecific,verbosity);
00012   std::ostringstream logdata;
00013   logdata << EventBase::getDescription(showTypeSpecific,verbosity) << '\t' << x << '\t' << y << '\t' << a;
00014   return logdata.str();
00015 }
00016   
00017 unsigned int
00018 LocomotionEvent::getBinSize() const {
00019   unsigned int used=EventBase::getBinSize();
00020   if(saveFormat==XML)
00021     return used; //if using XML, the XMLLoadSave::getBinSize (called by EventBase::getBinSize) is all we need
00022   //otherwise need to add our own fields
00023   used+=creatorSize("EventBase::LocomotionEvent");
00024   used+=sizeof(x);
00025   used+=sizeof(y);
00026   used+=sizeof(a);
00027   return used;
00028 }
00029 
00030 unsigned int
00031 LocomotionEvent::LoadBinaryBuffer(const char buf[], unsigned int len) {
00032   unsigned int origlen=len;
00033   unsigned int used;
00034   if(0==(used=EventBase::LoadBinaryBuffer(buf,len))) return 0;
00035   len-=used; buf+=used;
00036   if(0==(used=checkCreator("EventBase::LocomotionEvent",buf,len,true))) return 0;
00037   len-=used; buf+=used;
00038   if(0==(used=decode(x,buf,len))) return 0;
00039   len-=used; buf+=used;
00040   if(0==(used=decode(y,buf,len))) return 0;
00041   len-=used; buf+=used;
00042   if(0==(used=decode(a,buf,len))) return 0;
00043   len-=used; buf+=used;
00044   return origlen-len; 
00045 }
00046 
00047 unsigned int
00048 LocomotionEvent::SaveBinaryBuffer(char buf[], unsigned int len) const {
00049   unsigned int origlen=len;
00050   unsigned int used;
00051   if(0==(used=EventBase::SaveBinaryBuffer(buf,len))) return 0;
00052   len-=used; buf+=used;
00053   if(0==(used=saveCreator("EventBase::LocomotionEvent",buf,len))) return 0;
00054   len-=used; buf+=used;
00055   if(0==(used=encode(x,buf,len))) return 0;
00056   len-=used; buf+=used;
00057   if(0==(used=encode(y,buf,len))) return 0;
00058   len-=used; buf+=used;
00059   if(0==(used=encode(a,buf,len))) return 0;
00060   len-=used; buf+=used;
00061   return origlen-len;
00062 }
00063 
00064 void LocomotionEvent::LoadXML(xmlNode* node) {
00065   if(node==NULL)
00066     return;
00067   
00068   EventBase::LoadXML(node);
00069   
00070   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00071     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00072       continue;
00073     
00074     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00075     if(name==NULL)
00076       throw bad_format(cur,"property missing name");
00077     
00078     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00079     if(val==NULL)
00080       throw bad_format(cur,"property missing value");
00081     
00082     cout << "LoadXML: " << name << "=" << val << endl;
00083     
00084     if(xmlStrcmp(name, (const xmlChar *)"x")==0)
00085       x=atof((const char*)val);
00086     else if(xmlStrcmp(name, (const xmlChar *)"y")==0)
00087       y=atof((const char*)val);
00088     else if(xmlStrcmp(name, (const xmlChar *)"a")==0)
00089       a=atof((const char*)val);
00090     
00091     xmlFree(val);
00092     xmlFree(name);
00093   }
00094 }
00095 
00096 //! a little local macro to make saving fields easier
00097 #define SAVE_PARAM(name) { \
00098 xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL); \
00099 if(cur==NULL) \
00100 throw bad_format(node,"Error: LocomotionEvent xml error on saving param"); \
00101 xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)#name); \
00102 char valbuf[20]; \
00103 snprintf(valbuf,20,"%g",name); \
00104 xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)valbuf); }
00105 
00106 void LocomotionEvent::SaveXML(xmlNode * node) const {
00107   if(node==NULL)
00108     return;
00109   EventBase::SaveXML(node);
00110   
00111   //clear old params first
00112   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00113     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00114       xmlUnlinkNode(cur);
00115       xmlFreeNode(cur);
00116       cur = skipToElement(node->children); //restart the search (boo)
00117     } else
00118       cur = skipToElement(cur->next);
00119   }
00120   
00121   cout << "SaveXML: " << x << ' ' << y << ' ' << a << endl;
00122 
00123   SAVE_PARAM(x);
00124   SAVE_PARAM(y);
00125   SAVE_PARAM(a);
00126 }
00127 
00128 /*! @file
00129  * @brief Implements LocomotionEvent, which gives updates regarding the current movement of the robot through the world
00130  * @author ejt (Creator)
00131  *
00132  * $Author: ejt $
00133  * $Name: tekkotsu-2_4_1 $
00134  * $Revision: 1.4 $
00135  * $State: Exp $
00136  * $Date: 2005/06/29 22:03:35 $
00137  */

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