Skip to content

Commit 8b20e27

Browse files
committed
GPS - Implement KAT, debugging
1 parent 522a62f commit 8b20e27

File tree

4 files changed

+82
-20
lines changed

4 files changed

+82
-20
lines changed

src/Wippersnapper_demo.ino.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmp19uci3gr"
2+
#include <Arduino.h>
3+
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
4+
# 11 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
5+
#include "ws_adapters.h"
6+
ws_adapter_wifi wipper;
7+
8+
9+
#define WS_DEBUG
10+
void setup();
11+
void loop();
12+
#line 17 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
13+
void setup() {
14+
Serial.begin(115200);
15+
while (!Serial)
16+
delay(10);
17+
wipper.provision();
18+
wipper.connect();
19+
}
20+
21+
void loop() { wipper.run(); }

src/components/gps/controller.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ int GPSController::NmeaBufPush(const char *new_sentence) {
9191
strncpy(_nmea_buff.sentences[_nmea_buff.head], new_sentence,
9292
MAX_LEN_NMEA_SENTENCE - 1);
9393
_nmea_buff.sentences[_nmea_buff.head][MAX_LEN_NMEA_SENTENCE - 1] = '\0';
94+
WS_DEBUG_PRINT("[gps] Pushing NMEA sentence: ");
95+
WS_DEBUG_PRINTLN(_nmea_buff.sentences[_nmea_buff.head]);
9496
_nmea_buff.head = next;
9597
return 0;
9698
}
@@ -128,6 +130,13 @@ void GPSController::update() {
128130

129131
for (GPSHardware *drv : _gps_drivers) {
130132

133+
// Perform a keep-alive check by sending an antenna check command every 2
134+
// seconds
135+
if (millis() - drv->GetPrvKat() > 2000) {
136+
drv->GetAdaGps()->sendCommand(CMD_MTK_CHECK_ANTENNA);
137+
drv->SetPrvKat(millis());
138+
}
139+
131140
// Did read period elapse?
132141
ulong cur_time = millis();
133142
if (cur_time - drv->GetPollPeriodPrv() < drv->GetPollPeriod())
@@ -153,25 +162,31 @@ void GPSController::update() {
153162

154163
// Let's attempt to get a sentence from the GPS module
155164
// Convert the NMEA update rate to milliseconds
165+
// TODO: This should be stored as a member within the hardware class
156166
ulong update_rate = 1000 / drv->GetNmeaUpdateRate();
157167

158168
// Read from the GPS module for update_rate milliseconds
159169
ulong start_time = millis();
160170
int read_calls = 0;
171+
172+
WS_DEBUG_PRINT("[gps] Reading GPS data for ");
173+
WS_DEBUG_PRINT(update_rate);
174+
WS_DEBUG_PRINTLN(" milliseconds...");
161175
while (millis() - start_time < update_rate) {
162-
if (read_calls > 9) {
163-
// Check if we have a new NMEA sentence
164-
if (drv->GetAdaGps()->newNMEAreceived()) {
165-
// If we have a new sentence, push it to the buffer and mark the
166-
// received flag as false
167-
// TODO: Check result of this operation actually
168-
NmeaBufPush(drv->GetAdaGps()->lastNMEA());
169-
read_calls = 0; // Keep reading until update_rate elapses
170-
}
171-
char c = drv->GetAdaGps()->read();
172-
read_calls++;
176+
char c = drv->GetAdaGps()->read();
177+
read_calls++;
178+
179+
// Check if we have a new NMEA sentence
180+
if (drv->GetAdaGps()->newNMEAreceived()) {
181+
// If we have a new sentence, push it to the buffer
182+
// TODO: Check result of this operation actually
183+
WS_DEBUG_PRINTLN("[gps] New data, pushing sentence to buffer.");
184+
// Push the last NMEA sentence to the buffer
185+
char *last_nmea = drv->GetAdaGps()->lastNMEA();
186+
NmeaBufPush(drv->GetAdaGps()->lastNMEA());
173187
}
174188
}
189+
WS_DEBUG_PRINTLN("[gps] Finished reading GPS data.");
175190

176191
// We are done reading for this period
177192

@@ -184,6 +199,8 @@ void GPSController::update() {
184199
// Pop until we have no more sentences in the buffer
185200
while (NmeaBufPop(nmea_sentence) != -1) {
186201
// Parse the NMEA sentence
202+
WS_DEBUG_PRINT("[gps] Parsing NMEA sentence: ");
203+
WS_DEBUG_PRINTLN(nmea_sentence);
187204
if (!drv->GetAdaGps()->parse(nmea_sentence)) {
188205
WS_DEBUG_PRINT("[gps] ERROR: Failed to parse NMEA sentence: ");
189206
WS_DEBUG_PRINTLN(nmea_sentence);

src/components/gps/hardware.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ bool GPSHardware::Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config) {
5959
for (size_t i = 0; i < gps_config->commands_count; i++) {
6060
WS_DEBUG_PRINT("[gps] Sending command to MediaTek GPS: ");
6161
WS_DEBUG_PRINTLN(gps_config->commands[i]);
62-
// Send the command to the GPS module
63-
_hw_serial->flush(); // Flush the serial buffer before sending
64-
_ada_gps->sendCommand(gps_config->commands[i]);
65-
// and wait for the corresponding response from the GPS module
62+
// Build the PMTK ACK response for the command
6663
char msg_resp[MAX_NEMA_SENTENCE_LEN];
6764
if (!BuildPmtkAck(gps_config->commands[i], msg_resp)) {
6865
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to build PMTK ACK response!");
6966
return false;
7067
}
68+
// Send the command to the GPS module
69+
_hw_serial->flush(); // Flush the serial buffer before sending
70+
_ada_gps->sendCommand(gps_config->commands[i]);
71+
// and wait for the corresponding response from the GPS module
7172
if (!_ada_gps->waitForSentence(msg_resp)) {
7273
WS_DEBUG_PRINT("[gps] ERROR: Failed to get response | cmd:");
7374
WS_DEBUG_PRINTLN(gps_config->commands[i]);
@@ -302,4 +303,23 @@ void GPSHardware::SetNmeaBaudRate(int nmea_baud_rate) {
302303
* @brief Returns the NMEA port baud rate for GPS data.
303304
* @returns The NMEA baud rate, in bits per second.
304305
*/
305-
int GPSHardware::GetNmeaBaudRate() { return _nmea_baud_rate; }
306+
int GPSHardware::GetNmeaBaudRate() { return _nmea_baud_rate; }
307+
308+
/*!
309+
* @brief Sets the last time the GPS hardware was polled.
310+
* @param kat_prv
311+
* The last time the GPS hardware was polled, in milliseconds.
312+
*/
313+
void GPSHardware::SetPrvKat(ulong kat_prv) {
314+
if (kat_prv < 0) {
315+
_kat_prv = 0;
316+
return;
317+
}
318+
_kat_prv = kat_prv;
319+
}
320+
321+
/*!
322+
* @brief Gets the last time the GPS hardware was polled.
323+
* @returns The last time the GPS hardware was polled, in milliseconds.
324+
*/
325+
ulong GPSHardware::GetPrvKat() { return _kat_prv; }

src/components/gps/hardware.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define CMD_MTK_QUERY_FW_RESP \
2323
"$PMTK705" ///< Response from querying MediaTek firmware version without the
2424
///< ReleaseStr
25+
#define CMD_MTK_CHECK_ANTENNA "$PGCMD,33,1*6C" ///< Command to check antenna
2526
#define DEFAULT_MTK_NMEA_UPDATE_RATE 1 ///< Default NMEA update rate in Hz
2627
#define DEFAULT_MTK_NMEA_BAUD_RATE 9600 ///< Default NMEA baud rate in bits per
2728
#define MAX_NEMA_SENTENCE_LEN 82 ///< Maximum length of a NMEA sentence
@@ -59,6 +60,8 @@ class GPSHardware {
5960
void SetPollPeriodPrv(ulong poll_period_prv);
6061
ulong GetPollPeriod();
6162
ulong GetPollPeriodPrv();
63+
ulong GetPrvKat();
64+
void SetPrvKat(ulong kat_prv);
6265
void SetNmeaUpdateRate(int nmea_update_rate);
6366
int GetNmeaUpdateRate();
6467
void SetNmeaBaudRate(int nmea_baud_rate);
@@ -75,10 +78,11 @@ class GPSHardware {
7578
GpsDriverType _driver_type; ///< Type of GPS driver used
7679
HardwareSerial *_hw_serial = nullptr; ///< HardwareSerial instance for GPS;
7780
Adafruit_GPS *_ada_gps = nullptr; ///< Adafruit GPS instance
78-
ulong _period; ///< Polling period for GPS data (Specified by IO), in
79-
///< milliseconds
80-
ulong _period_prv; ///< Previous period for GPS data (Specified by IO), in
81-
///< milliseconds
81+
ulong _period; ///< Polling period for GPS data (Specified by IO), in
82+
///< milliseconds
83+
ulong _period_prv; ///< Previous period for GPS data (Specified by IO), in
84+
///< milliseconds
85+
ulong _kat_prv; ///< Last time the GPS hardware was polled, in milliseconds
8286
int _nmea_update_rate; ///< NMEA update rate for GPS data, in Hz
8387
int _nmea_baud_rate; ///< NMEA baud rate for GPS data, in bits per second
8488
};

0 commit comments

Comments
 (0)