|
| 1 | +/* |
| 2 | + By: Paul CLark |
| 3 | + SparkFun Electronics |
| 4 | + Date: January, 2022 |
| 5 | + License: MIT. See license file for more information but you can |
| 6 | + basically do whatever you want with this code. |
| 7 | +
|
| 8 | + Feel like supporting open source hardware? |
| 9 | + Buy a board from SparkFun! |
| 10 | + ZED-F9R: https://www.sparkfun.com/products/16344 |
| 11 | +
|
| 12 | + Hardware Connections: |
| 13 | + Plug a Qwiic cable into the GNSS and a Redboard Qwiic |
| 14 | + If you don't have a platform with a Qwiic connection use the |
| 15 | + SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 16 | + Open the serial monitor at 115200 baud to see the output |
| 17 | +
|
| 18 | + After calibrating the module and securing it to your vehicle such that it's |
| 19 | + stable within 2 degrees, and the board is oriented correctly with regards to |
| 20 | + the vehicle's frame, you can now read the vehicle's "attitude". The attitude |
| 21 | + includes the vehicle's heading, pitch, and roll. |
| 22 | +
|
| 23 | +*/ |
| 24 | + |
| 25 | +#include <Wire.h> //Needed for I2C to GNSS |
| 26 | + |
| 27 | +#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS |
| 28 | +SFE_UBLOX_GNSS myGNSS; |
| 29 | + |
| 30 | +void setup() |
| 31 | +{ |
| 32 | + Serial.begin(115200); |
| 33 | + while (!Serial); //Wait for user to open terminal |
| 34 | + Serial.println(F("SparkFun u-blox Example")); |
| 35 | + |
| 36 | + Wire.begin(); |
| 37 | + |
| 38 | + if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port |
| 39 | + { |
| 40 | + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing.")); |
| 41 | + while (1); |
| 42 | + } |
| 43 | + |
| 44 | + myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 45 | +} |
| 46 | + |
| 47 | +void loop() |
| 48 | +{ |
| 49 | + // PVAT data is produced at the navigation rate, so by default we'll get fresh data once per second |
| 50 | + if (myGNSS.getNAVPVAT()) // Poll new PVAT |
| 51 | + { |
| 52 | + Serial.print(F("Roll: ")); |
| 53 | + Serial.print((float)myGNSS.getVehicleRoll() / 100000.0, 5); // Use the helper function to get the roll in degrees * 10^-5 |
| 54 | + |
| 55 | + Serial.print(F(" Pitch: ")); |
| 56 | + Serial.print((float)myGNSS.getVehiclePitch() / 100000.0, 5); // Use the helper function to get the pitch in degrees * 10^-5 |
| 57 | + |
| 58 | + Serial.print(F(" Heading: ")); |
| 59 | + Serial.print((float)myGNSS.getVehicleHeading() / 100000.0, 5); // Use the helper function to get the heading in degrees * 10^-5 |
| 60 | + |
| 61 | + // We don't have helper functions to extract the roll, pitch and heading valid flags from the PVAT message. But we can do it manually: |
| 62 | + |
| 63 | + Serial.print(F(" Roll Valid: ")); |
| 64 | + Serial.print(myGNSS.packetUBXNAVPVAT->data.flags.bits.vehRollValid); |
| 65 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.vehRollValid = false; // Mark the data as stale |
| 66 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 67 | + |
| 68 | + Serial.print(F(" Pitch Valid: ")); |
| 69 | + Serial.print(myGNSS.packetUBXNAVPVAT->data.flags.bits.vehPitchValid); |
| 70 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.vehPitchValid = false; // Mark the data as stale |
| 71 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 72 | + |
| 73 | + Serial.print(F(" Heading Valid: ")); |
| 74 | + Serial.print(myGNSS.packetUBXNAVPVAT->data.flags.bits.vehHeadingValid); |
| 75 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.vehHeadingValid = false; // Mark the data as stale |
| 76 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 77 | + |
| 78 | + // We don't have helper functions to extract the roll, pitch and heading accuracy from the PVAT message. But we can do it manually: |
| 79 | + |
| 80 | + Serial.print(F(" Roll Acc: ")); |
| 81 | + Serial.print(((float)myGNSS.packetUBXNAVPVAT->data.accRoll) / 100, 2); |
| 82 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried2.bits.accRoll = false; // Mark the data as stale |
| 83 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 84 | + |
| 85 | + Serial.print(F(" Pitch Acc: ")); |
| 86 | + Serial.print(((float)myGNSS.packetUBXNAVPVAT->data.accPitch) / 100, 2); |
| 87 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried2.bits.accPitch = false; // Mark the data as stale |
| 88 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 89 | + |
| 90 | + Serial.print(F(" Heading Acc: ")); |
| 91 | + Serial.print(((float)myGNSS.packetUBXNAVPVAT->data.accHeading) / 100, 2); |
| 92 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried2.bits.accHeading = false; // Mark the data as stale |
| 93 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 94 | + |
| 95 | + // We don't have helper functions to extract the lat and lon from the PVAT message. But we can do it manually: |
| 96 | + |
| 97 | + Serial.print(F(" Lat: ")); |
| 98 | + Serial.print(((float)myGNSS.packetUBXNAVPVAT->data.lat) / 10000000.0, 7); |
| 99 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.lat = false; // Mark the data as stale |
| 100 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 101 | + |
| 102 | + Serial.print(F(" Lon: ")); |
| 103 | + Serial.print(((float)myGNSS.packetUBXNAVPVAT->data.lon) / 10000000.0, 7); |
| 104 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.lon = false; // Mark the data as stale |
| 105 | + myGNSS.packetUBXNAVPVAT->moduleQueried.moduleQueried1.bits.all = false; |
| 106 | + |
| 107 | + Serial.println(); |
| 108 | + } |
| 109 | + |
| 110 | + delay(250); |
| 111 | +} |
0 commit comments