gotoActionExample.cpp

Uses ArActionGoto to drive the robot in a square

This program will make the robot drive in a 2.5x2.5 meter square by setting each corner in turn as the goal for an ArActionGoto action. It also uses speed limiting actions to avoid collisions. After some time, it cancels the goal (and the robot stops due to a stopping action) and exits.

Press escape to shut down Aria and exit.

00001 #include "Aria.h"
00002 
00014 int main(int argc, char **argv)
00015 {
00016   Aria::init();
00017   ArArgumentParser parser(&argc, argv);
00018   parser.loadDefaultArguments();
00019   ArSimpleConnector simpleConnector(&parser);
00020   ArRobot robot;
00021   ArSonarDevice sonar;
00022   ArAnalogGyro gyro(&robot);
00023   robot.addRangeDevice(&sonar);
00024 
00025   // Make a key handler, so that escape will shut down the program
00026   // cleanly
00027   ArKeyHandler keyHandler;
00028   Aria::setKeyHandler(&keyHandler);
00029   robot.attachKeyHandler(&keyHandler);
00030   printf("You may press escape to exit\n");
00031 
00032   // Collision avoidance actions at higher priority
00033   ArActionLimiterForwards limiterAction("speed limiter near", 300, 600, 250);
00034   ArActionLimiterForwards limiterFarAction("speed limiter far", 300, 1100, 400);
00035   ArActionLimiterTableSensor tableLimiterAction;
00036   robot.addAction(&tableLimiterAction, 100);
00037   robot.addAction(&limiterAction, 95);
00038   robot.addAction(&limiterFarAction, 90);
00039 
00040   // Goto action at lower priority
00041   ArActionGoto gotoPoseAction("goto");
00042   robot.addAction(&gotoPoseAction, 50);
00043   
00044   // Stop action at lower priority, so the robot stops if it has no goal
00045   ArActionStop stopAction("stop");
00046   robot.addAction(&stopAction, 40);
00047 
00048   // Parse all command line arguments
00049   if (!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
00050   {    
00051     Aria::logOptions();
00052     exit(1);
00053   }
00054   
00055   // Connect to the robot
00056   if (!simpleConnector.connectRobot(&robot))
00057   {
00058     printf("Could not connect to robot... exiting\n");
00059     Aria::exit(1);
00060   }
00061   robot.runAsync(true);
00062 
00063   // turn on the motors, turn off amigobot sounds
00064   robot.enableMotors();
00065   robot.comInt(ArCommands::SOUNDTOG, 0);
00066 
00067   const int duration = 30000; //msec
00068   ArLog::log(ArLog::Normal, "Going to four goals in turn for %d seconds, then cancelling goal and exiting.", duration/1000);
00069 
00070   bool first = true;
00071   int goalNum = 0;
00072   ArTime start;
00073   start.setToNow();
00074   while (Aria::getRunning()) 
00075   {
00076     robot.lock();
00077 
00078     // Choose a new goal if this is the first loop iteration, or if we 
00079     // achieved the previous goal.
00080     if (first || gotoPoseAction.haveAchievedGoal())
00081     {
00082       first = false;
00083       goalNum++;
00084       if (goalNum > 4)
00085         goalNum = 1; // start again at goal #1
00086 
00087       // set our positions for the different goals
00088       if (goalNum == 1)
00089         gotoPoseAction.setGoal(ArPose(2500, 0));
00090       else if (goalNum == 2)
00091         gotoPoseAction.setGoal(ArPose(2500, 2500));
00092       else if (goalNum == 3)
00093         gotoPoseAction.setGoal(ArPose(0, 2500));
00094       else if (goalNum == 4)
00095         gotoPoseAction.setGoal(ArPose(0, 0));
00096 
00097       ArLog::log(ArLog::Normal, "Going to next goal at %.0f %.0f", 
00098             gotoPoseAction.getGoal().getX(), gotoPoseAction.getGoal().getY());
00099     }
00100 
00101     if(start.mSecSince() >= duration) {
00102       ArLog::log(ArLog::Normal, "%d seconds have elapsed. Cancelling current goal, waiting 3 seconds, and exiting.", duration/1000);
00103       gotoPoseAction.cancelGoal();
00104       robot.unlock();
00105       ArUtil::sleep(3000);
00106       break;
00107     }
00108 
00109     robot.unlock();
00110     ArUtil::sleep(100);
00111   }
00112   
00113   // Robot disconnected or time elapsed, shut down
00114   Aria::shutdown();
00115   return 0;
00116 }

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7