00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "AshtechG12_GPS_PhysicalDevice.h"
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 AshtechG12_GPS_PhysicalDevice::AshtechG12_GPS_PhysicalDevice( )
00028 : m_clientSocket( NULL )
00029 {
00030 }
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 AshtechG12_GPS_PhysicalDevice::AshtechG12_GPS_PhysicalDevice( const char *serverName )
00042 : m_clientSocket( NULL )
00043 {
00044 Connect( serverName );
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 AshtechG12_GPS_PhysicalDevice::AshtechG12_GPS_PhysicalDevice( const char *serverName, int port )
00059 : m_clientSocket( NULL )
00060 {
00061 Connect( serverName, port );
00062 }
00063
00064
00065
00066 AshtechG12_GPS_PhysicalDevice::~AshtechG12_GPS_PhysicalDevice( )
00067 {
00068 delete m_clientSocket;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 int AshtechG12_GPS_PhysicalDevice::Connect( const char *serverName )
00080 {
00081 return this->Connect( serverName, DEFAULT_PORT );
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 int AshtechG12_GPS_PhysicalDevice::Connect( const char *serverName, int portNumber )
00093 {
00094 int returnValue = -1;
00095
00096 try
00097 {
00098
00099 if ( m_clientSocket != NULL)
00100 {
00101 delete m_clientSocket;
00102 }
00103
00104 m_clientSocket = new ClientSocket;
00105
00106 m_clientSocket -> Connect( serverName, portNumber );
00107 }
00108 catch ( SocketException& error )
00109 {
00110 std::cout << "Exception was caught: " << error.description( ) << "\n";
00111 return( returnValue );
00112 }
00113 returnValue = 0;
00114 return( returnValue );
00115 }
00116
00117
00118 int AshtechG12_GPS_PhysicalDevice::Send_i( const char *data )
00119 {
00120 return this->Send_i(data, strlen(data));
00121 }
00122
00123
00124 int AshtechG12_GPS_PhysicalDevice::Send_i( const char *data, unsigned int len )
00125 {
00126 int returnValue = -1;
00127
00128
00129
00130
00131 string strData;
00132
00133 strData.assign(data, data + len );
00134
00135 try
00136 {
00137 *m_clientSocket << strData;
00138 }
00139 catch ( SocketException& error )
00140 {
00141 std::cout << "Exception was caught: " << error.description( ) << "\n";
00142 return( returnValue );
00143 }
00144
00145 returnValue = strData.length( );
00146 return ( returnValue );
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 int AshtechG12_GPS_PhysicalDevice::Recv_i( char buf[], unsigned int len )
00168 {
00169
00170 int returnValue = -1;
00171
00172 std::string buffer;
00173
00174 try
00175 {
00176 *m_clientSocket >> buffer;
00177 }
00178 catch ( SocketException& error )
00179 {
00180 std::cout << "Exception was caught: " << error.description( ) << "\n";
00181 return( returnValue );
00182 }
00183
00184 returnValue = buffer.length( );
00185 char* tmp = new char [ len-1 ];
00186 strcpy( tmp, buffer.c_str() );
00187
00188 for( int i=0; i<returnValue+1; i++ )
00189 {
00190 buf[i] = tmp[i];
00191
00192 }
00193
00194 delete [ ] tmp;
00195 buf[ returnValue+1 ] ='\0';
00196
00197 return ( returnValue );
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 int AshtechG12_GPS_PhysicalDevice::GetCurrentPosition( Position &pos )
00227 {
00228 int BUFSIZE=200;
00229 int sendStatus;
00230 int recvStatus;
00231 int posStatus;
00232 int status=0;
00233 char *RESPONSE = (char*)malloc(sizeof(char)*BUFSIZE);
00234
00235 const char *COMMAND = "$PASHQ,POS\r\n\0";
00236
00237 if ( (sendStatus = Send_i( COMMAND ) ) <= 0)
00238 {
00239 status = -1;
00240 }
00241
00242
00243
00244
00245
00246 if ( ( recvStatus = Recv_i( RESPONSE, BUFSIZE ) ) <= 0 )
00247 {
00248 status = -2;
00249 }
00250
00251
00252
00253
00254
00255 if ( ( posStatus = pos.SetFromPOS_Message( RESPONSE ) ) != 0)
00256 {
00257 status = -3;
00258 }
00259
00260
00261
00262 free(RESPONSE);
00263
00264 return (status);
00265
00266 }
00267
00268
00269
00270
00271
00272
00273 int AshtechG12_GPS_PhysicalDevice::ValidateChecksum(const char *message)
00274 {
00275 if (message == NULL)
00276 {
00277 cout << " ERROR Checksum" << endl;
00278
00279
00280 }
00281
00282 int msgBodyLen=strlen(message)-5;
00283 if (msgBodyLen < 0)
00284 {
00285 cout << " ERROR Checksum" << endl;
00286
00287
00288 }
00289
00290 int checksum;
00291 if (sscanf(message+msgBodyLen, "*%2x\r\n", &checksum) != 1)
00292 {
00293 return -2;
00294 }
00295
00296
00297 unsigned char computedChecksum=0;
00298 for (int i=1; i < msgBodyLen; ++i)
00299 {
00300 computedChecksum ^= (unsigned char)message[i];
00301 }
00302
00303 if (checksum != computedChecksum)
00304 {
00305 return -1;
00306 }
00307
00308 return 0;
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 int AshtechG12_GPS_PhysicalDevice::SendDynamicLimitsSetCmd(int vel, int accel)
00359 {
00360 char buf[23];
00361 sprintf(buf, "$PASHS,DYN,%d,%d\r\n", vel, accel);
00362 return (1); // RecvAckNak();
00363 }
00364 */
00365
00366
00367
00368 AshtechG12_GPS_PhysicalDevice::Position::Position()
00369 : m_RTCM('\0'), m_numSatellites(0), m_UTC_Timetag(0.0F), m_latitude(0.0F),
00370 m_latitudeSector('N'), m_longitude(0.0F), m_longitudeSector('E'),
00371 m_altitude(0.0F), m_groundTrack(0.0F), m_groundSpeed(0.0F),
00372 m_verticalVelocity(0.0F), m_PDOP(0.0F), m_HDOP(0.0F), m_VDOP(0.0F),
00373 m_TDOP(0.0F)
00374 {
00375 }
00376
00377
00378 AshtechG12_GPS_PhysicalDevice::Position::Position(const Position &other)
00379 : m_RTCM(other.m_RTCM), m_numSatellites(other.m_numSatellites),
00380 m_UTC_Timetag(other.m_UTC_Timetag), m_latitude(other.m_latitude),
00381 m_latitudeSector(other.m_latitudeSector),
00382 m_longitude(other.m_longitude),
00383 m_longitudeSector(other.m_longitudeSector),
00384 m_altitude(other.m_altitude), m_groundTrack(other.m_groundTrack),
00385 m_groundSpeed(other.m_groundSpeed),
00386 m_verticalVelocity(other.m_verticalVelocity), m_PDOP(other.m_PDOP),
00387 m_HDOP(other.m_HDOP), m_VDOP(other.m_VDOP), m_TDOP(other.m_TDOP)
00388 {
00389 }
00390
00391
00392 AshtechG12_GPS_PhysicalDevice::Position::Position(const char *message)
00393 : m_RTCM('\0'), m_numSatellites(0), m_UTC_Timetag(0.0F), m_latitude(0.0F),
00394 m_latitudeSector('N'), m_longitude(0.0F), m_longitudeSector('E'),
00395 m_altitude(0.0F), m_groundTrack(0.0F), m_groundSpeed(0.0F),
00396 m_verticalVelocity(0.0F), m_PDOP(0.0F), m_HDOP(0.0F), m_VDOP(0.0F),
00397 m_TDOP(0.0F)
00398 {
00399 SetFromPOS_Message(message);
00400 }
00401
00402
00403 int AshtechG12_GPS_PhysicalDevice::Position::SetFromPOS_Message(const char *message)
00404 {
00405 static const char MSG_HEADER[]="$PASHR,POS,";
00406 static const int NUM_FIELDS=17;
00407 static const int MAX_FIELD_LEN=12;
00408 static const unsigned int MIN_MSG_LEN=sizeof(MSG_HEADER)+NUM_FIELDS+4;
00409
00410 if (AshtechG12_GPS_PhysicalDevice::ValidateChecksum(message) != 0)
00411 {
00412 cerr << "ERROR Checksum" << endl;
00413 }
00414
00415 if ( strlen( message ) < MIN_MSG_LEN )
00416 {
00417 cerr << "ERROR String Length" << endl;
00418 }
00419
00420 if (!memcmp(MSG_HEADER, message, sizeof(MSG_HEADER)))
00421 {
00422 cerr << "ERROR Message Header" << endl;
00423 }
00424
00425 char tokens[NUM_FIELDS][MAX_FIELD_LEN];
00426 const char *tokPtr=message+strlen(MSG_HEADER);
00427
00428 for (int i = 0; i < NUM_FIELDS; ++i)
00429 {
00430 int tokLen=strcspn(tokPtr, ",*");
00431 memcpy(tokens[i], tokPtr, tokLen);
00432 tokens[i][tokLen] = '\0';
00433 tokPtr+=tokLen+1;
00434
00435 if (tokPtr == NULL)
00436 {
00437 cerr << " ERROR Nothing in tokPtr" << endl;
00438 }
00439 }
00440
00441 if (*(tokPtr-1) != '*')
00442 {
00443 cerr << "ERROR: Malformed GPS Position message " << message << endl;
00444 }
00445
00446 int tmpInt;
00447 float ddmmPointmmmmm;
00448
00449 if (sscanf(tokens[0], "%d", &tmpInt) == 1)
00450 {
00451
00452 m_RTCM = tmpInt;
00453 }
00454 if (sscanf(tokens[1], "%d", &tmpInt) == 1)
00455 {
00456 m_numSatellites = tmpInt;
00457 }
00458 sscanf(tokens[2], "%f", &m_UTC_Timetag);
00459 if (sscanf(tokens[3], "%f", &ddmmPointmmmmm) == 1)
00460 {
00461 float minutes=fmod(ddmmPointmmmmm, 100);
00462 m_latitude=(ddmmPointmmmmm-minutes)/100 + minutes/60;
00463 }
00464 sscanf(tokens[4], "%c", &m_latitudeSector);
00465 if (sscanf(tokens[5], "%f", &ddmmPointmmmmm) == 1)
00466 {
00467 float minutes=fmod(ddmmPointmmmmm, 100);
00468 m_longitude=(ddmmPointmmmmm-minutes)/100 + minutes/60;
00469 }
00470 sscanf(tokens[6], "%c", &m_longitudeSector);
00471 sscanf(tokens[7], "%f", &m_altitude);
00472
00473 sscanf(tokens[9], "%f", &m_groundTrack);
00474 sscanf(tokens[10], "%f", &m_groundSpeed);
00475 sscanf(tokens[11], "%f", &m_verticalVelocity);
00476 sscanf(tokens[12], "%f", &m_PDOP);
00477 sscanf(tokens[13], "%f", &m_HDOP);
00478 sscanf(tokens[14], "%f", &m_VDOP);
00479 sscanf(tokens[15], "%f", &m_TDOP);
00480
00481
00482 return 0;
00483 }
00484
00485
00486 void AshtechG12_GPS_PhysicalDevice::Position::toString(std::string &str) const
00487 {
00488 std::ostringstream ss;
00489
00490 ss << "{ "
00491 "RTCM: " << static_cast<int>(m_RTCM) << ", "
00492 "Satellites: " << static_cast<int>(m_numSatellites) << ", "
00493 "Timetag: " << m_UTC_Timetag << ", "
00494 "Latitude: " << m_latitude << " " << m_latitudeSector << ", "
00495 "Longitude: " << m_longitude << " " << m_longitudeSector << ", "
00496 "Altitude: " << m_altitude << ", "
00497 "Ground Track: " << m_groundTrack << ", "
00498 "Ground Speed: " << m_groundSpeed << ", "
00499 "Vert. Velocity: " << m_verticalVelocity << ", "
00500 "PDOP: " << m_PDOP << ", "
00501 "HDOP: " << m_HDOP << ", "
00502 "VDOP: " << m_VDOP << ", "
00503 "TDOP: " << m_TDOP
00504 << " }";
00505
00506 str=ss.str();
00507 }
00508
00509
00510 std::string AshtechG12_GPS_PhysicalDevice::Position::toString() const
00511 {
00512 std::string retVal;
00513 toString(retVal);
00514
00515 return retVal;
00516 }
00517
00518
00519 AshtechG12_GPS_PhysicalDevice::Position &
00520 AshtechG12_GPS_PhysicalDevice::Position::operator =(
00521 const AshtechG12_GPS_PhysicalDevice::Position &other
00522 )
00523 {
00524 if (this != &other)
00525 {
00526 memcpy(this, &other, sizeof(Position));
00527 }
00528
00529 return *this;
00530 }
00531
00532
00533 bool AshtechG12_GPS_PhysicalDevice::Position::operator ==(
00534 const AshtechG12_GPS_PhysicalDevice::Position &other
00535 ) const
00536 {
00537 if (this == &other)
00538 {
00539 return true;
00540 }
00541
00542 return !memcmp(this, &other, sizeof(Position));
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567