Skip to content

Commit c97f192

Browse files
authored
Merge pull request #7 from sparkfun/release_candidate
Update
2 parents c912fce + 1220ef2 commit c97f192

File tree

5 files changed

+305
-42
lines changed

5 files changed

+305
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
Demonstrate how to log NMEA and UBX data simultaneously
3+
By: Paul Clark
4+
SparkFun Electronics
5+
Date: April 13th, 2021
6+
License: MIT. See license file for more information but you can
7+
basically do whatever you want with this code.
8+
9+
This example shows how to configure the u-blox GNSS to send PVT reports automatically
10+
and log those and any incoming NMEA messages to SD card in UBX format
11+
12+
** Please note: this example will only work on processors like the Artemis which have plenty of RAM available **
13+
14+
This code is intended to be run on the MicroMod Data Logging Carrier Board using the Artemis Processor
15+
but can be adapted by changing the chip select pin and SPI definitions:
16+
https://www.sparkfun.com/products/16829
17+
https://www.sparkfun.com/products/16401
18+
19+
Hardware Connections:
20+
Please see: https://learn.sparkfun.com/tutorials/micromod-data-logging-carrier-board-hookup-guide
21+
Insert the Artemis Processor into the MicroMod Data Logging Carrier Board and secure with the screw.
22+
Connect your GNSS breakout to the Carrier Board using a Qwiic cable.
23+
Connect an antenna to your GNSS board if required.
24+
Insert a formatted micro-SD card into the socket on the Carrier Board.
25+
Connect the Carrier Board to your computer using a USB-C cable.
26+
Ensure you have the SparkFun Apollo3 boards installed: http://boardsmanager/All#SparkFun_Apollo3
27+
This code has been tested using version 1.2.1 of the Apollo3 boards on Arduino IDE 1.8.13.
28+
Select "SparkFun Artemis MicroMod" as the board type.
29+
Press upload to upload the code onto the Artemis.
30+
Open the Serial Monitor at 115200 baud to see the output.
31+
32+
To minimise I2C bus errors, it is a good idea to open the I2C pull-up split pad links on
33+
both the MicroMod Data Logging Carrier Board and the u-blox module breakout.
34+
35+
Feel like supporting open source hardware?
36+
Buy a board from SparkFun!
37+
ZED-F9P RTK2: https://www.sparkfun.com/products/15136
38+
NEO-M8P RTK: https://www.sparkfun.com/products/15005
39+
ZOE-M8Q: https://www.sparkfun.com/products/15193
40+
SAM-M8Q: https://www.sparkfun.com/products/15210
41+
*/
42+
43+
#include <SPI.h>
44+
#include <SD.h>
45+
#include <Wire.h> //Needed for I2C to GNSS
46+
47+
#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_GNSS
48+
SFE_UBLOX_GNSS myGNSS;
49+
50+
File myFile; //File that all GNSS data is written to
51+
52+
#define sdChipSelect CS //Primary SPI Chip Select is CS for the MicroMod Artemis Processor. Adjust for your processor if necessary.
53+
54+
#define sdWriteSize 512 // Write data to the SD card in blocks of 512 bytes
55+
#define fileBufferSize 16384 // Allocate 16KBytes of RAM for UBX message storage
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+
Serial.begin(115200);
63+
while (!Serial); //Wait for user to open terminal
64+
Serial.println("SparkFun u-blox Example");
65+
66+
pinMode(LED_BUILTIN, OUTPUT); // Flash LED_BUILTIN each time we write to the SD card
67+
digitalWrite(LED_BUILTIN, LOW);
68+
69+
Wire.begin(); // Start I2C communication
70+
71+
#if defined(AM_PART_APOLLO3)
72+
Wire.setPullups(0); // On the Artemis, we can disable the internal I2C pull-ups too to help reduce bus errors
73+
#endif
74+
75+
while (Serial.available()) // Make sure the Serial buffer is empty
76+
{
77+
Serial.read();
78+
}
79+
80+
Serial.println(F("Press any key to start logging."));
81+
82+
while (!Serial.available()) // Wait for the user to press a key
83+
{
84+
; // Do nothing
85+
}
86+
87+
delay(100); // Wait, just in case multiple characters were sent
88+
89+
while (Serial.available()) // Empty the Serial buffer
90+
{
91+
Serial.read();
92+
}
93+
94+
Serial.println("Initializing SD card...");
95+
96+
// See if the card is present and can be initialized:
97+
if (!SD.begin(sdChipSelect))
98+
{
99+
Serial.println("Card failed, or not present. Freezing...");
100+
// don't do anything more:
101+
while (1);
102+
}
103+
Serial.println("SD card initialized.");
104+
105+
// Create or open a file called "PVT_NMEA.ubx" on the SD card.
106+
// If the file already exists, the new data is appended to the end of the file.
107+
myFile = SD.open("PVT_NMEA.ubx", FILE_WRITE);
108+
if(!myFile)
109+
{
110+
Serial.println(F("Failed to create UBX data file! Freezing..."));
111+
while (1);
112+
}
113+
114+
//myGNSS.enableDebugging(); // Uncomment this line to enable lots of helpful GNSS debug messages on Serial
115+
//myGNSS.enableDebugging(Serial, true); // Or, uncomment this line to enable only the important GNSS debug messages on Serial
116+
117+
//myGNSS.disableUBX7Fcheck(); // RAWX data can legitimately contain 0x7F. Uncomment this line to disable the "7F" check in checkUbloxI2C
118+
119+
// SD cards can occasionally 'hiccup' and a write takes much longer than usual. The buffer needs to be big enough
120+
// to hold the backlog of data if/when this happens.
121+
// getMaxFileBufferAvail will tell us the maximum number of bytes which the file buffer has contained.
122+
myGNSS.setFileBufferSize(fileBufferSize); // setFileBufferSize must be called _before_ .begin
123+
124+
if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port
125+
{
126+
Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing..."));
127+
while (1);
128+
}
129+
130+
// Uncomment the next line if you want to reset your module back to the default settings with 1Hz navigation rate
131+
// This will (re)enable the standard NMEA messages too
132+
// This will also disable any "auto" UBX messages that were enabled and saved by other examples and reduce the load on the I2C bus
133+
//myGNSS.factoryDefault(); delay(5000);
134+
135+
myGNSS.setI2COutput(COM_TYPE_UBX | COM_TYPE_NMEA); //Set the I2C port to output both UBX and NMEA messages
136+
137+
//myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Optional: save (only) the communications port settings to flash and BBR
138+
139+
myGNSS.setNavigationFrequency(1); //Produce one navigation solution per second
140+
141+
myGNSS.setAutoPVT(true, false); // Enable automatic NAV PVT messages: without callback; without implicit update
142+
myGNSS.logNAVPVT(); // Enable NAV PVT data logging
143+
144+
myGNSS.enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_I2C, 1); // Ensure the GxGGA (Global positioning system fix data) message is enabled. Send every measurement.
145+
myGNSS.enableNMEAMessage(UBX_NMEA_GSA, COM_PORT_I2C, 1); // Ensure the GxGSA (GNSS DOP and Active satellites) message is enabled. Send every measurement.
146+
myGNSS.enableNMEAMessage(UBX_NMEA_GSV, COM_PORT_I2C, 1); // Ensure the GxGSV (GNSS satellites in view) message is enabled. Send every measurement.
147+
myGNSS.logNMEA(); // Enable NMEA logging
148+
149+
Serial.println(F("Press any key to stop logging."));
150+
151+
lastPrint = millis(); // Initialize lastPrint
152+
}
153+
154+
void loop()
155+
{
156+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
157+
158+
myGNSS.checkUblox(); // Check for the arrival of new data and process it.
159+
160+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
161+
162+
while (myGNSS.fileBufferAvailable() >= sdWriteSize) // Check to see if we have at least sdWriteSize waiting in the buffer
163+
{
164+
digitalWrite(LED_BUILTIN, HIGH); // Flash LED_BUILTIN each time we write to the SD card
165+
166+
uint8_t myBuffer[sdWriteSize]; // Create our own buffer to hold the data while we write it to SD card
167+
168+
myGNSS.extractFileBufferData((uint8_t *)&myBuffer, sdWriteSize); // Extract exactly sdWriteSize bytes from the UBX file buffer and put them into myBuffer
169+
170+
myFile.write(myBuffer, sdWriteSize); // Write exactly sdWriteSize bytes from myBuffer to the ubxDataFile on the SD card
171+
172+
bytesWritten += sdWriteSize; // Update bytesWritten
173+
174+
// In case the SD writing is slow or there is a lot of data to write, keep checking for the arrival of new data
175+
myGNSS.checkUblox(); // Check for the arrival of new data and process it.
176+
177+
digitalWrite(LED_BUILTIN, LOW); // Turn LED_BUILTIN off again
178+
}
179+
180+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
181+
182+
if (millis() > (lastPrint + 1000)) // Print bytesWritten once per second
183+
{
184+
Serial.print(F("The number of bytes written to SD card is ")); // Print how many bytes have been written to SD card
185+
Serial.println(bytesWritten);
186+
187+
uint16_t maxBufferBytes = myGNSS.getMaxFileBufferAvail(); // Get how full the file buffer has been (not how full it is now)
188+
189+
//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
190+
//Serial.println(maxBufferBytes);
191+
192+
if (maxBufferBytes > ((fileBufferSize / 5) * 4)) // Warn the user if fileBufferSize was more than 80% full
193+
{
194+
Serial.println(F("Warning: the file buffer has been over 80% full. Some data may have been lost."));
195+
}
196+
197+
lastPrint = millis(); // Update lastPrint
198+
}
199+
200+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
201+
202+
if (Serial.available()) // Check if the user wants to stop logging
203+
{
204+
uint16_t remainingBytes = myGNSS.fileBufferAvailable(); // Check if there are any bytes remaining in the file buffer
205+
206+
while (remainingBytes > 0) // While there is still data in the file buffer
207+
{
208+
digitalWrite(LED_BUILTIN, HIGH); // Flash LED_BUILTIN while we write to the SD card
209+
210+
uint8_t myBuffer[sdWriteSize]; // Create our own buffer to hold the data while we write it to SD card
211+
212+
uint16_t bytesToWrite = remainingBytes; // Write the remaining bytes to SD card sdWriteSize bytes at a time
213+
if (bytesToWrite > sdWriteSize)
214+
{
215+
bytesToWrite = sdWriteSize;
216+
}
217+
218+
myGNSS.extractFileBufferData((uint8_t *)&myBuffer, bytesToWrite); // Extract bytesToWrite bytes from the UBX file buffer and put them into myBuffer
219+
220+
myFile.write(myBuffer, bytesToWrite); // Write bytesToWrite bytes from myBuffer to the ubxDataFile on the SD card
221+
222+
bytesWritten += bytesToWrite; // Update bytesWritten
223+
224+
remainingBytes -= bytesToWrite; // Decrement remainingBytes
225+
}
226+
227+
digitalWrite(LED_BUILTIN, LOW); // Turn LED_BUILTIN off
228+
229+
Serial.print(F("The total number of bytes written to SD card is ")); // Print how many bytes have been written to SD card
230+
Serial.println(bytesWritten);
231+
232+
myFile.close(); // Close the data file
233+
234+
Serial.println(F("Logging stopped. Freezing..."));
235+
while(1); // Do nothing more
236+
}
237+
238+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
239+
}

examples/Example25_MeasurementAndNavigationRate/Example25_MeasurementAndNavigationRate.ino

-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ void setup()
8181
while (1);
8282
}
8383

84-
// Another trick we can use is to mark the CFG RATE data as stale so we can be sure we read fresh data
85-
myGNSS.packetUBXCFGRATE->moduleQueried.moduleQueried.all = 0; // Mark all of the CFG RATE data as stale
86-
8784
// Read and print the updated measurement rate and navigation rate
8885

8986
rate = myGNSS.getMeasurementRate(); //Get the measurement rate of this module

keywords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,15 @@ initPacketUBXHNRPVT KEYWORD2
382382
flushHNRPVT KEYWORD2
383383
logHNRPVT KEYWORD2
384384

385+
logNMEA KEYWORD2
386+
385387
setNavigationFrequency KEYWORD2
386388
getNavigationFrequency KEYWORD2
387389
setMeasurementRate KEYWORD2
388390
getMeasurementRate KEYWORD2
389391
setNavigationRate KEYWORD2
390392
getNavigationRate KEYWORD2
393+
flushCFGRATE KEYWORD2
391394

392395
getGeometricDOP KEYWORD2
393396
getPositionDOP KEYWORD2

0 commit comments

Comments
 (0)