Movement.C

00001 #ifndef __MOVEMENT_AGENT_C__
00002 #define __MOVEMENT_AGENT_C__
00003 
00004 #include "Movement.H"
00005 
00006 
00007 // ######################################################################
00008 MovementAgent::MovementAgent(OptionManager& mgr,
00009                              nub::soft_ref<AgentManager> ama,
00010                              nub::soft_ref<SubController> subController,
00011                              const std::string& name) :
00012   SubmarineAgent(mgr, ama, name),
00013   itsDepthErrThresh("setDepthErrThresh", this, 0, ALLOW_ONLINE_CHANGES),
00014   itsHeadingErrThresh("setHeadingErrThresh", this, 0, ALLOW_ONLINE_CHANGES),
00015   pipeP("pipeP", this, 0, ALLOW_ONLINE_CHANGES),
00016   pipeI("pipeI", this, 0, ALLOW_ONLINE_CHANGES),
00017   pipeD("pipeD", this, 0, ALLOW_ONLINE_CHANGES),
00018   itsPipePID(pipeP.getVal(), pipeI.getVal(), pipeD.getVal(), -20, 20, 5, 0, 0, 100, -100, 150, true, 1, 25, -25),
00019   setDiveValue("setDiveValue", this, 0, ALLOW_ONLINE_CHANGES),
00020   setGoStraightTimeValue("setGoStraightTimeValue", this, 0, ALLOW_ONLINE_CHANGES),
00021   setSpeedValue("setSpeedValue", this, 20, ALLOW_ONLINE_CHANGES),
00022   setHeadingValue("setHeadingValue", this, 0, ALLOW_ONLINE_CHANGES),
00023   setRelative("setRelative", this, false, ALLOW_ONLINE_CHANGES),
00024   setTimeout("setTimeout", this, 100, ALLOW_ONLINE_CHANGES),
00025   itsSubController(subController),
00026 {
00027   itsFrameNumber = 0;
00028   itsTimer.reset(new Timer(1000000));
00029 }
00030 
00031 
00032 // ######################################################################
00033 MovementAgent::~MovementAgent()
00034 {
00035 
00036 }
00037 
00038 // ######################################################################
00039 //Scheduler
00040 bool MovementAgent::pickAndExecuteAnAction()
00041 {
00042   return true;
00043 }
00044 
00045 // ######################################################################
00046 bool MovementAgent::dive(int depth, bool relative, int timeout)
00047 {
00048   if(relative)
00049     {
00050       itsSubController->setDepth(depth + itsSubController->getDepth());
00051     }
00052   else
00053     {
00054       itsSubController->setDepth(depth);
00055     }
00056 
00057   int time = 0;
00058 
00059   while(itsSubController->getDepthErr() > itsDepthErrThresh.getVal())
00060     {
00061       usleep(10000);
00062       if(time++ > timeout) return false;
00063       //<TODO mmontalbo> turn off diving
00064     }
00065 
00066   return true;
00067 }
00068 
00069 // ######################################################################
00070 bool MovementAgent::goStraight(int speed, int time)
00071 {
00072   itsSubController->setSpeed(speed);
00073   sleep(time);
00074   itsSubController->setSpeed(0);
00075   return true;
00076 }
00077 
00078 // ######################################################################
00079 bool MovementAgent::setHeading(int heading, bool relative, int timeout)
00080 {
00081   if(relative)
00082     {
00083       itsSubController->setHeading(heading + itsSubController->getHeading());
00084     }
00085   else
00086     {
00087       itsSubController->setHeading(heading);
00088     }
00089 
00090   int time = 0;
00091 
00092   while(itsSubController->getHeadingErr() > itsHeadingErrThresh.getVal())
00093     {
00094       usleep(10000);
00095       if(time++ > timeout) return false;
00096       //<TODO mmontalbo> turn off turning
00097     }
00098 
00099   return true;
00100 }
00101 
00102 // ######################################################################
00103 int MovementAgent::trackPipe(const Point2D<int>& pointToTrack,
00104                                   const Point2D<int>& desiredPoint)
00105 {
00106   float pipeCorrection = (float)itsPipePID.update(pointToTrack.i, desiredPoint.i);
00107 
00108   //  itsSubController->setHeading(itsSubController->getHeading()
00109   //                          + pipeCorrection);
00110 
00111   itsSubController->setTurningSpeed(pipeCorrection);
00112 
00113   return abs(pointToTrack.i - desiredPoint.i);
00114 }
00115 
00116 // ######################################################################
00117 void MovementAgent::paramChanged(ModelParamBase* const param,
00118                                  const bool valueChanged,
00119                                  ParamClient::ChangeStatus* status)
00120 {
00121   if (param == &setDiveValue && valueChanged)
00122     {
00123       if(dive(setDiveValue.getVal(), setRelative.getVal(), setTimeout.getVal()))
00124         LINFO("Dive completed successfully");
00125       else
00126         LINFO("Dive failed");
00127     }
00128   else if(param == &setGoStraightTimeValue && valueChanged)
00129     {
00130       if(goStraight(setSpeedValue.getVal(), setGoStraightTimeValue.getVal()))
00131         LINFO("Go straight completed successfully");
00132       else
00133         LINFO("Go straight failed");
00134     }
00135   else if(param == &setHeadingValue && valueChanged)
00136     {
00137       if(setHeading(setHeadingValue.getVal(), setRelative.getVal(), setTimeout.getVal()))
00138         LINFO("Turn completed successfully");
00139       else
00140         LINFO("Turn failed");
00141     }
00142   //////// Pipe PID constants/gain change ////////
00143   else if (param == &pipeP && valueChanged)
00144     itsPipePID.setPIDPgain(pipeP.getVal());
00145   else if(param == &pipeI && valueChanged)
00146     itsPipePID.setPIDIgain(pipeI.getVal());
00147   else if(param == &pipeD && valueChanged)
00148     itsPipePID.setPIDDgain(pipeD.getVal());
00149 
00150 }
00151 
00152 // ######################################################################
00153 /* So things look consistent in everyone's emacs... */
00154 /* Local Variables: */
00155 /* indent-tabs-mode: nil */
00156 /* End: */
00157 
00158 #endif
Generated on Sun May 8 08:42:14 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3