|
| 1 | +/* |
| 2 | + Demonstrate how to log RTCM and NMEA data simultaneously |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: February 21st, 2023 |
| 6 | + License: MIT. See license file for more information. |
| 7 | +
|
| 8 | + This example shows how to configure the u-blox GNSS to output an RTCM message automatically |
| 9 | + and log those and any NMEA messages to SD card in UBX format |
| 10 | +
|
| 11 | + ** Please note: this example will only work on processors like the ESP32 which have plenty of RAM available ** |
| 12 | +
|
| 13 | + This code is intended to be run on the ESP32 Thing Plus USB-C |
| 14 | + but can be adapted by changing the chip select pin and SPI definitions: |
| 15 | + https://www.sparkfun.com/products/20168 |
| 16 | + |
| 17 | + Hardware Connections: |
| 18 | + Please see: https://learn.sparkfun.com/tutorials/esp32-thing-plus-usb-c-hookup-guide |
| 19 | + Connect your GNSS breakout to the Thing Plus C using a Qwiic cable. |
| 20 | + Insert a formatted micro-SD card into the socket on the Thing Plus. |
| 21 | + Connect the Thing Plus to your computer using a USB-C cable. |
| 22 | + This code has been tested using version 2.0.5 of the Espressif Systems ESP32 board package on Arduino IDE 1.8.19. |
| 23 | + Select "SparkFun ESP32 Thing Plus C" as the board type. |
| 24 | + Press upload to upload the code onto the ESP32. |
| 25 | + Open the Serial Monitor at 115200 baud to see the output. |
| 26 | +
|
| 27 | + To minimise I2C bus errors, it is a good idea to open the I2C pull-up split pad links on |
| 28 | + the u-blox module breakout. |
| 29 | +
|
| 30 | + Feel like supporting open source hardware? |
| 31 | + Buy a board from SparkFun! |
| 32 | + SparkFun GPS-RTK2 - ZED-F9P (GPS-15136) https://www.sparkfun.com/products/15136 |
| 33 | + SparkFun GPS-RTK-SMA - ZED-F9P (GPS-16481) https://www.sparkfun.com/products/16481 |
| 34 | + SparkFun MAX-M10S Breakout (GPS-18037) https://www.sparkfun.com/products/18037 |
| 35 | + SparkFun ZED-F9K Breakout (GPS-18719) https://www.sparkfun.com/products/18719 |
| 36 | + SparkFun ZED-F9R Breakout (GPS-16344) https://www.sparkfun.com/products/16344 |
| 37 | +
|
| 38 | +*/ |
| 39 | + |
| 40 | +#include "FS.h" |
| 41 | +#include <SPI.h> |
| 42 | +#include <SD.h> |
| 43 | +#include <Wire.h> //Needed for I2C to GNSS |
| 44 | + |
| 45 | +#include <SparkFun_u-blox_GNSS_v3.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_GNSS_v3 |
| 46 | +SFE_UBLOX_GNSS myGNSS; |
| 47 | + |
| 48 | +File myFile; //File that all GNSS data is written to |
| 49 | + |
| 50 | +//Define the microSD (SPI) Chip Select pin. Adjust for your processor if necessary. |
| 51 | +const int sd_cs = 5; //Thing Plus C |
| 52 | + |
| 53 | +#define sdWriteSize 512 // Write data to the SD card in blocks of 512 bytes |
| 54 | +#define fileBufferSize 16384 // Allocate 16KBytes of RAM for UBX message storage |
| 55 | +uint8_t *myBuffer; // A buffer to hold the data while we write it to SD card |
| 56 | + |
| 57 | +unsigned long lastPrint; // Record when the last Serial print took place |
| 58 | +unsigned long bytesWritten = 0; // Record how many bytes have been written to SD card |
| 59 | + |
| 60 | +void setup() |
| 61 | +{ |
| 62 | + delay(1000); |
| 63 | + |
| 64 | + Serial.begin(115200); |
| 65 | + Serial.println("SparkFun u-blox Example"); |
| 66 | + |
| 67 | + pinMode(LED_BUILTIN, OUTPUT); // Flash LED_BUILTIN each time we write to the SD card |
| 68 | + digitalWrite(LED_BUILTIN, LOW); |
| 69 | + |
| 70 | + Wire.begin(); // Start I2C communication |
| 71 | + |
| 72 | + while (Serial.available()) // Make sure the Serial buffer is empty |
| 73 | + { |
| 74 | + Serial.read(); |
| 75 | + } |
| 76 | + |
| 77 | + Serial.println(F("Press any key to start logging.")); |
| 78 | + |
| 79 | + while (!Serial.available()) // Wait for the user to press a key |
| 80 | + { |
| 81 | + ; // Do nothing |
| 82 | + } |
| 83 | + |
| 84 | + delay(100); // Wait, just in case multiple characters were sent |
| 85 | + |
| 86 | + while (Serial.available()) // Empty the Serial buffer |
| 87 | + { |
| 88 | + Serial.read(); |
| 89 | + } |
| 90 | + |
| 91 | + Serial.println("Initializing SD card..."); |
| 92 | + |
| 93 | + // See if the card is present and can be initialized: |
| 94 | + if (!SD.begin(sd_cs)) |
| 95 | + { |
| 96 | + Serial.println("Card failed, or not present. Freezing..."); |
| 97 | + // don't do anything more: |
| 98 | + while (1); |
| 99 | + } |
| 100 | + Serial.println("SD card initialized."); |
| 101 | + |
| 102 | + // Create or open a file called "RTCM_NMEA.ubx" on the SD card. |
| 103 | + // If the file already exists, the new data is appended to the end of the file. |
| 104 | + myFile = SD.open("/RTCM_NMEA.ubx", FILE_WRITE); |
| 105 | + if(!myFile) |
| 106 | + { |
| 107 | + Serial.println(F("Failed to create UBX data file! Freezing...")); |
| 108 | + while (1); |
| 109 | + } |
| 110 | + |
| 111 | + //myGNSS.enableDebugging(); // Uncomment this line to enable lots of helpful GNSS debug messages on Serial |
| 112 | + //myGNSS.enableDebugging(Serial, true); // Or, uncomment this line to enable only the important GNSS debug messages on Serial |
| 113 | + |
| 114 | + // SD cards can occasionally 'hiccup' and a write takes much longer than usual. The buffer needs to be big enough |
| 115 | + // to hold the backlog of data if/when this happens. |
| 116 | + // getMaxFileBufferAvail will tell us the maximum number of bytes which the file buffer has contained. |
| 117 | + myGNSS.setFileBufferSize(fileBufferSize); // setFileBufferSize must be called _before_ .begin |
| 118 | + |
| 119 | + if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port |
| 120 | + { |
| 121 | + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing...")); |
| 122 | + while (1); |
| 123 | + } |
| 124 | + |
| 125 | + // Uncomment the next line if you want to reset your module back to the default settings with 1Hz navigation rate |
| 126 | + // This will (re)enable the standard NMEA messages too |
| 127 | + // This will also disable any "auto" UBX messages that were enabled and saved by other examples and reduce the load on the I2C bus |
| 128 | + //myGNSS.factoryDefault(); delay(5000); |
| 129 | + |
| 130 | + myGNSS.setI2COutput(COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3); //Set the I2C port to output UBX, NMEA and RTCM messages |
| 131 | + |
| 132 | + //myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Optional: save (only) the communications port settings to flash and BBR |
| 133 | + |
| 134 | + myGNSS.setNavigationFrequency(1); //Produce one navigation solution per second |
| 135 | + |
| 136 | + myGNSS.newCfgValset(VAL_LAYER_RAM_BBR); // Use cfgValset to disable / enable individual NMEA messages |
| 137 | + |
| 138 | + myGNSS.addCfgValset8(UBLOX_CFG_MSGOUT_NMEA_ID_GGA_I2C, 1); // Ensure the GxGGA (Global positioning system fix data) message is enabled. Send every measurement. |
| 139 | + myGNSS.addCfgValset8(UBLOX_CFG_MSGOUT_NMEA_ID_GSA_I2C, 1); // Ensure the GxGSA (GNSS DOP and Active satellites) message is enabled. Send every measurement. |
| 140 | + myGNSS.addCfgValset8(UBLOX_CFG_MSGOUT_NMEA_ID_GSV_I2C, 1); // Ensure the GxGSV (GNSS satellites in view) message is enabled. Send every measurement. |
| 141 | + |
| 142 | + myGNSS.addCfgValset(UBLOX_CFG_MSGOUT_RTCM_3X_TYPE4072_0_I2C, 1); // Enable the RTCM 4072-0 message (Reference station PVT (u-blox proprietary)) on I2C |
| 143 | + myGNSS.addCfgValset(UBLOX_CFG_MSGOUT_RTCM_3X_TYPE4072_1_I2C, 1); // Enable the RTCM 4072-1 message (Additional reference station information (u-blox proprietary)) on I2C |
| 144 | + |
| 145 | + myGNSS.sendCfgValset(); // Send the configuration VALSET |
| 146 | + |
| 147 | + myGNSS.setNMEALoggingMask(SFE_UBLOX_FILTER_NMEA_ALL); // Enable logging of all enabled NMEA messages |
| 148 | + //myGNSS.setNMEALoggingMask(SFE_UBLOX_FILTER_NMEA_GGA | SFE_UBLOX_FILTER_NMEA_GSA); // Or we can, for example, log only GxGGA & GxGSA and ignore GxGSV |
| 149 | + |
| 150 | + myGNSS.setRTCMLoggingMask(SFE_UBLOX_FILTER_RTCM_ALL); // Enable logging of all enabled RTCM messages |
| 151 | + //myGNSS.setRTCMLoggingMask(SFE_UBLOX_FILTER_RTCM_4072_0 | SFE_UBLOX_FILTER_RTCM_4072_1); // Or we can, for example, log only RTCM 4072 messages |
| 152 | + |
| 153 | + myBuffer = new uint8_t[sdWriteSize]; // Create our own buffer to hold the data while we write it to SD card |
| 154 | + |
| 155 | + Serial.println(F("Press any key to stop logging.")); |
| 156 | + |
| 157 | + lastPrint = millis(); // Initialize lastPrint |
| 158 | +} |
| 159 | + |
| 160 | +void loop() |
| 161 | +{ |
| 162 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 163 | + |
| 164 | + myGNSS.checkUblox(); // Check for the arrival of new data and process it. |
| 165 | + |
| 166 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 167 | + |
| 168 | + while (myGNSS.fileBufferAvailable() >= sdWriteSize) // Check to see if we have at least sdWriteSize waiting in the buffer |
| 169 | + { |
| 170 | + digitalWrite(LED_BUILTIN, HIGH); // Flash LED_BUILTIN each time we write to the SD card |
| 171 | + |
| 172 | + myGNSS.extractFileBufferData(myBuffer, sdWriteSize); // Extract exactly sdWriteSize bytes from the UBX file buffer and put them into myBuffer |
| 173 | + |
| 174 | + myFile.write(myBuffer, sdWriteSize); // Write exactly sdWriteSize bytes from myBuffer to the ubxDataFile on the SD card |
| 175 | + |
| 176 | + bytesWritten += sdWriteSize; // Update bytesWritten |
| 177 | + |
| 178 | + // In case the SD writing is slow or there is a lot of data to write, keep checking for the arrival of new data |
| 179 | + myGNSS.checkUblox(); // Check for the arrival of new data and process it. |
| 180 | + |
| 181 | + digitalWrite(LED_BUILTIN, LOW); // Turn LED_BUILTIN off again |
| 182 | + } |
| 183 | + |
| 184 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 185 | + |
| 186 | + if (millis() > (lastPrint + 1000)) // Print bytesWritten once per second |
| 187 | + { |
| 188 | + Serial.print(F("The number of bytes written to SD card is ")); // Print how many bytes have been written to SD card |
| 189 | + Serial.println(bytesWritten); |
| 190 | + |
| 191 | + uint16_t maxBufferBytes = myGNSS.getMaxFileBufferAvail(); // Get how full the file buffer has been (not how full it is now) |
| 192 | + |
| 193 | + //Serial.print(F("The maximum number of bytes which the file buffer has contained is: ")); // It is a fun thing to watch how full the buffer gets |
| 194 | + //Serial.println(maxBufferBytes); |
| 195 | + |
| 196 | + if (maxBufferBytes > ((fileBufferSize / 5) * 4)) // Warn the user if fileBufferSize was more than 80% full |
| 197 | + { |
| 198 | + Serial.println(F("Warning: the file buffer has been over 80% full. Some data may have been lost.")); |
| 199 | + } |
| 200 | + |
| 201 | + lastPrint = millis(); // Update lastPrint |
| 202 | + } |
| 203 | + |
| 204 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 205 | + |
| 206 | + if (Serial.available()) // Check if the user wants to stop logging |
| 207 | + { |
| 208 | + uint16_t remainingBytes = myGNSS.fileBufferAvailable(); // Check if there are any bytes remaining in the file buffer |
| 209 | + |
| 210 | + while (remainingBytes > 0) // While there is still data in the file buffer |
| 211 | + { |
| 212 | + digitalWrite(LED_BUILTIN, HIGH); // Flash LED_BUILTIN while we write to the SD card |
| 213 | + |
| 214 | + uint16_t bytesToWrite = remainingBytes; // Write the remaining bytes to SD card sdWriteSize bytes at a time |
| 215 | + if (bytesToWrite > sdWriteSize) |
| 216 | + { |
| 217 | + bytesToWrite = sdWriteSize; |
| 218 | + } |
| 219 | + |
| 220 | + myGNSS.extractFileBufferData(myBuffer, bytesToWrite); // Extract bytesToWrite bytes from the UBX file buffer and put them into myBuffer |
| 221 | + |
| 222 | + myFile.write(myBuffer, bytesToWrite); // Write bytesToWrite bytes from myBuffer to the ubxDataFile on the SD card |
| 223 | + |
| 224 | + bytesWritten += bytesToWrite; // Update bytesWritten |
| 225 | + |
| 226 | + remainingBytes -= bytesToWrite; // Decrement remainingBytes |
| 227 | + } |
| 228 | + |
| 229 | + digitalWrite(LED_BUILTIN, LOW); // Turn LED_BUILTIN off |
| 230 | + |
| 231 | + Serial.print(F("The total number of bytes written to SD card is ")); // Print how many bytes have been written to SD card |
| 232 | + Serial.println(bytesWritten); |
| 233 | + |
| 234 | + myFile.close(); // Close the data file |
| 235 | + |
| 236 | + Serial.println(F("Logging stopped. Freezing...")); |
| 237 | + while(1); // Do nothing more |
| 238 | + } |
| 239 | + |
| 240 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 241 | +} |
0 commit comments