Homepage Demos Overview Downloads Tutorials Reference
Credits

WorldState.cc

Go to the documentation of this file.
00001 #ifdef PLATFORM_APERIOS
00002 #include <OPENR/core_macro.h>
00003 #include <OPENR/ObjcommTypes.h>
00004 #include <OPENR/OPENR.h>
00005 #include <OPENR/OPENRAPI.h>
00006 #include <OPENR/OPENRMessages.h>
00007 #include <OPENR/OPower.h>
00008 #endif
00009 
00010 #include "WorldState.h"
00011 #include "Shared/get_time.h"
00012 #include "Events/EventRouter.h"
00013 #include "ERS210Info.h"
00014 #include "ERS220Info.h"
00015 #include "ERS7Info.h"
00016 #include "Shared/Config.h"
00017 
00018 #define GETD(cpc) (((float)sensor.GetData(cpc)->frame[lastFrame].value) / 1.0E6f) //!< returns value from OPEN-R, converted from micro in int to base in float
00019 #define GETB(cpc) ((bool)sensor.GetData(cpc)->frame[lastFrame].value) //!< returns value from OPEN-R, as bool
00020 #define GETSENSOR(cpc) ((float)sensor.GetData(cpc)->frame[lastFrame].value) //!< return value from OPEN-R, as int
00021 #define GETSIG(cpc) ((word)sensor.GetData(cpc)->frame[lastFrame].signal) //!< returns signal from OPEN-R as word
00022 #define GETDUTY(cpc) ((float)((OJointValue*)&sensor.GetData(cpc)->frame[lastFrame])->pwmDuty/512.0f) //!< returns duty cycle from OPEN-R as float; -1 (full reverse) to 0 (idle) to 1 (full forward)
00023 
00024 const double WorldState::g=9.80665;
00025 const double WorldState::IROORDist = 900.0;
00026 
00027 WorldState * state=NULL;
00028 
00029 WorldState::WorldState()
00030   : alwaysGenerateStatus(false), vel_x(0), vel_y(0), vel_a(0), vel_time(0),
00031     robotStatus(0), batteryStatus(0),
00032     lastSensorUpdateTime(0), mainProfile(), motionProfile(),
00033     robotDesign(0), curtime(get_time())
00034 {
00035   for(unsigned int i=0; i< NumOutputs; i++)
00036     outputs[i]=0;
00037   for(unsigned int i=0; i< NumButtons; i++)
00038     buttons[i]=0;
00039   for(unsigned int i=0; i< NumSensors; i++)
00040     sensors[i]=0;
00041   for(unsigned int i=0; i< NumPIDJoints; i++)
00042     for(unsigned int j=0; j<3; j++)
00043       pids[i][j]=0;
00044   for(unsigned int i=0; i< NumPIDJoints; i++)
00045     pidduties[i]=0;
00046   memset(powerFlags,0,sizeof(unsigned int)*PowerSourceID::NumPowerSIDs);
00047   memset(button_times,0,sizeof(unsigned int)*NumButtons);
00048 
00049 #ifdef PLATFORM_APERIOS
00050   //Thanks Daishi:
00051   char robotDesignStr[orobotdesignNAME_MAX + 1];
00052   memset(robotDesignStr, 0, sizeof(robotDesignStr));
00053   if (OPENR::GetRobotDesign(robotDesignStr) != oSUCCESS) {
00054     cout << "OPENR::GetRobotDesign() failed." << endl;
00055   } else {
00056     if(strcmp(robotDesignStr,"ERS-210")==0)
00057       robotDesign=ERS210Mask;
00058     else if(strcmp(robotDesignStr,"ERS-220")==0)
00059       robotDesign=ERS220Mask;
00060     else if(strcmp(robotDesignStr,"ERS-7")==0)
00061       robotDesign=ERS7Mask;
00062     else {
00063       cout << "ERROR: Unrecognized model: "<<robotDesignStr<<"\nTrying ERS-210..."<<endl;
00064       robotDesign=ERS210Mask;
00065     }
00066   }
00067 #else
00068 # if TGT_ERS220
00069   robotDesign=ERS220Mask;
00070 # elif TGT_ERS210
00071   robotDesign=ERS210Mask;
00072 # elif TGT_ERS7
00073   robotDesign=ERS7Mask;
00074 # else //default case, ERS2xx
00075 #  if TGT_ERS2xx==0
00076 #   warning "TGT_<model> undefined - defaulting to TGT_ERS210"
00077 #  else
00078 #   warning "TGT_ERS2xx can't be determined on non-Aperios - defaulting to TGT_ERS210"
00079 #  endif
00080   robotDesign=ERS210Mask;
00081 # endif //model selection
00082 #endif
00083 }
00084 
00085 #ifdef PLATFORM_APERIOS
00086 
00087 /*! This will cause events to be posted
00088  *  @todo change to use most recent instead of oldest - is a buffer! */
00089 void WorldState::read(OSensorFrameVectorData& sensor, EventRouter* er) {
00090   curtime=get_time();
00091 
00092   std::vector<EventBase*> evtBuf;
00093   unsigned int lastFrame=sensor.GetInfo(0)->numFrames-1;
00094 
00095   if(robotDesign&ERS210Mask) {
00096     outputs[LFrLegOffset + RotatorOffset   ] = GETD(ERS210Info::CPCJointLFRotator);
00097     outputs[LFrLegOffset + ElevatorOffset  ] = GETD(ERS210Info::CPCJointLFElevator);
00098     outputs[LFrLegOffset + KneeOffset      ] = GETD(ERS210Info::CPCJointLFKnee);
00099     pidduties[LFrLegOffset + RotatorOffset ] = GETDUTY(ERS210Info::CPCJointLFRotator);
00100     pidduties[LFrLegOffset + ElevatorOffset] = GETDUTY(ERS210Info::CPCJointLFElevator);
00101     pidduties[LFrLegOffset + KneeOffset    ] = GETDUTY(ERS210Info::CPCJointLFKnee);
00102   
00103     outputs[RFrLegOffset + RotatorOffset   ] = GETD(ERS210Info::CPCJointRFRotator);
00104     outputs[RFrLegOffset + ElevatorOffset  ] = GETD(ERS210Info::CPCJointRFElevator);
00105     outputs[RFrLegOffset + KneeOffset      ] = GETD(ERS210Info::CPCJointRFKnee);
00106     pidduties[RFrLegOffset + RotatorOffset ] = GETDUTY(ERS210Info::CPCJointRFRotator);
00107     pidduties[RFrLegOffset + ElevatorOffset] = GETDUTY(ERS210Info::CPCJointRFElevator);
00108     pidduties[RFrLegOffset + KneeOffset    ] = GETDUTY(ERS210Info::CPCJointRFKnee);
00109 
00110     outputs[LBkLegOffset + RotatorOffset   ] = GETD(ERS210Info::CPCJointLHRotator);
00111     outputs[LBkLegOffset + ElevatorOffset  ] = GETD(ERS210Info::CPCJointLHElevator);
00112     outputs[LBkLegOffset + KneeOffset      ] = GETD(ERS210Info::CPCJointLHKnee);
00113     pidduties[LBkLegOffset + RotatorOffset ] = GETDUTY(ERS210Info::CPCJointLHRotator);
00114     pidduties[LBkLegOffset + ElevatorOffset] = GETDUTY(ERS210Info::CPCJointLHElevator);
00115     pidduties[LBkLegOffset + KneeOffset    ] = GETDUTY(ERS210Info::CPCJointLHKnee);
00116 
00117     outputs[RBkLegOffset + RotatorOffset   ] = GETD(ERS210Info::CPCJointRHRotator);
00118     outputs[RBkLegOffset + ElevatorOffset  ] = GETD(ERS210Info::CPCJointRHElevator);
00119     outputs[RBkLegOffset + KneeOffset      ] = GETD(ERS210Info::CPCJointRHKnee);
00120     pidduties[RBkLegOffset + RotatorOffset ] = GETDUTY(ERS210Info::CPCJointRHRotator);
00121     pidduties[RBkLegOffset + ElevatorOffset] = GETDUTY(ERS210Info::CPCJointRHElevator);
00122     pidduties[RBkLegOffset + KneeOffset    ] = GETDUTY(ERS210Info::CPCJointRHKnee);
00123 
00124     // Get head tilt,pan,roll joint angles
00125     outputs[HeadOffset+TiltOffset] = GETD(ERS210Info::CPCJointNeckTilt);
00126     outputs[HeadOffset+PanOffset ] = GETD(ERS210Info::CPCJointNeckPan);
00127     outputs[HeadOffset+RollOffset] = GETD(ERS210Info::CPCJointNeckRoll);
00128     pidduties[HeadOffset+TiltOffset] = GETDUTY(ERS210Info::CPCJointNeckTilt);
00129     pidduties[HeadOffset+PanOffset ] = GETDUTY(ERS210Info::CPCJointNeckPan);
00130     pidduties[HeadOffset+RollOffset] = GETDUTY(ERS210Info::CPCJointNeckRoll);
00131 
00132     outputs[ERS210Info::TailOffset+TiltOffset] = GETD(ERS210Info::CPCJointTailTilt);
00133     outputs[ERS210Info::TailOffset+PanOffset]  = GETD(ERS210Info::CPCJointTailPan);
00134     pidduties[ERS210Info::TailOffset+TiltOffset] = GETDUTY(ERS210Info::CPCJointTailTilt);
00135     pidduties[ERS210Info::TailOffset+PanOffset]  = GETDUTY(ERS210Info::CPCJointTailPan);
00136     
00137     outputs[ERS210Info::MouthOffset] = GETD(ERS210Info::CPCJointMouth);
00138     pidduties[ERS210Info::MouthOffset] = GETDUTY(ERS210Info::CPCJointMouth);
00139 
00140     // Get foot switches
00141     chkEvent(evtBuf,LFrPawOffset,GETB(ERS210Info::CPCSensorLFPaw),"LFrPaw");
00142     chkEvent(evtBuf,RFrPawOffset,GETB(ERS210Info::CPCSensorRFPaw),"RFrPaw");
00143     chkEvent(evtBuf,LBkPawOffset,GETB(ERS210Info::CPCSensorLHPaw),"LBkPaw");
00144     chkEvent(evtBuf,RBkPawOffset,GETB(ERS210Info::CPCSensorRHPaw),"RBkPaw");
00145 
00146     // Get buttons
00147     chkEvent(evtBuf,ERS210Info::ChinButOffset,  GETB(ERS210Info::CPCSensorChinSwitch),"ChinBut");
00148     chkEvent(evtBuf,ERS210Info::BackButOffset,  GETB(ERS210Info::CPCSensorBackSwitch),"BackBut");
00149     chkEvent(evtBuf,ERS210Info::HeadFrButOffset,GETD(ERS210Info::CPCSensorHeadFrontPressure),"HeadFrBut");
00150     chkEvent(evtBuf,ERS210Info::HeadBkButOffset,GETD(ERS210Info::CPCSensorHeadBackPressure),"HeadBkBut");
00151 
00152     // Get IR distance sensor
00153     sensors[ERS210Info::IRDistOffset]=GETSENSOR(ERS210Info::CPCSensorPSD) / 1000.0f;
00154 
00155     // Get acceleration sensors
00156     sensors[BAccelOffset] = GETD(ERS210Info::CPCSensorAccelFB);
00157     sensors[LAccelOffset] = GETD(ERS210Info::CPCSensorAccelLR);
00158     sensors[DAccelOffset] = GETD(ERS210Info::CPCSensorAccelUD);
00159 
00160     sensors[ERS210Info::ThermoOffset] = GETD(ERS210Info::CPCSensorThermoSensor);
00161   }
00162 
00163   // (ERS-220 only)
00164   if(robotDesign&ERS220Mask) {
00165     outputs[LFrLegOffset + RotatorOffset   ] = GETD(ERS220Info::CPCJointLFRotator);
00166     outputs[LFrLegOffset + ElevatorOffset  ] = GETD(ERS220Info::CPCJointLFElevator);
00167     outputs[LFrLegOffset + KneeOffset      ] = GETD(ERS220Info::CPCJointLFKnee);
00168     pidduties[LFrLegOffset + RotatorOffset ] = GETDUTY(ERS220Info::CPCJointLFRotator);
00169     pidduties[LFrLegOffset + ElevatorOffset] = GETDUTY(ERS220Info::CPCJointLFElevator);
00170     pidduties[LFrLegOffset + KneeOffset    ] = GETDUTY(ERS220Info::CPCJointLFKnee);
00171   
00172     outputs[RFrLegOffset + RotatorOffset   ] = GETD(ERS220Info::CPCJointRFRotator);
00173     outputs[RFrLegOffset + ElevatorOffset  ] = GETD(ERS220Info::CPCJointRFElevator);
00174     outputs[RFrLegOffset + KneeOffset      ] = GETD(ERS220Info::CPCJointRFKnee);
00175     pidduties[RFrLegOffset + RotatorOffset ] = GETDUTY(ERS220Info::CPCJointRFRotator);
00176     pidduties[RFrLegOffset + ElevatorOffset] = GETDUTY(ERS220Info::CPCJointRFElevator);
00177     pidduties[RFrLegOffset + KneeOffset    ] = GETDUTY(ERS220Info::CPCJointRFKnee);
00178   
00179     outputs[LBkLegOffset + RotatorOffset   ] = GETD(ERS220Info::CPCJointLHRotator);
00180     outputs[LBkLegOffset + ElevatorOffset  ] = GETD(ERS220Info::CPCJointLHElevator);
00181     outputs[LBkLegOffset + KneeOffset      ] = GETD(ERS220Info::CPCJointLHKnee);
00182     pidduties[LBkLegOffset + RotatorOffset ] = GETDUTY(ERS220Info::CPCJointLHRotator);
00183     pidduties[LBkLegOffset + ElevatorOffset] = GETDUTY(ERS220Info::CPCJointLHElevator);
00184     pidduties[LBkLegOffset + KneeOffset    ] = GETDUTY(ERS220Info::CPCJointLHKnee);
00185 
00186     outputs[RBkLegOffset + RotatorOffset   ] = GETD(ERS220Info::CPCJointRHRotator);
00187     outputs[RBkLegOffset + ElevatorOffset  ] = GETD(ERS220Info::CPCJointRHElevator);
00188     outputs[RBkLegOffset + KneeOffset      ] = GETD(ERS220Info::CPCJointRHKnee);
00189     pidduties[RBkLegOffset + RotatorOffset ] = GETDUTY(ERS220Info::CPCJointRHRotator);
00190     pidduties[RBkLegOffset + ElevatorOffset] = GETDUTY(ERS220Info::CPCJointRHElevator);
00191     pidduties[RBkLegOffset + KneeOffset    ] = GETDUTY(ERS220Info::CPCJointRHKnee);
00192 
00193     // Get head tilt,pan,roll joint angles
00194     outputs[HeadOffset+TiltOffset] = GETD(ERS220Info::CPCJointNeckTilt);
00195     outputs[HeadOffset+PanOffset ] = GETD(ERS220Info::CPCJointNeckPan);
00196     outputs[HeadOffset+RollOffset] = GETD(ERS220Info::CPCJointNeckRoll);
00197     pidduties[HeadOffset+TiltOffset] = GETDUTY(ERS220Info::CPCJointNeckTilt);
00198     pidduties[HeadOffset+PanOffset ] = GETDUTY(ERS220Info::CPCJointNeckPan);
00199     pidduties[HeadOffset+RollOffset] = GETDUTY(ERS220Info::CPCJointNeckRoll);
00200 
00201     chkEvent(evtBuf, ERS220Info::TailLeftButOffset, GETB(ERS220Info::CPCSensorTailLeftSwitch),  "LBkBut");
00202     chkEvent(evtBuf, ERS220Info::TailCenterButOffset, GETB(ERS220Info::CPCSensorTailCenterSwitch),"CBkBut");
00203     chkEvent(evtBuf, ERS220Info::TailRightButOffset, GETB(ERS220Info::CPCSensorTailRightSwitch), "RBkBut");
00204 
00205     // Get foot switches
00206     chkEvent(evtBuf,LFrPawOffset,GETB(ERS220Info::CPCSensorLFPaw),"LFrPaw");
00207     chkEvent(evtBuf,RFrPawOffset,GETB(ERS220Info::CPCSensorRFPaw),"RFrPaw");
00208     chkEvent(evtBuf,LBkPawOffset,GETB(ERS220Info::CPCSensorLHPaw),"LBkPaw");
00209     chkEvent(evtBuf,RBkPawOffset,GETB(ERS220Info::CPCSensorRHPaw),"RBkPaw");
00210 
00211     // Get buttons
00212     chkEvent(evtBuf,ERS220Info::ChinButOffset,  GETB(ERS220Info::CPCSensorChinSwitch),"ChinBut");
00213     chkEvent(evtBuf,ERS220Info::BackButOffset,  GETB(ERS220Info::CPCSensorBackSwitch),"BackBut");
00214     chkEvent(evtBuf,ERS220Info::HeadFrButOffset,GETD(ERS220Info::CPCSensorHeadFrontPressure),"HeadFrBut");
00215     chkEvent(evtBuf,ERS220Info::HeadBkButOffset,GETD(ERS220Info::CPCSensorHeadBackPressure),"HeadBkBut");
00216 
00217     // Get IR distance sensor
00218     sensors[ERS220Info::IRDistOffset]=GETSENSOR(ERS220Info::CPCSensorPSD) / 1000.0f;
00219 
00220     // Get acceleration sensors
00221     sensors[BAccelOffset] = GETD(ERS220Info::CPCSensorAccelFB);
00222     sensors[LAccelOffset] = GETD(ERS220Info::CPCSensorAccelLR);
00223     sensors[DAccelOffset] = GETD(ERS220Info::CPCSensorAccelUD);
00224 
00225     sensors[ERS220Info::ThermoOffset] = GETD(ERS220Info::CPCSensorThermoSensor);
00226   }
00227 
00228   // (ERS-7 only)
00229   if(robotDesign&ERS7Mask) {
00230     outputs[LFrLegOffset + RotatorOffset   ] = GETD(ERS7Info::CPCJointLFRotator);
00231     outputs[LFrLegOffset + ElevatorOffset  ] = GETD(ERS7Info::CPCJointLFElevator);
00232     outputs[LFrLegOffset + KneeOffset      ] = GETD(ERS7Info::CPCJointLFKnee);
00233     pidduties[LFrLegOffset + RotatorOffset ] = GETDUTY(ERS7Info::CPCJointLFRotator);
00234     pidduties[LFrLegOffset + ElevatorOffset] = GETDUTY(ERS7Info::CPCJointLFElevator);
00235     pidduties[LFrLegOffset + KneeOffset    ] = GETDUTY(ERS7Info::CPCJointLFKnee);
00236   
00237     outputs[RFrLegOffset + RotatorOffset   ] = GETD(ERS7Info::CPCJointRFRotator);
00238     outputs[RFrLegOffset + ElevatorOffset  ] = GETD(ERS7Info::CPCJointRFElevator);
00239     outputs[RFrLegOffset + KneeOffset      ] = GETD(ERS7Info::CPCJointRFKnee);
00240     pidduties[RFrLegOffset + RotatorOffset ] = GETDUTY(ERS7Info::CPCJointRFRotator);
00241     pidduties[RFrLegOffset + ElevatorOffset] = GETDUTY(ERS7Info::CPCJointRFElevator);
00242     pidduties[RFrLegOffset + KneeOffset    ] = GETDUTY(ERS7Info::CPCJointRFKnee);
00243   
00244     outputs[LBkLegOffset + RotatorOffset   ] = GETD(ERS7Info::CPCJointLHRotator);
00245     outputs[LBkLegOffset + ElevatorOffset  ] = GETD(ERS7Info::CPCJointLHElevator);
00246     outputs[LBkLegOffset + KneeOffset      ] = GETD(ERS7Info::CPCJointLHKnee);
00247     pidduties[LBkLegOffset + RotatorOffset ] = GETDUTY(ERS7Info::CPCJointLHRotator);
00248     pidduties[LBkLegOffset + ElevatorOffset] = GETDUTY(ERS7Info::CPCJointLHElevator);
00249     pidduties[LBkLegOffset + KneeOffset    ] = GETDUTY(ERS7Info::CPCJointLHKnee);
00250 
00251     outputs[RBkLegOffset + RotatorOffset   ] = GETD(ERS7Info::CPCJointRHRotator);
00252     outputs[RBkLegOffset + ElevatorOffset  ] = GETD(ERS7Info::CPCJointRHElevator);
00253     outputs[RBkLegOffset + KneeOffset      ] = GETD(ERS7Info::CPCJointRHKnee);
00254     pidduties[RBkLegOffset + RotatorOffset ] = GETDUTY(ERS7Info::CPCJointRHRotator);
00255     pidduties[RBkLegOffset + ElevatorOffset] = GETDUTY(ERS7Info::CPCJointRHElevator);
00256     pidduties[RBkLegOffset + KneeOffset    ] = GETDUTY(ERS7Info::CPCJointRHKnee);
00257 
00258     // Get head tilt,pan,nod joint angles
00259     outputs[HeadOffset+TiltOffset] = GETD(ERS7Info::CPCJointNeckTilt);
00260     outputs[HeadOffset+PanOffset ] = GETD(ERS7Info::CPCJointNeckPan);
00261     outputs[HeadOffset+RollOffset] = GETD(ERS7Info::CPCJointNeckNod);
00262     pidduties[HeadOffset+TiltOffset] = GETDUTY(ERS7Info::CPCJointNeckTilt);
00263     pidduties[HeadOffset+PanOffset ] = GETDUTY(ERS7Info::CPCJointNeckPan);
00264     pidduties[HeadOffset+RollOffset] = GETDUTY(ERS7Info::CPCJointNeckNod);
00265 
00266     outputs[ERS7Info::TailOffset+TiltOffset] = GETD(ERS7Info::CPCJointTailTilt);
00267     outputs[ERS7Info::TailOffset+PanOffset]  = GETD(ERS7Info::CPCJointTailPan);
00268     pidduties[ERS7Info::TailOffset+TiltOffset] = GETDUTY(ERS7Info::CPCJointTailTilt);
00269     pidduties[ERS7Info::TailOffset+PanOffset]  = GETDUTY(ERS7Info::CPCJointTailPan);
00270     
00271     outputs[ERS7Info::MouthOffset] = GETD(ERS7Info::CPCJointMouth);
00272     pidduties[ERS7Info::MouthOffset] = GETDUTY(ERS7Info::CPCJointMouth);
00273 
00274     // Get foot switches
00275     chkEvent(evtBuf,LFrPawOffset,GETB(ERS7Info::CPCSwitchLFPaw),"LFrPaw");
00276     chkEvent(evtBuf,RFrPawOffset,GETB(ERS7Info::CPCSwitchRFPaw),"RFrPaw");
00277     chkEvent(evtBuf,LBkPawOffset,GETB(ERS7Info::CPCSwitchLHPaw),"LBkPaw");
00278     chkEvent(evtBuf,RBkPawOffset,GETB(ERS7Info::CPCSwitchRHPaw),"RBkPaw");
00279 
00280     // Get buttons/switches
00281     // the sensors are scaled to be relatively similar to the pressure values given by the head on the 210
00282     chkEvent(evtBuf, ERS7Info::ChinButOffset,       GETSENSOR(ERS7Info::CPCSwitchChin),      "ChinBut");
00283     chkEvent(evtBuf, ERS7Info::HeadButOffset,       GETSENSOR(ERS7Info::CPCSensorHead)/120,      "HeadBut");
00284     chkEvent(evtBuf, ERS7Info::FrontBackButOffset,  GETSENSOR(ERS7Info::CPCSensorBackFront)/150, "BkFrBut");
00285     chkEvent(evtBuf, ERS7Info::MiddleBackButOffset, GETSENSOR(ERS7Info::CPCSensorBackMiddle)/150,"BkMdBut");
00286     chkEvent(evtBuf, ERS7Info::RearBackButOffset,   GETSENSOR(ERS7Info::CPCSensorBackRear)/150,  "BkRrBut");
00287     chkEvent(evtBuf, ERS7Info::WirelessSwOffset,GETSENSOR(ERS7Info::CPCSwitchWireless),  "WirelessSw");
00288 
00289     // Get IR distance sensor
00290     sensors[ERS7Info::NearIRDistOffset] = GETSENSOR(ERS7Info::CPCSensorNearPSD) / 1000.0f;
00291     sensors[ERS7Info::FarIRDistOffset] = GETSENSOR(ERS7Info::CPCSensorFarPSD) / 1000.0f;
00292     sensors[ERS7Info::ChestIRDistOffset] = GETSENSOR(ERS7Info::CPCSensorChestPSD) / 1000.0f;
00293 
00294     // Get acceleration sensors
00295     sensors[BAccelOffset] = GETD(ERS7Info::CPCSensorAccelFB);
00296     sensors[LAccelOffset] = GETD(ERS7Info::CPCSensorAccelLR);
00297     sensors[DAccelOffset] = GETD(ERS7Info::CPCSensorAccelUD);
00298   }
00299 
00300   //Apply sensor calibrations (currently only joint positions - perhaps IR as well?)
00301   for(unsigned int i=0; i<NumPIDJoints; i++)
00302     outputs[PIDJointOffset+i]/=config->motion.calibration[i];
00303 
00304   unsigned int dif=curtime-lastSensorUpdateTime;
00305   lastSensorUpdateTime=curtime;
00306   for(unsigned int i=0; i<evtBuf.size(); i++)
00307     er->postEvent(evtBuf[i]);
00308   //we don't delete the events in evtBuf - we're handing them off to erouter, which will delete them itself
00309   er->postEvent(EventBase::sensorEGID,SensorSourceID::UpdatedSID,EventBase::statusETID,dif);
00310 }
00311 
00312 /*! This will cause events to be posted */
00313 void WorldState::read(const OPowerStatus& power, EventRouter* er) {
00314   std::string actnames[PowerSourceID::NumPowerSIDs];
00315   std::string denames[PowerSourceID::NumPowerSIDs];
00316   unsigned int actmasks[PowerSourceID::NumPowerSIDs];
00317   memset(actmasks,0,sizeof(unsigned int)*PowerSourceID::NumPowerSIDs);
00318 
00319   //RobotStatus
00320   chkPowerEvent(PowerSourceID::PauseSID,          power.robotStatus,orsbPAUSE,                        "Pause",actnames,denames,actmasks);
00321   chkPowerEvent(PowerSourceID::MotorPowerSID,     power.robotStatus,orsbMOTOR_POWER,                  "MotorPower",actnames,denames,actmasks);
00322   chkPowerEvent(PowerSourceID::VibrationSID,      power.robotStatus,orsbVIBRATION_DETECT,             "Vibration",actnames,denames,actmasks);
00323   chkPowerEvent(PowerSourceID::ExternalPortSID,   power.robotStatus,orsbEX_PORT_CONNECTED,            "ExternalPort",actnames,denames,actmasks);
00324   chkPowerEvent(PowerSourceID::StationConnectSID, power.robotStatus,orsbSTATION_CONNECTED,            "StationConnect",actnames,denames,actmasks);
00325   chkPowerEvent(PowerSourceID::ExternalPowerSID,  power.robotStatus,orsbEX_POWER_CONNECTED,           "ExternalPower",actnames,denames,actmasks);
00326   chkPowerEvent(PowerSourceID::BatteryConnectSID, power.robotStatus,orsbBATTERY_CONNECTED,            "BatteryConnect",actnames,denames,actmasks);
00327   chkPowerEvent(PowerSourceID::ChargingSID,       power.robotStatus,orsbBATTERY_CHARGING,             "BatteryCharging",actnames,denames,actmasks);
00328   chkPowerEvent(PowerSourceID::BatteryFullSID,    power.robotStatus,orsbBATTERY_CAPACITY_FULL,        "BatteryFull",actnames,denames,actmasks);
00329   chkPowerEvent(PowerSourceID::LowPowerWarnSID,   power.robotStatus,orsbBATTERY_CAPACITY_LOW,         "BatteryLow",actnames,denames,actmasks);
00330   chkPowerEvent(PowerSourceID::OverChargedSID,    power.robotStatus,orsbBATTERY_OVER_CURRENT,         "BatteryOverCurrent",actnames,denames,actmasks);
00331   chkPowerEvent(PowerSourceID::OverheatingSID,    power.robotStatus,orsbBATTERY_OVER_TEMP_DISCHARGING,"BatteryOverTempDischarge",actnames,denames,actmasks);
00332   chkPowerEvent(PowerSourceID::OverheatingSID,    power.robotStatus,orsbBATTERY_OVER_TEMP_CHARGING,   "BatteryOverTempCharge",actnames,denames,actmasks);
00333   chkPowerEvent(PowerSourceID::ErrorSID,          power.robotStatus,orsbBATTERY_ERROR_OF_CHARGING,    "BatteryChargeError",actnames,denames,actmasks);
00334   chkPowerEvent(PowerSourceID::ErrorSID,          power.robotStatus,orsbERROR_OF_PLUNGER,             "PlungerError",actnames,denames,actmasks);
00335   chkPowerEvent(PowerSourceID::PowerGoodSID,      power.robotStatus,orsbOPEN_R_POWER_GOOD,            "PowerGood",actnames,denames,actmasks);
00336   chkPowerEvent(PowerSourceID::ErrorSID,          power.robotStatus,orsbERROR_OF_FAN,                 "FanError",actnames,denames,actmasks);
00337   chkPowerEvent(PowerSourceID::DataFromStationSID,power.robotStatus,orsbDATA_STREAM_FROM_STATION,     "DataFromStation",actnames,denames,actmasks);
00338   chkPowerEvent(PowerSourceID::RegisterUpdateSID, power.robotStatus,orsbREGISTER_UPDATED_BY_STATION,  "RegisterUpdate",actnames,denames,actmasks);
00339   chkPowerEvent(PowerSourceID::ErrorSID,          power.robotStatus,orsbRTC_ERROR,                    "RTCError",actnames,denames,actmasks);
00340   chkPowerEvent(PowerSourceID::RTCSID,            power.robotStatus,orsbRTC_OVERFLOW,                 "RTCOverflow",actnames,denames,actmasks);
00341   chkPowerEvent(PowerSourceID::RTCSID,            power.robotStatus,orsbRTC_RESET,                    "RTCReset",actnames,denames,actmasks);
00342   chkPowerEvent(PowerSourceID::RTCSID,            power.robotStatus,orsbRTC_SET,                      "RTCSet",actnames,denames,actmasks);
00343   chkPowerEvent(PowerSourceID::SpecialModeSID,    power.robotStatus,orsbSPECIAL_MODE,                 "SpecialMode",actnames,denames,actmasks);
00344   chkPowerEvent(PowerSourceID::BMNDebugModeSID,   power.robotStatus,orsbBMN_DEBUG_MODE,               "BMNDebugMode",actnames,denames,actmasks);
00345   chkPowerEvent(PowerSourceID::ChargerStatusSID,  power.robotStatus,orsbCHARGER_STATUS,               "ChargerStatus",actnames,denames,actmasks);
00346   chkPowerEvent(PowerSourceID::PlungerSID,        power.robotStatus,orsbPLUNGER,                      "Plunger",actnames,denames,actmasks);
00347   chkPowerEvent(PowerSourceID::SuspendedSID,      power.robotStatus,orsbSUSPENDED,                    "Suspended",actnames,denames,actmasks);
00348 
00349   //BatteryStatus
00350   chkPowerEvent(PowerSourceID::ErrorSID,        power.batteryStatus,obsbERROR_CODE_MASK,             "BatteryError",actnames,denames,actmasks);
00351   chkPowerEvent(PowerSourceID::BatteryEmptySID, power.batteryStatus,obsbFULLY_DISCHARGED,            "FullyDischarged",actnames,denames,actmasks);
00352   chkPowerEvent(PowerSourceID::BatteryFullSID,  power.batteryStatus,obsbFULLY_CHARGED,               "FullyCharged",actnames,denames,actmasks);
00353   chkPowerEvent(PowerSourceID::DischargingSID,  power.batteryStatus,obsbDISCHARGING,                 "Discharging",actnames,denames,actmasks);
00354   chkPowerEvent(PowerSourceID::BatteryInitSID,  power.batteryStatus,obsbINITIALIZED,                 "BatteryInit",actnames,denames,actmasks);
00355   chkPowerEvent(PowerSourceID::LowPowerWarnSID, power.batteryStatus,obsbREMAINING_TIME_ALARM,        "RemainingTimeAlarm",actnames,denames,actmasks);
00356   chkPowerEvent(PowerSourceID::LowPowerWarnSID, power.batteryStatus,obsbREMAINING_CAPACITY_ALARM,    "RemainingCapacityAlarm",actnames,denames,actmasks);
00357   chkPowerEvent(PowerSourceID::TermDischargeSID,power.batteryStatus,obsbTERMINATED_DISCHARGING_ALARM,"TermDischargeAlarm",actnames,denames,actmasks);
00358   chkPowerEvent(PowerSourceID::OverheatingSID,  power.batteryStatus,obsbOVER_TEMP_ALARM,             "OverTempAlarm",actnames,denames,actmasks);
00359   chkPowerEvent(PowerSourceID::TermChargeSID,   power.batteryStatus,obsbTERMINATED_CHARGING_ALARM,   "TermChargeAlarm",actnames,denames,actmasks);
00360   chkPowerEvent(PowerSourceID::OverChargedSID,  power.batteryStatus,obsbOVER_CHARGED_ALARM,          "OverChargeAlarm",actnames,denames,actmasks);
00361   
00362   sensors[PowerRemainOffset] = power.remainingCapacity/100.0;
00363   sensors[PowerThermoOffset] = power.temperature/100.0;
00364   sensors[PowerCapacityOffset] = power.fullyChargedCapacity;
00365   sensors[PowerVoltageOffset] = power.voltage/1000.0;
00366   sensors[PowerCurrentOffset] = power.current;
00367 
00368   //only generate status events when a change happens
00369   for(unsigned int i=0; i<PowerSourceID::NumPowerSIDs; i++) {
00370     if(actmasks[i]) { //now on
00371       if(!powerFlags[i]) //was off: activation
00372         er->postEvent(EventBase::powerEGID,i,EventBase::activateETID,0,actnames[i],1);
00373       else if(actmasks[i]!=powerFlags[i]) //already on - change? : status
00374         er->postEvent(EventBase::powerEGID,i,EventBase::statusETID,0,actnames[i],1);
00375     } else { // now off
00376       if(powerFlags[i]) //was on: deactivation
00377         er->postEvent(EventBase::powerEGID,i,EventBase::deactivateETID,0,denames[i],0);
00378     }
00379     powerFlags[i]=actmasks[i];
00380   }
00381 
00382   er->postEvent(EventBase::powerEGID,PowerSourceID::UpdatedSID,EventBase::statusETID,0);
00383 }
00384 
00385 #endif //PLATFORM_APERIOS
00386 
00387 void WorldState::chkEvent(std::vector<EventBase*>& evtBuf, unsigned int sid, float newval, const char* name) {
00388   if(newval>=0.1) { //now on
00389     if(buttons[sid]<0.1) { //was off: activation
00390       evtBuf.push_back(new EventBase(EventBase::buttonEGID,sid,EventBase::activateETID,0,name,newval));
00391       button_times[sid]=curtime;
00392     } else if(alwaysGenerateStatus || buttons[sid]!=newval) { //already on - always or change? : status
00393       unsigned int dur=curtime-button_times[sid];
00394       evtBuf.push_back(new EventBase(EventBase::buttonEGID,sid,EventBase::statusETID,dur,name,newval));
00395     }
00396   } else { //now off
00397     if(buttons[sid]>=0.1) { //was on: deactivation
00398       unsigned int dur=curtime-button_times[sid];
00399       button_times[sid]=0;
00400       evtBuf.push_back(new EventBase(EventBase::buttonEGID,sid,EventBase::deactivateETID,dur,name,0));
00401     }
00402   }
00403   //update value
00404   buttons[sid]=newval;
00405 }
00406 
00407 /*! @file
00408  * @brief Implements WorldState, maintains information about the robot's environment, namely sensors and power status
00409  * @author ejt (Creator)
00410  *
00411  * $Author: ejt $
00412  * $Name: tekkotsu-2_2_1 $
00413  * $Revision: 1.26 $
00414  * $State: Exp $
00415  * $Date: 2004/11/04 19:45:17 $
00416  */

Tekkotsu v2.2.1
Generated Tue Nov 23 16:36:42 2004 by Doxygen 1.3.9.1