|
| 1 | +/* |
| 2 | + Configuring the GNSS to automatically send position reports over I2C and display them using a callback |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: April 15th, 2022 |
| 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 access the callback data from the main loop. |
| 10 | + The simple way is to use a global flag: set it in the callback, check it and clear it in the main loop. |
| 11 | + Or, you can be more sophisticated and use the callback flags themselves. |
| 12 | +
|
| 13 | + Uncomment the #define useGlobalFlag below to use the simple method. |
| 14 | +
|
| 15 | + Feel like supporting open source hardware? |
| 16 | + Buy a board from SparkFun! |
| 17 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 18 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 19 | + SAM-M8Q: https://www.sparkfun.com/products/15106 |
| 20 | +
|
| 21 | + Hardware Connections: |
| 22 | + Plug a Qwiic cable into the GPS and a BlackBoard |
| 23 | + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 24 | + Open the serial monitor at 115200 baud to see the output |
| 25 | +*/ |
| 26 | + |
| 27 | +//#define useGlobalFlag // Uncomment this line to use a global flag to indicate that the callback data is valid |
| 28 | + |
| 29 | +#ifdef useGlobalFlag |
| 30 | +bool newPVTdata = false; |
| 31 | +#endif |
| 32 | + |
| 33 | +#include <Wire.h> //Needed for I2C to GPS |
| 34 | + |
| 35 | +#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS |
| 36 | +SFE_UBLOX_GNSS myGNSS; |
| 37 | + |
| 38 | +// Callback: callbackPVT will be called when new NAV PVT data arrives |
| 39 | +// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t |
| 40 | +// _____ You can use any name you like for the callback. Use the same name when you call setAutoPVTcallbackPtr |
| 41 | +// / _____ This _must_ be UBX_NAV_PVT_data_t |
| 42 | +// | / _____ You can use any name you like for the struct |
| 43 | +// | | / |
| 44 | +// | | | |
| 45 | +void callbackPVT(UBX_NAV_PVT_data_t *ubxDataStruct) |
| 46 | +{ |
| 47 | + |
| 48 | +#ifdef useGlobalFlag // If we are using the global flag |
| 49 | + |
| 50 | + newPVTdata = true; |
| 51 | + |
| 52 | +#endif |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +void setup() |
| 57 | +{ |
| 58 | + Serial.begin(115200); |
| 59 | + while (!Serial); //Wait for user to open terminal |
| 60 | + Serial.println("SparkFun u-blox Example"); |
| 61 | + |
| 62 | + Wire.begin(); |
| 63 | + |
| 64 | + //myGNSS.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial |
| 65 | + |
| 66 | + if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port |
| 67 | + { |
| 68 | + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing.")); |
| 69 | + while (1); |
| 70 | + } |
| 71 | + |
| 72 | + myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 73 | + myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR |
| 74 | + |
| 75 | + myGNSS.setNavigationFrequency(2); //Produce two solutions per second |
| 76 | + |
| 77 | + myGNSS.setAutoPVTcallbackPtr(&callbackPVT); // Enable automatic NAV PVT messages with callback to callbackPVT |
| 78 | +} |
| 79 | + |
| 80 | +void loop() |
| 81 | +{ |
| 82 | + myGNSS.checkUblox(); // Check for the arrival of new data and process it. |
| 83 | + |
| 84 | + |
| 85 | +#ifndef useGlobalFlag // If we are not using the global flag |
| 86 | + |
| 87 | + static bool callbackDataValid = false; // Flag to show that fresh callback data is available |
| 88 | + |
| 89 | + // Is the callback data valid? |
| 90 | + // If automaticFlags.flags.bits.callbackCopyValid is true, it indicates new PVT data has been received and has been copied |
| 91 | + // automaticFlags.flags.bits.callbackCopyValid will be cleared when the callback is called |
| 92 | + |
| 93 | + if ((callbackDataValid == false) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true)) |
| 94 | + { |
| 95 | + Serial.println(F("NAV PVT callback data is now valid")); |
| 96 | + callbackDataValid = true; // Set the flag |
| 97 | + } |
| 98 | + |
| 99 | +#endif |
| 100 | + |
| 101 | + |
| 102 | + myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. |
| 103 | + |
| 104 | + // Check if new PVT data has been received |
| 105 | + |
| 106 | + |
| 107 | +#ifdef useGlobalFlag |
| 108 | + |
| 109 | + if (newPVTdata) |
| 110 | + { |
| 111 | + |
| 112 | + |
| 113 | +#else // If we are not using the global flag |
| 114 | + |
| 115 | + |
| 116 | + // automaticFlags.flags.bits.callbackCopyValid will have been cleared after the callback was called |
| 117 | + |
| 118 | + if ((callbackDataValid == true) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == false)) |
| 119 | + { |
| 120 | + callbackDataValid = false; // Clear the flag |
| 121 | + |
| 122 | + |
| 123 | +#endif |
| 124 | + |
| 125 | + |
| 126 | + Serial.println(); |
| 127 | + |
| 128 | + Serial.print(F("Time: ")); // Print the time |
| 129 | + uint8_t hms = myGNSS.packetUBXNAVPVT->callbackData->hour; // Print the hours |
| 130 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 131 | + Serial.print(hms); |
| 132 | + Serial.print(F(":")); |
| 133 | + hms = myGNSS.packetUBXNAVPVT->callbackData->min; // Print the minutes |
| 134 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 135 | + Serial.print(hms); |
| 136 | + Serial.print(F(":")); |
| 137 | + hms = myGNSS.packetUBXNAVPVT->callbackData->sec; // Print the seconds |
| 138 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 139 | + Serial.print(hms); |
| 140 | + Serial.print(F(".")); |
| 141 | + unsigned long millisecs = myGNSS.packetUBXNAVPVT->callbackData->iTOW % 1000; // Print the milliseconds |
| 142 | + if (millisecs < 100) Serial.print(F("0")); // Print the trailing zeros correctly |
| 143 | + if (millisecs < 10) Serial.print(F("0")); |
| 144 | + Serial.print(millisecs); |
| 145 | + |
| 146 | + long latitude = myGNSS.packetUBXNAVPVT->callbackData->lat; // Print the latitude |
| 147 | + Serial.print(F(" Lat: ")); |
| 148 | + Serial.print(latitude); |
| 149 | + |
| 150 | + long longitude = myGNSS.packetUBXNAVPVT->callbackData->lon; // Print the longitude |
| 151 | + Serial.print(F(" Long: ")); |
| 152 | + Serial.print(longitude); |
| 153 | + Serial.print(F(" (degrees * 10^-7)")); |
| 154 | + |
| 155 | + long altitude = myGNSS.packetUBXNAVPVT->callbackData->hMSL; // Print the height above mean sea level |
| 156 | + Serial.print(F(" Height above MSL: ")); |
| 157 | + Serial.print(altitude); |
| 158 | + Serial.println(F(" (mm)")); |
| 159 | + } |
| 160 | + |
| 161 | + Serial.print("."); |
| 162 | + delay(50); |
| 163 | +} |
0 commit comments