|
| 1 | +/* |
| 2 | + Using the SFE_UBLOX_GNSS_SUPER class |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: January 20th, 2023 |
| 6 | + License: MIT. See license file for more information. |
| 7 | +
|
| 8 | + This example shows how to use the SFE_UBLOX_GNSS_SUPER class. |
| 9 | + It allows you to write multi-purpose code that can: |
| 10 | + * Detect what hardware the code is running on |
| 11 | + * Select the correct GNSS interface (I2C/SPI/Serial) for that hardware |
| 12 | +
|
| 13 | + Feel like supporting open source hardware? |
| 14 | + Buy a board from SparkFun! |
| 15 | + SparkFun GPS-RTK2 - ZED-F9P (GPS-15136) https://www.sparkfun.com/products/15136 |
| 16 | + SparkFun GPS-RTK-SMA - ZED-F9P (GPS-16481) https://www.sparkfun.com/products/16481 |
| 17 | + SparkFun MAX-M10S Breakout (GPS-18037) https://www.sparkfun.com/products/18037 |
| 18 | + SparkFun ZED-F9K Breakout (GPS-18719) https://www.sparkfun.com/products/18719 |
| 19 | + SparkFun ZED-F9R Breakout (GPS-16344) https://www.sparkfun.com/products/16344 |
| 20 | +
|
| 21 | + Hardware Connections: |
| 22 | + Plug a Qwiic cable into the GNSS and a BlackBoard |
| 23 | + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 24 | + Open the serial monitor at 115200 baud to see the output |
| 25 | +*/ |
| 26 | + |
| 27 | +#include <Wire.h> // Needed for I2C to GNSS |
| 28 | +#include <SPI.h> // Needed for SPI to GNSS |
| 29 | + |
| 30 | +#include <SparkFun_u-blox_GNSS_v3.h> //http://librarymanager/All#SparkFun_u-blox_GNSS_v3 |
| 31 | + |
| 32 | +// Create an object of the GNSS super-class |
| 33 | +SFE_UBLOX_GNSS_SUPER theGNSS; |
| 34 | + |
| 35 | +// The super class supports I2C, SPI and Serial. |
| 36 | +// Create a global enum that defines which interface to use |
| 37 | +typedef enum { |
| 38 | + GNSS_IS_I2C, |
| 39 | + GNSS_IS_SPI, |
| 40 | + GNSS_IS_SERIAL |
| 41 | +} GNSS_INTERFACE; |
| 42 | +GNSS_INTERFACE theGNSSinterface; |
| 43 | + |
| 44 | +// Define which Serial port the GNSS is (or could be) connected to: |
| 45 | +#define gnssSerial Serial1 // Use hardware Serial1 to connect to the GNSS |
| 46 | +uint32_t gnssBaudRate = 38400; // Define what Baud Rate to use. Both F9 and M10 devices default to 38400. |
| 47 | + |
| 48 | +// If the GNSS is (or could be) connected via SPI, we need to define the port and the Chip Select pin: |
| 49 | +#define gnssSPI SPI // Use SPI to connect to the GNSS |
| 50 | +const int gnssSPICS = 4; // Define the Chip Select pin for the SPI interface - Pin 4 is the "free" pin on Thing Plus C |
| 51 | + |
| 52 | +// Define which Wire (I2C) port the GNSS is (or could be) connected to: |
| 53 | +#define gnssWire Wire // Use Wire to connect to the GNSS |
| 54 | +const uint8_t gnssI2CAddress = 0x42; // Define the GNSS I2C address. The default is usually 0x42. (NEO-D9S uses 0x43) |
| 55 | + |
| 56 | +void setup() |
| 57 | +{ |
| 58 | + delay(1000); |
| 59 | + |
| 60 | + Serial.begin(115200); |
| 61 | + Serial.println("SFE_UBLOX_GNSS_SUPER example"); |
| 62 | + |
| 63 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 64 | + // Define which interface to test. |
| 65 | + // The code could auto-detect what board it is running on, e.g. by measuring the |
| 66 | + // voltage on an analog pin (defined by a pair of resistors) or reading the board |
| 67 | + // type from EEPROM, and then set theGNSSinterface to match. |
| 68 | + |
| 69 | + theGNSSinterface = GNSS_IS_I2C; // Select I2C for this test |
| 70 | + |
| 71 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 72 | + // Now prepare the correct hardware interface for GNSS communication |
| 73 | + |
| 74 | + if (theGNSSinterface == GNSS_IS_SERIAL) |
| 75 | + { |
| 76 | + gnssSerial.begin(gnssBaudRate); // Begin the Serial port |
| 77 | + } |
| 78 | + else if (theGNSSinterface == GNSS_IS_SPI) |
| 79 | + { |
| 80 | + pinMode(gnssSPICS, OUTPUT); // Pull the chip select pin high |
| 81 | + digitalWrite(gnssSPICS, HIGH); |
| 82 | + SPI.begin(); |
| 83 | + } |
| 84 | + else // if (theGNSSinterface == GNSS_IS_I2C) // Catch-all. Default to I2C |
| 85 | + { |
| 86 | + Wire.begin(); // Begin the Wire port |
| 87 | + //Wire.setClock(400000); |
| 88 | + } |
| 89 | + |
| 90 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 91 | + // Enable debug messages if required |
| 92 | + |
| 93 | + //theGNSS.enableDebugging(Serial); // Uncomment this line to enable debug messages on Serial |
| 94 | + |
| 95 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 96 | + // Now begin the GNSS |
| 97 | + |
| 98 | + bool beginSuccess = false; |
| 99 | + |
| 100 | + if (theGNSSinterface == GNSS_IS_SERIAL) |
| 101 | + { |
| 102 | + beginSuccess = theGNSS.begin(gnssSerial); |
| 103 | + } |
| 104 | + |
| 105 | + else if (theGNSSinterface == GNSS_IS_SPI) |
| 106 | + { |
| 107 | + beginSuccess = theGNSS.begin(gnssSPI, gnssSPICS); // SPI, default to 4MHz |
| 108 | + |
| 109 | + //beginSuccess = theGNSS.begin(gnssSPI, gnssSPICS, 4000000); // Custom |
| 110 | + |
| 111 | + //SPISettings customSPIsettings = SPISettings(4000000, MSBFIRST, SPI_MODE0); |
| 112 | + //beginSuccess = theGNSS.begin(gnssSPI, gnssSPICS, customSPIsettings); // Custom |
| 113 | + } |
| 114 | + |
| 115 | + else // if (theGNSSinterface == GNSS_IS_I2C) // Catch-all. Default to I2C |
| 116 | + { |
| 117 | + beginSuccess = theGNSS.begin(gnssWire, gnssI2CAddress); |
| 118 | + } |
| 119 | + |
| 120 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 121 | + // Check begin was successful |
| 122 | + |
| 123 | + if (beginSuccess == false) |
| 124 | + { |
| 125 | + Serial.println(F("u-blox GNSS not detected. Please check wiring. Freezing...")); |
| 126 | + while (1) |
| 127 | + { |
| 128 | + ; // Do nothing more |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 133 | + // Disable the NMEA messages, just to show how to do it on the three interfaces: |
| 134 | + |
| 135 | + if (theGNSSinterface == GNSS_IS_SERIAL) |
| 136 | + { |
| 137 | + // Assume we're connected to UART1. Could be UART2 |
| 138 | + theGNSS.setUART1Output(COM_TYPE_UBX); //Set the UART1 port to output UBX only (turn off NMEA noise) |
| 139 | + } |
| 140 | + else if (theGNSSinterface == GNSS_IS_SPI) |
| 141 | + { |
| 142 | + theGNSS.setSPIOutput(COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise) |
| 143 | + } |
| 144 | + else // if (theGNSSinterface == GNSS_IS_I2C) // Catch-all. Default to I2C |
| 145 | + { |
| 146 | + theGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 147 | + } |
| 148 | + |
| 149 | + // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 150 | + // Read the module info |
| 151 | + |
| 152 | + if (theGNSS.getModuleInfo()) // This line is optional. getModuleName() etc. will read the info if required |
| 153 | + { |
| 154 | + Serial.print(F("The GNSS module is: ")); |
| 155 | + Serial.println(theGNSS.getModuleName()); |
| 156 | + |
| 157 | + Serial.print(F("The firmware type is: ")); |
| 158 | + Serial.println(theGNSS.getFirmwareType()); |
| 159 | + |
| 160 | + Serial.print(F("The firmware version is: ")); |
| 161 | + Serial.print(theGNSS.getFirmwareVersionHigh()); |
| 162 | + Serial.print(F(".")); |
| 163 | + Serial.println(theGNSS.getFirmwareVersionLow()); |
| 164 | + |
| 165 | + Serial.print(F("The protocol version is: ")); |
| 166 | + Serial.print(theGNSS.getProtocolVersionHigh()); |
| 167 | + Serial.print(F(".")); |
| 168 | + Serial.println(theGNSS.getProtocolVersionLow()); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +void loop() |
| 173 | +{ |
| 174 | + // Request (poll) the position, velocity and time (PVT) information. |
| 175 | + // The module only responds when a new position is available. Default is once per second. |
| 176 | + // getPVT() returns true when new data is received. |
| 177 | + if (theGNSS.getPVT() == true) |
| 178 | + { |
| 179 | + int32_t latitude = theGNSS.getLatitude(); |
| 180 | + Serial.print(F("Lat: ")); |
| 181 | + Serial.print(latitude); |
| 182 | + |
| 183 | + int32_t longitude = theGNSS.getLongitude(); |
| 184 | + Serial.print(F(" Long: ")); |
| 185 | + Serial.print(longitude); |
| 186 | + Serial.print(F(" (degrees * 10^-7)")); |
| 187 | + |
| 188 | + int32_t altitude = theGNSS.getAltitudeMSL(); // Altitude above Mean Sea Level |
| 189 | + Serial.print(F(" Alt: ")); |
| 190 | + Serial.print(altitude); |
| 191 | + Serial.print(F(" (mm)")); |
| 192 | + |
| 193 | + Serial.println(); |
| 194 | + } |
| 195 | +} |
0 commit comments