wander.cpp

Example using actions and range devices to implement a random wander avoiding obstacles.

This program will just have the robot wander around. It uses some avoidance actions if obstacles are detected with the sonar or laser (if robot has a laser), otherwise it just has a constant forward velocity.

Press Control-C or Escape keys to exit.

This program will work either with the MobileSim simulator or on a real robot's onboard computer. (Or use -remoteHost to connect to a wireless ethernet-serial bridge.)

00001 #include "Aria.h"
00002 
00017 int main(int argc, char **argv)
00018 {
00019   Aria::init();
00020   ArArgumentParser argParser(&argc, argv);
00021   argParser.loadDefaultArguments();
00022   ArRobot robot;
00023   ArRobotConnector robotConnector(&argParser, &robot);
00024   ArLaserConnector laserConnector(&argParser, &robot, &robotConnector);
00025 
00026   // Always try to connect to the first laser:
00027   argParser.addDefaultArgument("-connectLaser");
00028 
00029   if(!robotConnector.connectRobot())
00030   {
00031     ArLog::log(ArLog::Terse, "Could not connect to the robot.");
00032     if(argParser.checkHelpAndWarnUnparsed())
00033     {
00034         // -help not given, just exit.
00035         Aria::logOptions();
00036         Aria::exit(1);
00037     }
00038   }
00039 
00040 
00041   // Trigger argument parsing
00042   if (!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00043   {
00044     Aria::logOptions();
00045     Aria::exit(1);
00046   }
00047 
00048   ArKeyHandler keyHandler;
00049   Aria::setKeyHandler(&keyHandler);
00050   robot.attachKeyHandler(&keyHandler);
00051 
00052   puts("This program will make the robot wander around. It uses some avoidance\n"
00053   "actions if obstacles are detected, otherwise it just has a\n"
00054   "constant forward velocity.\n\nPress CTRL-C or Escape to exit.");
00055   
00056   ArSonarDevice sonar;
00057   robot.addRangeDevice(&sonar);
00058 
00059   robot.runAsync(true);
00060 
00061   
00062   // try to connect to laser. if fail, warn but continue, using sonar only
00063   if(!laserConnector.connectLasers())
00064   {
00065     ArLog::log(ArLog::Normal, "Warning: unable to connect to requested lasers, will wander using robot sonar only.");
00066   }
00067 
00068 
00069   // turn on the motors, turn off amigobot sounds
00070   robot.enableMotors();
00071   robot.comInt(ArCommands::SOUNDTOG, 0);
00072 
00073   // add a set of actions that combine together to effect the wander behavior
00074   ArActionStallRecover recover;
00075   ArActionBumpers bumpers;
00076   ArActionAvoidFront avoidFrontNear("Avoid Front Near", 225, 0);
00077   ArActionAvoidFront avoidFrontFar;
00078   ArActionConstantVelocity constantVelocity("Constant Velocity", 400);
00079   robot.addAction(&recover, 100);
00080   robot.addAction(&bumpers, 75);
00081   robot.addAction(&avoidFrontNear, 50);
00082   robot.addAction(&avoidFrontFar, 49);
00083   robot.addAction(&constantVelocity, 25);
00084   
00085   // wait for robot task loop to end before exiting the program
00086   robot.waitForRunExit();
00087   
00088   Aria::exit(0);
00089 }

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