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

ArActionGoto.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 #include "ariaOSDef.h"
00028 #include "ArActionGoto.h"
00029 #include "ArRobot.h"
00030 
00031 AREXPORT ArActionGoto::ArActionGoto(const char *name, ArPose goal, 
00032                     double closeDist, double speed,
00033                     double speedToTurnAt, double turnAmount) :
00034   ArAction(name, "Goes to the given goal.")
00035 {
00036   myDirectionToTurn = myCurTurnDir = 1;
00037   myTurnedBack = false;
00038   myPrinting = false;
00039 
00040   setNextArgument(ArArg("goal", &myGoal, "ArPose to go to. (ArPose)"));
00041   setGoal(goal);
00042   
00043   setNextArgument(ArArg("close dist", &myCloseDist, 
00044             "Distance that is close enough to goal. (mm)"));
00045   myCloseDist = closeDist;
00046 
00047   setNextArgument(ArArg("speed", &mySpeed, 
00048             "Speed to travel to goal at. (mm/sec)"));
00049   mySpeed = speed;
00050 
00051   setNextArgument(ArArg("speed to turn at", &mySpeedToTurnAt,
00052             "Speed to start obstacle avoiding at (mm/sec)"));
00053   mySpeedToTurnAt = speedToTurnAt;
00054   
00055   setNextArgument(ArArg("amount to turn", &myTurnAmount,
00056             "Amount to turn when avoiding (deg)"));
00057   myTurnAmount = turnAmount;
00058   
00059 }
00060 
00061 AREXPORT ArActionGoto::~ArActionGoto()
00062 {
00063 
00064 }
00065 
00066 AREXPORT bool ArActionGoto::haveAchievedGoal(void)
00067 {
00068   if (myState == STATE_ACHIEVED_GOAL)
00069     return true;
00070   else
00071     return false;
00072 }
00073 
00074 AREXPORT void ArActionGoto::cancelGoal(void)
00075 {
00076   myState = STATE_NO_GOAL;
00077 }
00078 
00079 AREXPORT void ArActionGoto::setGoal(ArPose goal)
00080 {
00081   myState = STATE_GOING_TO_GOAL;
00082   myGoal = goal;
00083   myTurnedBack = false;
00084   myCurTurnDir = myDirectionToTurn;
00085   myOldGoal = myGoal;
00086 }
00087 
00088 AREXPORT ArActionDesired *ArActionGoto::fire(ArActionDesired currentDesired)
00089 {
00090   double angle;
00091   double dist;
00092   double vel;
00093 
00094   if (myGoal.findDistanceTo(myOldGoal) > 5) 
00095     setGoal(myGoal);
00096   
00097   // if we're there we don't do anything
00098   if (myState == STATE_ACHIEVED_GOAL || myState == STATE_NO_GOAL)
00099     return NULL;
00100 
00101   dist = myRobot->getPose().findDistanceTo(myGoal);
00102   if (dist < myCloseDist && ArMath::fabs(myRobot->getVel() < 5))
00103   {
00104     if (myPrinting)
00105       printf("Achieved goal\n");
00106     myState = STATE_ACHIEVED_GOAL;
00107     myDesired.setVel(0);
00108     myDesired.setDeltaHeading(0);
00109     return &myDesired;
00110   }
00111 
00112   if (myPrinting)
00113     printf("%.0f  ", dist);
00114   // see where we want to point
00115   angle = myRobot->getPose().findAngleTo(myGoal);
00116 
00117   if (ArMath::fabs(ArMath::subAngle(angle, myRobot->getTh())) > 120)
00118   {
00119     myCurTurnDir *= -1;
00120   }
00121 
00122 
00123   // see if somethings in front of us
00124   if (currentDesired.getMaxVelStrength() > 0 &&
00125       currentDesired.getMaxVel() < mySpeedToTurnAt)
00126   {
00127     if (myPrinting)
00128       printf("Something slowing us down.  ");
00129     myDesired.setDeltaHeading(myTurnAmount * myCurTurnDir);
00130   }
00131   else
00132   {
00133     if (myPrinting)
00134       printf("Can speed up and turn back again.  ");
00135     // see if we want to just point at the goal or not
00136     if (ArMath::fabs(
00137         ArMath::subAngle(angle, 
00138                  ArMath::addAngle(myTurnAmount * 
00139                           myCurTurnDir * -1, 
00140                           myRobot->getTh())))
00141     > myTurnAmount/2)
00142     {
00143       if (myPrinting)
00144     printf("Pointing to goal  ");
00145       myDesired.setHeading(angle);
00146     }
00147     else
00148     {
00149       if (myPrinting)
00150     printf("turning back  ");
00151       myDesired.setDeltaHeading(myTurnAmount * myCurTurnDir * -1);
00152     }
00153   }
00154   if (dist < myCloseDist && ArMath::fabs(myRobot->getVel() < 5))
00155   {
00156     if (myPrinting)
00157       printf("#achieved\n");
00158     myState = STATE_ACHIEVED_GOAL;
00159     myDesired.setVel(0);
00160     myDesired.setDeltaHeading(0);
00161   }
00162   // if we're close, stop
00163   else if (dist < myCloseDist)
00164   {
00165     if (myPrinting)
00166       printf("#stop\n");
00167     myDesired.setVel(0);
00168   }
00169   else
00170   {
00171     vel = sqrt(dist * 200 * 2);
00172     if (vel > mySpeed)
00173       vel = mySpeed;
00174     if (myPrinting)
00175       printf("#go %.0f\n", vel);
00176     myDesired.setVel(vel);
00177   }
00178   return &myDesired;
00179 }

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