Skip to content

Commit c4aca50

Browse files
committed
GPS - Fill RMC, datetime setters
1 parent a2ab695 commit c4aca50

File tree

3 files changed

+113
-30
lines changed

3 files changed

+113
-30
lines changed

src/components/gps/controller.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ void GPSController::update() {
128128

129129
for (GPSHardware *drv : _gps_drivers) {
130130

131-
// TODO: Commented out due to parsing failures, stability issue (failed to parse NMEA acks for this)
132-
// Perform a keep-alive check by sending an antenna check command every 2
133-
// seconds
134-
/* if (millis() - drv->GetPrvKat() > 2000) {
135-
drv->GetAdaGps()->sendCommand(CMD_MTK_CHECK_ANTENNA);
136-
drv->SetPrvKat(millis());
137-
} */
131+
// TODO: Commented out due to parsing failures, stability issue (failed to
132+
// parse NMEA acks for this) Perform a keep-alive check by sending an
133+
// antenna check command every 2 seconds
134+
/* if (millis() - drv->GetPrvKat() > 2000) {
135+
drv->GetAdaGps()->sendCommand(CMD_MTK_CHECK_ANTENNA);
136+
drv->SetPrvKat(millis());
137+
} */
138138

139139
// Did read period elapse?
140140
ulong cur_time = millis();
@@ -224,7 +224,7 @@ void GPSController::update() {
224224
Serial.println((int)drv->GetAdaGps()->antenna);
225225
}
226226

227-
// TODO: Okay so here we are going to create the model of gps data
227+
// TODO: Okay so here we are going to create the model of gps data
228228
}
229229
WS_DEBUG_PRINTLN("[gps] Finished processing NMEA sentences.");
230230
// TODO: Successfully parsed the NMEA sentence, update the model

src/components/gps/model.cpp

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@
1919
*/
2020
GPSModel::GPSModel() {
2121
memset(&_msg_gps_config, 0, sizeof(_msg_gps_config));
22-
memset(&_msg_gpgga_response, 0, sizeof(_msg_gpgga_response));
23-
memset(&_msg_gpsrmc_response, 0, sizeof(_msg_gpsrmc_response));
22+
memset(&_msg_gps_event, 0, sizeof(_msg_gps_event));
2423
}
2524

2625
/*!
2726
* @brief Destructor for a GPSModel object.
2827
*/
2928
GPSModel::~GPSModel() {
3029
memset(&_msg_gps_config, 0, sizeof(_msg_gps_config));
31-
memset(&_msg_gpgga_response, 0, sizeof(_msg_gpgga_response));
32-
memset(&_msg_gpsrmc_response, 0, sizeof(_msg_gpsrmc_response));
30+
memset(&_msg_gps_event, 0, sizeof(_msg_gps_event));
3331
}
3432

3533
/*!
3634
* @brief Decodes a GPSConfig message from an input stream.
3735
* @param stream A pointer to the pb_istream_t stream.
38-
* @returns True if the GPSConfig message was decoded successfully, False otherwise.
36+
* @returns True if the GPSConfig message was decoded successfully, False
37+
* otherwise.
3938
*/
4039
bool GPSModel::DecodeGPSConfig(pb_istream_t *stream) {
41-
return pb_decode(stream, wippersnapper_gps_GPSConfig_fields, &_msg_gps_config);
40+
return pb_decode(stream, wippersnapper_gps_GPSConfig_fields,
41+
&_msg_gps_config);
4242
}
4343

4444
/*!
@@ -50,25 +50,94 @@ wippersnapper_gps_GPSConfig *GPSModel::GetGPSConfigMsg() {
5050
}
5151

5252
/*!
53-
* @brief Encodes a GPGGA response message.
54-
* @returns True if the GPGGA response message was encoded successfully, False otherwise.
53+
* @brief Creates a new GPSEvent message and initializes it.
54+
* @NOTE: This function will clear an existing GPSEvent message. Only call when
55+
* you are creating a NEW gps event, not modifying an existing one.
5556
*/
56-
bool GPSModel::EncodeGPGGAResponse() {
57-
// TODO: Implement the encoding logic for GPGGAResponse
58-
return false;
57+
void GPSModel::CreateGPSEvent() {
58+
// Zero-out whatever was previously in the GPSEvent message
59+
memset(&_msg_gps_event, 0, sizeof(_msg_gps_event));
60+
// Create new GPSEvent message with initializer
61+
_msg_gps_event = wippersnapper_gps_GPSEvent_init_zero;
62+
_msg_gps_event.gga_responses_count = 0;
63+
_msg_gps_event.rmc_responses_count = 0;
5964
}
6065

6166
/*!
62-
* @brief Returns a pointer to the GPGGA response message.
63-
* @returns Pointer to the GPGGA response message.
67+
* @brief Creates a GPSDateTime message with the provided parameters.
68+
* @param hour GMT hour of the day (0-23).
69+
* @param minute GMT minute of the hour (0-59).
70+
* @param seconds GMT seconds of the minute (0-59).
71+
* @param milliseconds GMT milliseconds (0-999).
72+
* @param day GMT day of the month (1-31).
73+
* @param month GMT month of the year (1-12).
74+
* @param year GMT year (e.g., 25).
75+
* @returns A wippersnapper_gps_GPSDateTime message.
6476
*/
65-
wippersnapper_gps_GPGGAResponse *GPSModel::GetGPGGAResponseMsg() {
66-
return &_msg_gpgga_response;
77+
wippersnapper_gps_GPSDateTime
78+
GPSModel::CreateGpsDatetime(uint8_t hour, uint8_t minute, uint8_t seconds,
79+
uint8_t milliseconds, uint8_t day, uint8_t month,
80+
uint8_t year) {
81+
wippersnapper_gps_GPSDateTime datetime;
82+
// Fill in the datetime structure with the provided values
83+
datetime.hour = (uint32_t)(hour);
84+
datetime.minute = (uint32_t)(minute);
85+
datetime.seconds = (uint32_t)(seconds);
86+
datetime.milliseconds = (uint32_t)(milliseconds);
87+
datetime.day = (uint32_t)(day);
88+
datetime.month = (uint32_t)(month);
89+
datetime.year = (uint32_t)(year);
90+
return datetime;
6791
}
6892

69-
/*!
70-
* @brief Gets the GPGGA response message.
71-
*/
72-
wippersnapper_gps_GPSRMCResponse *GPSModel::GetGPSRMCResponseMsg() {
73-
return &_msg_gpsrmc_response;
74-
}
93+
bool GPSModel::AddGpsEventRMC(wippersnapper_gps_GPSDateTime *datetime,
94+
uint8_t fix_status, nmea_float_t latitude,
95+
char *lat_dir, nmea_float_t longitude,
96+
char *lon_dir, nmea_float_t speed,
97+
nmea_float_t angle) {
98+
// Check if we've reached the maximum number of RMC responses
99+
if (_msg_gps_event.rmc_responses_count >= MAX_COUNT_RMC_GGA) {
100+
return false; // Cannot add more RMC responses
101+
}
102+
103+
// Validate pointers have been provided correctly
104+
if (!lat_dir || !lon_dir) {
105+
return false;
106+
}
107+
108+
wippersnapper_gps_GPSRMCResponse rmc_response;
109+
rmc_response = wippersnapper_gps_GPSRMCResponse_init_zero;
110+
// Assign the datetime, if provided
111+
if (datetime) {
112+
rmc_response.has_datetime = true;
113+
rmc_response.datetime = *datetime;
114+
} else {
115+
rmc_response.has_datetime = false;
116+
}
117+
118+
// Determine the fix status
119+
if (fix_status == 1 || fix_status == 2) {
120+
rmc_response.fix_status[0] = 'A'; // Active fix
121+
} else {
122+
rmc_response.fix_status[0] = 'V'; // Void fix
123+
}
124+
rmc_response.fix_status[1] = '\0';
125+
126+
// Fill lat/lon and direction
127+
snprintf(rmc_response.lat, sizeof(rmc_response.lat), "%.6f", latitude);
128+
snprintf(rmc_response.lon, sizeof(rmc_response.lon), "%.6f", longitude);
129+
strncpy(rmc_response.lat_dir, lat_dir, sizeof(rmc_response.lat_dir) - 1);
130+
rmc_response.lat_dir[sizeof(rmc_response.lat_dir) - 1] = '\0';
131+
strncpy(rmc_response.lon_dir, lon_dir, sizeof(rmc_response.lon_dir) - 1);
132+
rmc_response.lon_dir[sizeof(rmc_response.lon_dir) - 1] = '\0';
133+
134+
// Fill current speed over ground, in knots
135+
snprintf(rmc_response.speed, sizeof(rmc_response.speed), "%.1f", speed);
136+
// Fill course in degrees from true north
137+
snprintf(rmc_response.angle, sizeof(rmc_response.angle) "%.1f", angle);
138+
139+
_msg_gps_event.rmc_responses[_msg_gps_event.rmc_responses_count] =
140+
rmc_response;
141+
_msg_gps_event.rmc_responses_count++;
142+
return true;
143+
}

src/components/gps/model.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define WS_GPS_MODEL_H
1717
#include "Wippersnapper_V2.h"
1818
#include <protos/gps.pb.h>
19+
#define MAX_COUNT_RMC_GGA 10; ///< Maximum number of RMC or GGA responses
1920

2021
/*!
2122
@brief Provides an interface for creating, encoding, and parsing
@@ -27,7 +28,20 @@ class GPSModel {
2728
~GPSModel();
2829
bool DecodeGPSConfig(pb_istream_t *stream);
2930
wippersnapper_gps_GPSConfig *GetGPSConfigMsg();
31+
// GPSEvent API
32+
void CreateGPSEvent();
33+
wippersnapper_gps_GPSDateTime CreateGpsDatetime(uint8_t hour, uint8_t minute,
34+
uint8_t seconds,
35+
uint8_t milliseconds,
36+
uint8_t day, uint8_t month,
37+
uint8_t year);
38+
bool AddGpsEventRMC(wippersnapper_gps_GPSDateTime *datetime,
39+
uint8_t fix_status, nmea_float_t latitude, char *lat_dir,
40+
nmea_float_t longitude, char *lon_dir, nmea_float_t speed,
41+
nmea_float_t angle);
42+
3043
private:
31-
wippersnapper_gps_GPSConfig _msg_gps_config; ///< GPS configuration message
44+
wippersnapper_gps_GPSConfig _msg_gps_config; ///< GPS configuration message
45+
wippersnapper_gps_GPSEvent _msg_gps_event; ///< GPS event message
3246
};
3347
#endif // WS_GPS_MODEL_H

0 commit comments

Comments
 (0)