|
| 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 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. |
| 11 | + Or, you can be more sophisticated and use the callback flags themselves. |
| 12 | + This example shows how to use the sophisticated method. |
| 13 | +
|
| 14 | + Feel like supporting open source hardware? |
| 15 | + Buy a board from SparkFun! |
| 16 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 17 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 18 | + SAM-M8Q: https://www.sparkfun.com/products/15106 |
| 19 | +
|
| 20 | + Hardware Connections: |
| 21 | + Plug a Qwiic cable into the GPS and a BlackBoard |
| 22 | + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 23 | + Open the serial monitor at 115200 baud to see the output |
| 24 | +*/ |
| 25 | + |
| 26 | +#include <Wire.h> //Needed for I2C to GPS |
| 27 | + |
| 28 | +#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS |
| 29 | +SFE_UBLOX_GNSS myGNSS; |
| 30 | + |
| 31 | +// Callback: callbackPVT will be called when new NAV PVT data arrives |
| 32 | +// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t |
| 33 | +// _____ You can use any name you like for the callback. Use the same name when you call setAutoPVTcallbackPtr |
| 34 | +// / _____ This _must_ be UBX_NAV_PVT_data_t |
| 35 | +// | / _____ You can use any name you like for the struct |
| 36 | +// | | / |
| 37 | +// | | | |
| 38 | +void callbackPVT(UBX_NAV_PVT_data_t *ubxDataStruct) |
| 39 | +{ |
| 40 | + Serial.println(F("Hey! The NAV PVT callback has been called!")); |
| 41 | +} |
| 42 | + |
| 43 | +void setup() |
| 44 | +{ |
| 45 | + Serial.begin(115200); |
| 46 | + while (!Serial); //Wait for user to open terminal |
| 47 | + Serial.println("SparkFun u-blox Example"); |
| 48 | + |
| 49 | + Wire.begin(); |
| 50 | + |
| 51 | + //myGNSS.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial |
| 52 | + |
| 53 | + if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port |
| 54 | + { |
| 55 | + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing.")); |
| 56 | + while (1); |
| 57 | + } |
| 58 | + |
| 59 | + myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 60 | + myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR |
| 61 | + |
| 62 | + myGNSS.setNavigationFrequency(2); //Produce two solutions per second |
| 63 | + |
| 64 | + myGNSS.setAutoPVTcallbackPtr(&callbackPVT); // Enable automatic NAV PVT messages with callback to callbackPVT |
| 65 | +} |
| 66 | + |
| 67 | +void loop() |
| 68 | +{ |
| 69 | + |
| 70 | + myGNSS.checkUblox(); // Check for the arrival of new data and process it. |
| 71 | + |
| 72 | + // Check if new NAV PVT data has been received: |
| 73 | + // If myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid is true, it indicates new PVT data has been received and has been copied. |
| 74 | + // automaticFlags.flags.bits.callbackCopyValid will be cleared automatically when the callback is called. |
| 75 | + |
| 76 | + if (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true) |
| 77 | + { |
| 78 | + // But, we can manually clear the callback flag too. This will prevent the callback from being called! |
| 79 | + myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid = false; // Comment this line if you still want the callback to be called |
| 80 | + |
| 81 | + Serial.println(); |
| 82 | + |
| 83 | + Serial.print(F("Time: ")); // Print the time |
| 84 | + uint8_t hms = myGNSS.packetUBXNAVPVT->callbackData->hour; // Print the hours |
| 85 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 86 | + Serial.print(hms); |
| 87 | + Serial.print(F(":")); |
| 88 | + hms = myGNSS.packetUBXNAVPVT->callbackData->min; // Print the minutes |
| 89 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 90 | + Serial.print(hms); |
| 91 | + Serial.print(F(":")); |
| 92 | + hms = myGNSS.packetUBXNAVPVT->callbackData->sec; // Print the seconds |
| 93 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 94 | + Serial.print(hms); |
| 95 | + Serial.print(F(".")); |
| 96 | + unsigned long millisecs = myGNSS.packetUBXNAVPVT->callbackData->iTOW % 1000; // Print the milliseconds |
| 97 | + if (millisecs < 100) Serial.print(F("0")); // Print the trailing zeros correctly |
| 98 | + if (millisecs < 10) Serial.print(F("0")); |
| 99 | + Serial.print(millisecs); |
| 100 | + |
| 101 | + long latitude = myGNSS.packetUBXNAVPVT->callbackData->lat; // Print the latitude |
| 102 | + Serial.print(F(" Lat: ")); |
| 103 | + Serial.print(latitude); |
| 104 | + |
| 105 | + long longitude = myGNSS.packetUBXNAVPVT->callbackData->lon; // Print the longitude |
| 106 | + Serial.print(F(" Long: ")); |
| 107 | + Serial.print(longitude); |
| 108 | + Serial.print(F(" (degrees * 10^-7)")); |
| 109 | + |
| 110 | + long altitude = myGNSS.packetUBXNAVPVT->callbackData->hMSL; // Print the height above mean sea level |
| 111 | + Serial.print(F(" Height above MSL: ")); |
| 112 | + Serial.print(altitude); |
| 113 | + Serial.println(F(" (mm)")); |
| 114 | + } |
| 115 | + |
| 116 | + 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 |
| 117 | + |
| 118 | + Serial.print("."); |
| 119 | + delay(50); |
| 120 | +} |
0 commit comments