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
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
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
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
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
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
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 }