Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArActionAvoidFront.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 
00028 #include "ariaOSDef.h"
00029 #include "ArActionAvoidFront.h"
00030 #include "ArResolver.h"
00031 #include "ArRobot.h"
00032 #include "ArLog.h"
00033 
00044 AREXPORT ArActionAvoidFront::ArActionAvoidFront(const char *name, 
00045                         double obstacleDistance,
00046                         double avoidVelocity,
00047                         double turnAmount, 
00048                         bool useTableIRIfAvail) :
00049   ArAction(name, "Slows down and avoids obstacles in front of the robot.")
00050 {
00051   setNextArgument(ArArg("obstacle distance", &myObsDist, 
00052             "Distance at which to turn. (mm)"));
00053   myObsDist = obstacleDistance;
00054   
00055   setNextArgument(ArArg("avoid speed", &myAvoidVel,
00056     "Speed at which to go while avoiding an obstacle. (mm/sec)"));
00057   myAvoidVel = avoidVelocity;
00058 
00059   setNextArgument(ArArg("turn ammount", &myTurnAmountParam, 
00060     "Degrees to turn relative to current heading while avoiding obstacle (deg)"));
00061   myTurnAmountParam = turnAmount;
00062 
00063   setNextArgument(ArArg("use table IR", &myUseTableIRIfAvail,
00064         "true to use table sensing IR for avoidance if the robot has them, false otherwise"));
00065   myUseTableIRIfAvail = useTableIRIfAvail;
00066 
00067   myTurning = 0;
00068 }
00069 
00070 AREXPORT ArActionAvoidFront::~ArActionAvoidFront()
00071 {
00072 
00073 }
00074 
00075 AREXPORT ArActionDesired *ArActionAvoidFront::fire(ArActionDesired currentDesired)
00076 {
00077   double dist, angle;
00078 
00079   if (currentDesired.getDeltaHeadingStrength() >= 1.0)
00080     myTurning = 0;
00081 
00082   myDesired.reset();
00083 
00084   dist = (myRobot->checkRangeDevicesCurrentPolar(-70, 70, &angle) 
00085       - myRobot->getRobotRadius());
00086   
00087   //  printf("%5.0f %3.0f ", dist, angle);
00088 
00089   if (dist > myObsDist && 
00090       (!myUseTableIRIfAvail || 
00091        (myUseTableIRIfAvail && !myRobot->hasTableSensingIR()) || 
00092        (myUseTableIRIfAvail && myRobot->hasTableSensingIR() && 
00093        !myRobot->isLeftTableSensingIRTriggered() &&
00094        !myRobot->isRightTableSensingIRTriggered())))
00095   {
00096     if (myTurning != 0)
00097     {
00098       myDesired.setDeltaHeading(0);
00099       myTurning = 0;
00100       return &myDesired;
00101     }
00102     else
00103     {
00104       //printf("\n");
00105       myTurning = 0;
00106       return NULL;
00107     }
00108   }
00109   
00110 //  printf("Avoiding ");
00111   
00112   if (myTurning == 0)
00113   {
00114     if (myUseTableIRIfAvail && myRobot->hasTableSensingIR() && 
00115         myRobot->isLeftTableSensingIRTriggered())
00116       myTurning = 1;
00117     else if (myUseTableIRIfAvail && myRobot->hasTableSensingIR() && 
00118              myRobot->isRightTableSensingIRTriggered())
00119       myTurning = -1;
00120     else if (angle < 0)
00121       myTurning = 1;
00122     else
00123       myTurning = -1;
00124     myTurnAmount = myTurnAmountParam;
00125     myQuadrants.clear();
00126   }
00127 
00128   myQuadrants.update(myRobot->getTh());
00129   if (myTurning && myQuadrants.didAll())
00130   {
00131     myQuadrants.clear();
00132     myTurnAmount /= 2;
00133     if (myTurnAmount == 0)
00134       myTurnAmount = myTurnAmountParam;
00135   }
00136 
00137   myDesired.setDeltaHeading(myTurning * myTurnAmount);
00138 
00139   if (dist > myObsDist/2 && 
00140       (!myUseTableIRIfAvail || 
00141        (myUseTableIRIfAvail && !myRobot->hasTableSensingIR()) || 
00142        (myUseTableIRIfAvail && myRobot->hasTableSensingIR() && 
00143        !myRobot->isLeftTableSensingIRTriggered() &&
00144        !myRobot->isRightTableSensingIRTriggered())))
00145   {
00146     //printf(" scaling %f %f %f ", myAvoidVel * dist / myObsDist, 
00147     //dist, myObsDist);
00148     myDesired.setVel(myAvoidVel * dist / myObsDist);
00149   }
00150   else
00151   {
00152 //   printf(" zerovel\n");
00153     myDesired.setVel(0);
00154   }
00155     
00156   //printf("\n");
00157   return &myDesired;
00158 }

Generated on Tue Feb 20 10:51:38 2007 for Aria by  doxygen 1.4.0