demo.cpp

General purpose testing and demo program, using ArMode classes to provide keyboard control of various robot functions.

demo uses ArMode subclasses, which are pre-made classes in ARIA that provide keyboard control of various aspects and accessories of the robot. The ArMode classes are defined in ArModes.cpp.

"demo" is a useful program for testing out the operation of the robot for diagnostic or demonstration purposes. Other example programs focus on individual areas.

00001 #include "Aria.h"
00002 
00003 
00016 int main(int argc, char** argv)
00017 {
00018   // Initialize some global data
00019   Aria::init();
00020 
00021   // If you want ArLog to print "Verbose" level messages uncomment this:
00022   //ArLog::init(ArLog::StdOut, ArLog::Verbose);
00023 
00024   // This object parses program options from the command line
00025   ArArgumentParser parser(&argc, argv);
00026 
00027   // Load some default values for command line arguments from /etc/Aria.args
00028   // (Linux) or the ARIAARGS environment variable.
00029   parser.loadDefaultArguments();
00030 
00031   // Central object that is an interface to the robot and its integrated
00032   // devices, and which manages control of the robot by the rest of the program.
00033   ArRobot robot;
00034 
00035   // Object that connects to the robot or simulator using program options
00036   ArRobotConnector robotConnector(&parser, &robot);
00037 
00038   // If the robot has an Analog Gyro, this object will activate it, and 
00039   // if the robot does not automatically use the gyro to correct heading,
00040   // this object reads data from it and corrects the pose in ArRobot
00041   ArAnalogGyro gyro(&robot);
00042 
00043   // Connect to the robot, get some initial data from it such as type and name,
00044   // and then load parameter files for this robot.
00045   if (!robotConnector.connectRobot())
00046   {
00047     // Error connecting:
00048     // if the user gave the -help argumentp, then just print out what happened,
00049     // and continue so options can be displayed later.
00050     if (!parser.checkHelpAndWarnUnparsed())
00051     {
00052       ArLog::log(ArLog::Terse, "Could not connect to robot, will not have parameter file so options displayed later may not include everything");
00053     }
00054     // otherwise abort
00055     else
00056     {
00057       ArLog::log(ArLog::Terse, "Error, could not connect to robot.");
00058       Aria::logOptions();
00059       Aria::exit(1);
00060     }
00061   }
00062 
00063   // Connector for laser rangefinders
00064   ArLaserConnector laserConnector(&parser, &robot, &robotConnector);
00065 
00066   // Connector for compasses
00067   ArCompassConnector compassConnector(&parser);
00068 
00069   // Parse the command line options. Fail and print the help message if the parsing fails
00070   // or if the help was requested with the -help option
00071   if (!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
00072   {    
00073     Aria::logOptions();
00074     exit(1);
00075   }
00076 
00077   // Used to access and process sonar range data
00078   ArSonarDevice sonarDev;
00079   
00080   // Used to perform actions when keyboard keys are pressed
00081   ArKeyHandler keyHandler;
00082   Aria::setKeyHandler(&keyHandler);
00083 
00084   // ArRobot contains an exit action for the Escape key. It also 
00085   // stores a pointer to the keyhandler so that other parts of the program can
00086   // use the same keyhandler.
00087   robot.attachKeyHandler(&keyHandler);
00088   printf("You may press escape to exit\n");
00089 
00090   // Attach sonarDev to the robot so it gets data from it.
00091   robot.addRangeDevice(&sonarDev);
00092 
00093   
00094   // Start the robot task loop running in a new background thread. The 'true' argument means if it loses
00095   // connection the task loop stops and the thread exits.
00096   robot.runAsync(true);
00097 
00098   // Connect to the laser(s) if lasers were configured in this robot's parameter
00099   // file or on the command line, and run laser processing thread if applicable
00100   // for that laser class.  (For the purposes of this demo, add all
00101   // possible lasers to ArRobot's list rather than just the ones that were
00102   // specified with the connectLaser option (so when you enter laser mode, you
00103   // can then interactively choose which laser to use from the list which will
00104   // show both connected and unconnected lasers.)
00105   if (!laserConnector.connectLasers(false, false, true))
00106   {
00107     printf("Could not connect to lasers... exiting\n");
00108     Aria::exit(2);
00109   }
00110 
00111   // Create and connect to the compass if the robot has one.
00112   ArTCM2 *compass = compassConnector.create(&robot);
00113   if(compass && !compass->blockingConnect()) {
00114     compass = NULL;
00115   }
00116   
00117   // Sleep for a second so some messages from the initial responses
00118   // from robots and cameras and such can catch up
00119   ArUtil::sleep(1000);
00120 
00121   // We need to lock the robot since we'll be setting up these modes
00122   // while the robot task loop thread is already running, and they 
00123   // need to access some shared data in ArRobot.
00124   robot.lock();
00125 
00126   // now add all the modes for this demo
00127   // these classes are defined in ArModes.cpp in ARIA's source code.
00128   ArModeLaser laser(&robot, "laser", 'l', 'L');
00129   ArModeTeleop teleop(&robot, "teleop", 't', 'T');
00130   ArModeUnguardedTeleop unguardedTeleop(&robot, "unguarded teleop", 'u', 'U');
00131   ArModeWander wander(&robot, "wander", 'w', 'W');
00132   ArModeGripper gripper(&robot, "gripper", 'g', 'G');
00133   ArModeCamera camera(&robot, "camera", 'c', 'C');
00134   ArModeSonar sonar(&robot, "sonar", 's', 'S');
00135   ArModeBumps bumps(&robot, "bumps", 'b', 'B');
00136   ArModePosition position(&robot, "position", 'p', 'P', &gyro);
00137   ArModeIO io(&robot, "io", 'i', 'I');
00138   ArModeActs actsMode(&robot, "acts", 'a', 'A');
00139   ArModeCommand command(&robot, "command", 'd', 'D');
00140   ArModeTCM2 tcm2(&robot, "tcm2", 'm', 'M', compass);
00141 
00142 
00143   // activate the default mode
00144   teleop.activate();
00145 
00146   // turn on the motors
00147   robot.comInt(ArCommands::ENABLE, 1);
00148 
00149   robot.unlock();
00150   
00151   // Block execution of the main thread here and wait for the robot's task loop
00152   // thread to exit (e.g. by robot disconnecting, escape key pressed, or OS
00153   // signal)
00154   robot.waitForRunExit();
00155 
00156   Aria::exit(0);
00157 
00158 
00159 }
00160 

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