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
GPSPARSER.cpp@13:b41779d6cddb, 2018-01-31 (annotated)
- Committer:
- Evan Hosseini
- Date:
- Wed Jan 31 09:14:03 2018 -0600
- Revision:
- 13:b41779d6cddb
- Parent:
- 12:179474537b42
Pull LED i2c write out of the Ticker ISR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
falingtrea | 0:d2169fecf3fb | 1 | /** |
falingtrea | 0:d2169fecf3fb | 2 | * @file NemaParser.cpp |
falingtrea | 0:d2169fecf3fb | 3 | * @brief NEMA String to Packet Parser - NEMA strings to compact packet data |
falingtrea | 0:d2169fecf3fb | 4 | * @author Tim Barr |
falingtrea | 1:58c50bb3c60c | 5 | * @version 1.01 |
falingtrea | 0:d2169fecf3fb | 6 | * @see http://www.kh-gps.de/nmea.faq |
falingtrea | 0:d2169fecf3fb | 7 | * @see http://www.catb.org/gpsd/NMEA.html |
falingtrea | 0:d2169fecf3fb | 8 | * |
falingtrea | 0:d2169fecf3fb | 9 | * Copyright (c) 2015 |
falingtrea | 0:d2169fecf3fb | 10 | * |
falingtrea | 0:d2169fecf3fb | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
falingtrea | 0:d2169fecf3fb | 12 | * you may not use this file except in compliance with the License. |
falingtrea | 0:d2169fecf3fb | 13 | * You may obtain a copy of the License at |
falingtrea | 0:d2169fecf3fb | 14 | * |
falingtrea | 0:d2169fecf3fb | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
falingtrea | 0:d2169fecf3fb | 16 | * |
falingtrea | 0:d2169fecf3fb | 17 | * Unless required by applicable law or agreed to in writing, software |
falingtrea | 0:d2169fecf3fb | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
falingtrea | 0:d2169fecf3fb | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
falingtrea | 0:d2169fecf3fb | 20 | * See the License for the specific language governing permissions and |
falingtrea | 0:d2169fecf3fb | 21 | * limitations under the License. |
falingtrea | 0:d2169fecf3fb | 22 | * |
falingtrea | 1:58c50bb3c60c | 23 | * 09/16/15 TAB V1.01 Changed report rate of GGA and GSA NEMA sentences |
falingtrea | 3:662aa99c5266 | 24 | * 09/22/15 TAB V1.02 Fixed status value for no GPS detected. Increased report rate |
falingtrea | 3:662aa99c5266 | 25 | * of GSV, GGA, and GSA NEMA Sentences |
falingtrea | 1:58c50bb3c60c | 26 | * |
falingtrea | 0:d2169fecf3fb | 27 | * TODO: Add speed, compass direction, error (DOP) data. Make init more generic |
falingtrea | 0:d2169fecf3fb | 28 | */ |
falingtrea | 0:d2169fecf3fb | 29 | |
falingtrea | 0:d2169fecf3fb | 30 | #include "GPSPARSER.h" |
falingtrea | 0:d2169fecf3fb | 31 | |
falingtrea | 0:d2169fecf3fb | 32 | using namespace mts; |
falingtrea | 0:d2169fecf3fb | 33 | |
Mike Fiore |
6:2050b4795b66 | 34 | GPSPARSER::GPSPARSER(MTSSerial *uart, NCP5623B* led) |
Mike Fiore |
6:2050b4795b66 | 35 | : _getSentenceThread(&GPSPARSER::startSentenceThread,this), |
Mike Fiore |
6:2050b4795b66 | 36 | _led(led), |
Mike Fiore |
7:bd1013ba8afd | 37 | _led_state(0), |
Evan Hosseini |
13:b41779d6cddb | 38 | _led_state_out(0), |
Mike Fiore |
7:bd1013ba8afd | 39 | _tick_running(false) |
falingtrea | 0:d2169fecf3fb | 40 | { |
falingtrea | 0:d2169fecf3fb | 41 | _gps_uart = uart; |
falingtrea | 0:d2169fecf3fb | 42 | _gps_uart->baud(9600); //set GPS baud rate here |
mfiore | 4:ae35903cd8d2 | 43 | |
falingtrea | 0:d2169fecf3fb | 44 | _gps_latitude.degrees = 0; |
falingtrea | 0:d2169fecf3fb | 45 | _gps_longitude.degrees = 0; |
falingtrea | 0:d2169fecf3fb | 46 | _timestamp.tm_sec = 0; |
falingtrea | 0:d2169fecf3fb | 47 | _timestamp.tm_min = 0; |
falingtrea | 0:d2169fecf3fb | 48 | _timestamp.tm_hour = 0; |
falingtrea | 0:d2169fecf3fb | 49 | _timestamp.tm_mday = 0; |
falingtrea | 0:d2169fecf3fb | 50 | _timestamp.tm_mon = 0; |
falingtrea | 0:d2169fecf3fb | 51 | _timestamp.tm_year = 0; |
falingtrea | 0:d2169fecf3fb | 52 | _timestamp.tm_wday = 0; |
falingtrea | 0:d2169fecf3fb | 53 | _timestamp.tm_yday = 0; |
falingtrea | 0:d2169fecf3fb | 54 | _timestamp.tm_isdst = -1; // time is UTC so no Daylight savings time |
mfiore | 4:ae35903cd8d2 | 55 | |
falingtrea | 0:d2169fecf3fb | 56 | _gps_status = false; |
falingtrea | 2:fbc098f1e7c0 | 57 | _fix_status = 1; |
falingtrea | 0:d2169fecf3fb | 58 | _num_satellites =0; |
falingtrea | 0:d2169fecf3fb | 59 | _gps_detected = false; |
falingtrea | 0:d2169fecf3fb | 60 | |
Mike Fiore |
10:465f9db415ea | 61 | _getSentenceThread.signal_set(START_THREAD); |
mfiore | 4:ae35903cd8d2 | 62 | |
falingtrea | 0:d2169fecf3fb | 63 | return; |
falingtrea | 0:d2169fecf3fb | 64 | } |
falingtrea | 0:d2169fecf3fb | 65 | |
mfiore | 4:ae35903cd8d2 | 66 | GPSPARSER::~GPSPARSER(void) |
mfiore | 4:ae35903cd8d2 | 67 | { |
falingtrea | 0:d2169fecf3fb | 68 | if (_gps_detected) |
falingtrea | 0:d2169fecf3fb | 69 | _getSentenceThread.terminate(); |
falingtrea | 0:d2169fecf3fb | 70 | } |
falingtrea | 0:d2169fecf3fb | 71 | |
mfiore | 4:ae35903cd8d2 | 72 | void GPSPARSER::startSentenceThread(void const *p) |
mfiore | 4:ae35903cd8d2 | 73 | { |
mfiore | 4:ae35903cd8d2 | 74 | GPSPARSER *instance = (GPSPARSER*)p; |
mfiore | 4:ae35903cd8d2 | 75 | instance->readNemaSentence(); |
falingtrea | 0:d2169fecf3fb | 76 | } |
falingtrea | 0:d2169fecf3fb | 77 | |
mfiore | 4:ae35903cd8d2 | 78 | void GPSPARSER::readNemaSentence (void) |
mfiore | 4:ae35903cd8d2 | 79 | { |
Mike Fiore |
10:465f9db415ea | 80 | // this code is specific to the Skytraq Venus GPS chip. This code could be re-written to detect another type of |
Mike Fiore |
10:465f9db415ea | 81 | // GPS device. Maybe read serial port for a specific time and detect $GP from NEMA string |
Mike Fiore |
10:465f9db415ea | 82 | |
Mike Fiore |
10:465f9db415ea | 83 | // Sets Skytraq Venus GPS to output GGA,GSA,GSV every 10 seconds, and RMC every second, no ZDA,GLL,VTG |
Mike Fiore |
10:465f9db415ea | 84 | // setup string for GPS GGA GSA GSV GLL RMC VTG ZDA cksum |
Mike Fiore |
10:465f9db415ea | 85 | char init_gps[16] = {0xA0,0xA1,0x00,0x09,0x08,0x0A,0x0A,0x0A,0x00,0x01,0x00,0x00,0x00,0x03,0x0D,0x0A}; |
falingtrea | 0:d2169fecf3fb | 86 | char chk_char; |
mfiore | 4:ae35903cd8d2 | 87 | uint8_t calc_cksum; |
falingtrea | 0:d2169fecf3fb | 88 | uint8_t nema_cksum; |
falingtrea | 0:d2169fecf3fb | 89 | char nema_id[2]; |
falingtrea | 0:d2169fecf3fb | 90 | char nema_str[80]; |
falingtrea | 0:d2169fecf3fb | 91 | char cksum_str[2]; |
mfiore | 4:ae35903cd8d2 | 92 | |
falingtrea | 0:d2169fecf3fb | 93 | _getSentenceThread.signal_wait(START_THREAD); |
Mike Fiore |
10:465f9db415ea | 94 | //printf("GPS starting\r\n"); |
rklaassen | 12:179474537b42 | 95 | for(int i = 0; i < 20; i++){ |
Mike Fiore |
10:465f9db415ea | 96 | _gps_uart->rxClear(); |
Mike Fiore |
10:465f9db415ea | 97 | _gps_uart->write(init_gps, sizeof(init_gps)); |
Mike Fiore |
10:465f9db415ea | 98 | while (! _gps_uart->readable()) |
Mike Fiore |
10:465f9db415ea | 99 | osDelay(100); |
Mike Fiore |
10:465f9db415ea | 100 | do { |
Mike Fiore |
10:465f9db415ea | 101 | _gps_uart->read(chk_char); |
Mike Fiore |
10:465f9db415ea | 102 | //printf("read char %#X\r\n", chk_char); |
Mike Fiore |
10:465f9db415ea | 103 | } while ((chk_char != 0xA0) && (!_gps_uart->rxEmpty())); |
Mike Fiore |
10:465f9db415ea | 104 | if (chk_char == 0xA0) { |
Mike Fiore |
10:465f9db415ea | 105 | _gps_uart->read(chk_char); |
Mike Fiore |
10:465f9db415ea | 106 | //printf("read char %#X\r\n", chk_char); |
Mike Fiore |
10:465f9db415ea | 107 | if (chk_char == 0xA1) { |
Mike Fiore |
10:465f9db415ea | 108 | _gps_uart->read(chk_char); |
Mike Fiore |
10:465f9db415ea | 109 | //printf("read char %#X\r\n", chk_char); |
Mike Fiore |
10:465f9db415ea | 110 | _gps_uart->read(chk_char); |
Mike Fiore |
10:465f9db415ea | 111 | //printf("read char %#X\r\n", chk_char); |
Mike Fiore |
10:465f9db415ea | 112 | _gps_uart->read(chk_char); |
Mike Fiore |
10:465f9db415ea | 113 | //printf("read char %#X\r\n", chk_char); |
Mike Fiore |
10:465f9db415ea | 114 | if (chk_char == 0x83) { |
Mike Fiore |
10:465f9db415ea | 115 | _gps_detected = true; |
rklaassen | 11:73e776e41d23 | 116 | break; |
Mike Fiore |
10:465f9db415ea | 117 | } |
Mike Fiore |
10:465f9db415ea | 118 | } |
Mike Fiore |
10:465f9db415ea | 119 | } |
rklaassen | 11:73e776e41d23 | 120 | osDelay(50); |
rklaassen | 11:73e776e41d23 | 121 | } |
Mike Fiore |
10:465f9db415ea | 122 | //printf("GPS detected %s\r\n", _gps_detected ? "true" : "false"); |
Mike Fiore |
10:465f9db415ea | 123 | |
Mike Fiore |
10:465f9db415ea | 124 | if (! _gps_detected) { |
Mike Fiore |
10:465f9db415ea | 125 | _fix_status = 0; |
Mike Fiore |
10:465f9db415ea | 126 | return; |
Mike Fiore |
10:465f9db415ea | 127 | } |
Mike Fiore |
10:465f9db415ea | 128 | |
Mike Fiore |
7:bd1013ba8afd | 129 | if (_led) { |
Mike Fiore |
7:bd1013ba8afd | 130 | _tick.attach(this, &GPSPARSER::blinker, 0.5); |
Mike Fiore |
7:bd1013ba8afd | 131 | _tick_running = true; |
Mike Fiore |
7:bd1013ba8afd | 132 | } |
mfiore | 4:ae35903cd8d2 | 133 | |
falingtrea | 0:d2169fecf3fb | 134 | do { |
mfiore | 4:ae35903cd8d2 | 135 | if (_gps_uart->readable() > 80) { |
mfiore | 4:ae35903cd8d2 | 136 | do { |
falingtrea | 0:d2169fecf3fb | 137 | _gps_uart->read(chk_char); |
falingtrea | 0:d2169fecf3fb | 138 | } while ((chk_char != '$') && (!_gps_uart->rxEmpty())); |
mfiore | 4:ae35903cd8d2 | 139 | |
falingtrea | 0:d2169fecf3fb | 140 | if (chk_char == '$') { |
falingtrea | 0:d2169fecf3fb | 141 | _gps_uart->read(nema_id,2); |
mfiore | 4:ae35903cd8d2 | 142 | if (strpbrk(nema_id,"GP") != NULL) { |
falingtrea | 0:d2169fecf3fb | 143 | uint8_t i = 0; |
falingtrea | 0:d2169fecf3fb | 144 | calc_cksum = 0x17; // 8 bit XOR of G and P checksum seed |
falingtrea | 0:d2169fecf3fb | 145 | nema_cksum = 0; // initialize nema string checksum |
falingtrea | 0:d2169fecf3fb | 146 | memset(nema_str,0x00,80); // clear nema_str array |
falingtrea | 0:d2169fecf3fb | 147 | do { |
falingtrea | 0:d2169fecf3fb | 148 | _gps_uart->read(chk_char); |
mfiore | 4:ae35903cd8d2 | 149 | if ((chk_char != 0x0D) && (chk_char != '*')) { |
falingtrea | 0:d2169fecf3fb | 150 | nema_str[i++] = chk_char; |
falingtrea | 0:d2169fecf3fb | 151 | calc_cksum ^= chk_char; // 8 bit XOR checksum |
falingtrea | 0:d2169fecf3fb | 152 | } |
falingtrea | 0:d2169fecf3fb | 153 | if (chk_char == '*') { |
mfiore | 4:ae35903cd8d2 | 154 | _gps_uart->read(cksum_str,2); |
falingtrea | 0:d2169fecf3fb | 155 | nema_cksum = (uint8_t)strtoul(cksum_str,NULL,16); |
falingtrea | 0:d2169fecf3fb | 156 | } |
falingtrea | 0:d2169fecf3fb | 157 | } while (( chk_char != 0x0D) && !_gps_uart->rxEmpty()); |
mfiore | 4:ae35903cd8d2 | 158 | |
Mike Fiore |
9:a4d4ab3b0f23 | 159 | if (nema_cksum == calc_cksum) { |
falingtrea | 0:d2169fecf3fb | 160 | if (strncmp (nema_str,"GGA",3) == 0) |
falingtrea | 0:d2169fecf3fb | 161 | parseGGA(nema_str); |
falingtrea | 0:d2169fecf3fb | 162 | else if (strncmp (nema_str,"GSA",3) == 0) |
mfiore | 4:ae35903cd8d2 | 163 | parseGSA(nema_str); |
mfiore | 4:ae35903cd8d2 | 164 | else if (strncmp (nema_str,"GSV",3) == 0) |
mfiore | 4:ae35903cd8d2 | 165 | parseGSV(nema_str); |
mfiore | 4:ae35903cd8d2 | 166 | else if (strncmp (nema_str,"GLL",3) == 0) |
mfiore | 4:ae35903cd8d2 | 167 | parseGLL(nema_str); |
mfiore | 4:ae35903cd8d2 | 168 | else if (strncmp (nema_str,"RMC",3) == 0) |
mfiore | 4:ae35903cd8d2 | 169 | parseRMC(nema_str); |
mfiore | 4:ae35903cd8d2 | 170 | else if (strncmp (nema_str,"VTG",3) == 0) |
mfiore | 4:ae35903cd8d2 | 171 | parseVTG(nema_str); |
mfiore | 4:ae35903cd8d2 | 172 | else if (strncmp (nema_str,"ZDA",3) == 0) |
mfiore | 4:ae35903cd8d2 | 173 | parseZDA(nema_str); |
mfiore | 4:ae35903cd8d2 | 174 | else |
Mike Fiore |
9:a4d4ab3b0f23 | 175 | printf("Unknown NEMA String Type\r\n"); |
Mike Fiore |
9:a4d4ab3b0f23 | 176 | } else { |
Mike Fiore |
9:a4d4ab3b0f23 | 177 | printf("NEMA String checksum error %x != %x\r\n",nema_cksum,calc_cksum); |
Mike Fiore |
9:a4d4ab3b0f23 | 178 | } |
falingtrea | 0:d2169fecf3fb | 179 | } |
mfiore | 4:ae35903cd8d2 | 180 | } else |
Mike Fiore |
9:a4d4ab3b0f23 | 181 | printf("RX empty before all data read\r\n"); |
falingtrea | 0:d2169fecf3fb | 182 | } |
falingtrea | 0:d2169fecf3fb | 183 | |
Mike Fiore |
6:2050b4795b66 | 184 | if (_led) { |
Mike Fiore |
6:2050b4795b66 | 185 | if (_fix_status >= 2) { |
Mike Fiore |
7:bd1013ba8afd | 186 | if (_tick_running) { |
Mike Fiore |
7:bd1013ba8afd | 187 | _tick.detach(); |
Mike Fiore |
7:bd1013ba8afd | 188 | _tick_running = false; |
Mike Fiore |
7:bd1013ba8afd | 189 | } |
Mike Fiore |
6:2050b4795b66 | 190 | _led->setPWM(NCP5623B::LED_3, 8); |
Mike Fiore |
7:bd1013ba8afd | 191 | } else { |
Mike Fiore |
7:bd1013ba8afd | 192 | if (! _tick_running) { |
Mike Fiore |
7:bd1013ba8afd | 193 | _tick.attach(this, &GPSPARSER::blinker, 0.5); |
Mike Fiore |
7:bd1013ba8afd | 194 | _tick_running = true; |
Evan Hosseini |
13:b41779d6cddb | 195 | } else { |
Evan Hosseini |
13:b41779d6cddb | 196 | if (_led_state_out != _led_state) |
Evan Hosseini |
13:b41779d6cddb | 197 | { |
Evan Hosseini |
13:b41779d6cddb | 198 | _led_state_out = _led_state; |
Evan Hosseini |
13:b41779d6cddb | 199 | _led->setPWM(NCP5623B::LED_3, _led_state_out); |
Evan Hosseini |
13:b41779d6cddb | 200 | } |
Mike Fiore |
7:bd1013ba8afd | 201 | } |
Mike Fiore |
6:2050b4795b66 | 202 | } |
Mike Fiore |
6:2050b4795b66 | 203 | } |
Mike Fiore |
6:2050b4795b66 | 204 | |
falingtrea | 0:d2169fecf3fb | 205 | osDelay(100); |
falingtrea | 0:d2169fecf3fb | 206 | |
falingtrea | 0:d2169fecf3fb | 207 | } while(true); |
falingtrea | 0:d2169fecf3fb | 208 | |
falingtrea | 0:d2169fecf3fb | 209 | } |
falingtrea | 0:d2169fecf3fb | 210 | |
mfiore | 4:ae35903cd8d2 | 211 | uint8_t GPSPARSER::parseGGA(char *nema_buf) |
mfiore | 4:ae35903cd8d2 | 212 | { |
falingtrea | 0:d2169fecf3fb | 213 | char* token_str; |
falingtrea | 0:d2169fecf3fb | 214 | uint8_t ret = 0; |
mfiore | 4:ae35903cd8d2 | 215 | |
Mike Fiore |
8:5fa40fa372e2 | 216 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 217 | |
falingtrea | 0:d2169fecf3fb | 218 | token_str = strtok(nema_buf, ","); |
falingtrea | 0:d2169fecf3fb | 219 | // skip timestamp |
falingtrea | 0:d2169fecf3fb | 220 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 221 | // skip latitude degrees minutes |
falingtrea | 0:d2169fecf3fb | 222 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 223 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 224 | // skip longitude degree minutes |
falingtrea | 0:d2169fecf3fb | 225 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 226 | token_str = strtok(NULL, ","); |
mfiore | 4:ae35903cd8d2 | 227 | // read fix quality |
falingtrea | 0:d2169fecf3fb | 228 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 229 | _fix_quality = atoi(token_str); |
falingtrea | 0:d2169fecf3fb | 230 | // skip number of satellites and horizontal dilution |
falingtrea | 0:d2169fecf3fb | 231 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 232 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 233 | // read msl altitude in meters |
falingtrea | 0:d2169fecf3fb | 234 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 235 | _msl_altitude = atoi(token_str); |
falingtrea | 0:d2169fecf3fb | 236 | |
Mike Fiore |
8:5fa40fa372e2 | 237 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 238 | |
falingtrea | 0:d2169fecf3fb | 239 | return ret; |
falingtrea | 0:d2169fecf3fb | 240 | } |
mfiore | 4:ae35903cd8d2 | 241 | |
mfiore | 4:ae35903cd8d2 | 242 | uint8_t GPSPARSER::parseGSA(char *nema_buf) |
mfiore | 4:ae35903cd8d2 | 243 | { |
falingtrea | 0:d2169fecf3fb | 244 | char* token_str; |
falingtrea | 0:d2169fecf3fb | 245 | uint8_t ret = 0; |
mfiore | 4:ae35903cd8d2 | 246 | |
Mike Fiore |
8:5fa40fa372e2 | 247 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 248 | |
falingtrea | 0:d2169fecf3fb | 249 | token_str = strtok(nema_buf, ","); |
falingtrea | 0:d2169fecf3fb | 250 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 251 | // read fix status |
falingtrea | 0:d2169fecf3fb | 252 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 253 | _fix_status = atoi(token_str); |
falingtrea | 0:d2169fecf3fb | 254 | // read satellite PRNs |
mfiore | 4:ae35903cd8d2 | 255 | for (uint8_t i=0; i<12; i++) { |
falingtrea | 0:d2169fecf3fb | 256 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 257 | _satellite_prn[i] = atoi(token_str); |
falingtrea | 0:d2169fecf3fb | 258 | } |
Mike Fiore |
8:5fa40fa372e2 | 259 | |
Mike Fiore |
8:5fa40fa372e2 | 260 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 261 | |
falingtrea | 0:d2169fecf3fb | 262 | return ret; |
falingtrea | 0:d2169fecf3fb | 263 | } |
falingtrea | 0:d2169fecf3fb | 264 | |
mfiore | 4:ae35903cd8d2 | 265 | uint8_t GPSPARSER::parseGSV(char *nema_buf) |
mfiore | 4:ae35903cd8d2 | 266 | { |
falingtrea | 0:d2169fecf3fb | 267 | char* token_str; |
falingtrea | 0:d2169fecf3fb | 268 | uint8_t ret = 0; |
mfiore | 4:ae35903cd8d2 | 269 | |
Mike Fiore |
8:5fa40fa372e2 | 270 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 271 | |
falingtrea | 0:d2169fecf3fb | 272 | token_str = strtok(nema_buf, ","); |
falingtrea | 0:d2169fecf3fb | 273 | // skip number of sentences and sentence number for now |
falingtrea | 0:d2169fecf3fb | 274 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 275 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 276 | // read Number of satellites |
falingtrea | 0:d2169fecf3fb | 277 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 278 | _num_satellites = atoi(token_str); |
falingtrea | 0:d2169fecf3fb | 279 | // add code to read satellite specs if needed |
Mike Fiore |
8:5fa40fa372e2 | 280 | |
Mike Fiore |
8:5fa40fa372e2 | 281 | _mutex.unlock(); |
falingtrea | 0:d2169fecf3fb | 282 | |
falingtrea | 0:d2169fecf3fb | 283 | return ret; |
falingtrea | 0:d2169fecf3fb | 284 | } |
falingtrea | 0:d2169fecf3fb | 285 | |
mfiore | 4:ae35903cd8d2 | 286 | uint8_t GPSPARSER::parseRMC(char *nema_buf) |
mfiore | 4:ae35903cd8d2 | 287 | { |
falingtrea | 0:d2169fecf3fb | 288 | char* token_str; |
falingtrea | 0:d2169fecf3fb | 289 | char temp_str[6]; |
falingtrea | 0:d2169fecf3fb | 290 | uint8_t ret = 0; |
mfiore | 4:ae35903cd8d2 | 291 | |
Mike Fiore |
8:5fa40fa372e2 | 292 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 293 | |
falingtrea | 0:d2169fecf3fb | 294 | token_str = strtok(nema_buf, ","); |
falingtrea | 0:d2169fecf3fb | 295 | // read timestamp |
falingtrea | 0:d2169fecf3fb | 296 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 297 | strncpy(temp_str,token_str,2); |
falingtrea | 0:d2169fecf3fb | 298 | _timestamp.tm_hour = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 299 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 300 | strncpy(temp_str,token_str+2,2); |
falingtrea | 0:d2169fecf3fb | 301 | _timestamp.tm_min = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 302 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 303 | strncpy(temp_str,token_str+4,2); |
falingtrea | 0:d2169fecf3fb | 304 | _timestamp.tm_sec = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 305 | // set gps_status true = active |
falingtrea | 0:d2169fecf3fb | 306 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 307 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 308 | strncpy(temp_str,token_str,1); |
falingtrea | 0:d2169fecf3fb | 309 | if (temp_str[0] == 'A') |
falingtrea | 0:d2169fecf3fb | 310 | _gps_status = true; |
falingtrea | 0:d2169fecf3fb | 311 | else |
falingtrea | 0:d2169fecf3fb | 312 | _gps_status = false; |
falingtrea | 0:d2169fecf3fb | 313 | // read latitude degrees minutes |
falingtrea | 0:d2169fecf3fb | 314 | token_str = strtok(NULL, "."); |
falingtrea | 0:d2169fecf3fb | 315 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 316 | strncpy(temp_str,token_str,2); |
falingtrea | 0:d2169fecf3fb | 317 | _gps_latitude.degrees = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 318 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 319 | strncpy(temp_str,token_str+2,2); |
falingtrea | 0:d2169fecf3fb | 320 | _gps_latitude.minutes = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 321 | // read fractional minutes |
falingtrea | 0:d2169fecf3fb | 322 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 323 | _gps_latitude.seconds = atoi(token_str); |
falingtrea | 0:d2169fecf3fb | 324 | // read latitude hemisphere change sign if 'S' |
falingtrea | 0:d2169fecf3fb | 325 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 326 | if (token_str[0] == 'S') |
falingtrea | 0:d2169fecf3fb | 327 | _gps_latitude.degrees *= -1; |
falingtrea | 0:d2169fecf3fb | 328 | // read longitude degree minutes |
falingtrea | 0:d2169fecf3fb | 329 | token_str = strtok(NULL, "."); |
falingtrea | 0:d2169fecf3fb | 330 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 331 | strncpy(temp_str,token_str,3); |
falingtrea | 0:d2169fecf3fb | 332 | _gps_longitude.degrees = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 333 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 334 | strncpy(temp_str,token_str+3,2); |
falingtrea | 0:d2169fecf3fb | 335 | _gps_longitude.minutes = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 336 | // read fractional minutes |
falingtrea | 0:d2169fecf3fb | 337 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 338 | _gps_longitude.seconds = atoi(token_str); |
falingtrea | 0:d2169fecf3fb | 339 | // read longitude hemisphere change sign if 'W' |
falingtrea | 0:d2169fecf3fb | 340 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 341 | if (token_str[0] == 'W') |
falingtrea | 0:d2169fecf3fb | 342 | _gps_longitude.degrees *= -1; |
falingtrea | 0:d2169fecf3fb | 343 | // skip speed and track angle |
falingtrea | 0:d2169fecf3fb | 344 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 345 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 346 | // read date |
falingtrea | 0:d2169fecf3fb | 347 | token_str = strtok(NULL, ","); |
falingtrea | 0:d2169fecf3fb | 348 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 349 | strncpy(temp_str,token_str,2); |
falingtrea | 0:d2169fecf3fb | 350 | _timestamp.tm_mday = atoi(temp_str); |
falingtrea | 0:d2169fecf3fb | 351 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 352 | strncpy(temp_str,token_str+2,2); |
falingtrea | 0:d2169fecf3fb | 353 | _timestamp.tm_mon = atoi(temp_str) - 1; |
falingtrea | 0:d2169fecf3fb | 354 | memset(temp_str,0x00,6); |
falingtrea | 0:d2169fecf3fb | 355 | strncpy(temp_str,token_str+4,2); |
falingtrea | 0:d2169fecf3fb | 356 | _timestamp.tm_year = atoi(temp_str) + 100; |
falingtrea | 0:d2169fecf3fb | 357 | |
Mike Fiore |
8:5fa40fa372e2 | 358 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 359 | |
falingtrea | 0:d2169fecf3fb | 360 | return ret; |
falingtrea | 0:d2169fecf3fb | 361 | } |
falingtrea | 0:d2169fecf3fb | 362 | |
mfiore | 4:ae35903cd8d2 | 363 | uint8_t GPSPARSER::parseVTG(char *nema_buf) |
mfiore | 4:ae35903cd8d2 | 364 | { |
falingtrea | 0:d2169fecf3fb | 365 | uint8_t ret = 0; |
Mike Fiore |
8:5fa40fa372e2 | 366 | //printf("ParseVTG****\r\n"); |
Mike Fiore |
8:5fa40fa372e2 | 367 | //printf(nema_buf); |
Mike Fiore |
8:5fa40fa372e2 | 368 | //printf("\r\n"); |
falingtrea | 0:d2169fecf3fb | 369 | return ret; |
falingtrea | 0:d2169fecf3fb | 370 | } |
falingtrea | 0:d2169fecf3fb | 371 | |
mfiore | 4:ae35903cd8d2 | 372 | uint8_t GPSPARSER::parseGLL(char *nema_buf) |
mfiore | 4:ae35903cd8d2 | 373 | { |
falingtrea | 0:d2169fecf3fb | 374 | uint8_t ret = 0; |
Mike Fiore |
8:5fa40fa372e2 | 375 | //printf("ParseGLL*****\r\n"); |
Mike Fiore |
8:5fa40fa372e2 | 376 | //printf(nema_buf); |
Mike Fiore |
8:5fa40fa372e2 | 377 | //printf("\r\n"); |
falingtrea | 0:d2169fecf3fb | 378 | return ret; |
falingtrea | 0:d2169fecf3fb | 379 | } |
falingtrea | 0:d2169fecf3fb | 380 | |
mfiore | 4:ae35903cd8d2 | 381 | uint8_t GPSPARSER::parseZDA(char *nema_buf) |
mfiore | 4:ae35903cd8d2 | 382 | { |
falingtrea | 0:d2169fecf3fb | 383 | uint8_t ret = 0; |
Mike Fiore |
8:5fa40fa372e2 | 384 | //printf("ParseZDA******\r\n"); |
Mike Fiore |
8:5fa40fa372e2 | 385 | //printf(nema_buf); |
Mike Fiore |
8:5fa40fa372e2 | 386 | //printf("\r\n"); |
falingtrea | 0:d2169fecf3fb | 387 | return ret; |
falingtrea | 0:d2169fecf3fb | 388 | } |
falingtrea | 0:d2169fecf3fb | 389 | |
mfiore | 4:ae35903cd8d2 | 390 | bool GPSPARSER::gpsDetected(void) |
mfiore | 4:ae35903cd8d2 | 391 | { |
Mike Fiore |
8:5fa40fa372e2 | 392 | bool detected; |
Mike Fiore |
8:5fa40fa372e2 | 393 | |
Mike Fiore |
8:5fa40fa372e2 | 394 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 395 | detected = _gps_detected; |
Mike Fiore |
8:5fa40fa372e2 | 396 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 397 | |
Mike Fiore |
8:5fa40fa372e2 | 398 | return detected; |
falingtrea | 0:d2169fecf3fb | 399 | } |
falingtrea | 0:d2169fecf3fb | 400 | |
mfiore | 4:ae35903cd8d2 | 401 | GPSPARSER::longitude GPSPARSER::getLongitude(void) |
mfiore | 4:ae35903cd8d2 | 402 | { |
Mike Fiore |
8:5fa40fa372e2 | 403 | longitude lon; |
Mike Fiore |
8:5fa40fa372e2 | 404 | |
Mike Fiore |
8:5fa40fa372e2 | 405 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 406 | lon = _gps_longitude; |
Mike Fiore |
8:5fa40fa372e2 | 407 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 408 | |
Mike Fiore |
8:5fa40fa372e2 | 409 | return lon; |
falingtrea | 0:d2169fecf3fb | 410 | } |
falingtrea | 0:d2169fecf3fb | 411 | |
mfiore | 4:ae35903cd8d2 | 412 | GPSPARSER::latitude GPSPARSER::getLatitude(void) |
mfiore | 4:ae35903cd8d2 | 413 | { |
Mike Fiore |
8:5fa40fa372e2 | 414 | latitude lat; |
Mike Fiore |
8:5fa40fa372e2 | 415 | |
Mike Fiore |
8:5fa40fa372e2 | 416 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 417 | lat = _gps_latitude; |
Mike Fiore |
8:5fa40fa372e2 | 418 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 419 | |
Mike Fiore |
8:5fa40fa372e2 | 420 | return lat; |
falingtrea | 0:d2169fecf3fb | 421 | } |
falingtrea | 0:d2169fecf3fb | 422 | |
mfiore | 4:ae35903cd8d2 | 423 | struct tm GPSPARSER::getTimestamp(void) { |
Mike Fiore |
8:5fa40fa372e2 | 424 | struct tm time; |
Mike Fiore |
8:5fa40fa372e2 | 425 | |
Mike Fiore |
8:5fa40fa372e2 | 426 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 427 | time = _timestamp; |
Mike Fiore |
8:5fa40fa372e2 | 428 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 429 | |
Mike Fiore |
8:5fa40fa372e2 | 430 | return time; |
falingtrea | 0:d2169fecf3fb | 431 | } |
falingtrea | 0:d2169fecf3fb | 432 | |
mfiore | 4:ae35903cd8d2 | 433 | bool GPSPARSER::getLockStatus(void) |
mfiore | 4:ae35903cd8d2 | 434 | { |
Mike Fiore |
8:5fa40fa372e2 | 435 | bool status; |
Mike Fiore |
8:5fa40fa372e2 | 436 | |
Mike Fiore |
8:5fa40fa372e2 | 437 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 438 | status = _gps_status; |
Mike Fiore |
8:5fa40fa372e2 | 439 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 440 | |
Mike Fiore |
8:5fa40fa372e2 | 441 | return status; |
falingtrea | 0:d2169fecf3fb | 442 | } |
falingtrea | 0:d2169fecf3fb | 443 | |
mfiore | 4:ae35903cd8d2 | 444 | uint8_t GPSPARSER::getFixStatus(void) |
mfiore | 4:ae35903cd8d2 | 445 | { |
Mike Fiore |
8:5fa40fa372e2 | 446 | uint8_t fix; |
Mike Fiore |
8:5fa40fa372e2 | 447 | |
Mike Fiore |
8:5fa40fa372e2 | 448 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 449 | fix = _fix_status; |
Mike Fiore |
8:5fa40fa372e2 | 450 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 451 | |
Mike Fiore |
8:5fa40fa372e2 | 452 | return fix; |
falingtrea | 0:d2169fecf3fb | 453 | } |
falingtrea | 0:d2169fecf3fb | 454 | |
mfiore | 4:ae35903cd8d2 | 455 | uint8_t GPSPARSER::getFixQuality(void) |
mfiore | 4:ae35903cd8d2 | 456 | { |
Mike Fiore |
8:5fa40fa372e2 | 457 | uint8_t fix; |
Mike Fiore |
8:5fa40fa372e2 | 458 | |
Mike Fiore |
8:5fa40fa372e2 | 459 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 460 | fix = _fix_quality; |
Mike Fiore |
8:5fa40fa372e2 | 461 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 462 | |
Mike Fiore |
8:5fa40fa372e2 | 463 | return fix; |
falingtrea | 0:d2169fecf3fb | 464 | } |
falingtrea | 0:d2169fecf3fb | 465 | |
mfiore | 4:ae35903cd8d2 | 466 | uint8_t GPSPARSER::getNumSatellites(void) |
mfiore | 4:ae35903cd8d2 | 467 | { |
Mike Fiore |
8:5fa40fa372e2 | 468 | uint8_t sats; |
Mike Fiore |
8:5fa40fa372e2 | 469 | |
Mike Fiore |
8:5fa40fa372e2 | 470 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 471 | sats = _num_satellites; |
Mike Fiore |
8:5fa40fa372e2 | 472 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 473 | |
Mike Fiore |
8:5fa40fa372e2 | 474 | return sats; |
falingtrea | 0:d2169fecf3fb | 475 | } |
falingtrea | 0:d2169fecf3fb | 476 | |
mfiore | 4:ae35903cd8d2 | 477 | int16_t GPSPARSER::getAltitude(void) |
mfiore | 4:ae35903cd8d2 | 478 | { |
Mike Fiore |
8:5fa40fa372e2 | 479 | int16_t alt; |
Mike Fiore |
8:5fa40fa372e2 | 480 | |
Mike Fiore |
8:5fa40fa372e2 | 481 | _mutex.lock(); |
Mike Fiore |
8:5fa40fa372e2 | 482 | alt = _msl_altitude; |
Mike Fiore |
8:5fa40fa372e2 | 483 | _mutex.unlock(); |
Mike Fiore |
8:5fa40fa372e2 | 484 | |
Mike Fiore |
8:5fa40fa372e2 | 485 | return alt; |
falingtrea | 0:d2169fecf3fb | 486 | } |
Mike Fiore |
6:2050b4795b66 | 487 | |
Mike Fiore |
7:bd1013ba8afd | 488 | void GPSPARSER::blinker() { |
Mike Fiore |
7:bd1013ba8afd | 489 | _led_state = (_led_state == 0) ? 8 : 0; |
Mike Fiore |
7:bd1013ba8afd | 490 | } |
Mike Fiore |
7:bd1013ba8afd | 491 |