From 03802cb9593ff22f8d70d799045ffca092375b92 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Fri, 15 Apr 2022 10:12:37 +0100 Subject: [PATCH 1/4] Create CallbackExample9_UseCallbackDataInLoop.ino --- ...CallbackExample9_UseCallbackDataInLoop.ino | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino diff --git a/examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino b/examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino new file mode 100644 index 00000000..8775bd0b --- /dev/null +++ b/examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino @@ -0,0 +1,163 @@ +/* + Configuring the GNSS to automatically send position reports over I2C and display them using a callback + By: Paul Clark + SparkFun Electronics + Date: April 15th, 2022 + License: MIT. See license file for more information but you can + basically do whatever you want with this code. + + This example shows how to access the callback data from the main loop. + The simple way is to use a global flag: set it in the callback, check it and clear it in the main loop. + Or, you can be more sophisticated and use the callback flags themselves. + + Uncomment the #define useGlobalFlag below to use the simple method. + + Feel like supporting open source hardware? + Buy a board from SparkFun! + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 + NEO-M8P RTK: https://www.sparkfun.com/products/15005 + SAM-M8Q: https://www.sparkfun.com/products/15106 + + Hardware Connections: + Plug a Qwiic cable into the GPS and a BlackBoard + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) + Open the serial monitor at 115200 baud to see the output +*/ + +//#define useGlobalFlag // Uncomment this line to use a global flag to indicate that the callback data is valid + +#ifdef useGlobalFlag +bool newPVTdata = false; +#endif + +#include //Needed for I2C to GPS + +#include //http://librarymanager/All#SparkFun_u-blox_GNSS +SFE_UBLOX_GNSS myGNSS; + +// Callback: callbackPVT will be called when new NAV PVT data arrives +// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t +// _____ You can use any name you like for the callback. Use the same name when you call setAutoPVTcallbackPtr +// / _____ This _must_ be UBX_NAV_PVT_data_t +// | / _____ You can use any name you like for the struct +// | | / +// | | | +void callbackPVT(UBX_NAV_PVT_data_t *ubxDataStruct) +{ + +#ifdef useGlobalFlag // If we are using the global flag + + newPVTdata = true; + +#endif + +} + +void setup() +{ + Serial.begin(115200); + while (!Serial); //Wait for user to open terminal + Serial.println("SparkFun u-blox Example"); + + Wire.begin(); + + //myGNSS.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial + + if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port + { + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing.")); + while (1); + } + + myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) + myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR + + myGNSS.setNavigationFrequency(2); //Produce two solutions per second + + myGNSS.setAutoPVTcallbackPtr(&callbackPVT); // Enable automatic NAV PVT messages with callback to callbackPVT +} + +void loop() +{ + myGNSS.checkUblox(); // Check for the arrival of new data and process it. + + +#ifndef useGlobalFlag // If we are not using the global flag + + static bool callbackDataValid = false; // Flag to show that fresh callback data is available + + // Is the callback data valid? + // If automaticFlags.flags.bits.callbackCopyValid is true, it indicates new PVT data has been received and has been copied + // automaticFlags.flags.bits.callbackCopyValid will be cleared when the callback is called + + if ((callbackDataValid == false) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true)) + { + Serial.println(F("NAV PVT callback data is now valid")); + callbackDataValid = true; // Set the flag + } + +#endif + + + myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. + + // Check if new PVT data has been received + + +#ifdef useGlobalFlag + + if (newPVTdata) + { + + +#else // If we are not using the global flag + + + // automaticFlags.flags.bits.callbackCopyValid will have been cleared after the callback was called + + if ((callbackDataValid == true) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == false)) + { + callbackDataValid = false; // Clear the flag + + +#endif + + + Serial.println(); + + Serial.print(F("Time: ")); // Print the time + uint8_t hms = myGNSS.packetUBXNAVPVT->callbackData->hour; // Print the hours + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required + Serial.print(hms); + Serial.print(F(":")); + hms = myGNSS.packetUBXNAVPVT->callbackData->min; // Print the minutes + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required + Serial.print(hms); + Serial.print(F(":")); + hms = myGNSS.packetUBXNAVPVT->callbackData->sec; // Print the seconds + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required + Serial.print(hms); + Serial.print(F(".")); + unsigned long millisecs = myGNSS.packetUBXNAVPVT->callbackData->iTOW % 1000; // Print the milliseconds + if (millisecs < 100) Serial.print(F("0")); // Print the trailing zeros correctly + if (millisecs < 10) Serial.print(F("0")); + Serial.print(millisecs); + + long latitude = myGNSS.packetUBXNAVPVT->callbackData->lat; // Print the latitude + Serial.print(F(" Lat: ")); + Serial.print(latitude); + + long longitude = myGNSS.packetUBXNAVPVT->callbackData->lon; // Print the longitude + Serial.print(F(" Long: ")); + Serial.print(longitude); + Serial.print(F(" (degrees * 10^-7)")); + + long altitude = myGNSS.packetUBXNAVPVT->callbackData->hMSL; // Print the height above mean sea level + Serial.print(F(" Height above MSL: ")); + Serial.print(altitude); + Serial.println(F(" (mm)")); + } + + Serial.print("."); + delay(50); +} From 5d06159a793835139031c24aa4035e66a44e01c6 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Fri, 15 Apr 2022 10:59:02 +0100 Subject: [PATCH 2/4] Update CallbackExample9_UseCallbackDataInLoop.ino --- ...CallbackExample9_UseCallbackDataInLoop.ino | 67 ++++--------------- 1 file changed, 12 insertions(+), 55 deletions(-) diff --git a/examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino b/examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino index 8775bd0b..37f98be4 100644 --- a/examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino +++ b/examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino @@ -7,10 +7,9 @@ basically do whatever you want with this code. This example shows how to access the callback data from the main loop. - The simple way is to use a global flag: set it in the callback, check it and clear it in the main loop. + The simple way to check if new data is available is to use a global flag: set it in the callback, check it and clear it in the main loop. Or, you can be more sophisticated and use the callback flags themselves. - - Uncomment the #define useGlobalFlag below to use the simple method. + This example shows how to use the sophisticated method. Feel like supporting open source hardware? Buy a board from SparkFun! @@ -24,12 +23,6 @@ Open the serial monitor at 115200 baud to see the output */ -//#define useGlobalFlag // Uncomment this line to use a global flag to indicate that the callback data is valid - -#ifdef useGlobalFlag -bool newPVTdata = false; -#endif - #include //Needed for I2C to GPS #include //http://librarymanager/All#SparkFun_u-blox_GNSS @@ -44,13 +37,7 @@ SFE_UBLOX_GNSS myGNSS; // | | | void callbackPVT(UBX_NAV_PVT_data_t *ubxDataStruct) { - -#ifdef useGlobalFlag // If we are using the global flag - - newPVTdata = true; - -#endif - + Serial.println(F("Hey! The NAV PVT callback has been called!")); } void setup() @@ -79,49 +66,17 @@ void setup() void loop() { - myGNSS.checkUblox(); // Check for the arrival of new data and process it. - - -#ifndef useGlobalFlag // If we are not using the global flag - - static bool callbackDataValid = false; // Flag to show that fresh callback data is available - - // Is the callback data valid? - // If automaticFlags.flags.bits.callbackCopyValid is true, it indicates new PVT data has been received and has been copied - // automaticFlags.flags.bits.callbackCopyValid will be cleared when the callback is called - - if ((callbackDataValid == false) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true)) - { - Serial.println(F("NAV PVT callback data is now valid")); - callbackDataValid = true; // Set the flag - } - -#endif - - myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. - - // Check if new PVT data has been received - - -#ifdef useGlobalFlag - - if (newPVTdata) - { - - -#else // If we are not using the global flag - + myGNSS.checkUblox(); // Check for the arrival of new data and process it. - // automaticFlags.flags.bits.callbackCopyValid will have been cleared after the callback was called + // Check if new NAV PVT data has been received: + // If myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid is true, it indicates new PVT data has been received and has been copied. + // automaticFlags.flags.bits.callbackCopyValid will be cleared automatically when the callback is called. - if ((callbackDataValid == true) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == false)) + if (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true) { - callbackDataValid = false; // Clear the flag - - -#endif - + // But, we can manually clear the callback flag too. This will prevent the callback from being called! + myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid = false; // Comment this line if you still want the callback to be called Serial.println(); @@ -158,6 +113,8 @@ void loop() Serial.println(F(" (mm)")); } + myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. There will not be any in this example, unless you commented the line above + Serial.print("."); delay(50); } From 54bbd55cf870e7d50562b79081951d13a1f576a1 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Thu, 28 Apr 2022 11:35:45 +0100 Subject: [PATCH 3/4] Fix error in processUBXpacket for UBX-CFG-RATE This error explains why the measRate was sometimes being returned as zero! --- src/SparkFun_u-blox_GNSS_Arduino_Library.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp index bc4a5b7d..9063b091 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp @@ -3959,8 +3959,7 @@ void SFE_UBLOX_GNSS::processUBXpacket(ubxPacket *msg) packetUBXCFGPRT->dataValid = true; } } - break; - if (msg->id == UBX_CFG_RATE && msg->len == UBX_CFG_RATE_LEN) + else if (msg->id == UBX_CFG_RATE && msg->len == UBX_CFG_RATE_LEN) { // Parse various byte fields into storage - but only if we have memory allocated for it if (packetUBXCFGRATE != NULL) @@ -12994,7 +12993,7 @@ bool SFE_UBLOX_GNSS::getPortSettingsInternal(uint8_t portID, uint16_t maxWait) if (result == SFE_UBLOX_STATUS_DATA_OVERWRITTEN) retVal = true; - // Now disable automatic support for CFG-RATE (see above) + // Now disable automatic support for CFG-PRT (see above) delete packetUBXCFGPRT; packetUBXCFGPRT = NULL; From c38560637e561b9b8bc563745cacdee60ff06590 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Thu, 28 Apr 2022 12:42:39 +0100 Subject: [PATCH 4/4] v2.2.10 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index fbcebda6..0d36c412 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun u-blox GNSS Arduino Library -version=2.2.9 +version=2.2.10 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for I2C, Serial and SPI Communication with u-blox GNSS modules