2010-03-20 16:45:15 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file GPS.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief GPS module, handles GPS and NMEA stream
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
|
|
|
#include "buffer.h"
|
2010-04-15 04:15:46 +02:00
|
|
|
#include "GPS.h"
|
2010-04-17 10:49:19 +02:00
|
|
|
#include "gpsobject.h"
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
// constants/macros/typdefs
|
2010-03-25 20:36:52 +01:00
|
|
|
#define NMEA_BUFFERSIZE 128
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
// Message Codes
|
|
|
|
#define NMEA_NODATA 0 // No data. Packet not available, bad, or not decoded
|
|
|
|
#define NMEA_GPGGA 1 // Global Positioning System Fix Data
|
|
|
|
#define NMEA_GPVTG 2 // Course over ground and ground speed
|
|
|
|
#define NMEA_GPGLL 3 // Geographic position - latitude/longitude
|
|
|
|
#define NMEA_GPGSV 4 // GPS satellites in view
|
|
|
|
#define NMEA_GPGSA 5 // GPS DOP and active satellites
|
|
|
|
#define NMEA_GPRMC 6 // Recommended minimum specific GPS data
|
|
|
|
#define NMEA_UNKNOWN 0xFF// Packet received but not known
|
|
|
|
|
|
|
|
// Debugging
|
|
|
|
|
2010-03-25 20:36:52 +01:00
|
|
|
//#define GPSDEBUG
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
#ifdef GPSDEBUG
|
|
|
|
#define NMEA_DEBUG_PKT ///< define to enable debug of all NMEA messages
|
|
|
|
#define NMEA_DEBUG_GGA ///< define to enable debug of GGA messages
|
|
|
|
#define NMEA_DEBUG_VTG ///< define to enable debug of VTG messages
|
2010-05-24 21:49:37 +02:00
|
|
|
#define NMEA_DEBUG_RMC ///< define to enable debug of RMC messages
|
|
|
|
#define NMEA_DEBUG_GSA ///< define to enable debug of GSA messages
|
2010-03-20 16:45:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void gpsTask(void* parameters);
|
|
|
|
|
2010-03-21 12:32:53 +01:00
|
|
|
// functions
|
2010-03-25 20:36:52 +01:00
|
|
|
char* nmeaGetPacketBuffer(void);
|
2010-03-27 14:13:39 +01:00
|
|
|
char nmeaChecksum(char* gps_buffer);
|
2010-03-21 12:32:53 +01:00
|
|
|
uint8_t nmeaProcess(cBuffer* rxBuffer);
|
2010-03-25 20:36:52 +01:00
|
|
|
void nmeaProcessGPGGA(char* packet);
|
2010-05-24 21:49:37 +02:00
|
|
|
void nmeaProcessGPRMC(char* packet);
|
2010-03-25 20:36:52 +01:00
|
|
|
void nmeaProcessGPVTG(char* packet);
|
2010-04-21 11:56:23 +02:00
|
|
|
void nmeaProcessGPGSA(char* packet);
|
2010-03-21 12:32:53 +01:00
|
|
|
|
2010-03-20 16:45:15 +01:00
|
|
|
// Global variables
|
|
|
|
|
|
|
|
// Private constants
|
2010-04-17 10:49:19 +02:00
|
|
|
#define STACK_SIZE configMINIMAL_STACK_SIZE
|
2010-04-18 04:13:59 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 3)
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static COMPortTypeDef gpsPort;
|
|
|
|
static xTaskHandle gpsTaskHandle;
|
2010-04-18 09:37:55 +02:00
|
|
|
cBuffer gpsRxBuffer;
|
2010-05-24 21:49:37 +02:00
|
|
|
static char gpsRxData[512];
|
2010-04-18 09:37:55 +02:00
|
|
|
char NmeaPacket[NMEA_BUFFERSIZE];
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the gps module
|
|
|
|
* \return -1 if initialisation failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
int32_t GpsInitialize(void)
|
|
|
|
{
|
2010-04-17 10:49:19 +02:00
|
|
|
signed portBASE_TYPE xReturn;
|
2010-03-20 16:45:15 +01:00
|
|
|
// TODO: Get gps settings object
|
|
|
|
gpsPort = COM_USART2;
|
|
|
|
|
|
|
|
// Init input buffer size 512
|
2010-05-24 21:49:37 +02:00
|
|
|
bufferInit(&gpsRxBuffer, (unsigned char *)gpsRxData, 512);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
// Start gps task
|
2010-04-17 10:49:19 +02:00
|
|
|
xReturn = xTaskCreate(gpsTask, (signed char*)"GPS", STACK_SIZE, NULL, TASK_PRIORITY, &gpsTaskHandle);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gps task. Processes input buffer. It does not return.
|
|
|
|
*/
|
|
|
|
static void gpsTask(void* parameters)
|
|
|
|
{
|
|
|
|
int32_t gpsRxOverflow=0;
|
2010-03-25 20:36:52 +01:00
|
|
|
char c;
|
2010-04-17 10:49:19 +02:00
|
|
|
portTickType xDelay = 100 / portTICK_RATE_MS;
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
// Loop forever
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
/* This blocks the task until there is something on the buffer */
|
|
|
|
while(PIOS_COM_ReceiveBufferUsed(gpsPort) > 0)
|
|
|
|
{
|
2010-03-25 20:36:52 +01:00
|
|
|
c=PIOS_COM_ReceiveBuffer(gpsPort);
|
|
|
|
if( !bufferAddToEnd(&gpsRxBuffer, c) )
|
2010-03-20 16:45:15 +01:00
|
|
|
{
|
|
|
|
// no space in buffer
|
|
|
|
// count overflow
|
|
|
|
gpsRxOverflow++;
|
|
|
|
break;
|
|
|
|
}
|
2010-05-24 21:49:37 +02:00
|
|
|
nmeaProcess(&gpsRxBuffer);
|
2010-03-20 16:45:15 +01:00
|
|
|
}
|
2010-03-25 20:36:52 +01:00
|
|
|
vTaskDelay(xDelay);
|
2010-03-20 16:45:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 20:36:52 +01:00
|
|
|
char* nmeaGetPacketBuffer(void)
|
2010-03-20 16:45:15 +01:00
|
|
|
{
|
|
|
|
return NmeaPacket;
|
|
|
|
}
|
|
|
|
|
2010-03-27 14:13:39 +01:00
|
|
|
/**
|
|
|
|
* Prosesses NMEA sentence checksum
|
|
|
|
* \param[in] Buffer for parsed nmea sentence
|
|
|
|
* \return 0 checksum not valid
|
|
|
|
* \return 1 checksum valid
|
|
|
|
*/
|
|
|
|
char nmeaChecksum(char* gps_buffer)
|
|
|
|
{
|
|
|
|
char checksum=0;
|
|
|
|
char checksum_received=0;
|
|
|
|
|
|
|
|
for(int x=0; x<NMEA_BUFFERSIZE; x++)
|
|
|
|
{
|
|
|
|
if(gps_buffer[x]=='*')
|
|
|
|
{
|
|
|
|
//Parsing received checksum...
|
|
|
|
checksum_received = strtol(&gps_buffer[x+1],NULL,16);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//XOR the received data...
|
|
|
|
checksum^=gps_buffer[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART,"$%d=%d\r\n",checksum_received,checksum);
|
|
|
|
if(checksum == checksum_received)
|
2010-05-24 21:49:37 +02:00
|
|
|
{
|
|
|
|
GpsObjectData GpsData;
|
|
|
|
GpsObjectGet(&GpsData);
|
|
|
|
GpsData.Updates++;
|
|
|
|
GpsObjectSet(&GpsData);
|
2010-03-27 14:13:39 +01:00
|
|
|
return 1;
|
2010-05-24 21:49:37 +02:00
|
|
|
}
|
2010-03-27 14:13:39 +01:00
|
|
|
else
|
2010-05-24 21:49:37 +02:00
|
|
|
{
|
2010-03-27 14:13:39 +01:00
|
|
|
return 0;
|
2010-05-24 21:49:37 +02:00
|
|
|
}
|
2010-03-27 14:13:39 +01:00
|
|
|
}
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prosesses NMEA sentences
|
|
|
|
* \param[in] cBuffer for prosessed nmea sentences
|
|
|
|
* \return Message code for found packet
|
|
|
|
* \return 0xFF NO packet found
|
|
|
|
*/
|
|
|
|
uint8_t nmeaProcess(cBuffer* rxBuffer)
|
|
|
|
{
|
|
|
|
uint8_t foundpacket = NMEA_NODATA;
|
|
|
|
uint8_t startFlag = FALSE;
|
|
|
|
//u08 data;
|
|
|
|
uint16_t i,j;
|
|
|
|
|
|
|
|
// process the receive buffer
|
|
|
|
// go through buffer looking for packets
|
|
|
|
while(rxBuffer->datalength)
|
|
|
|
{
|
|
|
|
// look for a start of NMEA packet
|
|
|
|
if(bufferGetAtIndex(rxBuffer,0) == '$')
|
|
|
|
{
|
|
|
|
// found start
|
|
|
|
startFlag = TRUE;
|
|
|
|
// when start is found, we leave it intact in the receive buffer
|
|
|
|
// in case the full NMEA string is not completely received. The
|
|
|
|
// start will be detected in the next nmeaProcess iteration.
|
|
|
|
|
|
|
|
// done looking for start
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bufferGetFromFront(rxBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we detected a start, look for end of packet
|
|
|
|
if(startFlag)
|
|
|
|
{
|
|
|
|
for(i=1; i<(rxBuffer->datalength)-1; i++)
|
|
|
|
{
|
|
|
|
// check for end of NMEA packet <CR><LF>
|
|
|
|
if((bufferGetAtIndex(rxBuffer,i) == '\r') && (bufferGetAtIndex(rxBuffer,i+1) == '\n'))
|
|
|
|
{
|
|
|
|
// have a packet end
|
|
|
|
// dump initial '$'
|
|
|
|
bufferGetFromFront(rxBuffer);
|
|
|
|
// copy packet to NmeaPacket
|
|
|
|
for(j=0; j<(i-1); j++)
|
|
|
|
{
|
|
|
|
// although NMEA strings should be 80 characters or less,
|
|
|
|
// receive buffer errors can generate erroneous packets.
|
|
|
|
// Protect against packet buffer overflow
|
|
|
|
if(j<(NMEA_BUFFERSIZE-1))
|
|
|
|
NmeaPacket[j] = bufferGetFromFront(rxBuffer);
|
|
|
|
else
|
|
|
|
bufferGetFromFront(rxBuffer);
|
|
|
|
}
|
|
|
|
// null terminate it
|
|
|
|
NmeaPacket[j] = 0;
|
|
|
|
// dump <CR><LF> from rxBuffer
|
|
|
|
bufferGetFromFront(rxBuffer);
|
|
|
|
bufferGetFromFront(rxBuffer);
|
|
|
|
//DEBUG
|
|
|
|
#ifdef NMEA_DEBUG_PKT
|
2010-03-25 20:36:52 +01:00
|
|
|
//PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART,"$%s\r\n",NmeaPacket);
|
2010-03-20 16:45:15 +01:00
|
|
|
#endif
|
|
|
|
// found a packet
|
|
|
|
// done with this processing session
|
|
|
|
foundpacket = NMEA_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(foundpacket)
|
|
|
|
{
|
|
|
|
// check message type and process appropriately
|
2010-03-25 20:36:52 +01:00
|
|
|
if(!strncmp(NmeaPacket, "GPGGA", 5))
|
2010-03-20 16:45:15 +01:00
|
|
|
{
|
|
|
|
// process packet of this type
|
|
|
|
nmeaProcessGPGGA(NmeaPacket);
|
|
|
|
// report packet type
|
|
|
|
foundpacket = NMEA_GPGGA;
|
|
|
|
}
|
2010-03-25 20:36:52 +01:00
|
|
|
else if(!strncmp(NmeaPacket, "GPVTG", 5))
|
2010-03-20 16:45:15 +01:00
|
|
|
{
|
|
|
|
// process packet of this type
|
|
|
|
nmeaProcessGPVTG(NmeaPacket);
|
|
|
|
// report packet type
|
|
|
|
foundpacket = NMEA_GPVTG;
|
|
|
|
}
|
2010-04-21 11:56:23 +02:00
|
|
|
else if(!strncmp(NmeaPacket, "GPGSA", 5))
|
|
|
|
{
|
|
|
|
// process packet of this type
|
|
|
|
nmeaProcessGPGSA(NmeaPacket);
|
|
|
|
// report packet type
|
|
|
|
foundpacket = NMEA_GPGSA;
|
|
|
|
}
|
2010-05-18 14:49:47 +02:00
|
|
|
else if(!strncmp(NmeaPacket, "GPRMC", 5))
|
|
|
|
{
|
|
|
|
// process packet of this type
|
2010-05-24 21:49:37 +02:00
|
|
|
nmeaProcessGPRMC(NmeaPacket);
|
2010-05-18 14:49:47 +02:00
|
|
|
// report packet type
|
|
|
|
foundpacket = NMEA_GPRMC;
|
|
|
|
}
|
2010-03-20 16:45:15 +01:00
|
|
|
}
|
|
|
|
else if(rxBuffer->datalength >= rxBuffer->size)
|
|
|
|
{
|
|
|
|
// if we found no packet, and the buffer is full
|
|
|
|
// we're logjammed, flush entire buffer
|
|
|
|
bufferFlush(rxBuffer);
|
|
|
|
}
|
|
|
|
return foundpacket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prosesses NMEA GPGGA sentences
|
|
|
|
* \param[in] Buffer for parsed nmea GPGGA sentence
|
|
|
|
*/
|
2010-03-25 20:36:52 +01:00
|
|
|
void nmeaProcessGPGGA(char* packet)
|
2010-03-20 16:45:15 +01:00
|
|
|
{
|
2010-04-17 10:49:19 +02:00
|
|
|
GpsObjectData GpsData;
|
|
|
|
|
2010-04-18 09:37:55 +02:00
|
|
|
char *tokens;
|
2010-03-25 20:36:52 +01:00
|
|
|
char *delimiter = ",";
|
|
|
|
char *pEnd;
|
2010-05-18 14:49:47 +02:00
|
|
|
char token_length=0;
|
2010-03-21 12:32:53 +01:00
|
|
|
|
2010-04-07 10:02:44 +02:00
|
|
|
long deg,min,desim;
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
#ifdef NMEA_DEBUG_GGA
|
2010-05-24 21:49:37 +02:00
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART,"$%s\r\n",packet);
|
2010-03-20 16:45:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// start parsing just after "GPGGA,"
|
|
|
|
// attempt to reject empty packets right away
|
2010-03-21 12:32:53 +01:00
|
|
|
if(packet[6]==',' && packet[7]==',')
|
2010-03-20 16:45:15 +01:00
|
|
|
return;
|
|
|
|
|
2010-03-27 14:13:39 +01:00
|
|
|
if(!nmeaChecksum(packet))
|
|
|
|
{
|
|
|
|
// checksum not valid
|
|
|
|
return;
|
|
|
|
}
|
2010-04-18 09:37:55 +02:00
|
|
|
|
2010-05-24 21:49:37 +02:00
|
|
|
GpsObjectGet(&GpsData);
|
2010-03-21 12:32:53 +01:00
|
|
|
// tokenizer for nmea sentence
|
2010-03-25 20:36:52 +01:00
|
|
|
//GPGGA header
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-21 12:32:53 +01:00
|
|
|
|
2010-03-20 16:45:15 +01:00
|
|
|
// get UTC time [hhmmss.sss]
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-04-18 09:37:55 +02:00
|
|
|
//strcpy(GpsInfo.TimeOfFix,tokens);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
2010-03-25 20:36:52 +01:00
|
|
|
// next field: latitude
|
2010-03-21 12:32:53 +01:00
|
|
|
// get latitude [ddmm.mmmmm]
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-05-18 14:49:47 +02:00
|
|
|
token_length=strlen(tokens);
|
|
|
|
deg=strtol (tokens,&pEnd,10);
|
|
|
|
min=deg%100;
|
|
|
|
deg=deg/100;
|
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
|
|
|
if(token_length==10)// 5 desimal output
|
2010-03-25 20:36:52 +01:00
|
|
|
{
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsData.Latitude=deg+(min+desim/100000.0)/60.0; // to desimal degrees
|
2010-03-25 20:36:52 +01:00
|
|
|
}
|
2010-05-18 14:49:47 +02:00
|
|
|
else if(token_length==9) // 4 desimal output
|
2010-03-25 20:36:52 +01:00
|
|
|
{
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsData.Latitude=deg+(min+desim/10000.0)/60.0; // to desimal degrees
|
2010-03-25 20:36:52 +01:00
|
|
|
}
|
2010-05-18 14:49:47 +02:00
|
|
|
else if(token_length==11) // 6 desimal output OPGPS
|
|
|
|
{
|
|
|
|
GpsData.Latitude=deg+(min+desim/1000000.0)/60.0; // to desimal degrees
|
|
|
|
}
|
2010-03-21 12:32:53 +01:00
|
|
|
|
|
|
|
// next field: N/S indicator
|
2010-03-20 16:45:15 +01:00
|
|
|
// correct latitute for N/S
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-04-18 09:37:55 +02:00
|
|
|
if(tokens[0] == 'S') GpsData.Latitude = -GpsData.Latitude;
|
2010-03-20 16:45:15 +01:00
|
|
|
|
2010-03-25 20:36:52 +01:00
|
|
|
// next field: longitude
|
2010-04-07 10:02:44 +02:00
|
|
|
// get longitude [dddmm.mmmmm]
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-05-18 14:49:47 +02:00
|
|
|
token_length=strlen(tokens);
|
|
|
|
deg=strtol (tokens,&pEnd,10);
|
|
|
|
min=deg%100;
|
|
|
|
deg=deg/100;
|
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
|
|
|
if(token_length==11)// 5 desimal output
|
2010-03-25 20:36:52 +01:00
|
|
|
{
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsData.Longitude=deg+(min+desim/100000.0)/60.0; // to desimal degrees
|
2010-03-25 20:36:52 +01:00
|
|
|
}
|
2010-05-18 14:49:47 +02:00
|
|
|
else if(token_length==10) // 4 desimal output
|
2010-03-25 20:36:52 +01:00
|
|
|
{
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsData.Longitude=deg+(min+desim/10000.0)/60.0; // to desimal degrees
|
2010-03-25 20:36:52 +01:00
|
|
|
}
|
2010-05-18 14:49:47 +02:00
|
|
|
else if(token_length==12) // 6 desimal output OPGPS
|
|
|
|
{
|
|
|
|
GpsData.Longitude=deg+(min+desim/1000000.0)/60.0; // to desimal degrees
|
|
|
|
}
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: E/W indicator
|
2010-03-20 16:45:15 +01:00
|
|
|
// correct latitute for E/W
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-04-18 09:37:55 +02:00
|
|
|
if(tokens[0] == 'W') GpsData.Longitude = -GpsData.Longitude;
|
2010-03-20 16:45:15 +01:00
|
|
|
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: position fix status
|
2010-03-20 16:45:15 +01:00
|
|
|
// position fix status
|
|
|
|
// 0 = Invalid, 1 = Valid SPS, 2 = Valid DGPS, 3 = Valid PPS
|
|
|
|
// check for good position fix
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-05-24 21:49:37 +02:00
|
|
|
//if((tokens[0] != '0') || (tokens[0] != 0))
|
|
|
|
// GpsData.Updates++;
|
2010-03-21 12:32:53 +01:00
|
|
|
|
|
|
|
// next field: satellites used
|
|
|
|
// get number of satellites used in GPS solution
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsData.Satellites=atoi(tokens);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: HDOP (horizontal dilution of precision)
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-21 12:32:53 +01:00
|
|
|
|
|
|
|
// next field: altitude
|
2010-03-25 20:36:52 +01:00
|
|
|
// get altitude (in meters mm.m)
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-25 20:36:52 +01:00
|
|
|
//reuse variables for alt
|
2010-04-07 10:02:44 +02:00
|
|
|
deg=strtol (tokens,&pEnd,10); // always 0.1m resolution?
|
2010-03-25 20:36:52 +01:00
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
2010-05-18 14:49:47 +02:00
|
|
|
if(1) // OPGPS 3 desimal
|
|
|
|
GpsData.Altitude=deg+desim/1000.0;
|
|
|
|
else
|
|
|
|
GpsData.Altitude=deg+desim/10.0;
|
2010-03-21 12:32:53 +01:00
|
|
|
|
|
|
|
// next field: altitude units, always 'M'
|
|
|
|
// next field: geoid seperation
|
|
|
|
// next field: seperation units
|
|
|
|
// next field: DGPS age
|
|
|
|
// next field: DGPS station ID
|
|
|
|
// next field: checksum
|
2010-04-17 10:49:19 +02:00
|
|
|
GpsObjectSet(&GpsData);
|
2010-03-20 16:45:15 +01:00
|
|
|
}
|
|
|
|
|
2010-05-24 21:49:37 +02:00
|
|
|
/**
|
|
|
|
* Prosesses NMEA GPRMC sentences
|
|
|
|
* \param[in] Buffer for parsed nmea GPRMC sentence
|
|
|
|
*/
|
|
|
|
void nmeaProcessGPRMC(char* packet)
|
|
|
|
{
|
|
|
|
GpsObjectData GpsData;
|
|
|
|
|
|
|
|
char *tokens;
|
|
|
|
char *delimiter = ",";
|
|
|
|
char *pEnd;
|
|
|
|
char token_length=0;
|
|
|
|
|
|
|
|
long deg,min,desim;
|
|
|
|
|
|
|
|
#ifdef NMEA_DEBUG_RMC
|
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART,"$%s\r\n",packet);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// start parsing just after "GPRMC,"
|
|
|
|
// attempt to reject empty packets right away
|
|
|
|
if(packet[6]==',' && packet[7]==',')
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!nmeaChecksum(packet))
|
|
|
|
{
|
|
|
|
// checksum not valid
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GpsObjectGet(&GpsData);
|
|
|
|
// tokenizer for nmea sentence
|
|
|
|
//GPRMC header
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// get UTC time [hhmmss.sss]
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
//strcpy(GpsInfo.TimeOfFix,tokens);
|
|
|
|
// next field: Navigation receiver warning A = OK, V = warning
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// next field: latitude
|
|
|
|
// get latitude [ddmm.mmmmm]
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
token_length=strlen(tokens);
|
|
|
|
deg=strtol (tokens,&pEnd,10);
|
|
|
|
min=deg%100;
|
|
|
|
deg=deg/100;
|
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
|
|
|
/*if(token_length==10)// 5 desimal output
|
|
|
|
{
|
|
|
|
GpsData.Latitude=deg+(min+desim/100000.0)/60.0; // to desimal degrees
|
|
|
|
}
|
|
|
|
else if(token_length==9) // 4 desimal output
|
|
|
|
{
|
|
|
|
GpsData.Latitude=deg+(min+desim/10000.0)/60.0; // to desimal degrees
|
|
|
|
}
|
|
|
|
else if(token_length==11) // 6 desimal output OPGPS
|
|
|
|
{
|
|
|
|
GpsData.Latitude=deg+(min+desim/1000000.0)/60.0; // to desimal degrees
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// next field: N/S indicator
|
|
|
|
// correct latitute for N/S
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
//if(tokens[0] == 'S') GpsData.Latitude = -GpsData.Latitude;
|
|
|
|
|
|
|
|
// next field: longitude
|
|
|
|
// get longitude [dddmm.mmmmm]
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
token_length=strlen(tokens);
|
|
|
|
deg=strtol (tokens,&pEnd,10);
|
|
|
|
min=deg%100;
|
|
|
|
deg=deg/100;
|
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
|
|
|
/*if(token_length==11)// 5 desimal output
|
|
|
|
{
|
|
|
|
GpsData.Longitude=deg+(min+desim/100000.0)/60.0; // to desimal degrees
|
|
|
|
}
|
|
|
|
else if(token_length==10) // 4 desimal output
|
|
|
|
{
|
|
|
|
GpsData.Longitude=deg+(min+desim/10000.0)/60.0; // to desimal degrees
|
|
|
|
}
|
|
|
|
else if(token_length==12) // 6 desimal output OPGPS
|
|
|
|
{
|
|
|
|
GpsData.Longitude=deg+(min+desim/1000000.0)/60.0; // to desimal degrees
|
|
|
|
}*/
|
|
|
|
// next field: E/W indicator
|
|
|
|
// correct latitute for E/W
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
//if(tokens[0] == 'W') GpsData.Longitude = -GpsData.Longitude;
|
|
|
|
|
|
|
|
// next field: speed (knots)
|
|
|
|
// get speed in knots
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// next field: True course
|
|
|
|
// get True course
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// next field: Date of fix
|
|
|
|
// get Date of fix
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// next field: Magnetic variation
|
|
|
|
// next field: E or W
|
|
|
|
// next field: checksum
|
|
|
|
GpsObjectSet(&GpsData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-20 16:45:15 +01:00
|
|
|
/**
|
|
|
|
* Prosesses NMEA GPVTG sentences
|
|
|
|
* \param[in] Buffer for parsed nmea GPVTG sentence
|
|
|
|
*/
|
2010-03-25 20:36:52 +01:00
|
|
|
void nmeaProcessGPVTG(char* packet)
|
2010-03-20 16:45:15 +01:00
|
|
|
{
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsObjectData GpsData;
|
|
|
|
|
|
|
|
char *tokens;
|
2010-03-25 20:36:52 +01:00
|
|
|
char *delimiter = ",";
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
#ifdef NMEA_DEBUG_VTG
|
2010-05-24 21:49:37 +02:00
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART,"$%s\r\n",packet);
|
2010-03-20 16:45:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// start parsing just after "GPVTG,"
|
|
|
|
// attempt to reject empty packets right away
|
2010-03-21 12:32:53 +01:00
|
|
|
if(packet[6]==',' && packet[7]==',')
|
2010-03-20 16:45:15 +01:00
|
|
|
return;
|
|
|
|
|
2010-03-27 14:13:39 +01:00
|
|
|
if(!nmeaChecksum(packet))
|
|
|
|
{
|
|
|
|
// checksum not valid
|
|
|
|
return;
|
|
|
|
}
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsObjectGet(&GpsData);
|
2010-03-21 12:32:53 +01:00
|
|
|
// tokenizer for nmea sentence
|
2010-03-25 20:36:52 +01:00
|
|
|
|
|
|
|
//GPVTG header
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-21 12:32:53 +01:00
|
|
|
|
2010-03-20 16:45:15 +01:00
|
|
|
// get course (true north ref) in degrees [ddd.dd]
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: 'T'
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: course (magnetic north)
|
|
|
|
// get course (magnetic north ref) in degrees [ddd.dd]
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: 'M'
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: speed (knots)
|
2010-03-20 16:45:15 +01:00
|
|
|
// get speed in knots
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: 'N'
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: speed (km/h)
|
2010-03-20 16:45:15 +01:00
|
|
|
// get speed in km/h
|
2010-04-21 11:56:23 +02:00
|
|
|
tokens = strsep(&packet, delimiter);
|
2010-03-21 12:32:53 +01:00
|
|
|
// next field: 'K'
|
|
|
|
// next field: checksum
|
2010-04-18 09:37:55 +02:00
|
|
|
GpsObjectSet(&GpsData);
|
2010-03-20 16:45:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-04-21 11:56:23 +02:00
|
|
|
/**
|
|
|
|
* Prosesses NMEA GPGSA sentences
|
|
|
|
* \param[in] Buffer for parsed nmea GPGSA sentence
|
|
|
|
*/
|
|
|
|
void nmeaProcessGPGSA(char* packet)
|
|
|
|
{
|
|
|
|
GpsObjectData GpsData;
|
|
|
|
|
|
|
|
char *tokens;
|
|
|
|
char *delimiter = ",";
|
|
|
|
char *pEnd;
|
|
|
|
long value,desim;
|
|
|
|
|
|
|
|
#ifdef NMEA_DEBUG_GSA
|
2010-05-24 21:49:37 +02:00
|
|
|
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART,"$%s\r\n",packet);
|
2010-04-21 11:56:23 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// start parsing just after "GPGSA,"
|
|
|
|
// attempt to reject empty packets right away
|
|
|
|
if(packet[6]==',' && packet[7]==',')
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!nmeaChecksum(packet))
|
|
|
|
{
|
|
|
|
// checksum not valid
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GpsObjectGet(&GpsData);
|
|
|
|
// tokenizer for nmea sentence
|
|
|
|
|
|
|
|
//GPGSA header
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// next field: Mode
|
|
|
|
// Mode: M=Manual, forced to operate in 2D or 3D, A=Automatic, 3D/2D
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
// next field: Mode
|
|
|
|
// Mode: 1=Fix not available, 2=2D, 3=3D
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// next field: 3-14 IDs of SVs used in position fix (null for unused fields)
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
|
|
|
|
// next field: PDOP
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
value=strtol (tokens,&pEnd,10);
|
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
|
|
|
//GpsData.PDOP=value+desim/100.0;
|
|
|
|
// next field: HDOP
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
value=strtol (tokens,&pEnd,10);
|
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
|
|
|
//GpsData.HDOP=value+desim/100.0;
|
|
|
|
// next field: VDOP
|
|
|
|
tokens = strsep(&packet, delimiter);
|
|
|
|
value=strtol (tokens,&pEnd,10);
|
|
|
|
desim=strtol (pEnd+1,NULL,10);
|
|
|
|
//GpsData.VDOP=value+desim/100.0;
|
|
|
|
// next field: checksum
|
|
|
|
GpsObjectSet(&GpsData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|