GPS NEMA String parser library. Only supports SkyTraq Venus chip at this time.
Dependents: MTDOT-EVB-LinkCheck-AL MTDOT-BOX-EVB-Factory-Firmware-LIB-108 TelitSensorToCloud mDot_sensor_to_cloud ... more
Revision 4:ae35903cd8d2, committed 2015-10-27
- Comitter:
- mfiore
- Date:
- Tue Oct 27 19:54:49 2015 +0000
- Parent:
- 3:662aa99c5266
- Child:
- 5:11f41dee386b
- Commit message:
- <CTRL><SHIFT><f> to fix formatting
Changed in this revision
| GPSPARSER.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/GPSPARSER.cpp Thu Oct 22 19:39:17 2015 +0000
+++ b/GPSPARSER.cpp Tue Oct 27 19:54:49 2015 +0000
@@ -36,7 +36,7 @@
{
_gps_uart = uart;
_gps_uart->baud(9600); //set GPS baud rate here
-
+
_gps_latitude.degrees = 0;
_gps_longitude.degrees = 0;
_timestamp.tm_sec = 0;
@@ -48,35 +48,36 @@
_timestamp.tm_wday = 0;
_timestamp.tm_yday = 0;
_timestamp.tm_isdst = -1; // time is UTC so no Daylight savings time
-
+
_gps_status = false;
_fix_status = 1;
_num_satellites =0;
_gps_detected = false;
- if (GPSPARSER::initGps() == 0){
+ if (GPSPARSER::initGps() == 0) {
_gps_detected = true;
_getSentenceThread.signal_set(START_THREAD);
printf("Started Nema Sentence Thread\r\n");
- }
- else{
+ } else {
printf("No GPS detected\r\n");
_fix_status = 0;
}
-
+
return;
}
-GPSPARSER::~GPSPARSER(void){
+GPSPARSER::~GPSPARSER(void)
+{
if (_gps_detected)
_getSentenceThread.terminate();
}
-uint8_t GPSPARSER::initGps (void){
-
+uint8_t GPSPARSER::initGps (void)
+{
+
// this code is specific to the Skytraq Venus GPS chip. This code could be re-written to detect another type of
// GPS device. Maybe read serial port for a specific time and detect $GP from NEMA string
-
+
// Sets Skytraq Venus GPS to output GGA,GSA,GSV every 10 seconds, and RMC every second, no ZDA,GLL,VTG
// setup string for GPS GGA GSA GSV GLL RMC VTG ZDA cksum
char init_gps[16] = {0xA0,0xA1,0x00,0x09,0x08,0x0A,0x0A,0x0A,0x00,0x01,0x00,0x00,0x00,0x03,0x0D,0x0A};
@@ -85,52 +86,48 @@
_gps_uart->rxClear();
printf("Starting initGPS \r\n");
-
+
_gps_uart->write(init_gps,16);
printf("wrote control data\r\n");
-
+
do {
osDelay(10);
} while (!_gps_uart->txEmpty());
-
+
osDelay(15);
-
- if (_gps_uart->readable()){
- do{
+
+ if (_gps_uart->readable()) {
+ do {
_gps_uart->read(chk_char,20);
} while ((chk_char != 0xA0) && (!_gps_uart->rxEmpty()));
-
+
printf ("found 0xA0 or rx empty\r\n");
-
- if (chk_char == 0xA0){
+
+ if (chk_char == 0xA0) {
_gps_uart->read(chk_char,15);
if (chk_char != 0xA1) {
printf( "initGPS failed no message\r\n");
ret = 1;
- }
- else {
+ } else {
printf("message received\r\n");
_gps_uart->read(chk_char);
_gps_uart->read(chk_char);
printf("message size - %x\r\n",chk_char);
_gps_uart->read(chk_char);
- if (chk_char != 0x83){
+ if (chk_char != 0x83) {
printf("initGPS failed not ack message\r\n");
ret = 1;
- }
- else {
+ } else {
printf("ACK message received\r\n");
_gps_uart->read(chk_char);
printf("config message acknowledged - ID - %x\r\n",chk_char);
}
}
- }
- else {
+ } else {
printf("rx empty \r\n");
ret = 1;
}
- }
- else {
+ } else {
printf("No readable characters\r\n");
ret = 1;
}
@@ -139,69 +136,70 @@
return ret;
}
-void GPSPARSER::startSentenceThread(void const *p){
- GPSPARSER *instance = (GPSPARSER*)p;
- instance->readNemaSentence();
+void GPSPARSER::startSentenceThread(void const *p)
+{
+ GPSPARSER *instance = (GPSPARSER*)p;
+ instance->readNemaSentence();
}
-void GPSPARSER::readNemaSentence (void){
+void GPSPARSER::readNemaSentence (void)
+{
char chk_char;
- uint8_t calc_cksum;
+ uint8_t calc_cksum;
uint8_t nema_cksum;
char nema_id[2];
char nema_str[80];
char cksum_str[2];
-
+
_getSentenceThread.signal_wait(START_THREAD);
printf("Got thread start\r\n");
-
+
do {
- if (_gps_uart->readable() > 80){
- do{
+ if (_gps_uart->readable() > 80) {
+ do {
_gps_uart->read(chk_char);
} while ((chk_char != '$') && (!_gps_uart->rxEmpty()));
-
+
if (chk_char == '$') {
_gps_uart->read(nema_id,2);
- if (strpbrk(nema_id,"GP") != NULL){
+ if (strpbrk(nema_id,"GP") != NULL) {
uint8_t i = 0;
calc_cksum = 0x17; // 8 bit XOR of G and P checksum seed
nema_cksum = 0; // initialize nema string checksum
memset(nema_str,0x00,80); // clear nema_str array
do {
_gps_uart->read(chk_char);
- if ((chk_char != 0x0D) && (chk_char != '*')){
+ if ((chk_char != 0x0D) && (chk_char != '*')) {
nema_str[i++] = chk_char;
calc_cksum ^= chk_char; // 8 bit XOR checksum
}
if (chk_char == '*') {
- _gps_uart->read(cksum_str,2);
+ _gps_uart->read(cksum_str,2);
nema_cksum = (uint8_t)strtoul(cksum_str,NULL,16);
}
} while (( chk_char != 0x0D) && !_gps_uart->rxEmpty());
-
+
if (nema_cksum == calc_cksum)
if (strncmp (nema_str,"GGA",3) == 0)
parseGGA(nema_str);
else if (strncmp (nema_str,"GSA",3) == 0)
- parseGSA(nema_str);
- else if (strncmp (nema_str,"GSV",3) == 0)
- parseGSV(nema_str);
- else if (strncmp (nema_str,"GLL",3) == 0)
- parseGLL(nema_str);
- else if (strncmp (nema_str,"RMC",3) == 0)
- parseRMC(nema_str);
- else if (strncmp (nema_str,"VTG",3) == 0)
- parseVTG(nema_str);
- else if (strncmp (nema_str,"ZDA",3) == 0)
- parseZDA(nema_str);
- else
- printf("Unknown NEMA String Type\r\n");
+ parseGSA(nema_str);
+ else if (strncmp (nema_str,"GSV",3) == 0)
+ parseGSV(nema_str);
+ else if (strncmp (nema_str,"GLL",3) == 0)
+ parseGLL(nema_str);
+ else if (strncmp (nema_str,"RMC",3) == 0)
+ parseRMC(nema_str);
+ else if (strncmp (nema_str,"VTG",3) == 0)
+ parseVTG(nema_str);
+ else if (strncmp (nema_str,"ZDA",3) == 0)
+ parseZDA(nema_str);
+ else
+ printf("Unknown NEMA String Type\r\n");
else
printf("NEMA String checksum error %x != %x\r\n",nema_cksum,calc_cksum);
}
- }
- else
+ } else
printf("RX empty before all data read\r\n");
}
@@ -211,10 +209,11 @@
}
-uint8_t GPSPARSER::parseGGA(char *nema_buf){
+uint8_t GPSPARSER::parseGGA(char *nema_buf)
+{
char* token_str;
uint8_t ret = 0;
-
+
token_str = strtok(nema_buf, ",");
// skip timestamp
token_str = strtok(NULL, ",");
@@ -224,7 +223,7 @@
// skip longitude degree minutes
token_str = strtok(NULL, ",");
token_str = strtok(NULL, ",");
-// read fix quality
+// read fix quality
token_str = strtok(NULL, ",");
_fix_quality = atoi(token_str);
// skip number of satellites and horizontal dilution
@@ -236,28 +235,30 @@
return ret;
}
-
-uint8_t GPSPARSER::parseGSA(char *nema_buf){
+
+uint8_t GPSPARSER::parseGSA(char *nema_buf)
+{
char* token_str;
uint8_t ret = 0;
-
+
token_str = strtok(nema_buf, ",");
token_str = strtok(NULL, ",");
// read fix status
token_str = strtok(NULL, ",");
_fix_status = atoi(token_str);
// read satellite PRNs
- for (uint8_t i=0;i<12;i++){
+ for (uint8_t i=0; i<12; i++) {
token_str = strtok(NULL, ",");
_satellite_prn[i] = atoi(token_str);
}
return ret;
}
-uint8_t GPSPARSER::parseGSV(char *nema_buf){
+uint8_t GPSPARSER::parseGSV(char *nema_buf)
+{
char* token_str;
uint8_t ret = 0;
-
+
token_str = strtok(nema_buf, ",");
// skip number of sentences and sentence number for now
token_str = strtok(NULL, ",");
@@ -270,11 +271,12 @@
return ret;
}
-uint8_t GPSPARSER::parseRMC(char *nema_buf){
+uint8_t GPSPARSER::parseRMC(char *nema_buf)
+{
char* token_str;
char temp_str[6];
uint8_t ret = 0;
-
+
token_str = strtok(nema_buf, ",");
// read timestamp
token_str = strtok(NULL, ",");
@@ -342,7 +344,8 @@
return ret;
}
-uint8_t GPSPARSER::parseVTG(char *nema_buf){
+uint8_t GPSPARSER::parseVTG(char *nema_buf)
+{
uint8_t ret = 0;
printf("ParseVTG****\r\n");
printf(nema_buf);
@@ -350,7 +353,8 @@
return ret;
}
-uint8_t GPSPARSER::parseGLL(char *nema_buf){
+uint8_t GPSPARSER::parseGLL(char *nema_buf)
+{
uint8_t ret = 0;
printf("ParseGLL*****\r\n");
printf(nema_buf);
@@ -358,7 +362,8 @@
return ret;
}
-uint8_t GPSPARSER::parseZDA(char *nema_buf){
+uint8_t GPSPARSER::parseZDA(char *nema_buf)
+{
uint8_t ret = 0;
printf("ParseZDA******\r\n");
printf(nema_buf);
@@ -366,38 +371,46 @@
return ret;
}
-bool GPSPARSER::gpsDetected(void){
+bool GPSPARSER::gpsDetected(void)
+{
return _gps_detected;
}
-GPSPARSER::longitude GPSPARSER::getLongitude(void){
+GPSPARSER::longitude GPSPARSER::getLongitude(void)
+{
return _gps_longitude;
}
-GPSPARSER::latitude GPSPARSER::getLatitude(void){
+GPSPARSER::latitude GPSPARSER::getLatitude(void)
+{
return _gps_latitude;
}
-struct tm GPSPARSER::getTimestamp(void){
+struct tm GPSPARSER::getTimestamp(void) {
return _timestamp;
}
-bool GPSPARSER::getLockStatus(void){
+bool GPSPARSER::getLockStatus(void)
+{
return _gps_status;
}
-uint8_t GPSPARSER::getFixStatus(void){
+uint8_t GPSPARSER::getFixStatus(void)
+{
return _fix_status;
}
-uint8_t GPSPARSER::getFixQuality(void){
+uint8_t GPSPARSER::getFixQuality(void)
+{
return _fix_quality;
}
-uint8_t GPSPARSER::getNumSatellites(void){
+uint8_t GPSPARSER::getNumSatellites(void)
+{
return _num_satellites;
}
-int16_t GPSPARSER::getAltitude(void){
+int16_t GPSPARSER::getAltitude(void)
+{
return _msl_altitude;
}