gpsRobotTaskExample.cpp

Connects to both robot and GPS, allows teleoperation, and prints robot position and GPS data.

00001 
00002 
00003 #include "Aria.h"
00004 #include "ArGPS.h"
00005 #include "ArGPSConnector.h"
00006 
00007 
00013 /*  
00014  *  This class encapsulates an ArRobot sensor interpretation task that prints the
00015  *  last set of GPS data along with a local timestamp and current robot pose to 
00016  *  standard output.
00017  *
00018  *  This class also contains a mutex, which it locks during the task (while
00019  *  accessing the ArGPS object).  If another thread is also accessing the GPS,
00020  *  you can lock this mutex.
00021  */
00022 class GPSLogTask {
00023 
00024 public:
00025   GPSLogTask(ArRobot *robot, ArGPS *gps, ArJoyHandler *joy = NULL) :
00026       myRobot(robot), 
00027       myGPS(gps),
00028       myTaskFunctor(this, &GPSLogTask::doTask),
00029       myJoyHandler(joy),
00030       myButtonDown(false)
00031   {
00032     myRobot->addSensorInterpTask("GPS", ArListPos::LAST, &myTaskFunctor);   
00033     puts("RobotX\tRobotY\tRobotTh\tRobotVel\tRobotRotVel\tRobotLatVel\tLatitude\tLongitude\tAltitude\tSpeed\tGPSTimeSec\tGPSTimeMSec\tFixType\tNumSats\tPDOP\tHDOP\tVDOP\tGPSDataReceived");
00034   }
00035 
00036   void lock() { myMutex.lock(); }
00037   void unlock() { myMutex.unlock(); }
00038 
00039 protected:
00040 
00041   void doTask()
00042   {
00043     // print a mark if a joystick button is pressed (other than 1, which is
00044     // needed to drive)
00045     if(myJoyHandler)
00046     {
00047       for(unsigned int b = 2; b <= myJoyHandler->getNumButtons(); ++b)
00048         if(myJoyHandler->getButton(b)) {
00049           if(!myButtonDown)
00050             printf("--------------- Joystick button %d pressed.\n", b);
00051           myButtonDown = true;
00052         }
00053         else
00054           myButtonDown = false;
00055     }
00056 
00057     lock();
00058     int f = myGPS->read(50);
00059     printf("%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f"
00060            "\t%2.8f\t%2.8f\t%4.4f\t%4.4f"
00061            "\t%lu\t%lu\t%s"
00062            "\t%u\t%2.4f\t%2.4f\t%2.4f"
00063            "\t%s\n",
00064       myRobot->getX(), myRobot->getY(), myRobot->getTh(), myRobot->getVel(), myRobot->getRotVel(), (myRobot->hasLatVel())?(myRobot->getLatVel()):0,
00065       myGPS->getLatitude(), myGPS->getLongitude(), myGPS->getAltitude(), myGPS->getSpeed(),
00066       myGPS->getGPSPositionTimestamp().getSec(), myGPS->getGPSPositionTimestamp().getMSec(), myGPS->getFixTypeName(),
00067       myGPS->getNumSatellitesTracked(), myGPS->getPDOP(), myGPS->getHDOP(), myGPS->getVDOP(),
00068       ((f&ArGPS::ReadUpdated)?"yes":"no")
00069     );
00070     unlock();
00071   }
00072 
00073 private:
00074   ArRobot *myRobot;
00075   ArGPS *myGPS;
00076   ArFunctorC<GPSLogTask> myTaskFunctor;
00077   ArMutex myMutex;
00078   ArJoyHandler *myJoyHandler;
00079   bool myButtonDown;
00080 };
00081 
00082 
00083 
00084 int main(int argc, char** argv)
00085 {
00086   Aria::init();
00087   ArLog::init(ArLog::StdErr, ArLog::Normal);
00088 
00089   ArArgumentParser argParser(&argc, argv);
00090   ArSimpleConnector connector(&argParser);
00091   ArGPSConnector gpsConnector(&argParser);
00092   ArRobot robot;
00093 
00094   ArActionLimiterForwards nearLimitAction("limit near", 300, 600, 250);
00095   ArActionLimiterForwards farLimitAction("limit far", 300, 1100, 400);
00096   ArActionLimiterBackwards limitBackwardsAction;
00097   ArActionJoydrive joydriveAction;
00098   ArActionKeydrive keydriveAction;
00099 
00100   ArSonarDevice sonar;
00101   ArSick laser;
00102 
00103   argParser.loadDefaultArguments();
00104   if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00105   {
00106     Aria::logOptions();
00107     return -1;
00108   }
00109 
00110   robot.addRangeDevice(&sonar);
00111   robot.addRangeDevice(&laser);
00112 
00113   ArLog::log(ArLog::Normal, "gpsRobotTaskExample: Connecting to robot...");
00114   if(!connector.connectRobot(&robot))
00115   {
00116     ArLog::log(ArLog::Terse, "gpsRobotTaskExample: Could not connect to the robot. Exiting.");
00117     return -2;
00118   }
00119   ArLog::log(ArLog::Normal, "gpsRobotTaskExample: Connected to the robot.");
00120 
00121 
00122   // Connect to GPS
00123   ArLog::log(ArLog::Normal, "gpsRobotTaskExample: Connecting to GPS, it may take a few seconds...");
00124   ArGPS *gps = gpsConnector.createGPS();
00125   if(!gps || !gps->connect());
00126   {
00127     ArLog::log(ArLog::Terse, "gpsRobotTaskExample: Error connecting to GPS device.  Try -gpsType, -gpsPort, and/or -gpsBaud command-line arguments. Use -help for help. Exiting.");
00128     return -3;
00129   }
00130 
00131 
00132   // Create an GPSLogTask which will register a task with the robot.
00133   GPSLogTask gpsTask(&robot, gps, joydriveAction.getJoyHandler()->haveJoystick() ? joydriveAction.getJoyHandler() : NULL);
00134 
00135 
00136   // Add actions
00137   robot.addAction(&nearLimitAction, 100);
00138   robot.addAction(&farLimitAction, 90);
00139   robot.addAction(&limitBackwardsAction, 80);
00140   robot.addAction(&joydriveAction, 50);
00141   robot.addAction(&keydriveAction, 40);
00142 
00143   // allow keydrive action to drive robot even if joystick button isn't pressed
00144   joydriveAction.setStopIfNoButtonPressed(false);
00145 
00146   // Start the robot  running
00147   robot.runAsync(true);
00148 
00149   // Connect to the laser
00150   connector.setupLaser(&laser);
00151   laser.runAsync();
00152   if(!laser.blockingConnect())
00153     ArLog::log(ArLog::Normal, "gpsRobotTaskExample: Warning, could not connect to SICK laser, will not use it.");
00154 
00155   robot.lock();
00156 
00157   robot.enableMotors();
00158   robot.comInt(47, 1);  // enable joystick driving on some robots
00159 
00160   // Add exit callback to reset/unwrap steering wheels on seekur (critical if the robot doesn't have sliprings); does nothing for other robots 
00161   Aria::addExitCallback(new ArRetFunctor1C<bool, ArRobot, unsigned char>(&robot, &ArRobot::com, (unsigned char)120));
00162   Aria::addExitCallback(new ArRetFunctor1C<bool, ArRobot, unsigned char>(&robot, &ArRobot::com, (unsigned char)120));
00163 
00164   robot.unlock();
00165 
00166   ArLog::log(ArLog::Normal, "gpsRobotTaskExample: Running... (drive robot with joystick or arrow keys)");
00167   robot.waitForRunExit();
00168 
00169 
00170   return 0;
00171 }

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