Skip to content

Commit 6509167

Browse files
committed
GPS - Read/discard approach, add nmea rates to hardware
1 parent bede6ab commit 6509167

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed

src/components/gps/controller.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,28 @@ void GPSController::update() {
7777

7878
// Did read period elapse?
7979
ulong cur_time = millis();
80-
}
81-
}
80+
if (cur_time - drv->GetPollPeriodPrv() < drv->GetPollPeriod())
81+
continue; // Not yet elapsed, skip this driver
82+
83+
// Discard the GPS buffer before we attempt to do a fresh read
84+
size_t bytes_avail = drv->GetAdaGps()->available();
85+
if (bytes_avail > 0) {
86+
// TODO: Remove these two WS_DEBUG_PRINTs!
87+
WS_DEBUG_PRINT("[gps] Discarding GPS data: ");
88+
WS_DEBUG_PRINTLN(bytes_avail);
89+
// Read the available data from the GPS module
90+
// and discard it
91+
for (size_t i = 0; i < bytes_avail; i++) {
92+
drv->GetAdaGps()->read();
93+
}
94+
}
95+
96+
// Unset the received flag
97+
if (drv->GetAdaGps()->newNMEAreceived()) {
98+
drv->GetAdaGps()->lastNMEA();
99+
}
100+
101+
// Let's attempt to get a sentence from the GPS module
102+
// TODO: Are we expecting RMC, GGA, or other sentences?
103+
// TODO: Use a timeout for the read here
104+
}

src/components/gps/hardware.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ GPSHardware::~GPSHardware() {
4040
* @returns True if the message was handled successfully, False otherwise.
4141
*/
4242
bool GPSHardware::Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config) {
43+
WS_DEBUG_PRINTLN("[gps] Handling GPSConfig message...");
44+
if (gps_config == nullptr)
45+
return false;
46+
// Set the polling period for GPS data
47+
SetPollPeriod(gps_config->period);
48+
4349
// Attempt to decode the GPSConfig message
4450
if (_driver_type == GPS_DRV_MTK) {
4551
WS_DEBUG_PRINTLN("[gps] Handling GPSConfig for MediaTek driver...");
@@ -206,6 +212,8 @@ bool GPSHardware::DetectMediatek() {
206212
return false;
207213
}
208214
_driver_type = GPS_DRV_MTK;
215+
_nmea_baud_rate = DEFAULT_MTK_NMEA_BAUD_RATE;
216+
_nmea_update_rate = DEFAULT_MTK_NMEA_UPDATE_RATE;
209217
return true;
210218
}
211219

@@ -231,7 +239,13 @@ bool GPSHardware::BuildPmtkAck(char *msg_cmd, char *msg_resp) {
231239
* @param poll_period
232240
* The polling period in milliseconds.
233241
*/
234-
void GPSHardware::SetPollPeriod(ulong poll_period) { _period = poll_period; }
242+
void GPSHardware::SetPollPeriod(ulong poll_period) {
243+
if (poll_period < 0) {
244+
_period = 0;
245+
return;
246+
}
247+
_period = (unsigned long)(poll_period * 1000.0f);
248+
}
235249

236250
/*!
237251
* @brief Sets the previous polling period for GPS data.
@@ -252,4 +266,40 @@ ulong GPSHardware::GetPollPeriod() { return _period; }
252266
* @brief Gets the previous polling period for GPS data.
253267
* @returns The previous polling period in milliseconds.
254268
*/
255-
ulong GPSHardware::GetPollPeriodPrv() { return _period_prv; }
269+
ulong GPSHardware::GetPollPeriodPrv() { return _period_prv; }
270+
271+
/*!
272+
* @brief Returns the Adafruit_GPS instance.
273+
* @returns Pointer to the Adafruit_GPS instance.
274+
*/
275+
Adafruit_GPS *GPSHardware::GetAdaGps() { return _ada_gps; }
276+
277+
/*!
278+
* @brief Sets the NMEA update rate for GPS data.
279+
* @param nmea_update_rate
280+
* The NMEA update rate, in Hz.
281+
*/
282+
void GPSHardware::SetNmeaUpdateRate(int nmea_update_rate) {
283+
_nmea_update_rate = nmea_update_rate;
284+
}
285+
286+
/*!
287+
* @brief Returns the NMEA port update rate for GPS data.
288+
* @returns The NMEA update rate, in Hz.
289+
*/
290+
int GPSHardware::GetNmeaUpdateRate() { return _nmea_update_rate; }
291+
292+
/*!
293+
* @brief Sets the NMEA baud rate for GPS data.
294+
* @param nmea_baud_rate
295+
* The NMEA baud rate, in bits per second.
296+
*/
297+
void GPSHardware::SetNmeaBaudRate(int nmea_baud_rate) {
298+
_nmea_baud_rate = nmea_baud_rate;
299+
}
300+
301+
/*!
302+
* @brief Returns the NMEA port baud rate for GPS data.
303+
* @returns The NMEA baud rate, in bits per second.
304+
*/
305+
int GPSHardware::GetNmeaBaudRate() { return _nmea_baud_rate; }

src/components/gps/hardware.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#define CMD_MTK_QUERY_FW_RESP \
2323
"$PMTK705" ///< Response from querying MediaTek firmware version without the
2424
///< ReleaseStr
25-
#define MAX_NEMA_SENTENCE_LEN 82 ///< Maximum length of a NMEA sentence
25+
#define DEFAULT_MTK_NMEA_UPDATE_RATE 1 ///< Default NMEA update rate in Hz
26+
#define DEFAULT_MTK_NMEA_BAUD_RATE 9600 ///< Default NMEA baud rate in bits per
27+
#define MAX_NEMA_SENTENCE_LEN 82 ///< Maximum length of a NMEA sentence
2628

2729
class Wippersnapper_V2; ///< Forward declaration
2830
class UARTHardware; ///< Forward declaration
@@ -57,8 +59,13 @@ class GPSHardware {
5759
void SetPollPeriodPrv(ulong poll_period_prv);
5860
ulong GetPollPeriod();
5961
ulong GetPollPeriodPrv();
62+
void SetNmeaUpdateRate(int nmea_update_rate);
63+
int GetNmeaUpdateRate();
64+
void SetNmeaBaudRate(int nmea_baud_rate);
65+
int GetNmeaBaudRate();
6066
// TODO: Add SetInterface(I2C *_i2c_hardware) for I2C support here!
6167
bool Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config);
68+
Adafruit_GPS *GetAdaGps();
6269

6370
private:
6471
bool QueryModuleType();
@@ -68,10 +75,12 @@ class GPSHardware {
6875
GpsDriverType _driver_type; ///< Type of GPS driver used
6976
HardwareSerial *_hw_serial = nullptr; ///< HardwareSerial instance for GPS;
7077
Adafruit_GPS *_ada_gps = nullptr; ///< Adafruit GPS instance
71-
ulong _period; ///< Polling period for GPS data (Specified by IO), in
72-
///< milliseconds
73-
ulong _period_prv; ///< Previous period for GPS data (Specified by IO), in
74-
///< milliseconds
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
82+
int _nmea_update_rate; ///< NMEA update rate for GPS data, in Hz
83+
int _nmea_baud_rate; ///< NMEA baud rate for GPS data, in bits per second
7584
};
7685
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
7786
#endif // WS_GPS_HARDWARE_H

0 commit comments

Comments
 (0)