00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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
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
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
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 }