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

ArGPSConnector.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 
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArGPSConnector.h"
00030 #include "ArGPS.h"
00031 #include "ArNovatelGPS.h"
00032 #include "ArDeviceConnection.h"
00033 #include "ArSerialConnection.h"
00034 #include "ArTcpConnection.h"
00035 
00036 #include "Aria.h"
00037 #include <iostream>
00038 
00039 
00040 const int ARGPS_DEFAULT_SERIAL_BAUD = 9600;
00041 const char* const ARGPS_DEFAULT_SERIAL_PORT = ArUtil::COM2;
00042 const int ARGPS_DEFAULT_REMOTE_TCP_PORT = 8103;
00043 
00044 AREXPORT ArGPSConnector::ArGPSConnector(ArArgumentParser *argParser) :
00045   myDeviceCon(NULL),
00046   myArgParser(argParser),
00047   myParseArgsCallback(this, &ArGPSConnector::parseArgs),
00048   myLogArgsCallback(this, &ArGPSConnector::logArgs),
00049   myBaud(ARGPS_DEFAULT_SERIAL_BAUD), 
00050   myPort(ARGPS_DEFAULT_SERIAL_PORT),
00051   myTCPHost(NULL),
00052   myTCPPort(8103),
00053   myDeviceType(Standard)
00054 {
00055   myParseArgsCallback.setName("ArGPSConnector");
00056   myLogArgsCallback.setName("ArGPSConnector");
00057   Aria::addParseArgsCB(&myParseArgsCallback);
00058   Aria::addLogOptionsCB(&myLogArgsCallback);
00059 }
00060 
00061 
00062 AREXPORT ArGPSConnector::~ArGPSConnector()
00063 {
00064   if(myDeviceCon) delete myDeviceCon;
00065 }
00066 
00067 
00068 AREXPORT bool ArGPSConnector::parseArgs() 
00069 {
00070   if (!myArgParser) return false;
00071   if (!myArgParser->checkParameterArgumentString("-gpsPort", &myPort)) return false;
00072   if (!myArgParser->checkParameterArgumentInteger("-gpsBaud", &myBaud)) return false;
00073   if (!myArgParser->checkParameterArgumentString("-remoteGpsTcpHost", &myTCPHost)) return false;
00074   if (!myArgParser->checkParameterArgumentInteger("-remoteGpsTcpPort", &myTCPPort)) return false;
00075   char *deviceType = myArgParser->checkParameterArgument("-gpsType");
00076   if (deviceType) // if -gpsType was given
00077   {
00078     if (strcasecmp(deviceType, "novatel") == 0)
00079     {
00080       myDeviceType = Novatel;
00081     }
00082     else if (strcasecmp(deviceType, "standard") == 0)
00083     {
00084       myDeviceType = Standard;
00085     }
00086     else
00087     {
00088       ArLog::log(ArLog::Terse, "GPSConnector: Error: unrecognized GPS type.");
00089       return false;
00090     }
00091   }
00092   return true;
00093 }
00094 
00095 AREXPORT void ArGPSConnector::logArgs()
00096 {
00097   ArLog::log(ArLog::Terse, "GPS options:"); 
00098   ArLog::log(ArLog::Terse, "-gpsPort <gpsSerialPort>\tUse the given serial port (default \"%s\")", ARGPS_DEFAULT_SERIAL_PORT);
00099   ArLog::log(ArLog::Terse, "-gpsBaud <gpsSerialBaudRate>\tUse the given serial Baud rate (default %d)", ARGPS_DEFAULT_SERIAL_BAUD);
00100   ArLog::log(ArLog::Terse, "-gpsType <standard|novatel>\tSelect GPS device type (default \"standard\")");
00101   ArLog::log(ArLog::Terse, "-remoteGpsTcpHost <host>\tUse a TCP connection instead of serial, and connect to remote host <host>");
00102   ArLog::log(ArLog::Terse, "-remoteGpsTcpPort <host>\tUse the given port number for TCP connection, if using TCP. (default %d)", ARGPS_DEFAULT_REMOTE_TCP_PORT);
00103 }
00104 
00105 
00106 
00107 AREXPORT ArGPS* ArGPSConnector::createGPS()
00108 {
00109   // Create gps:
00110   ArGPS* newGPS = NULL;
00111   switch (myDeviceType)
00112   {
00113     case Novatel:
00114       newGPS = new ArNovatelGPS;
00115       break;
00116     default:
00117       newGPS = new ArGPS;
00118       break;
00119   }
00120 
00121   if (myTCPHost == NULL)
00122   {
00123     // Setup serial connection
00124     ArSerialConnection *serialCon = new ArSerialConnection;
00125     if (!serialCon->setBaud(myBaud)) { delete serialCon; return false; }
00126     if (serialCon->open(myPort) != 0) {
00127       ArLog::log(ArLog::Terse, "ArGPSConnector: Error: could not open GPS serial port %s.", myPort);
00128       delete serialCon;
00129       return NULL;
00130     }
00131     newGPS->setDeviceConnection(serialCon);
00132     myDeviceCon = serialCon;
00133   }
00134   else
00135   {
00136     // Setup TCP connection
00137     ArTcpConnection *tcpCon = new ArTcpConnection;
00138     ArLog::log(ArLog::Normal, "ArGPSConnector: Opening TCP connection to %s:%d...", myTCPHost, myTCPPort);
00139     int openState = tcpCon->open(myTCPHost, myTCPPort);
00140     if (openState != 0) {
00141       ArLog::log(ArLog::Terse, "ArGPSConnector: Error: could not open TCP connection to %s port %d: %s", tcpCon->getOpenMessage(openState));
00142       delete tcpCon;
00143       return NULL;
00144     }
00145     newGPS->setDeviceConnection(tcpCon);
00146     myDeviceCon = tcpCon;
00147   }
00148 
00149   return newGPS;
00150 }
00151 
00152 AREXPORT bool ArGPSConnector::connectGPS(ArGPS *gps)
00153 {
00154   // TODO try different BAUD rates if using a serial connection.
00155   return gps->blockingConnect();
00156 }
00157 

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