|
| 1 | +/* |
| 2 | + Configuring the GNSS to automatically send position reports over SPI |
| 3 | + Based on code by: Nathan Seidle and Thorsten von Eicken |
| 4 | + SparkFun Electronics |
| 5 | + Date: January 3rd, 2019 |
| 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 configure the U-Blox GNSS the send navigation reports automatically |
| 10 | + and retrieving the latest one via getPVT. This eliminates the blocking in getPVT while the GNSS |
| 11 | + produces a fresh navigation solution at the expense of returning a slighly old solution. |
| 12 | +
|
| 13 | + Feel like supporting open source hardware? |
| 14 | + Buy a board from SparkFun! |
| 15 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 16 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 17 | + NEO-M8U: https://www.sparkfun.com/products/16329 |
| 18 | + NEO-M9N: https://www.sparkfun.com/products/17285 |
| 19 | +
|
| 20 | + Hardware Connections: |
| 21 | + You need to connect the SPI pins from your microcontroller to the specific pins on your SparkFun product. |
| 22 | + Connections will vary based on your microcontroller, but for reference please refer to this tutorial on SPI: |
| 23 | + https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all |
| 24 | + |
| 25 | + Most new boards now use the terms: |
| 26 | + CS - Chip Select |
| 27 | + COPI - Controller Out, Peripheral In |
| 28 | + CIPO - Controller in, Peripheral Out |
| 29 | + SCK - Serial Clock |
| 30 | +
|
| 31 | + You can choose any pin for Chip Select, but the others are likely either defined by the library you are using |
| 32 | + (see here for the standard Arduino one: https://www.arduino.cc/en/reference/SPI) or the microcontroller. The |
| 33 | + ESP32 has two standard, selectable SPI ports, for example. |
| 34 | +
|
| 35 | + To enable SPI communication, you will need to solder the DSEL/SPI jumper closed on your u-blox board. |
| 36 | +
|
| 37 | + IMPORTANT: there have been reports that some u-blox devices do not respond to the UBX protocol over SPI |
| 38 | + with the factory settings. You may find you need to connect to the device via USB and u-center and set |
| 39 | + the incoming protocol to UBX only. Make sure you disable all other protocols as inputs if you can't |
| 40 | + get things to work! Hopefully this is just a bug in the u-blox firmware that will be fixed soon ;-) |
| 41 | +
|
| 42 | +*/ |
| 43 | + |
| 44 | +#include <SPI.h> //Needed for SPI to GNSS |
| 45 | + |
| 46 | +#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS |
| 47 | +SFE_UBLOX_GNSS myGNSS; |
| 48 | + |
| 49 | +// ######################################### |
| 50 | + |
| 51 | +// Instantiate an instance of the SPI class. |
| 52 | +// Your configuration may be different, depending on the microcontroller you are using! |
| 53 | + |
| 54 | +#define spiPort SPI // This is the SPI port on standard Ardino boards. Comment this line if you want to use a different port. |
| 55 | + |
| 56 | +//SPIClass spiPort (HSPI); // This is the default SPI interface on some ESP32 boards. Uncomment this line if you are using ESP32. |
| 57 | + |
| 58 | +// ######################################### |
| 59 | + |
| 60 | +const uint8_t csPin = 10; // On ATmega328 boards, SPI Chip Select is usually pin 10. Change this to match your board. |
| 61 | + |
| 62 | +// ######################################### |
| 63 | + |
| 64 | +void setup() |
| 65 | +{ |
| 66 | + Serial.begin(115200); |
| 67 | + while (!Serial); //Wait for user to open terminal |
| 68 | + Serial.println("SparkFun u-blox Example"); |
| 69 | + |
| 70 | + spiPort.begin(); // begin the SPI port |
| 71 | + |
| 72 | + //myGNSS.enableDebugging(); // Uncomment this line to see helpful debug messages on Serial |
| 73 | + |
| 74 | + // Connect to the u-blox module using SPI port, csPin and speed setting |
| 75 | + // ublox devices generally work up to 5MHz. We'll use 4MHz for this example: |
| 76 | + if (myGNSS.begin(spiPort, csPin, 4000000) == false) |
| 77 | + { |
| 78 | + Serial.println(F("u-blox GNSS not detected on SPI bus. Please check wiring. Freezing.")); |
| 79 | + while (1); |
| 80 | + } |
| 81 | + |
| 82 | + myGNSS.setPortOutput(COM_PORT_SPI, COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise) |
| 83 | + myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR |
| 84 | + |
| 85 | + myGNSS.setNavigationFrequency(2); //Produce two solutions per second |
| 86 | + myGNSS.setAutoPVT(true); //Tell the GNSS to "send" each solution |
| 87 | + //myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR |
| 88 | +} |
| 89 | + |
| 90 | +void loop() |
| 91 | +{ |
| 92 | + // Calling getPVT returns true if there actually is a fresh navigation solution available. |
| 93 | + // Start the reading only when valid LLH is available |
| 94 | + if (myGNSS.getPVT() && (myGNSS.getInvalidLlh() == false)) |
| 95 | + { |
| 96 | + Serial.println(); |
| 97 | + long latitude = myGNSS.getLatitude(); |
| 98 | + Serial.print(F("Lat: ")); |
| 99 | + Serial.print(latitude); |
| 100 | + |
| 101 | + long longitude = myGNSS.getLongitude(); |
| 102 | + Serial.print(F(" Long: ")); |
| 103 | + Serial.print(longitude); |
| 104 | + Serial.print(F(" (degrees * 10^-7)")); |
| 105 | + |
| 106 | + long altitude = myGNSS.getAltitude(); |
| 107 | + Serial.print(F(" Alt: ")); |
| 108 | + Serial.print(altitude); |
| 109 | + Serial.print(F(" (mm)")); |
| 110 | + |
| 111 | + byte SIV = myGNSS.getSIV(); |
| 112 | + Serial.print(F(" SIV: ")); |
| 113 | + Serial.print(SIV); |
| 114 | + |
| 115 | + int PDOP = myGNSS.getPDOP(); |
| 116 | + Serial.print(F(" PDOP: ")); |
| 117 | + Serial.print(PDOP); |
| 118 | + Serial.print(F(" (10^-2)")); |
| 119 | + |
| 120 | + int nedNorthVel = myGNSS.getNedNorthVel(); |
| 121 | + Serial.print(F(" VelN: ")); |
| 122 | + Serial.print(nedNorthVel); |
| 123 | + Serial.print(F(" (mm/s)")); |
| 124 | + |
| 125 | + int nedEastVel = myGNSS.getNedEastVel(); |
| 126 | + Serial.print(F(" VelE: ")); |
| 127 | + Serial.print(nedEastVel); |
| 128 | + Serial.print(F(" (mm/s)")); |
| 129 | + |
| 130 | + int nedDownVel = myGNSS.getNedDownVel(); |
| 131 | + Serial.print(F(" VelD: ")); |
| 132 | + Serial.print(nedDownVel); |
| 133 | + Serial.print(F(" (mm/s)")); |
| 134 | + |
| 135 | + int verticalAccEst = myGNSS.getVerticalAccEst(); |
| 136 | + Serial.print(F(" VAccEst: ")); |
| 137 | + Serial.print(verticalAccEst); |
| 138 | + Serial.print(F(" (mm)")); |
| 139 | + |
| 140 | + int horizontalAccEst = myGNSS.getHorizontalAccEst(); |
| 141 | + Serial.print(F(" HAccEst: ")); |
| 142 | + Serial.print(horizontalAccEst); |
| 143 | + Serial.print(F(" (mm)")); |
| 144 | + |
| 145 | + int speedAccEst = myGNSS.getSpeedAccEst(); |
| 146 | + Serial.print(F(" SpeedAccEst: ")); |
| 147 | + Serial.print(speedAccEst); |
| 148 | + Serial.print(F(" (mm/s)")); |
| 149 | + |
| 150 | + int headAccEst = myGNSS.getHeadingAccEst(); |
| 151 | + Serial.print(F(" HeadAccEst: ")); |
| 152 | + Serial.print(headAccEst); |
| 153 | + Serial.print(F(" (degrees * 10^-5)")); |
| 154 | + |
| 155 | + if (myGNSS.getHeadVehValid() == true) { |
| 156 | + int headVeh = myGNSS.getHeadVeh(); |
| 157 | + Serial.print(F(" HeadVeh: ")); |
| 158 | + Serial.print(headVeh); |
| 159 | + Serial.print(F(" (degrees * 10^-5)")); |
| 160 | + |
| 161 | + int magDec = myGNSS.getMagDec(); |
| 162 | + Serial.print(F(" MagDec: ")); |
| 163 | + Serial.print(magDec); |
| 164 | + Serial.print(F(" (degrees * 10^-2)")); |
| 165 | + |
| 166 | + int magAcc = myGNSS.getMagAcc(); |
| 167 | + Serial.print(F(" MagAcc: ")); |
| 168 | + Serial.print(magAcc); |
| 169 | + Serial.print(F(" (degrees * 10^-2)")); |
| 170 | + } |
| 171 | + |
| 172 | + Serial.println(); |
| 173 | + } else { |
| 174 | + Serial.print("."); |
| 175 | + delay(50); |
| 176 | + } |
| 177 | +} |
0 commit comments