directMotionExample.cpp

Sends a sequence of motion commands directly to the robot using ArRobot's direct motion command methods.

This demo starts up the robot in its own thread, then calls a series of the motion commands methods in ArRobot to simply send commands directly to the robot (rather than using e.g. ArAction objects.)

Note that if you want to stop direct motion commands and let ArActions take over, you must call ArRobot::clearDirectMotion() (otherwise ArRobot may continue sending motion commands that conflict with the requests of the action resolver). See the section on motion commands and actions for details.

00001 #include "Aria.h"
00002 
00016 /*
00017   This is a connection handler class, to demonstrate how to run code in
00018   response to events such as the program connecting an disconnecting
00019   from the robot.
00020 */
00021 class ConnHandler
00022 {
00023 public:
00024   // Constructor
00025   ConnHandler(ArRobot *robot);
00026   // Destructor, its just empty
00027   ~ConnHandler(void) {}
00028   // to be called if the connection was made
00029   void connected(void);
00030   // to call if the connection failed
00031   void connFail(void);
00032   // to be called if the connection was lost
00033   void disconnected(void);
00034 protected:
00035   // robot pointer
00036   ArRobot *myRobot;
00037   // the functor callbacks
00038   ArFunctorC<ConnHandler> myConnectedCB;
00039   ArFunctorC<ConnHandler> myConnFailCB;
00040   ArFunctorC<ConnHandler> myDisconnectedCB;
00041 };
00042 
00043 ConnHandler::ConnHandler(ArRobot *robot) :
00044   myConnectedCB(this, &ConnHandler::connected),  
00045   myConnFailCB(this, &ConnHandler::connFail),
00046   myDisconnectedCB(this, &ConnHandler::disconnected)
00047 
00048 {
00049   myRobot = robot;
00050   myRobot->addConnectCB(&myConnectedCB, ArListPos::FIRST);
00051   myRobot->addFailedConnectCB(&myConnFailCB, ArListPos::FIRST);
00052   myRobot->addDisconnectNormallyCB(&myDisconnectedCB, ArListPos::FIRST);
00053   myRobot->addDisconnectOnErrorCB(&myDisconnectedCB, ArListPos::FIRST);
00054 }
00055 
00056 // just exit if the connection failed
00057 void ConnHandler::connFail(void)
00058 {
00059   printf("directMotionDemo connection handler: Failed to connect.\n");
00060   myRobot->stopRunning();
00061   Aria::shutdown();
00062   return;
00063 }
00064 
00065 // turn on motors, and off sonar, and off amigobot sounds, when connected
00066 void ConnHandler::connected(void)
00067 {
00068   printf("directMotionDemo connection handler: Connected\n");
00069   myRobot->comInt(ArCommands::SONAR, 0);
00070   myRobot->comInt(ArCommands::ENABLE, 1);
00071   myRobot->comInt(ArCommands::SOUNDTOG, 0);
00072 }
00073 
00074 // lost connection, so just exit
00075 void ConnHandler::disconnected(void)
00076 {
00077   printf("directMotionDemo connection handler: Lost connection, exiting program.\n");
00078   exit(0);
00079 }
00080 
00081 
00082 
00083 int main(int argc, char **argv) 
00084 {
00085   Aria::init();
00086   
00087   ArArgumentParser argParser(&argc, argv);
00088 
00089   ArSimpleConnector con(&argParser);
00090 
00091   ArRobot robot;
00092 
00093   // the connection handler from above
00094   ConnHandler ch(&robot);
00095 
00096   if(!Aria::parseArgs())
00097   {
00098     Aria::logOptions();
00099     Aria::shutdown();
00100     return 1;
00101   }
00102 
00103   if(!con.connectRobot(&robot))
00104   {
00105     ArLog::log(ArLog::Normal, "directMotionExample: Could not connect to the robot. Exiting.");
00106     return 1;
00107   }
00108 
00109   ArLog::log(ArLog::Normal, "directMotionExample: Connected.");
00110 
00111   // Run the robot processing cycle in its own thread. Note that after starting this
00112   // thread, we must lock and unlock the ArRobot object before and after
00113   // accessing it.
00114   robot.runAsync(false);
00115 
00116 
00117   // Send the robot a series of motion commands directly, sleeping for a 
00118   // few seconds afterwards to give the robot time to execute them.
00119   printf("directMotionExample: Setting rot velocity to 100 deg/sec then sleeping 3 seconds\n");
00120   robot.lock();
00121   robot.setRotVel(100);
00122   robot.unlock();
00123   ArUtil::sleep(3*1000);
00124   printf("Stopping\n");
00125   robot.lock();
00126   robot.setRotVel(0);
00127   robot.unlock();
00128   ArUtil::sleep(200);
00129 
00130   printf("directMotionExample: Telling the robot to go 300 mm on left wheel and 100 mm on right wheel for 5 seconds\n");
00131   robot.lock();
00132   robot.setVel2(300, 100);
00133   robot.unlock();
00134   ArTime start;
00135   start.setToNow();
00136   while (1)
00137   {
00138     robot.lock();
00139     if (start.mSecSince() > 5000)
00140     {
00141       robot.unlock();
00142       break;
00143     }   
00144     robot.unlock();
00145     ArUtil::sleep(50);
00146   }
00147   
00148   printf("directMotionExample: Telling the robot to move forwards one meter, then sleeping 5 seconds\n");
00149   robot.lock();
00150   robot.move(1000);
00151   robot.unlock();
00152   start.setToNow();
00153   while (1)
00154   {
00155     robot.lock();
00156     if (robot.isMoveDone())
00157     {
00158       printf("directMotionExample: Finished distance\n");
00159       robot.unlock();
00160       break;
00161     }
00162     if (start.mSecSince() > 5000)
00163     {
00164       printf("directMotionExample: Distance timed out\n");
00165       robot.unlock();
00166       break;
00167     }   
00168     robot.unlock();
00169     ArUtil::sleep(50);
00170   }
00171   printf("directMotionExample: Telling the robot to move backwards one meter, then sleeping 5 seconds\n");
00172   robot.lock();
00173   robot.move(-1000);
00174   robot.unlock();
00175   start.setToNow();
00176   while (1)
00177   {
00178     robot.lock();
00179     if (robot.isMoveDone())
00180     {
00181       printf("directMotionExample: Finished distance\n");
00182       robot.unlock();
00183       break;
00184     }
00185     if (start.mSecSince() > 10000)
00186     {
00187       printf("directMotionExample: Distance timed out\n");
00188       robot.unlock();
00189       break;
00190     }
00191     robot.unlock();
00192     ArUtil::sleep(50);
00193   }
00194   printf("directMotionExample: Telling the robot to turn to 180, then sleeping 4 seconds\n");
00195   robot.lock();
00196   robot.setHeading(180);
00197   robot.unlock();
00198   start.setToNow();
00199   while (1)
00200   {
00201     robot.lock();
00202     if (robot.isHeadingDone(5))
00203     {
00204       printf("directMotionExample: Finished turn\n");
00205       robot.unlock();
00206       break;
00207     }
00208     if (start.mSecSince() > 5000)
00209     {
00210       printf("directMotionExample: Turn timed out\n");
00211       robot.unlock();
00212       break;
00213     }
00214     robot.unlock();
00215     ArUtil::sleep(100);
00216   }
00217   printf("directMotionExample: Telling the robot to turn to 90, then sleeping 2 seconds\n");
00218   robot.lock();
00219   robot.setHeading(90);
00220   robot.unlock();
00221   start.setToNow();
00222   while (1)
00223   {
00224     robot.lock();
00225     if (robot.isHeadingDone(5))
00226     {
00227       printf("directMotionExample: Finished turn\n");
00228       robot.unlock();
00229       break;
00230     }
00231     if (start.mSecSince() > 5000)
00232     {
00233       printf("directMotionExample: turn timed out\n");
00234       robot.unlock();
00235       break;
00236     }
00237     robot.unlock();
00238     ArUtil::sleep(100);
00239   }
00240   printf("directMotionExample: Setting vel2 to 200 mm/sec on both wheels, then sleeping 3 seconds\n");
00241   robot.lock();
00242   robot.setVel2(200, 200);
00243   robot.unlock();
00244   ArUtil::sleep(3000);
00245   printf("directMotionExample: Stopping the robot, then sleeping for 2 seconds\n");
00246   robot.lock();
00247   robot.stop();
00248   robot.unlock();
00249   ArUtil::sleep(2000);
00250   printf("directMotionExample: Setting velocity to 200 mm/sec then sleeping 3 seconds\n");
00251   robot.lock();
00252   robot.setVel(200);
00253   robot.unlock();
00254   ArUtil::sleep(3000);
00255   printf("directMotionExample: Stopping the robot, then sleeping for 2 seconds\n");
00256   robot.lock();
00257   robot.stop();
00258   robot.unlock();
00259   ArUtil::sleep(2000);
00260   printf("directMotionExample: Setting vel2 with 0 on left wheel, 200 mm/sec on right, then sleeping 5 seconds\n");
00261   robot.lock();
00262   robot.setVel2(0, 200);
00263   robot.unlock();
00264   ArUtil::sleep(5000);
00265   printf("directMotionExample: Telling the robot to rotate at 50 deg/sec then sleeping 5 seconds\n");
00266   robot.lock();
00267   robot.setRotVel(50);
00268   robot.unlock();
00269   ArUtil::sleep(5000);
00270   printf("directMotionExample: Telling the robot to rotate at -50 deg/sec then sleeping 5 seconds\n");
00271   robot.lock();
00272   robot.setRotVel(-50);
00273   robot.unlock();
00274   ArUtil::sleep(5000);
00275   printf("directMotionExample: Setting vel2 with 0 on both wheels, then sleeping 3 seconds\n");
00276   robot.lock();
00277   robot.setVel2(0, 0);
00278   robot.unlock();
00279   ArUtil::sleep(3000);
00280   printf("directMotionExample: Now having the robot change heading by -125 degrees, then sleeping for 6 seconds\n");
00281   robot.lock();
00282   robot.setDeltaHeading(-125);
00283   robot.unlock();
00284   ArUtil::sleep(6000);
00285   printf("directMotionExample: Now having the robot change heading by 45 degrees, then sleeping for 6 seconds\n");
00286   robot.lock();
00287   robot.setDeltaHeading(45);
00288   robot.unlock();
00289   ArUtil::sleep(6000);
00290   printf("directMotionExample: Setting vel2 with 200 on left wheel, 0 on right wheel, then sleeping 5 seconds\n");
00291   robot.lock();
00292   robot.setVel2(200, 0);
00293   robot.unlock();
00294   ArUtil::sleep(5000);
00295 
00296   printf("directMotionExample: Done, shutting down Aria and exiting.\n");
00297   Aria::shutdown();
00298   return 0;
00299 }
00300 

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