|
| 1 | +/* |
| 2 | + Reading Position, Velocity and Time (PVT) via UBX binary commands |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: December 21st, 2022 |
| 6 | + License: MIT. Please see LICENSE.md for more information. |
| 7 | +
|
| 8 | + This example shows how to query a u-blox module for its position, velocity and time (PVT) data using UART2. |
| 9 | +
|
| 10 | + UART2 is only available on modules like the ZED-F9P/R/T/K etc.. It is not available on the MAX-M10. |
| 11 | +
|
| 12 | + Important note: |
| 13 | + By default, the UBX protocol is enabled for INPUT on UART2, but not for OUTPUT. |
| 14 | + The code needs to enable UBX output for the begin to succeed. |
| 15 | +
|
| 16 | + Feel like supporting open source hardware? |
| 17 | + Buy a board from SparkFun! |
| 18 | + SparkFun GPS-RTK2 - ZED-F9P (GPS-15136) https://www.sparkfun.com/products/15136 |
| 19 | + SparkFun GPS-RTK-SMA - ZED-F9P (GPS-16481) https://www.sparkfun.com/products/16481 |
| 20 | + SparkFun MAX-M10S Breakout (GPS-18037) https://www.sparkfun.com/products/18037 |
| 21 | + SparkFun ZED-F9K Breakout (GPS-18719) https://www.sparkfun.com/products/18719 |
| 22 | + SparkFun ZED-F9R Breakout (GPS-16344) https://www.sparkfun.com/products/16344 |
| 23 | +
|
| 24 | + Hardware Connections: |
| 25 | + Hook up the TX, RX and GND pins, plus 3V3 or 5V depending on your needs |
| 26 | + Connect: GNSS TX to microcontroller RX; GNSS RX to microcontroller TX |
| 27 | + Open the serial monitor at 115200 baud to see the output |
| 28 | +*/ |
| 29 | + |
| 30 | +#include <SparkFun_u-blox_GNSS_v3.h> //http://librarymanager/All#SparkFun_u-blox_GNSS_v3 |
| 31 | + |
| 32 | +SFE_UBLOX_GNSS_SERIAL myGNSS; |
| 33 | + |
| 34 | +#define mySerial Serial1 // Use Serial1 to connect to the GNSS module. Change this if required |
| 35 | + |
| 36 | +void setup() |
| 37 | +{ |
| 38 | + Serial.begin(115200); |
| 39 | + delay(1000); |
| 40 | + Serial.println("SparkFun u-blox Example"); |
| 41 | + |
| 42 | + mySerial.begin(38400); // u-blox F9 and M10 modules default to 38400 baud. Change this if required |
| 43 | + |
| 44 | + //myGNSS.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial |
| 45 | + |
| 46 | + myGNSS.connectedToUART2(); // This tells the library we are connecting to UART2 so it uses the correct configuration keys |
| 47 | + |
| 48 | + while (myGNSS.begin(mySerial) == false) //Connect to the u-blox module using mySerial (defined above) |
| 49 | + { |
| 50 | + Serial.println(F("u-blox GNSS not detected")); |
| 51 | + |
| 52 | + Serial.println(F("Attempting to enable the UBX protocol for output")); |
| 53 | + |
| 54 | + myGNSS.setUART2Output(COM_TYPE_UBX); // Enable UBX output. Disable NMEA output |
| 55 | + |
| 56 | + Serial.println(F("Retrying...")); |
| 57 | + delay (1000); |
| 58 | + } |
| 59 | + |
| 60 | + //myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Optional: save (only) the communications port settings to flash and BBR |
| 61 | +} |
| 62 | + |
| 63 | +void loop() |
| 64 | +{ |
| 65 | + // Request (poll) the position, velocity and time (PVT) information. |
| 66 | + // The module only responds when a new position is available. Default is once per second. |
| 67 | + // getPVT() returns true when new data is received. |
| 68 | + if (myGNSS.getPVT() == true) |
| 69 | + { |
| 70 | + int32_t latitude = myGNSS.getLatitude(); |
| 71 | + Serial.print(F("Lat: ")); |
| 72 | + Serial.print(latitude); |
| 73 | + |
| 74 | + int32_t longitude = myGNSS.getLongitude(); |
| 75 | + Serial.print(F(" Long: ")); |
| 76 | + Serial.print(longitude); |
| 77 | + Serial.print(F(" (degrees * 10^-7)")); |
| 78 | + |
| 79 | + int32_t altitude = myGNSS.getAltitudeMSL(); // Altitude above Mean Sea Level |
| 80 | + Serial.print(F(" Alt: ")); |
| 81 | + Serial.print(altitude); |
| 82 | + Serial.print(F(" (mm)")); |
| 83 | + |
| 84 | + Serial.println(); |
| 85 | + } |
| 86 | +} |
0 commit comments