Skip to content

Commit 0a2ad72

Browse files
committed
Use callbacks for GNSS data.
1 parent 7d9129b commit 0a2ad72

File tree

9 files changed

+133
-160
lines changed

9 files changed

+133
-160
lines changed

Firmware/RTK_Surveyor/Base.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
//Configure specific aspects of the receiver for base mode
33
bool configureUbloxModuleBase()
44
{
5-
if(online.gnss == false) return(false);
5+
if (online.gnss == false) return (false);
66

77
bool response = true;
88
int maxWait = 2000;
99

1010
//If our settings haven't changed, and this is first config since power on, trust ZED's settings
11-
if(updateZEDSettings == false && firstPowerOn == true)
11+
if (updateZEDSettings == false && firstPowerOn == true)
1212
{
1313
firstPowerOn = false; //Next time user switches modes, new settings will be applied
1414
log_d("Skipping ZED Base configuration");
15-
return(true);
15+
return (true);
1616
}
1717

18+
i2cGNSS.checkUblox(); //Regularly poll to get latest data and any RTCM
19+
1820
//The first thing we do is go to 1Hz to lighten any I2C traffic from a previous configuration
1921
if (i2cGNSS.getNavigationFrequency(maxWait) != 1)
2022
response &= i2cGNSS.setNavigationFrequency(1, maxWait);
23+
// response &= i2cGNSS.setNavigationFrequency(4, maxWait);
2124
if (response == false)
2225
Serial.println(F("Set rate failed"));
23-
26+
2427
i2cGNSS.checkUblox(); //Regularly poll to get latest data and any RTCM
2528

2629
if (i2cGNSS.getSurveyInActive() == true)

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ void beginDisplay()
335335

336336
Serial.println(F("Display started"));
337337
displaySplash();
338+
splashStart = millis();
338339
}
339340
else
340341
{
@@ -415,6 +416,9 @@ void beginGNSS()
415416
//Configuration can take >1s so configure during splash
416417
void configureGNSS()
417418
{
419+
i2cGNSS.setAutoPVTcallbackPtr(&storePVTdata); // Enable automatic NAV PVT messages with callback to storePVTdata
420+
i2cGNSS.setAutoHPPOSLLHcallbackPtr(&storeHPdata); // Enable automatic NAV HPPOSLLH messages with callback to storeHPdata
421+
418422
//Configuring the ZED can take more than 2000ms. We save configuration to
419423
//ZED so there is no need to update settings unless user has modified
420424
//the settings file or internal settings.

Firmware/RTK_Surveyor/Display.ino

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -305,31 +305,27 @@ void paintHorizontalAccuracy()
305305
oled.setFont(QW_FONT_8X16); //Set font to type 1: 8x16
306306
oled.setCursor(16, 20); //x, y
307307
oled.print(":");
308-
float hpa = 100;
309-
310-
if (online.gnss == true)
311-
hpa = i2cGNSS.getHorizontalAccuracy() / 10000.0;
312308

313309
if (online.gnss == false)
314310
{
315311
oled.print(F("N/A"));
316312
}
317-
else if (hpa > 30.0)
313+
else if (horizontalAccuracy > 30.0)
318314
{
319315
oled.print(F(">30m"));
320316
}
321-
else if (hpa > 9.9)
317+
else if (horizontalAccuracy > 9.9)
322318
{
323-
oled.print(hpa, 1); //Print down to decimeter
319+
oled.print(horizontalAccuracy, 1); //Print down to decimeter
324320
}
325-
else if (hpa > 1.0)
321+
else if (horizontalAccuracy > 1.0)
326322
{
327-
oled.print(hpa, 2); //Print down to centimeter
323+
oled.print(horizontalAccuracy, 2); //Print down to centimeter
328324
}
329325
else
330326
{
331327
oled.print("."); //Remove leading zero
332-
oled.printf("%03d", (int)(hpa * 1000)); //Print down to millimeter
328+
oled.printf("%03d", (int)(horizontalAccuracy * 1000)); //Print down to millimeter
333329
}
334330
}
335331
}
@@ -484,7 +480,8 @@ void paintSIV()
484480
if (online.display == true && online.gnss == true)
485481
{
486482
//Blink satellite dish icon if we don't have a fix
487-
if (i2cGNSS.getFixType() == 3 || i2cGNSS.getFixType() == 4 || i2cGNSS.getFixType() == 5) //3D, 3D+DR, or Time
483+
uint8_t fixType = fixType;
484+
if (fixType == 3 || fixType == 4 || fixType == 5) //3D, 3D+DR, or Time
488485
{
489486
//Fix, turn on icon
490487
displayBitmap(2, 35, SIV_Antenna_Width, SIV_Antenna_Height, SIV_Antenna);
@@ -511,13 +508,13 @@ void paintSIV()
511508
oled.setCursor(16, 36); //x, y
512509
oled.print(":");
513510

514-
if (i2cGNSS.getFixType() == 0) //0 = No Fix
511+
if (fixType == 0) //0 = No Fix
515512
{
516513
oled.print("0");
517514
}
518515
else
519516
{
520-
oled.print(i2cGNSS.getSIV());
517+
oled.print(numSV);
521518
}
522519

523520
paintResets();
@@ -1455,8 +1452,10 @@ void paintSystemTest()
14551452
oled.print(F("GNSS:"));
14561453
if (online.gnss == true)
14571454
{
1458-
i2cGNSS.checkUblox();
1459-
int satsInView = i2cGNSS.getSIV();
1455+
i2cGNSS.checkUblox(); //Regularly poll to get latest data and any RTCM
1456+
i2cGNSS.checkCallbacks(); //Process any callbacks: ie, eventTriggerReceived
1457+
1458+
int satsInView = numSV;
14601459
if (satsInView > 5)
14611460
{
14621461
oled.print(F("OK"));

Firmware/RTK_Surveyor/NVM.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ uint8_t getProfileNumber()
9595
{
9696
log_d("profileNumber.txt not found");
9797
profileNumber = 0;
98+
updateZEDSettings = true; //Force module update
9899
recordProfileNumber(profileNumber);
99100
}
100101
else

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ int pin_radio_rts;
105105

106106
//LittleFS for storing settings for different user profiles
107107
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
108-
#include "FS.h"
109108
#include <LittleFS.h>
110109
#define FORMAT_LITTLEFS_IF_FAILED true
111110

@@ -198,6 +197,27 @@ SFE_UBLOX_GNSS_ADD i2cGNSS;
198197
#define MAX_PAYLOAD_SIZE 384 // Override MAX_PAYLOAD_SIZE for getModuleInfo which can return up to 348 bytes
199198
#endif
200199
uint8_t settingPayload[MAX_PAYLOAD_SIZE];
200+
201+
//These globals are updated regularly via the storePVTdata callback
202+
bool ubloxUpdated = false;
203+
double latitude;
204+
double longitude;
205+
float altitude;
206+
float horizontalAccuracy;
207+
bool validDate;
208+
bool validTime;
209+
bool confirmedDate;
210+
bool confirmedTime;
211+
uint8_t gnssDay;
212+
uint8_t gnssMonth;
213+
uint16_t gnssYear;
214+
uint8_t gnssHour;
215+
uint8_t gnssMinute;
216+
uint8_t gnssSecond;
217+
uint16_t mseconds;
218+
uint8_t numSV;
219+
uint8_t fixType;
220+
uint8_t carrSoln;
201221
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
202222

203223
//Battery fuel gauge and PWM LEDs
@@ -401,6 +421,7 @@ unsigned int binBytesSent = 0; //Tracks firmware bytes sent over WiFi OTA update
401421
int binBytesLastUpdate = 0; //Allows websocket notification to be sent every 100k bytes
402422
bool updateZEDSettings = false; //If settings from file are different from LittleFS, config the ZED
403423
bool firstPowerOn = true; //After boot, apply new settings to ZED if user switches between base or rover
424+
unsigned long splashStart = 0; //Controls how long the splash is displayed for. Currently min of 2s.
404425

405426
unsigned long startTime = 0; //Used for checking longest running functions
406427
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
@@ -619,18 +640,18 @@ void updateRTC()
619640
{
620641
lastRTCAttempt = millis();
621642

622-
i2cGNSS.checkUblox();
623-
624643
bool timeValid = false;
625-
if (i2cGNSS.getTimeValid() == true && i2cGNSS.getDateValid() == true) //Will pass if ZED's RTC is reporting (regardless of GNSS fix)
644+
if (validTime == true && validDate == true) //Will pass if ZED's RTC is reporting (regardless of GNSS fix)
626645
timeValid = true;
627-
if (i2cGNSS.getConfirmedTime() == true && i2cGNSS.getConfirmedDate() == true) //Requires GNSS fix
646+
if (confirmedTime == true && confirmedDate == true) //Requires GNSS fix
628647
timeValid = true;
629648

630649
if (timeValid == true)
631650
{
632651
//Set the internal system time
633652
//This is normally set with WiFi NTP but we will rarely have WiFi
653+
//rtc.setTime(gnssSecond, gnssMinute, gnssHour, gnssDay, gnssMonth, gnssYear); // 17th Jan 2021 15:24:30
654+
i2cGNSS.checkUblox();
634655
rtc.setTime(i2cGNSS.getSecond(), i2cGNSS.getMinute(), i2cGNSS.getHour(), i2cGNSS.getDay(), i2cGNSS.getMonth(), i2cGNSS.getYear()); // 17th Jan 2021 15:24:30
635656

636657
online.rtc = true;

Firmware/RTK_Surveyor/Rover.ino

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ bool configureUbloxModuleRover()
88
int maxWait = 2000;
99

1010
//If our settings haven't changed, and this is first config since power on, trust ZED's settings
11-
if(updateZEDSettings == false && firstPowerOn == true)
11+
if (updateZEDSettings == false && firstPowerOn == true)
1212
{
1313
firstPowerOn = false; //Next time user switches modes, new settings will be applied
1414
log_d("Skipping ZED Rover configuration");
@@ -267,39 +267,33 @@ void updateAccuracyLEDs()
267267

268268
if (online.gnss == true)
269269
{
270-
uint32_t accuracy = i2cGNSS.getHorizontalAccuracy();
271-
272-
if (accuracy > 0)
270+
if (horizontalAccuracy > 0)
273271
{
274-
// Convert the horizontal accuracy (mm * 10^-1) to a float
275-
float f_accuracy = accuracy;
276-
f_accuracy = f_accuracy / 10000.0; // Convert from mm * 10^-1 to m
277-
278272
Serial.print(F("Rover Accuracy (m): "));
279-
Serial.print(f_accuracy, 4); // Print the accuracy with 4 decimal places
273+
Serial.print(horizontalAccuracy, 4); // Print the accuracy with 4 decimal places
280274
Serial.println();
281275

282276
if (productVariant == RTK_SURVEYOR)
283277
{
284-
if (f_accuracy <= 0.02)
278+
if (horizontalAccuracy <= 0.02)
285279
{
286280
digitalWrite(pin_positionAccuracyLED_1cm, HIGH);
287281
digitalWrite(pin_positionAccuracyLED_10cm, HIGH);
288282
digitalWrite(pin_positionAccuracyLED_100cm, HIGH);
289283
}
290-
else if (f_accuracy <= 0.100)
284+
else if (horizontalAccuracy <= 0.100)
291285
{
292286
digitalWrite(pin_positionAccuracyLED_1cm, LOW);
293287
digitalWrite(pin_positionAccuracyLED_10cm, HIGH);
294288
digitalWrite(pin_positionAccuracyLED_100cm, HIGH);
295289
}
296-
else if (f_accuracy <= 1.0000)
290+
else if (horizontalAccuracy <= 1.0000)
297291
{
298292
digitalWrite(pin_positionAccuracyLED_1cm, LOW);
299293
digitalWrite(pin_positionAccuracyLED_10cm, LOW);
300294
digitalWrite(pin_positionAccuracyLED_100cm, HIGH);
301295
}
302-
else if (f_accuracy > 1.0)
296+
else if (horizontalAccuracy > 1.0)
303297
{
304298
digitalWrite(pin_positionAccuracyLED_1cm, LOW);
305299
digitalWrite(pin_positionAccuracyLED_10cm, LOW);
@@ -310,12 +304,48 @@ void updateAccuracyLEDs()
310304
else
311305
{
312306
Serial.print(F("Rover Accuracy: "));
313-
Serial.print(accuracy);
307+
Serial.print(horizontalAccuracy);
314308
Serial.print(" ");
315309
Serial.print(F("No lock. SIV: "));
316-
Serial.print(i2cGNSS.getSIV());
310+
Serial.print(numSV);
317311
Serial.println();
318312
}
319313
} //End GNSS online checking
320314
} //Check every 2000ms
321315
}
316+
317+
//These are the callbacks that get regularly called, globals are updated
318+
void storePVTdata(UBX_NAV_PVT_data_t *ubxDataStruct)
319+
{
320+
altitude = ubxDataStruct->height / 1000.0;
321+
322+
gnssDay = ubxDataStruct->day;
323+
gnssMonth = ubxDataStruct->month;
324+
gnssYear = ubxDataStruct->year;
325+
326+
gnssHour = ubxDataStruct->hour;
327+
gnssMinute = ubxDataStruct->min;
328+
gnssSecond = ubxDataStruct->sec;
329+
mseconds = ceil((ubxDataStruct->iTOW % 1000) / 10.0); //Limit to first two digits
330+
331+
numSV = ubxDataStruct->numSV;
332+
fixType = ubxDataStruct->fixType;
333+
carrSoln = ubxDataStruct->flags.bits.carrSoln;
334+
335+
validDate = ubxDataStruct->valid.bits.validDate;
336+
validTime = ubxDataStruct->valid.bits.validTime;
337+
confirmedDate = ubxDataStruct->flags2.bits.confirmedDate;
338+
confirmedTime = ubxDataStruct->flags2.bits.confirmedTime;
339+
340+
ubloxUpdated = true;
341+
}
342+
343+
void storeHPdata(UBX_NAV_HPPOSLLH_data_t *ubxDataStruct)
344+
{
345+
horizontalAccuracy = ((float)ubxDataStruct->hAcc) / 10000.0; // Convert hAcc from mm*0.1 to m
346+
347+
latitude = ((double)ubxDataStruct->lat) / 10000000.0;
348+
latitude += ((double)ubxDataStruct->latHp) / 1000000000.0;
349+
longitude = ((double)ubxDataStruct->lon) / 10000000.0;
350+
longitude += ((double)ubxDataStruct->lonHp) / 1000000000.0;
351+
}

Firmware/RTK_Surveyor/States.ino

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void updateSystemState()
7171

7272
case (STATE_ROVER_NO_FIX):
7373
{
74-
if (i2cGNSS.getFixType() == 3 || i2cGNSS.getFixType() == 4) //3D, 3D+DR
74+
if (fixType == 3 || fixType == 4) //3D, 3D+DR
7575
changeState(STATE_ROVER_FIX);
7676
}
7777
break;
@@ -80,10 +80,9 @@ void updateSystemState()
8080
{
8181
updateAccuracyLEDs();
8282

83-
byte rtkType = i2cGNSS.getCarrierSolutionType();
84-
if (rtkType == 1) //RTK Float
83+
if (carrSoln == 1) //RTK Float
8584
changeState(STATE_ROVER_RTK_FLOAT);
86-
else if (rtkType == 2) //RTK Fix
85+
else if (carrSoln == 2) //RTK Fix
8786
changeState(STATE_ROVER_RTK_FIX);
8887
}
8988
break;
@@ -92,10 +91,9 @@ void updateSystemState()
9291
{
9392
updateAccuracyLEDs();
9493

95-
byte rtkType = i2cGNSS.getCarrierSolutionType();
96-
if (rtkType == 0) //No RTK
94+
if (carrSoln == 0) //No RTK
9795
changeState(STATE_ROVER_FIX);
98-
if (rtkType == 2) //RTK Fix
96+
if (carrSoln == 2) //RTK Fix
9997
changeState(STATE_ROVER_RTK_FIX);
10098
}
10199
break;
@@ -104,10 +102,9 @@ void updateSystemState()
104102
{
105103
updateAccuracyLEDs();
106104

107-
byte rtkType = i2cGNSS.getCarrierSolutionType();
108-
if (rtkType == 0) //No RTK
105+
if (carrSoln == 0) //No RTK
109106
changeState(STATE_ROVER_FIX);
110-
if (rtkType == 1) //RTK Float
107+
if (carrSoln == 1) //RTK Float
111108
changeState(STATE_ROVER_RTK_FLOAT);
112109
}
113110
break;
@@ -176,14 +173,9 @@ void updateSystemState()
176173
}
177174

178175
//Check for <1m horz accuracy before starting surveyIn
179-
uint32_t accuracy = i2cGNSS.getHorizontalAccuracy();
176+
Serial.printf("Waiting for Horz Accuracy < %0.2f meters: %0.2f\n\r", settings.surveyInStartingAccuracy, horizontalAccuracy);
180177

181-
float f_accuracy = accuracy;
182-
f_accuracy = f_accuracy / 10000.0; // Convert the horizontal accuracy (mm * 10^-1) to a float
183-
184-
Serial.printf("Waiting for Horz Accuracy < %0.2f meters: %0.2f\n\r", settings.surveyInStartingAccuracy, f_accuracy);
185-
186-
if (f_accuracy > 0.0 && f_accuracy < settings.surveyInStartingAccuracy)
178+
if (horizontalAccuracy > 0.0 && horizontalAccuracy < settings.surveyInStartingAccuracy)
187179
{
188180
displaySurveyStart(0); //Show 'Survey'
189181

@@ -210,10 +202,10 @@ void updateSystemState()
210202
}
211203

212204
//Get the data once to avoid duplicate slow responses
213-
svinObservationTime = i2cGNSS.getSurveyInObservationTime(100);
214-
svinMeanAccuracy = i2cGNSS.getSurveyInMeanAccuracy(100);
205+
svinObservationTime = i2cGNSS.getSurveyInObservationTime(50);
206+
svinMeanAccuracy = i2cGNSS.getSurveyInMeanAccuracy(50);
215207

216-
if (i2cGNSS.getSurveyInValid(100) == true) //Survey in complete
208+
if (i2cGNSS.getSurveyInValid(50) == true) //Survey in complete
217209
{
218210
Serial.printf("Observation Time: %d\n\r", svinObservationTime);
219211
Serial.println(F("Base survey complete! RTCM now broadcasting."));
@@ -231,7 +223,7 @@ void updateSystemState()
231223
Serial.print(F(" Accuracy: "));
232224
Serial.print(svinMeanAccuracy);
233225
Serial.print(F(" SIV: "));
234-
Serial.print(i2cGNSS.getSIV());
226+
Serial.print(numSV);
235227
Serial.println();
236228

237229
if (svinObservationTime > maxSurveyInWait_s)

0 commit comments

Comments
 (0)