|
| 1 | +/* |
| 2 | + Reading lat and long via UBX binary commands over SPI |
| 3 | + By: Andrew Berridge |
| 4 | + Date: 27th June 2021 |
| 5 | + License: MIT. See license file for more information but you can |
| 6 | + basically do whatever you want with this code. |
| 7 | +
|
| 8 | + This example shows how to query a u-blox module for its lat/long/altitude over SPI. We also |
| 9 | + turn off the NMEA output on the SPI port. This decreases the amount of SPI traffic |
| 10 | + dramatically. |
| 11 | +
|
| 12 | + Note: Long/lat are large numbers because they are * 10^7. To convert lat/long |
| 13 | + to something google maps understands simply divide the numbers by 10,000,000. We |
| 14 | + do this so that we don't have to use floating point numbers. |
| 15 | +
|
| 16 | + Leave NMEA parsing behind. Now you can simply ask the module for the datums you want! |
| 17 | +
|
| 18 | + Feel like supporting open source hardware? |
| 19 | + Buy a board from SparkFun! |
| 20 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 21 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 22 | + NEO-M8U: https://www.sparkfun.com/products/16329 |
| 23 | + NEO-M9N: https://www.sparkfun.com/products/17285 |
| 24 | +
|
| 25 | + Hardware Connections: |
| 26 | + You need to connect the SPI pins from your microcontroller to the specific pins on your SparkFun product. |
| 27 | + Connections will vary based on your microcontroller, but for reference please refer to this tutorial on SPI: |
| 28 | + https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all |
| 29 | + |
| 30 | + Most new boards now use the terms: |
| 31 | + CS - Chip Select |
| 32 | + COPI - Controller Out, Peripheral In |
| 33 | + CIPO - Controller in, Peripheral Out |
| 34 | + SCK - Serial Clock |
| 35 | +
|
| 36 | + You can choose any pin for Chip Select, but the others are likely either defined by the library you are using |
| 37 | + (see here for the standard Arduino one: https://www.arduino.cc/en/reference/SPI) or the microcontroller. The |
| 38 | + ESP32 has two standard, selectable SPI ports, for example. |
| 39 | +
|
| 40 | + To enable SPI communication, you will need to solder the DSEL/SPI jumper closed on your u-blox board. |
| 41 | +
|
| 42 | + IMPORTANT: there have been reports that some u-blox devices do not respond to the UBX protocol over SPI |
| 43 | + with the factory settings. You may find you need to connect to the device via USB and u-center and set |
| 44 | + the incoming protocol to UBX only. Make sure you disable all other protocols as inputs if you can't |
| 45 | + get things to work! Hopefully this is just a bug in the u-blox firmware that will be fixed soon ;-) |
| 46 | +
|
| 47 | +*/ |
| 48 | + |
| 49 | +#include <SPI.h> //Needed for SPI to GNSS |
| 50 | + |
| 51 | +#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS |
| 52 | +SFE_UBLOX_GNSS myGNSS; |
| 53 | + |
| 54 | +// ######################################### |
| 55 | + |
| 56 | +// Instantiate an instance of the SPI class. |
| 57 | +// Your configuration may be different, depending on the microcontroller you are using! |
| 58 | + |
| 59 | +#define spiPort SPI // This is the SPI port on standard Ardino boards. Comment this line if you want to use a different port. |
| 60 | + |
| 61 | +//SPIClass spiPort (HSPI); // This is the default SPI interface on some ESP32 boards. Uncomment this line if you are using ESP32. |
| 62 | + |
| 63 | +// ######################################### |
| 64 | + |
| 65 | +const uint8_t csPin = 10; // On ATmega328 boards, SPI Chip Select is usually pin 10. Change this to match your board. |
| 66 | + |
| 67 | +// ######################################### |
| 68 | + |
| 69 | +long lastTime = 0; //Simple local timer. Limits amount of SPI traffic to u-blox module. |
| 70 | + |
| 71 | +void setup() |
| 72 | +{ |
| 73 | + Serial.begin(115200); |
| 74 | + while (!Serial); //Wait for user to open terminal |
| 75 | + Serial.println(F("SparkFun u-blox Example")); |
| 76 | + |
| 77 | + spiPort.begin(); // begin the SPI port |
| 78 | + |
| 79 | + //myGNSS.enableDebugging(); // Uncomment this line to see helpful debug messages on Serial |
| 80 | + |
| 81 | + // Connect to the u-blox module using SPI port, csPin and speed setting |
| 82 | + // ublox devices generally work up to 5MHz. We'll use 4MHz for this example: |
| 83 | + if (myGNSS.begin(spiPort, csPin, 4000000) == false) |
| 84 | + { |
| 85 | + Serial.println(F("u-blox GNSS not detected on SPI bus. Please check wiring. Freezing.")); |
| 86 | + while (1); |
| 87 | + } |
| 88 | + |
| 89 | + //myGNSS.factoryDefault(); delay(5000); // Uncomment this line to reset the module back to its factory defaults |
| 90 | + |
| 91 | + myGNSS.setPortOutput(COM_PORT_SPI, COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise) |
| 92 | + myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR |
| 93 | +} |
| 94 | + |
| 95 | +void loop() |
| 96 | +{ |
| 97 | + //Query module only every second. Doing it more often will just cause SPI traffic. |
| 98 | + //The module only responds when a new position is available |
| 99 | + if (millis() - lastTime > 1000) |
| 100 | + { |
| 101 | + lastTime = millis(); //Update the timer |
| 102 | + |
| 103 | + long latitude = myGNSS.getLatitude(); |
| 104 | + Serial.print(F("Lat: ")); |
| 105 | + Serial.print(latitude); |
| 106 | + |
| 107 | + long longitude = myGNSS.getLongitude(); |
| 108 | + Serial.print(F(" Long: ")); |
| 109 | + Serial.print(longitude); |
| 110 | + Serial.print(F(" (degrees * 10^-7)")); |
| 111 | + |
| 112 | + long altitude = myGNSS.getAltitude(); |
| 113 | + Serial.print(F(" Alt: ")); |
| 114 | + Serial.print(altitude); |
| 115 | + Serial.print(F(" (mm)")); |
| 116 | + |
| 117 | + byte SIV = myGNSS.getSIV(); |
| 118 | + Serial.print(F(" SIV: ")); |
| 119 | + Serial.print(SIV); |
| 120 | + |
| 121 | + Serial.println(); |
| 122 | + } |
| 123 | +} |
0 commit comments