Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

directMotionExample.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 
00041 /*
00042   This is a connection handler, fairly simple, but quite useful, esp when
00043   the robot is running in another thread.  Its not really needed here
00044   since blockingConnect is used.  But it'd still be useful if there is an
00045   error after connected
00046 */
00047 class ConnHandler
00048 {
00049 public:
00050   // Constructor
00051   ConnHandler(ArRobot *robot);
00052   // Destructor, its just empty
00053   ~ConnHandler(void) {}
00054   // to be called if the connection was made
00055   void connected(void);
00056   // to call if the connection failed
00057   void connFail(void);
00058   // to be called if the connection was lost
00059   void disconnected(void);
00060 protected:
00061   // robot pointer
00062   ArRobot *myRobot;
00063   // the functor callbacks
00064   ArFunctorC<ConnHandler> myConnectedCB;
00065   ArFunctorC<ConnHandler> myConnFailCB;
00066   ArFunctorC<ConnHandler> myDisconnectedCB;
00067 };
00068 
00069 /*
00070   The constructor, note its use of contructor chaining to initalize the 
00071   callbacks.
00072 */
00073 ConnHandler::ConnHandler(ArRobot *robot) :
00074   myConnectedCB(this, &ConnHandler::connected),  
00075   myConnFailCB(this, &ConnHandler::connFail),
00076   myDisconnectedCB(this, &ConnHandler::disconnected)
00077 
00078 {
00079   // set the robot poitner
00080   myRobot = robot;
00081 
00082   // add the callbacks to the robot
00083   myRobot->addConnectCB(&myConnectedCB, ArListPos::FIRST);
00084   myRobot->addFailedConnectCB(&myConnFailCB, ArListPos::FIRST);
00085   myRobot->addDisconnectNormallyCB(&myDisconnectedCB, ArListPos::FIRST);
00086   myRobot->addDisconnectOnErrorCB(&myDisconnectedCB, ArListPos::FIRST);
00087 }
00088 
00089 // just exit if the connection failed
00090 void ConnHandler::connFail(void)
00091 {
00092   printf("Failed to connect.\n");
00093   myRobot->stopRunning();
00094   Aria::shutdown();
00095   return;
00096 }
00097 
00098 // turn on motors, and off sonar, and off amigobot sounds, when connected
00099 void ConnHandler::connected(void)
00100 {
00101   printf("Connected\n");
00102   myRobot->comInt(ArCommands::SONAR, 0);
00103   myRobot->comInt(ArCommands::ENABLE, 1);
00104   myRobot->comInt(ArCommands::SOUNDTOG, 0);
00105 }
00106 
00107 // lost connection, so just exit
00108 void ConnHandler::disconnected(void)
00109 {
00110   printf("Lost connection\n");
00111   exit(0);
00112 }
00113 
00114 
00115 
00116 int main(int argc, char **argv) 
00117 {
00118   std::string str;
00119   int ret;
00120   ArTime start;
00121   
00122   // connection to the robot
00123   ArTcpConnection con;
00124   // the robot
00125   ArRobot robot;
00126   // the connection handler from above
00127   ConnHandler ch(&robot);
00128 
00129   // init area with a dedicated signal handling thread
00130   Aria::init(Aria::SIGHANDLE_THREAD);
00131 
00132   // open the connection with the defaults, exit if failed
00133   if ((ret = con.open()) != 0)
00134   {
00135     str = con.getOpenMessage(ret);
00136     printf("Open failed: %s\n", str.c_str());
00137     Aria::shutdown();
00138     return 1;
00139   }
00140 
00141   // set the robots connection
00142   robot.setDeviceConnection(&con);
00143   // try to connect, if we fail, the connection handler should bail
00144   if (!robot.blockingConnect())
00145   {
00146     // this should have been taken care of by the connection handler
00147     // but just in case
00148     printf(
00149     "asyncConnect failed because robot is not running in its own thread.\n");
00150     Aria::shutdown();
00151     return 1;
00152   }
00153   // run the robot in its own thread, so it gets and processes packets and such
00154   robot.runAsync(false);
00155 
00156   // just a big long set of printfs, direct motion commands and sleeps,
00157   // it should be self-explanatory
00158 
00159   printf("Setting rot velocity to 100 deg/sec then sleeping 3 seconds\n");
00160   robot.lock();
00161   robot.setRotVel(100);
00162   robot.unlock();
00163   ArUtil::sleep(3*1000);
00164   printf("Stopping\n");
00165   robot.lock();
00166   robot.setRotVel(0);
00167   robot.unlock();
00168   ArUtil::sleep(200);
00169 
00170   printf("Telling the robot to go 300 mm on left wheel and 100 mm on right wheel for 5 seconds\n");
00171   robot.lock();
00172   robot.setVel2(300, 100);
00173   robot.unlock();
00174   start.setToNow();
00175   while (1)
00176   {
00177     robot.lock();
00178     if (start.mSecSince() > 5000)
00179     {
00180       robot.unlock();
00181       break;
00182     }   
00183     robot.unlock();
00184     ArUtil::sleep(50);
00185   }
00186   
00187   printf("Telling the robot to move forwards one meter, then sleeping 5 seconds\n");
00188   robot.lock();
00189   robot.move(1000);
00190   robot.unlock();
00191   start.setToNow();
00192   while (1)
00193   {
00194     robot.lock();
00195     if (robot.isMoveDone())
00196     {
00197       printf("Finished distance\n");
00198       robot.unlock();
00199       break;
00200     }
00201     if (start.mSecSince() > 5000)
00202     {
00203       printf("Distance timed out\n");
00204       robot.unlock();
00205       break;
00206     }   
00207     robot.unlock();
00208     ArUtil::sleep(50);
00209   }
00210   printf("Telling the robot to move backwards one meter, then sleeping 5 seconds\n");
00211   robot.lock();
00212   robot.move(-1000);
00213   robot.unlock();
00214   start.setToNow();
00215   while (1)
00216   {
00217     robot.lock();
00218     if (robot.isMoveDone())
00219     {
00220       printf("Finished distance\n");
00221       robot.unlock();
00222       break;
00223     }
00224     if (start.mSecSince() > 10000)
00225     {
00226       printf("Distance timed out\n");
00227       robot.unlock();
00228       break;
00229     }
00230     robot.unlock();
00231     ArUtil::sleep(50);
00232   }
00233   printf("Telling the robot to turn to 180, then sleeping 4 seconds\n");
00234   robot.lock();
00235   robot.setHeading(180);
00236   robot.unlock();
00237   start.setToNow();
00238   while (1)
00239   {
00240     robot.lock();
00241     if (robot.isHeadingDone(5))
00242     {
00243       printf("Finished turn\n");
00244       robot.unlock();
00245       break;
00246     }
00247     if (start.mSecSince() > 5000)
00248     {
00249       printf("Turn timed out\n");
00250       robot.unlock();
00251       break;
00252     }
00253     robot.unlock();
00254     ArUtil::sleep(100);
00255   }
00256   printf("Telling the robot to turn to 90, then sleeping 2 seconds\n");
00257   robot.lock();
00258   robot.setHeading(90);
00259   robot.unlock();
00260   start.setToNow();
00261   while (1)
00262   {
00263     robot.lock();
00264     if (robot.isHeadingDone(5))
00265     {
00266       printf("Finished turn\n");
00267       robot.unlock();
00268       break;
00269     }
00270     if (start.mSecSince() > 5000)
00271     {
00272       printf("turn timed out\n");
00273       robot.unlock();
00274       break;
00275     }
00276     robot.unlock();
00277     ArUtil::sleep(100);
00278   }
00279   printf("Setting vel2 to 200 mm/sec on both wheels, then sleeping 3 seconds\n");
00280   robot.lock();
00281   robot.setVel2(200, 200);
00282   robot.unlock();
00283   ArUtil::sleep(3000);
00284   printf("Stopping the robot, then sleeping for 2 seconds\n");
00285   robot.lock();
00286   robot.stop();
00287   robot.unlock();
00288   ArUtil::sleep(2000);
00289   printf("Setting velocity to 200 mm/sec then sleeping 3 seconds\n");
00290   robot.lock();
00291   robot.setVel(200);
00292   robot.unlock();
00293   ArUtil::sleep(3000);
00294   printf("Stopping the robot, then sleeping for 2 seconds\n");
00295   robot.lock();
00296   robot.stop();
00297   robot.unlock();
00298   ArUtil::sleep(2000);
00299   printf("Setting vel2 with 0 on left wheel, 200 mm/sec on right, then sleeping 5 seconds\n");
00300   robot.lock();
00301   robot.setVel2(0, 200);
00302   robot.unlock();
00303   ArUtil::sleep(5000);
00304   printf("Telling the robot to rotate at 50 deg/sec then sleeping 5 seconds\n");
00305   robot.lock();
00306   robot.setRotVel(50);
00307   robot.unlock();
00308   ArUtil::sleep(5000);
00309   printf("Telling the robot to rotate at -50 deg/sec then sleeping 5 seconds\n");
00310   robot.lock();
00311   robot.setRotVel(-50);
00312   robot.unlock();
00313   ArUtil::sleep(5000);
00314   printf("Setting vel2 with 0 on both wheels, then sleeping 3 seconds\n");
00315   robot.lock();
00316   robot.setVel2(0, 0);
00317   robot.unlock();
00318   ArUtil::sleep(3000);
00319   printf("Now having the robot change heading by -125 degrees, then sleeping for 6 seconds\n");
00320   robot.lock();
00321   robot.setDeltaHeading(-125);
00322   robot.unlock();
00323   ArUtil::sleep(6000);
00324   printf("Now having the robot change heading by 45 degrees, then sleeping for 6 seconds\n");
00325   robot.lock();
00326   robot.setDeltaHeading(45);
00327   robot.unlock();
00328   ArUtil::sleep(6000);
00329   printf("Setting vel2 with 200 on left wheel, 0 on right wheel, then sleeping 5 seconds\n");
00330   robot.lock();
00331   robot.setVel2(200, 0);
00332   robot.unlock();
00333   ArUtil::sleep(5000);
00334 
00335   printf("Done with tests, exiting\n");
00336   // shutdown and ge tout
00337   Aria::shutdown();
00338   return 0;
00339 }
00340 

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0