Skip to content

Commit

Permalink
[GPS] Only send valid GPS coordinates (cyberman54#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Jun 11, 2024
1 parent ac63491 commit a33574a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
10 changes: 5 additions & 5 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ typedef struct {
} MessageBuffer_t;

typedef struct {
int32_t latitude;
int32_t longitude;
uint8_t satellites;
uint16_t hdop;
int16_t altitude;
int32_t latitude{};
int32_t longitude{};
uint8_t satellites{};
uint16_t hdop{};
int16_t altitude{};
} gpsStatus_t;

typedef struct {
Expand Down
3 changes: 2 additions & 1 deletion include/gpsread.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
#endif

extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
extern bool gps_location_isupdated; // Keep track of whether it was updated when showing on the display
extern TaskHandle_t GpsTask;

int gps_init(void);
int gps_config();
bool gps_hasfix();
void gps_storelocation(gpsStatus_t *gps_store);
bool gps_storelocation(gpsStatus_t *gps_store);
void gps_loop(void *pvParameters);
time_t get_gpstime(uint16_t *msec);

Expand Down
8 changes: 8 additions & 0 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ void dp_refresh(bool nextPage) {
// show latitude and longitude
dp_setFont(MY_FONT_STRETCHED);
dp->setCursor(0, MY_DISPLAY_FIRSTLINE);
if (gps.location.isUpdated()) {
// When reading the GPS location, the updated flag is reset.
// However if we also need to send the GPS location data,
// we must know whether there has been an update since the last time
// we sent the location.
gps_location_isupdated = true;
}

dp->printf("%c%09.6f\r\n", gps.location.rawLat().negative ? 'S' : 'N',
gps.location.lat());
dp->printf("%c%09.6f", gps.location.rawLng().negative ? 'W' : 'E',
Expand Down
22 changes: 14 additions & 8 deletions src/gpsread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


TinyGPSPlus gps;
bool gps_location_isupdated = false;
TaskHandle_t GpsTask;
HardwareSerial GPS_Serial(1); // use UART #1

Expand Down Expand Up @@ -142,15 +143,20 @@ int gps_init(void) {
} // gps_init()

// store current GPS location data in struct
void gps_storelocation(gpsStatus_t *gps_store) {
if (gps.location.isUpdated() && gps.location.isValid() &&
(gps.location.age() < 1500)) {
gps_store->latitude = (int32_t)(gps.location.lat() * 1e6);
gps_store->longitude = (int32_t)(gps.location.lng() * 1e6);
gps_store->satellites = (uint8_t)gps.satellites.value();
gps_store->hdop = (uint16_t)gps.hdop.value();
gps_store->altitude = (int16_t)gps.altitude.meters();
bool gps_storelocation(gpsStatus_t *gps_store) {
if (gps.location.isUpdated() || gps_location_isupdated) {
gps_location_isupdated = false;
if (gps.location.isValid() &&
(gps.location.age() < 1500)) {
gps_store->latitude = (int32_t)(gps.location.lat() * 1e6);
gps_store->longitude = (int32_t)(gps.location.lng() * 1e6);
gps_store->satellites = (uint8_t)gps.satellites.value();
gps_store->hdop = (uint16_t)gps.hdop.value();
gps_store->altitude = (int16_t)gps.altitude.meters();
return true;
}
}
return false;
}

bool gps_hasfix() {
Expand Down
14 changes: 8 additions & 6 deletions src/senddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ void sendData() {
if (GPSPORT == COUNTERPORT) {
// send GPS position only if we have a fix
if (gps_hasfix()) {
gps_storelocation(&gps_status);
payload.addGPS(gps_status);
if (gps_storelocation(&gps_status)) {
payload.addGPS(gps_status);
}
} else
ESP_LOGD(TAG, "No valid GPS position");
}
Expand Down Expand Up @@ -134,10 +135,11 @@ void sendData() {
if (GPSPORT != COUNTERPORT) {
// send GPS position only if we have a fix
if (gps_hasfix()) {
gps_storelocation(&gps_status);
payload.reset();
payload.addGPS(gps_status);
SendPayload(GPSPORT);
if (gps_storelocation(&gps_status)) {
payload.reset();
payload.addGPS(gps_status);
SendPayload(GPSPORT);
}
} else
ESP_LOGD(TAG, "No valid GPS position");
}
Expand Down

0 comments on commit a33574a

Please sign in to comment.