gpsExample.cpp

Example program to connect to a GPS and read and display data on the terminal. Also tries to connect to a TCM compass through the computer serial port, and use that to set the ArGPS compass data.

00001 
00008 #include "Aria.h"
00009 #include "ArGPS.h"
00010 #include "ArGPSConnector.h"
00011 #include "ArTrimbleGPS.h"
00012 #include "ArTCMCompassDirect.h"
00013 #include <iostream>
00014 
00015 int main(int argc, char** argv)
00016 {
00017   Aria::init();
00018   ArLog::init(ArLog::StdErr, ArLog::Normal);
00019 
00020   ArArgumentParser argParser(&argc, argv);
00021   argParser.loadDefaultArguments();
00022   ArSimpleConnector connector(&argParser);
00023   ArGPSConnector gpsConnector(&argParser);
00024 
00025   if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00026   {
00027     Aria::logOptions();
00028     ArLog::log(ArLog::Terse, "gpsExample options:\n  -printTable   Print data to standard output in regular columns rather than a refreshing terminal display, and print more digits of precision");
00029     return 1;
00030   }
00031 
00032   // Try connecting to robot 
00033   ArRobot robot;
00034   if(!connector.connectRobot(&robot))
00035   {
00036     ArLog::log(ArLog::Terse, "gpsExample: Warning: Could not connect to robot.  Will not be able to switch GPS power on, or load GPS options from this robot's parameter file.");
00037   }
00038   else
00039   {
00040     ArLog::log(ArLog::Normal, "gpsExample: Connected to robot.");
00041     robot.runAsync(true);
00042   }
00043 
00044   // check command line arguments for -printTable
00045   bool printTable = argParser.checkArgument("printTable");
00046 
00047   // On the Seekur, power to the GPS receiver is switched on by this command.
00048   // (A third argument of 0 would turn it off). On other robots this command is
00049   // ignored.
00050   robot.com2Bytes(116, 6, 1);
00051 
00052   // Try connecting to a GPS. We pass the robot pointetr to the connector so it
00053   // can check the robot parameters for this robot type for default values for
00054   // GPS device connection information (receiver type, serial port, etc.)
00055   ArLog::log(ArLog::Normal, "gpsExample: Connecting to GPS, it may take a few seconds...");
00056   ArGPS *gps = gpsConnector.createGPS(&robot);
00057   if(!gps || !gps->connect())
00058   {
00059     ArLog::log(ArLog::Terse, "gpsExample: Error connecting to GPS device.  Try -gpsType, -gpsPort, and/or -gpsBaud command-line arguments. Use -help for help.");
00060     return -1;
00061   }
00062 
00063 
00064   ArLog::log(ArLog::Normal, "gpsExample: Reading data...");
00065   ArTime lastReadTime;
00066   if(printTable)
00067     gps->printDataLabelsHeader();
00068   while(true)
00069   {
00070     int r = gps->read();
00071     if(r & ArGPS::ReadError)
00072     {
00073       ArLog::log(ArLog::Terse, "gpsExample: Warning: error reading GPS data.");
00074       ArUtil::sleep(1000);
00075       continue;
00076     }
00077 
00078 
00079     if(r & ArGPS::ReadUpdated)
00080     {
00081       if(printTable)
00082       {
00083         gps->printData(false);
00084         printf("\n");
00085       }
00086       else
00087       {
00088         gps->printData();
00089         printf("\r");
00090       }
00091       fflush(stdout);
00092       ArUtil::sleep(500);
00093       lastReadTime.setToNow();
00094       continue;
00095     } else {
00096       if(lastReadTime.secSince() >= 5) {
00097         ArLog::log(ArLog::Terse, "gpsExample: Warning: haven't recieved any data from GPS for more than 5 seconds!");
00098       }
00099       ArUtil::sleep(1000);
00100       continue;
00101     }
00102 
00103   }
00104   return 0;
00105 }

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