Skip to content

Commit d3afe8a

Browse files
committed
GPS - UBX Parsing OK
1 parent bd4ec63 commit d3afe8a

File tree

10 files changed

+86
-45
lines changed

10 files changed

+86
-45
lines changed

src/components/gps/controller.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ void GPSController::update() {
165165

166166
for (GPSHardware *drv : _gps_drivers) {
167167
// Get the GPS driver interface from the hardware instance
168+
WS_DEBUG_PRINTLN("[gps] Updating GPS driver...");
168169
Adafruit_GPS *ada_gps = nullptr;
169170
if (drv->GetDriverType() == GPS_DRV_MTK) {
170171
// Interface shouldn't matter here because we already set it up in the
@@ -175,6 +176,13 @@ void GPSController::update() {
175176
"[gps] ERROR: Can't read - GPS instance not initialized!");
176177
continue;
177178
}
179+
} else if (drv->GetDriverType() == GPS_DRV_UBLOX) {
180+
SFE_UBLOX_GNSS *sfe_gps = drv->GetUbxGps();
181+
if (sfe_gps == nullptr) {
182+
WS_DEBUG_PRINTLN(
183+
"[gps] ERROR: Can't read - UBLOX instance not initialized!");
184+
continue;
185+
}
178186
} else {
179187
WS_DEBUG_PRINTLN(
180188
"[gps] ERROR: Unsupported GPS driver type, skipping update()!");

src/components/gps/hardware.cpp

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -106,36 +106,54 @@ bool GPSHardware::Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config) {
106106
WS_DEBUG_PRINTLN("[gps] Handling GPSConfig for U-Blox driver...");
107107
// Iterate through the command sentences and send them to the GPS module
108108
for (size_t i = 0; i < gps_config->commands_ubxes_count; i++) {
109+
I2cReadDiscard();
110+
// Write this to the GPS module
109111
// TODO - We are just printing here to debug
110-
WS_DEBUG_PRINT("[gps] UBX CMD #: ");
112+
WS_DEBUG_PRINT("[gps] Sending UBX CMD #: ");
111113
WS_DEBUG_PRINTLN(i);
112-
WS_DEBUG_PRINT("[gps] UBX CMDSZ: ");
114+
WS_DEBUG_PRINT("\tUBX CMDSZ: ");
113115
WS_DEBUG_PRINTLN(gps_config->commands_ubxes[i].size);
114-
WS_DEBUG_PRINT("[gps] UBX CMD (HEX, spaced out): ");
115-
for (pb_size_t i = 0; i < gps_config->commands_ubxes[i].size; i++) {
116-
WS_DEBUG_PRINT(gps_config->commands_ubxes[i].bytes[i], HEX);
116+
WS_DEBUG_PRINT("\tUBX CMD: ");
117+
for (pb_size_t j = 0; j < gps_config->commands_ubxes[i].size; j++) {
118+
WS_DEBUG_PRINT(gps_config->commands_ubxes[i].bytes[j], HEX);
117119
WS_DEBUG_PRINT(" ");
118120
}
119-
// TODO: Push these to the module and ACK
120-
WS_DEBUG_PRINT("[gps] Flushing I2C buffers befor send...");
121-
I2cReadDiscard();
122-
WS_DEBUG_PRINTLN(" done!");
123-
// Write this to the GPS module
124-
WS_DEBUG_PRINT("[gps] TX'ing");
125-
_sfe_gps->pushRawData(
126-
gps_config->commands_ubxes[i].bytes,
127-
gps_config->commands_ubxes[i].size);
128-
WS_DEBUG_PRINTLN(" done!");
121+
if (!_sfe_gps->pushRawData(gps_config->commands_ubxes[i].bytes,
122+
gps_config->commands_ubxes[i].size)) {
123+
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to push bytes to module!");
124+
return false;
125+
}
126+
WS_DEBUG_PRINTLN("Sent!");
129127
// Wait for the ACK response from the GPS module
130-
/* WS_DEBUG_PRINT("[gps] Waiting for ACK response...");
131-
ulong start_time = millis();
132-
while (millis() - start_time < 1000) {
133-
_sfe_gps->checkUblox(); //See if new data is available. Process bytes as they come in.
134-
if (_sfe_gps->processedAck()) {
135-
WS_DEBUG_PRINTLN(" done!");
136-
break; // Exit the loop if ACK is received
137-
}
138-
} */
128+
// Build a dummy packet for the ACK response
129+
uint8_t payloadAck[2];
130+
ubxPacket packetAck = {0,
131+
0,
132+
0,
133+
0,
134+
0,
135+
payloadAck,
136+
0,
137+
0,
138+
SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED,
139+
SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
140+
uint8_t cmdClass = gps_config->commands_ubxes[i].bytes[2];
141+
uint8_t cmdId = gps_config->commands_ubxes[i].bytes[3];
142+
143+
// Wait for ACK response with timeout
144+
sfe_ublox_status_e rc =
145+
_sfe_gps->waitForACKResponse(&packetAck, cmdClass, cmdId, 1000);
146+
WS_DEBUG_PRINT("waitForACKResponse res: ");
147+
WS_DEBUG_PRINTLN(rc);
148+
if (rc == SFE_UBLOX_STATUS_DATA_RECEIVED ||
149+
rc == SFE_UBLOX_STATUS_DATA_SENT) {
150+
WS_DEBUG_PRINTLN("UBX RX'd!");
151+
// TODO: We don't need this, we can just check rc != above instead of ==
152+
// and handle that case only
153+
} else {
154+
WS_DEBUG_PRINTLN("UBX timeout or error!");
155+
return false;
156+
}
139157
}
140158
} else {
141159
WS_DEBUG_PRINTLN("[gps] ERROR: Unsupported GPS driver type!");
@@ -191,7 +209,7 @@ bool GPSHardware::begin() {
191209
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to query GPS module type!");
192210
return false;
193211
}
194-
WS_DEBUG_PRINTLN("[gps] Module detected, ready for commands_pmtks!");
212+
WS_DEBUG_PRINTLN("[gps] Module detected and ready!");
195213
return true;
196214
}
197215

@@ -229,10 +247,18 @@ bool GPSHardware::DetectUbxI2C(uint32_t addr) {
229247
if (!_sfe_gps->begin(*_wire, _addr))
230248
return false;
231249
// Configure the u-blox GPS module
232-
_sfe_gps->setI2COutput(COM_TYPE_UBX | COM_TYPE_NMEA); //Set the I2C port to output both NMEA and UBX messages
233-
_sfe_gps->saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR
234-
_sfe_gps->setProcessNMEAMask(SFE_UBLOX_FILTER_NMEA_ALL); // Make sure the library is passing all NMEA messages to processNMEA
235-
_sfe_gps->setProcessNMEAMask(SFE_UBLOX_FILTER_NMEA_GGA); // Or, we can be kind to MicroNMEA and _only_ pass the GGA messages to it
250+
_sfe_gps->setI2COutput(
251+
COM_TYPE_UBX |
252+
COM_TYPE_NMEA); // Set the I2C port to output both NMEA and UBX messages
253+
_sfe_gps->saveConfigSelective(
254+
VAL_CFG_SUBSEC_IOPORT); // Save (only) the communications port settings to
255+
// flash and BBR
256+
_sfe_gps->setProcessNMEAMask(
257+
SFE_UBLOX_FILTER_NMEA_ALL); // Make sure the library is passing all NMEA
258+
// messages to processNMEA
259+
_sfe_gps->setProcessNMEAMask(
260+
SFE_UBLOX_FILTER_NMEA_GGA); // Or, we can be kind to MicroNMEA and _only_
261+
// pass the GGA messages to it
236262
_driver_type = GPS_DRV_UBLOX;
237263
return true;
238264
}
@@ -243,7 +269,7 @@ bool GPSHardware::DetectUbxI2C(uint32_t addr) {
243269
* @returns True if the driver type was detected successfully, False otherwise.
244270
*/
245271
bool GPSHardware::QueryModuleType() {
246-
WS_DEBUG_PRINTLN("[gps] Attempting to detect GPS module type...");
272+
WS_DEBUG_PRINTLN("[gps] Detecting module type...");
247273
if (_iface_type == GPS_IFACE_UART_HW) {
248274
// Try to detect MediaTek GPS module
249275
if (DetectMtkUart()) {
@@ -407,6 +433,12 @@ ulong GPSHardware::GetPollPeriodPrv() { return _period_prv; }
407433
*/
408434
Adafruit_GPS *GPSHardware::GetAdaGps() { return _ada_gps; }
409435

436+
/*!
437+
* @brief Returns the SFE_UBLOX_GNSS instance.
438+
* @returns Pointer to the SFE_UBLOX_GNSS instance.
439+
*/
440+
SFE_UBLOX_GNSS *GPSHardware::GetUbxGps() { return _sfe_gps; }
441+
410442
/*!
411443
* @brief Sets the NMEA update rate for GPS data.
412444
* @param nmea_update_rate

src/components/gps/hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class GPSHardware {
7474
void SetI2CAddress(uint32_t i2c_address);
7575
bool Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config);
7676
Adafruit_GPS *GetAdaGps();
77+
SFE_UBLOX_GNSS *GetUbxGps();
7778
GpsDriverType GetDriverType();
7879
GpsInterfaceType GetIfaceType();
7980
void I2cReadDiscard();

src/protos/gps.pb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#error Regenerate this file with the current version of nanopb generator.
77
#endif
88

9-
PB_BIND(wippersnapper_gps_GPSConfig, wippersnapper_gps_GPSConfig, 4)
9+
PB_BIND(wippersnapper_gps_GPSConfig, wippersnapper_gps_GPSConfig, 2)
1010

1111

1212
PB_BIND(wippersnapper_gps_GPSDateTime, wippersnapper_gps_GPSDateTime, AUTO)

src/protos/gps.pb.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
#endif
1111

1212
/* Struct definitions */
13-
typedef PB_BYTES_ARRAY_T(512) wippersnapper_gps_GPSConfig_commands_ubxes_t;
13+
typedef PB_BYTES_ARRAY_T(128) wippersnapper_gps_GPSConfig_commands_ubxes_t;
1414
/* *
1515
GPSConfig represents a message containing configuration data to set up and configure a GPS.
1616
Since GPS devices can output lots of data, this message allows users to select which data they want to receive
1717
and a resulting command string to initialize the GPS device with the selected options will be generated. */
1818
typedef struct _wippersnapper_gps_GPSConfig {
1919
/* NOTE: Baud rate is not included here as it is included in the UartAdd->UartSerialConfig message. */
2020
pb_size_t commands_pmtks_count;
21-
char commands_pmtks[16][90]; /* * List of PMTK commands in string format. * */
21+
char commands_pmtks[8][90]; /* * List of PMTK commands in string format. * */
2222
pb_size_t commands_ubxes_count;
23-
wippersnapper_gps_GPSConfig_commands_ubxes_t commands_ubxes[16]; /* * List of UBX commands in bytes format. * */
23+
wippersnapper_gps_GPSConfig_commands_ubxes_t commands_ubxes[8]; /* * List of UBX commands in bytes format. * */
2424
int32_t period; /* * Desired period to poll the GPS module, in milliseconds */
2525
} wippersnapper_gps_GPSConfig;
2626

@@ -77,12 +77,12 @@ extern "C" {
7777
#endif
7878

7979
/* Initializer values for message structs */
80-
#define wippersnapper_gps_GPSConfig_init_default {0, {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}, 0, {{0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}}, 0}
80+
#define wippersnapper_gps_GPSConfig_init_default {0, {"", "", "", "", "", "", "", ""}, 0, {{0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}}, 0}
8181
#define wippersnapper_gps_GPSDateTime_init_default {0, 0, 0, 0, 0, 0, 0}
8282
#define wippersnapper_gps_GPSRMCResponse_init_default {false, wippersnapper_gps_GPSDateTime_init_default, "", "", "", "", "", "", ""}
8383
#define wippersnapper_gps_GPGGAResponse_init_default {false, wippersnapper_gps_GPSDateTime_init_default, "", "", "", "", 0, 0, "", "", ""}
8484
#define wippersnapper_gps_GPSEvent_init_default {0, {wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default, wippersnapper_gps_GPSRMCResponse_init_default}, 0, {wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default, wippersnapper_gps_GPGGAResponse_init_default}}
85-
#define wippersnapper_gps_GPSConfig_init_zero {0, {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}, 0, {{0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}}, 0}
85+
#define wippersnapper_gps_GPSConfig_init_zero {0, {"", "", "", "", "", "", "", ""}, 0, {{0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}}, 0}
8686
#define wippersnapper_gps_GPSDateTime_init_zero {0, 0, 0, 0, 0, 0, 0}
8787
#define wippersnapper_gps_GPSRMCResponse_init_zero {false, wippersnapper_gps_GPSDateTime_init_zero, "", "", "", "", "", "", ""}
8888
#define wippersnapper_gps_GPGGAResponse_init_zero {false, wippersnapper_gps_GPSDateTime_init_zero, "", "", "", "", 0, 0, "", "", ""}
@@ -189,9 +189,9 @@ extern const pb_msgdesc_t wippersnapper_gps_GPSEvent_msg;
189189
#define wippersnapper_gps_GPSEvent_fields &wippersnapper_gps_GPSEvent_msg
190190

191191
/* Maximum encoded size of messages (where known) */
192-
#define WIPPERSNAPPER_GPS_GPS_PB_H_MAX_SIZE wippersnapper_gps_GPSConfig_size
192+
#define WIPPERSNAPPER_GPS_GPS_PB_H_MAX_SIZE wippersnapper_gps_GPSEvent_size
193193
#define wippersnapper_gps_GPGGAResponse_size 168
194-
#define wippersnapper_gps_GPSConfig_size 9707
194+
#define wippersnapper_gps_GPSConfig_size 1787
195195
#define wippersnapper_gps_GPSDateTime_size 77
196196
#define wippersnapper_gps_GPSEvent_size 3130
197197
#define wippersnapper_gps_GPSRMCResponse_size 139

src/protos/i2c.pb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PB_BIND(wippersnapper_i2c_I2cBusScan, wippersnapper_i2c_I2cBusScan, AUTO)
1818
PB_BIND(wippersnapper_i2c_I2cBusScanned, wippersnapper_i2c_I2cBusScanned, 4)
1919

2020

21-
PB_BIND(wippersnapper_i2c_I2cDeviceAddOrReplace, wippersnapper_i2c_I2cDeviceAddOrReplace, 4)
21+
PB_BIND(wippersnapper_i2c_I2cDeviceAddOrReplace, wippersnapper_i2c_I2cDeviceAddOrReplace, 2)
2222

2323

2424
PB_BIND(wippersnapper_i2c_I2cDeviceAddedOrReplaced, wippersnapper_i2c_I2cDeviceAddedOrReplaced, AUTO)

src/protos/i2c.pb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,11 @@ extern const pb_msgdesc_t wippersnapper_i2c_I2cDeviceOutputWrite_msg;
345345
#define wippersnapper_i2c_I2cDeviceOutputWrite_fields &wippersnapper_i2c_I2cDeviceOutputWrite_msg
346346

347347
/* Maximum encoded size of messages (where known) */
348-
#define WIPPERSNAPPER_I2C_I2C_PB_H_MAX_SIZE wippersnapper_i2c_I2cDeviceAddOrReplace_size
348+
#define WIPPERSNAPPER_I2C_I2C_PB_H_MAX_SIZE wippersnapper_i2c_I2cBusScanned_size
349349
#define wippersnapper_i2c_I2cBusDescriptor_size 32
350350
#define wippersnapper_i2c_I2cBusScan_size 42
351351
#define wippersnapper_i2c_I2cBusScanned_size 6242
352-
#define wippersnapper_i2c_I2cDeviceAddOrReplace_size 9837
352+
#define wippersnapper_i2c_I2cDeviceAddOrReplace_size 1917
353353
#define wippersnapper_i2c_I2cDeviceAddedOrReplaced_size 56
354354
#define wippersnapper_i2c_I2cDeviceDescriptor_size 50
355355
#define wippersnapper_i2c_I2cDeviceOutputWrite_size 569

src/protos/signal.pb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#error Regenerate this file with the current version of nanopb generator.
77
#endif
88

9-
PB_BIND(wippersnapper_signal_BrokerToDevice, wippersnapper_signal_BrokerToDevice, 4)
9+
PB_BIND(wippersnapper_signal_BrokerToDevice, wippersnapper_signal_BrokerToDevice, 2)
1010

1111

1212
PB_BIND(wippersnapper_signal_DeviceToBroker, wippersnapper_signal_DeviceToBroker, 4)

src/protos/signal.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ extern const pb_msgdesc_t wippersnapper_signal_DeviceToBroker_msg;
271271

272272
/* Maximum encoded size of messages (where known) */
273273
#if defined(wippersnapper_digitalio_DigitalIOEvent_size) && defined(wippersnapper_digitalio_DigitalIOWrite_size) && defined(wippersnapper_uart_UartAdd_size) && defined(wippersnapper_uart_UartWrite_size)
274-
union wippersnapper_signal_BrokerToDevice_payload_size_union {char f12[(6 + wippersnapper_digitalio_DigitalIOEvent_size)]; char f13[(6 + wippersnapper_digitalio_DigitalIOWrite_size)]; char f80[(7 + wippersnapper_uart_UartAdd_size)]; char f82[(7 + wippersnapper_uart_UartWrite_size)]; char f0[9841];};
274+
union wippersnapper_signal_BrokerToDevice_payload_size_union {char f12[(6 + wippersnapper_digitalio_DigitalIOEvent_size)]; char f13[(6 + wippersnapper_digitalio_DigitalIOWrite_size)]; char f80[(7 + wippersnapper_uart_UartAdd_size)]; char f82[(7 + wippersnapper_uart_UartWrite_size)]; char f0[1921];};
275275
#endif
276276
#if defined(wippersnapper_digitalio_DigitalIOEvent_size) && defined(wippersnapper_analogio_AnalogIOEvent_size) && defined(wippersnapper_ds18x20_Ds18x20Event_size) && defined(wippersnapper_uart_UartInputEvent_size) && defined(wippersnapper_i2c_I2cDeviceEvent_size)
277277
union wippersnapper_signal_DeviceToBroker_payload_size_union {char f10[(6 + wippersnapper_digitalio_DigitalIOEvent_size)]; char f20[(7 + wippersnapper_analogio_AnalogIOEvent_size)]; char f80[(7 + wippersnapper_ds18x20_Ds18x20Event_size)]; char f92[(7 + wippersnapper_uart_UartInputEvent_size)]; char f113[(7 + wippersnapper_i2c_I2cDeviceEvent_size)]; char f0[6246];};

src/protos/uart.pb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ PB_BIND(wippersnapper_uart_TrinamicDynamixelConfig, wippersnapper_uart_TrinamicD
1818
PB_BIND(wippersnapper_uart_PM25AQIConfig, wippersnapper_uart_PM25AQIConfig, AUTO)
1919

2020

21-
PB_BIND(wippersnapper_uart_UartDeviceConfig, wippersnapper_uart_UartDeviceConfig, 4)
21+
PB_BIND(wippersnapper_uart_UartDeviceConfig, wippersnapper_uart_UartDeviceConfig, 2)
2222

2323

24-
PB_BIND(wippersnapper_uart_UartAdd, wippersnapper_uart_UartAdd, 4)
24+
PB_BIND(wippersnapper_uart_UartAdd, wippersnapper_uart_UartAdd, 2)
2525

2626

2727
PB_BIND(wippersnapper_uart_UartAdded, wippersnapper_uart_UartAdded, AUTO)

0 commit comments

Comments
 (0)