Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

PostureMC.cc

Go to the documentation of this file.
00001 #include "PostureMC.h"
00002 #include "Shared/Config.h"
00003 #include "Shared/WorldState.h"
00004 #include "Shared/ERS7Info.h"
00005 #include "Shared/ERS210Info.h"
00006 #include "Wireless/Wireless.h"
00007 #include "Shared/get_time.h"
00008 #include "Events/EventBase.h"
00009 
00010 PostureMC& PostureMC::setDirty(bool d/*=true*/) {
00011   dirty=d;
00012   targetReached=false;
00013   for(unsigned int i=0; i<NumOutputs; i++)
00014     curPositions[i]=motman->getOutputCmd(i).value; //not state->outputs[i]; - see function documentation
00015   return *this;
00016 } 
00017 
00018 void PostureMC::defaultMaxSpeed(float x/*=1*/) {
00019   for(unsigned int i=0; i<NumOutputs; i++)
00020     maxSpeeds[i]=MaxOutputSpeed[i]*FrameTime*x/1000.f; // maxSpeeds is radians per frame
00021   //respect the config values for the neck joints (which are stored as rad/sec)
00022 #ifdef TGT_IS_AIBO
00023   maxSpeeds[HeadOffset+TiltOffset]=config->motion.max_head_tilt_speed*FrameTime*x/1000.f; 
00024   maxSpeeds[HeadOffset+PanOffset]=config->motion.max_head_pan_speed*FrameTime*x/1000.f;
00025   maxSpeeds[HeadOffset+RollOffset]=config->motion.max_head_roll_speed*FrameTime*x/1000.f;
00026 #else
00027   const char* n = ERS7Info::outputNames[ERS7Info::HeadOffset+ERS7Info::TiltOffset];
00028   unsigned int i = capabilities.findOutputOffset(n);
00029   if(i!=-1U)
00030     maxSpeeds[i]=config->motion.max_head_tilt_speed*FrameTime*x/1000.f; 
00031   n = ERS7Info::outputNames[ERS7Info::HeadOffset+ERS7Info::PanOffset];
00032   i = capabilities.findOutputOffset(n);
00033   if(i!=-1U)
00034     maxSpeeds[i]=config->motion.max_head_pan_speed*FrameTime*x/1000.f;
00035   n = ERS7Info::outputNames[ERS7Info::HeadOffset+ERS7Info::NodOffset];
00036   i = capabilities.findOutputOffset(n);
00037   if(i!=-1U)
00038     maxSpeeds[i]=config->motion.max_head_roll_speed*FrameTime*x/1000.f;
00039   n = ERS210Info::outputNames[ERS210Info::HeadOffset+ERS210Info::RollOffset];
00040   i = capabilities.findOutputOffset(n);
00041   if(i!=-1U)
00042     maxSpeeds[i]=config->motion.max_head_roll_speed*FrameTime*x/1000.f;
00043 #endif
00044 }
00045 
00046 int PostureMC::updateOutputs() {
00047   int tmp=isDirty();
00048   if(tmp || hold) {
00049     dirty=false;
00050     for(unsigned int i=0; i<NumOutputs; i++) {
00051       if(maxSpeeds[i]<=0) {
00052         curPositions[i]=cmds[i].value;
00053         motman->setOutput(this,i,cmds[i]);
00054       } else { // we may be trying to exceeded maxSpeed
00055         unsigned int f=0;
00056         while(cmds[i].value>curPositions[i]+maxSpeeds[i] && f<NumFrames) {
00057           curPositions[i]+=maxSpeeds[i];
00058           motman->setOutput(this,i,OutputCmd(curPositions[i],cmds[i].weight),f);
00059           f++;
00060         }
00061         while(cmds[i].value<curPositions[i]-maxSpeeds[i] && f<NumFrames) {
00062           curPositions[i]-=maxSpeeds[i];
00063           motman->setOutput(this,i,OutputCmd(curPositions[i],cmds[i].weight),f);
00064           f++;
00065         }
00066         if(f<NumFrames) { //we reached target value, fill in rest of frames
00067           curPositions[i]=cmds[i].value;
00068           for(;f<NumFrames;f++)
00069             motman->setOutput(this,i,cmds[i],f);
00070         } else // we didn't reach target value, still dirty
00071           dirty=true;
00072       }
00073     }
00074     if(!dirty && !targetReached) {
00075       postEvent(EventBase(EventBase::motmanEGID,getID(),EventBase::statusETID));
00076       targetReached=true;
00077       targetTimestamp=get_time();
00078     }
00079   }
00080   return tmp;
00081 }
00082 
00083 int PostureMC::isAlive() {
00084   if(dirty || !targetReached)
00085     return true;
00086   if(targetReached && (!hold || get_time()-targetTimestamp>timeout)) { //prevents a conflicted PostureMC's from fighting forever
00087     if(get_time()-targetTimestamp>timeout && getAutoPrune())
00088       serr->printf("WARNING: posture timed out - possible joint conflict or out-of-range target\n");
00089     return false;
00090   }
00091   float max=0;
00092   for(unsigned int i=0; i<NumOutputs; i++)
00093     if(cmds[i].weight>0) {
00094       float dif=cmds[i].value-state->outputs[i];
00095       if(dif>max)
00096         max=dif;
00097     }
00098   return (max>tolerance);
00099 }
00100 
00101 void PostureMC::init() {
00102   defaultMaxSpeed();
00103   setDirty();
00104 }
00105 
00106 /*! @file
00107  * @brief Implements PostureMC, a MotionCommand shell for PostureEngine
00108  * @author ejt (Creator)
00109  */

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