simpleMotionCommands.cpp

example showing how to connect and send basic motion commands to the robot

ARIA provides two levels of robot motion control, direct motion commands, and actions. This example shows direct motion commands. See actionExample.cpp, actionGroupExample.cpp, and others for examples on how to use actions. Actions provide a more modular way of performing more complex motion behaviors than the simple imperitive style used here.

See the ArRobot class documentation, as well as the overview of robot motion, for more information.

WARNING: this program does no sensing or avoiding of obstacles, the robot WILL collide with any objects in the way! Make sure the robot has about 2-3 meters of free space around it before starting the program.

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 
00024 int main(int argc, char **argv)
00025 {
00026 
00027   Aria::init();
00028   ArRobot robot;
00029   ArArgumentParser parser(&argc, argv);
00030   parser.loadDefaultArguments();
00031 
00032   ArLog::log(ArLog::Terse, "WARNING: this program does no sensing or avoiding of obstacles, the robot WILL collide with any objects in the way! Make sure the robot has approximately 3 meters of free space on all sides.");
00033 
00034   // ArRobotConnector connects to the robot, get some initial data from it such as type and name,
00035   // and then loads parameter files for this robot.
00036   ArRobotConnector robotConnector(&parser, &robot);
00037   if(!robotConnector.connectRobot())
00038   {
00039     ArLog::log(ArLog::Terse, "simpleMotionCommands: Could not connect to the robot.");
00040     if(parser.checkHelpAndWarnUnparsed())
00041     {
00042         Aria::logOptions();
00043         Aria::exit(1);
00044     }
00045   }
00046   if (!Aria::parseArgs())
00047   {
00048     Aria::logOptions();
00049     Aria::shutdown();
00050     return 1;
00051   }
00052   
00053   ArLog::log(ArLog::Normal, "simpleMotionCommands: Connected.");
00054 
00055   // Start the robot processing cycle running in the background.
00056   // True parameter means that if the connection is lost, then the 
00057   // run loop ends.
00058   robot.runAsync(true);
00059 
00060   // Print out some data from the SIP.  
00061 
00062   // We must "lock" the ArRobot object
00063   // before calling its methods, and "unlock" when done, to prevent conflicts
00064   // with the background thread started by the call to robot.runAsync() above.
00065   // See the section on threading in the manual for more about this.
00066   // Make sure you unlock before any sleep() call or any other code that will
00067   // take some time; if the robot remains locked during that time, then
00068   // ArRobot's background thread will be blocked and unable to communicate with
00069   // the robot, call tasks, etc.
00070   
00071   robot.lock();
00072   ArLog::log(ArLog::Normal, "simpleMotionCommands: Pose=(%.2f,%.2f,%.2f), Trans. Vel=%.2f, Rot. Vel=%.2f, Battery=%.2fV",
00073     robot.getX(), robot.getY(), robot.getTh(), robot.getVel(), robot.getRotVel(), robot.getBatteryVoltage());
00074   robot.unlock();
00075 
00076   // Sleep for 3 seconds.
00077   ArLog::log(ArLog::Normal, "simpleMotionCommands: Will start driving in 3 seconds...");
00078   ArUtil::sleep(3000);
00079 
00080   // Set forward velocity to 50 mm/s
00081   ArLog::log(ArLog::Normal, "simpleMotionCommands: Driving forward at 250 mm/s for 5 sec...");
00082   robot.lock();
00083   robot.enableMotors();
00084   robot.setVel(250);
00085   robot.unlock();
00086   ArUtil::sleep(5000);
00087 
00088   ArLog::log(ArLog::Normal, "simpleMotionCommands: Stopping.");
00089   robot.lock();
00090   robot.stop();
00091   robot.unlock();
00092   ArUtil::sleep(1000);
00093 
00094   ArLog::log(ArLog::Normal, "simpleMotionCommands: Rotating at 10 deg/s for 5 sec...");
00095   robot.lock();
00096   robot.setRotVel(10);
00097   robot.unlock();
00098   ArUtil::sleep(5000);
00099 
00100   ArLog::log(ArLog::Normal, "simpleMotionCommands: Rotating at -10 deg/s for 10 sec...");
00101   robot.lock();
00102   robot.setRotVel(-10);
00103   robot.unlock();
00104   ArUtil::sleep(10000);
00105 
00106   ArLog::log(ArLog::Normal, "simpleMotionCommands: Driving forward at 150 mm/s for 5 sec...");
00107   robot.lock();
00108   robot.setRotVel(0);
00109   robot.setVel(150);
00110   robot.unlock();
00111   ArUtil::sleep(5000);
00112 
00113   ArLog::log(ArLog::Normal, "simpleMotionCommands: Stopping.");
00114   robot.lock();
00115   robot.stop();
00116   robot.unlock();
00117   ArUtil::sleep(1000);
00118 
00119 
00120   // Other motion command functions include move(), setHeading(),
00121   // setDeltaHeading().  You can also adjust acceleration and deceleration
00122   // values used by the robot with setAccel(), setDecel(), setRotAccel(),
00123   // setRotDecel().  See the ArRobot class documentation for more.
00124 
00125   
00126   robot.lock();
00127   ArLog::log(ArLog::Normal, "simpleMotionCommands: Pose=(%.2f,%.2f,%.2f), Trans. Vel=%.2f, Rot. Vel=%.2f, Battery=%.2fV",
00128     robot.getX(), robot.getY(), robot.getTh(), robot.getVel(), robot.getRotVel(), robot.getBatteryVoltage());
00129   robot.unlock();
00130 
00131   
00132   ArLog::log(ArLog::Normal, "simpleMotionCommands: Ending robot thread...");
00133   robot.stopRunning();
00134 
00135   // wait for the thread to stop
00136   robot.waitForRunExit();
00137 
00138   // exit
00139   ArLog::log(ArLog::Normal, "simpleMotionCommands: Exiting.");
00140   return 0;
00141 }

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