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

ArActionGoto.cpp

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

Generated on Wed Oct 19 12:56:34 2005 for Aria by  doxygen 1.4.0