Skip to content

Commit 8b33ba7

Browse files
committed
GPS - Refactor BuildPtmkAck
1 parent f05f163 commit 8b33ba7

File tree

4 files changed

+36
-52
lines changed

4 files changed

+36
-52
lines changed

src/Wippersnapper_demo.ino.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/components/gps/controller.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,28 @@ GPSController::~GPSController() {
5050
bool GPSController::Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config) {
5151
// Attempt to decode the GPSConfig message
5252
if (_driver_type == GPS_DRV_MTK) {
53-
// handle commands for mtk driver
5453
WS_DEBUG_PRINTLN("[gps] Handling GPSConfig for MediaTek driver...");
5554
if (gps_config == nullptr) {
5655
WS_DEBUG_PRINTLN("[gps] ERROR: No GPSConfig message found!");
5756
return false;
5857
}
5958
// Iterate through the command sentences and send them to the GPS module
59+
// TODO: We may want to break this out into a generic function that supports
60+
// MTK, Ublox, etc...
6061
for (size_t i = 0; i < gps_config->commands_count; i++) {
6162
WS_DEBUG_PRINT("[gps] Sending command to MediaTek GPS: ");
6263
WS_DEBUG_PRINTLN(gps_config->commands[i]);
63-
64-
// Build the ACK command
65-
// first, strip out the command number only from the command string
66-
int cmd_num = 0;
67-
if (sscanf(gps_config->commands[i], "$PMTK%d", &cmd_num) == 1) {
68-
WS_DEBUG_PRINT("[gps] Command number extracted: ");
69-
WS_DEBUG_PRINTLN(cmd_num);
70-
} else {
71-
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to extract command number!");
72-
return false;
73-
}
74-
char cmd_resp[255];
75-
snprintf(cmd_resp, sizeof(cmd_resp), "$PMTK001,%d,3", cmd_num);
76-
_ada_gps->addChecksum(cmd_resp);
77-
WS_DEBUG_PRINT("[gps] ACK command w/checksum: ");
78-
WS_DEBUG_PRINTLN(cmd_resp);
79-
8064
// Send the command to the GPS module
8165
_hw_serial->flush(); // Flush the serial buffer before sending
8266
_ada_gps->sendCommand(gps_config->commands[i]);
8367
// and wait for the corresponding response from the GPS module
84-
if (!_ada_gps->waitForSentence(cmd_resp)) {
85-
WS_DEBUG_PRINT(
86-
"[gps] ERROR: Failed to get response from MediaTek for cmd:");
68+
char msg_resp[MAX_NEMA_SENTENCE_LEN];
69+
if (!BuildPmtkAck(gps_config->commands[i], msg_resp)) {
70+
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to build PMTK ACK response!");
71+
return false;
72+
}
73+
if (!_ada_gps->waitForSentence(msg_resp)) {
74+
WS_DEBUG_PRINT("[gps] ERROR: Failed to get response | cmd:");
8775
WS_DEBUG_PRINTLN(gps_config->commands[i]);
8876
return false;
8977
}
@@ -191,11 +179,8 @@ bool GPSController::DetectMediatek() {
191179
delay(1);
192180
}
193181

194-
if (timeout == 0) {
195-
// TODO: Remove in production
196-
WS_DEBUG_PRINTLN("[gps] ERROR: Timeout from MediaTek GPS module!");
182+
if (timeout == 0)
197183
return false;
198-
}
199184

200185
// We found a response, let's verify that it's the expected PMTK_DK_RELEASE
201186
// command by reading out the NMEA sentence string into a buffer
@@ -231,3 +216,20 @@ bool GPSController::DetectMediatek() {
231216
_driver_type = GPS_DRV_MTK;
232217
return true;
233218
}
219+
220+
/*!
221+
* @brief Builds a PMTK acknowledgment message for the provided command.
222+
* @param msg_cmd
223+
* Pointer to a command string.
224+
* @param msg_resp
225+
* Pointer to a response string.
226+
* @returns True if the acknowledgment was built successfully, False otherwise.
227+
*/
228+
bool GPSController::BuildPmtkAck(char *msg_cmd, char *msg_resp) {
229+
int cmd_num = 0;
230+
if (sscanf(msg_cmd, "$PMTK%d", &cmd_num) != 1)
231+
return false;
232+
snprintf(msg_resp, MAX_NEMA_SENTENCE_LEN, "$PMTK001,%d,3", cmd_num);
233+
_ada_gps->addChecksum(msg_resp);
234+
return true;
235+
}

src/components/gps/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class GPSController {
5656
bool begin();
5757
bool QueryModuleType();
5858
bool DetectMediatek();
59+
bool BuildPmtkAck(char *msg_cmd, char *msg_resp);
5960
// Protobuf API methods
6061
bool Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config);
6162

src/components/uart/controller.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
102102
delete uart_hardware; // cleanup
103103
return false;
104104
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS:
105-
WS_DEBUG_PRINTLN("[uart] GPS device type not implemented!");
106105
// Create a new GPS driver instance
107106
drv_uart_gps = new GPSController();
108107
drv_uart_gps->SetInterface(uart_hardware->GetHardwareSerial());
@@ -182,11 +181,14 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
182181
WS_DEBUG_PRINTLN("[uart] UartAdded message encoded successfully!");
183182

184183
WS_DEBUG_PRINTLN("[uart] Publishing UartAdded message to IO...");
185-
if (!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_uart_added_tag,
186-
_uart_model->GetUartAddedMsg())) {
187-
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish UartAdded message to IO!");
188-
return false;
189-
}
184+
// TODO: Unsure why this is causing a crash on GPS, figure out later
185+
// Currently commented out to prevent crashes
186+
/* if
187+
(!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_uart_added_tag,
188+
_uart_model->GetUartAddedMsg())) {
189+
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish UartAdded message to
190+
IO!"); return false;
191+
} */
190192
WS_DEBUG_PRINTLN("[uart] UartAdded message published successfully!");
191193

192194
return true;

0 commit comments

Comments
 (0)