Skip to content

Commit ac0cb30

Browse files
author
Nathan Seidle
committed
Add setting storage to NVM and file. Add max log setting. Add enable RAWX setting.
1 parent 59ffc27 commit ac0cb30

File tree

7 files changed

+603
-176
lines changed

7 files changed

+603
-176
lines changed

Firmware/RTK_Enclosed/RTK_Enclosed.ino

Lines changed: 150 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,32 @@
2828
Can we add NTRIP reception over Wifi to the ESP32 to aid in survey in time?
2929
Test lots of bt switching from setup switch. Test for null handles: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos.html
3030
(Done) Transmit battery level, RTK status, etc
31-
32-
Save settings to file
33-
I should be able to start a base anytime and have it log
34-
Test that we can log and do full BT + NTREIP reliably
35-
Update repo
36-
Clean up dirs
31+
(Done) Save settings to file
3732
3833
Menu System:
39-
Test system? Connection to GPS?
34+
(Done) Log RAWX to SD
35+
(Done) Display MAC address / broadcast name
36+
(Done) Test menu
4037
Enable various debug output over BT?
41-
Display MAC address / broadcast name
4238
Change broadcast name + MAC
4339
Change max survey in time before cold start
44-
Allow user to enter permanent coordinates.
45-
Allow user to enable/disable detection of permanent base
40+
Enter permanent coordinates
41+
Enable/disable detection of permanent base
4642
Set radius (5m default) for auto-detection of base
47-
Set nav rate. 4Hz is fun but may drown BT connection. 1Hz seems to be more stable.
48-
If more than 1Hz, turn off SV sentences.
43+
Set update rate
4944
*/
5045

5146
const int FIRMWARE_VERSION_MAJOR = 1;
52-
const int FIRMWARE_VERSION_MINOR = 6;
47+
const int FIRMWARE_VERSION_MINOR = 0;
48+
49+
//Define the RTK Surveyor board identifier:
50+
// This is an int which is unique to this variant of the RTK Surveyor and which allows us
51+
// to make sure that the settings in EEPROM are correct for this version of the RTK Surveyor
52+
// (sizeOfSettings is not necessarily unique and we want to avoid problems when swapping from one variant to another)
53+
// It is the sum of:
54+
// the major firmware version * 0x10
55+
// the minor firmware version
56+
#define RTK_IDENTIFIER (FIRMWARE_VERSION_MAJOR * 0x10 + FIRMWARE_VERSION_MINOR)
5357

5458
#include "settings.h"
5559

@@ -69,19 +73,28 @@ const int batteryLevelLED_Green = 33;
6973
const int batteryLevel_alert = 36;
7074
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7175

76+
//EEPROM for storing settings
77+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
78+
#include <EEPROM.h>
79+
#define EEPROM_SIZE 2048 //ESP32 emulates EEPROM in non-volatile storage (external flash IC). Max is 508k.
80+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
81+
7282
//microSD Interface
7383
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
7484
#include <SPI.h>
7585
#include <SdFat.h> //SdFat (FAT32) by Bill Greiman: http://librarymanager/All#SdFat
7686
SdFat sd;
7787
SdFile gnssDataFile; //File that all gnss data is written to
7888

89+
char settingsFileName[40] = "SFE_Surveyor_Settings.txt"; //File to read/write system settings to
90+
7991
unsigned long lastDataLogSyncTime = 0; //Used to record to SD every half second
92+
long startLogTime_minutes = 0; //Mark when we start logging so we can stop logging after maxLogTime_minutes
8093
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
8194

82-
#include <Wire.h> //Needed for I2C to GPS
95+
#include <Wire.h> //Needed for I2C to GNSS
8396

84-
//GPS configuration
97+
//GNSS configuration
8598
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8699
#define MAX_PAYLOAD_SIZE 384 // Override MAX_PAYLOAD_SIZE for getModuleInfo which can return up to 348 bytes
87100

@@ -150,7 +163,6 @@ uint8_t wBuffer[SERIAL_SIZE_RX]; //Buffer for writing to F9P
150163
MicroOLED oled(PIN_RESET, DC_JUMPER);
151164
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
152165

153-
154166
//Freeze and blink LEDs if we hit a bad error
155167
typedef enum
156168
{
@@ -194,6 +206,7 @@ char deviceName[20]; //The serial string that is broadcast. Ex: 'Surveyor Base-B
194206
const byte menuTimeout = 15; //Menus will exit/timeout after this number of seconds
195207
bool inTestMode = false; //Used to re-route BT traffic while in test sub menu
196208
int battLevel = 0; //SOC measured from fuel gauge, in %
209+
long systemTime_minutes = 0; //Used to test if logging is less than max minutes
197210

198211
uint32_t lastBluetoothLEDBlink = 0;
199212
uint32_t lastRoverUpdate = 0;
@@ -214,114 +227,30 @@ void setup()
214227
GPS.setRxBufferSize(SERIAL_SIZE_RX);
215228
GPS.setTimeout(1);
216229

217-
Wire.begin();
218-
219-
Serial.println("SparkFun RTK Surveyor v1.0");
220-
221-
pinMode(positionAccuracyLED_1cm, OUTPUT);
222-
pinMode(positionAccuracyLED_10cm, OUTPUT);
223-
pinMode(positionAccuracyLED_100cm, OUTPUT);
224-
pinMode(baseStatusLED, OUTPUT);
225-
pinMode(bluetoothStatusLED, OUTPUT);
226-
pinMode(baseSwitch, INPUT_PULLUP); //HIGH = rover, LOW = base
230+
Wire.begin(); //Start I2C
227231

228-
digitalWrite(positionAccuracyLED_1cm, LOW);
229-
digitalWrite(positionAccuracyLED_10cm, LOW);
230-
digitalWrite(positionAccuracyLED_100cm, LOW);
231-
digitalWrite(baseStatusLED, LOW);
232-
digitalWrite(bluetoothStatusLED, LOW);
232+
beginLEDs(); //LED and PWM setup
233233

234-
ledcSetup(ledRedChannel, freq, resolution);
235-
ledcSetup(ledGreenChannel, freq, resolution);
236-
237-
ledcAttachPin(batteryLevelLED_Red, ledRedChannel);
238-
ledcAttachPin(batteryLevelLED_Green, ledGreenChannel);
239-
240-
ledcWrite(ledRedChannel, 0);
241-
ledcWrite(ledGreenChannel, 0);
242-
243-
setupLiPo(); //Configure battery fuel guage monitor
244-
checkBatteryLevels(); //Display initial battery level
245-
246-
//Get unit MAC address
247-
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
248-
unitMACAddress[5] += 2; //Convert MAC address to Bluetooth MAC (add 2): https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/system.html#mac-address
249-
250-
SerialBT.register_callback(btCallback);
251-
if (startBluetooth() == false)
252-
{
253-
Serial.println("An error occurred initializing Bluetooth");
254-
bluetoothState = BT_OFF;
255-
digitalWrite(bluetoothStatusLED, LOW);
256-
}
257-
else
258-
{
259-
bluetoothState = BT_ON_NOCONNECTION;
260-
digitalWrite(bluetoothStatusLED, HIGH);
261-
lastBluetoothLEDBlink = millis();
262-
}
234+
//Start EEPROM and SD for settings, and display for output
235+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
236+
beginEEPROM();
237+
238+
beginSD(); //Test if SD is present
263239

264-
if (myGPS.begin() == false)
265-
{
266-
//Try again with power on delay
267-
delay(1000); //Wait for ZED-F9P to power up before it can respond to ACK
268-
if (myGPS.begin() == false)
269-
{
270-
Serial.println(F("u-blox GPS not detected at default I2C address. Please check wiring."));
271-
blinkError(ERROR_NO_I2C);
272-
}
273-
else
274-
Serial.println(F("u-blox GPS detected"));
275-
}
276-
else
277-
Serial.println(F("u-blox GPS detected"));
240+
loadSettings(); //Attempt to load settings after SD is started so we can read the settings file if available
278241

279-
//Based on Example21_ModuleInfo
280-
if (myGPS.getModuleInfo(1100) == true) // Try to get the module info
281-
{
282-
if (strcmp(myGPS.minfo.extension[1], latestZEDFirmware) != 0)
283-
{
284-
Serial.print("The ZED-F9P appears to have outdated firmware. Found: ");
285-
Serial.println(myGPS.minfo.extension[1]);
286-
Serial.print("The Surveyor works best with ");
287-
Serial.println(latestZEDFirmware);
288-
Serial.print("Please upgrade using u-center.");
289-
Serial.println();
290-
}
291-
else
292-
{
293-
Serial.println("ZED-F9P firmware is current");
294-
}
295-
}
242+
beginDisplay(); //Check if an external Qwiic OLED is attached
243+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
296244

297-
bool response = configureUbloxModule();
298-
if (response == false)
299-
{
300-
//Try once more
301-
Serial.println(F("Failed to configure module. Trying again."));
302-
delay(1000);
303-
response = configureUbloxModule();
245+
beginFuelGauge(); //Configure battery fuel guage monitor
246+
checkBatteryLevels(); //Force display so you see battery level immediately at power on
304247

305-
if (response == false)
306-
{
307-
Serial.println(F("Failed to configure module. Power cycle? Freezing..."));
308-
blinkError(ERROR_GPS_CONFIG_FAIL);
309-
}
310-
}
311-
Serial.println(F("GPS configuration complete"));
248+
beginBT(); //Get MAC, start radio
249+
250+
beginGNSS(); //Connect and configure ZED-F9P
312251

313-
beginSD(); //Test if SD is present
314252
if (online.microSD == true)
315-
{
316253
Serial.println(F("microSD card online"));
317-
}
318-
319-
//Check if an external Qwiic OLED is attached
320-
beginDisplay();
321-
if (online.display == true)
322-
{
323-
Serial.println(F("Display online"));
324-
}
325254

326255
//Display splash of some sort
327256
if (online.display == true)
@@ -330,8 +259,13 @@ void setup()
330259
oled.display();
331260
}
332261

262+
Serial.flush(); //Complete any previous prints
263+
Serial.printf("SparkFun RTK Surveyor v%d.%d\r\n", FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR);
264+
333265
danceLEDs(); //Turn on LEDs like a car dashboard
334266

267+
if(online.microSD == true && settings.zedOutputLogging == true) startLogTime_minutes = 0; //Mark now as start of logging
268+
335269
//myGPS.enableDebugging(); //Enable debug messages over Serial (default)
336270
}
337271

@@ -412,6 +346,9 @@ void loop()
412346
online.dataLogging = false;
413347
}
414348

349+
//Convert current system time to minutes. This is used in F9PSerialReadTask() to see if we are within max log window.
350+
systemTime_minutes = millis() / 1000L / 60;
351+
415352
delay(10); //A small delay prevents panic if no other I2C or functions are called
416353
}
417354

@@ -446,7 +383,7 @@ void updateDisplay()
446383
//oled.setFontType(1);
447384
oled.setFontType(0); //Set font to smallest
448385
oled.setCursor(0, 4);
449-
// oled.print(macAddress);
386+
// oled.print(macAddress);
450387
oled.print("O");
451388

452389
oled.display();
@@ -508,3 +445,100 @@ void beginDisplay()
508445
oled.clear(ALL); // Clear the library's display buffer
509446
}
510447
}
448+
449+
//Connect to and configure ZED-F9P
450+
void beginGNSS()
451+
{
452+
if (myGPS.begin() == false)
453+
{
454+
//Try again with power on delay
455+
delay(1000); //Wait for ZED-F9P to power up before it can respond to ACK
456+
if (myGPS.begin() == false)
457+
{
458+
Serial.println(F("u-blox GNSS not detected at default I2C address. Hard stop."));
459+
blinkError(ERROR_NO_I2C);
460+
}
461+
}
462+
463+
//Check the firmware version of the ZED-F9P. Based on Example21_ModuleInfo.
464+
// if (myGPS.getModuleInfo(1100) == true) // Try to get the module info
465+
// {
466+
// if (strcmp(myGPS.minfo.extension[1], latestZEDFirmware) != 0)
467+
// {
468+
// Serial.print("The ZED-F9P appears to have outdated firmware. Found: ");
469+
// Serial.println(myGPS.minfo.extension[1]);
470+
// Serial.print("The Surveyor works best with ");
471+
// Serial.println(latestZEDFirmware);
472+
// Serial.print("Please upgrade using u-center.");
473+
// Serial.println();
474+
// }
475+
// else
476+
// {
477+
// Serial.println("ZED-F9P firmware is current");
478+
// }
479+
// }
480+
481+
bool response = configureUbloxModule();
482+
if (response == false)
483+
{
484+
//Try once more
485+
Serial.println(F("Failed to configure module. Trying again."));
486+
delay(1000);
487+
response = configureUbloxModule();
488+
489+
if (response == false)
490+
{
491+
Serial.println(F("Failed to configure module. Hard stop."));
492+
blinkError(ERROR_GPS_CONFIG_FAIL);
493+
}
494+
}
495+
Serial.println(F("GNSS configuration complete"));
496+
}
497+
498+
//Get MAC, start radio
499+
void beginBT()
500+
{
501+
//Get unit MAC address
502+
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
503+
unitMACAddress[5] += 2; //Convert MAC address to Bluetooth MAC (add 2): https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/system.html#mac-address
504+
505+
SerialBT.register_callback(btCallback);
506+
if (startBluetooth() == false)
507+
{
508+
Serial.println("An error occurred initializing Bluetooth");
509+
bluetoothState = BT_OFF;
510+
digitalWrite(bluetoothStatusLED, LOW);
511+
}
512+
else
513+
{
514+
bluetoothState = BT_ON_NOCONNECTION;
515+
digitalWrite(bluetoothStatusLED, HIGH);
516+
lastBluetoothLEDBlink = millis();
517+
}
518+
}
519+
520+
//Set LEDs for output and configure PWM
521+
void beginLEDs()
522+
{
523+
pinMode(positionAccuracyLED_1cm, OUTPUT);
524+
pinMode(positionAccuracyLED_10cm, OUTPUT);
525+
pinMode(positionAccuracyLED_100cm, OUTPUT);
526+
pinMode(baseStatusLED, OUTPUT);
527+
pinMode(bluetoothStatusLED, OUTPUT);
528+
pinMode(baseSwitch, INPUT_PULLUP); //HIGH = rover, LOW = base
529+
530+
digitalWrite(positionAccuracyLED_1cm, LOW);
531+
digitalWrite(positionAccuracyLED_10cm, LOW);
532+
digitalWrite(positionAccuracyLED_100cm, LOW);
533+
digitalWrite(baseStatusLED, LOW);
534+
digitalWrite(bluetoothStatusLED, LOW);
535+
536+
ledcSetup(ledRedChannel, freq, resolution);
537+
ledcSetup(ledGreenChannel, freq, resolution);
538+
539+
ledcAttachPin(batteryLevelLED_Red, ledRedChannel);
540+
ledcAttachPin(batteryLevelLED_Green, ledGreenChannel);
541+
542+
ledcWrite(ledRedChannel, 0);
543+
ledcWrite(ledGreenChannel, 0);
544+
}

0 commit comments

Comments
 (0)