|
| 1 | +/* |
| 2 | + Send Custom Command |
| 3 | + By: Paul Clark (PaulZC) |
| 4 | + Date: April 18th, 2020 |
| 5 | +
|
| 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 you can create and send a custom UBX packet |
| 10 | + using the SparkFun u-blox library. |
| 11 | +
|
| 12 | + Previously it was possible to create and send a custom packet |
| 13 | + through the library but it would always appear to timeout as |
| 14 | + some of the internal functions referred to the internal private |
| 15 | + struct packetCfg. |
| 16 | + The most recent version of the library allows sendCommand to |
| 17 | + use a custom packet as if it were packetCfg and so: |
| 18 | + - sendCommand will return a sfe_ublox_status_e enum as if |
| 19 | + it had been called from within the library |
| 20 | + - the custom packet will be updated with data returned by the module |
| 21 | + (previously this was not possible from outside the library) |
| 22 | +
|
| 23 | + Feel like supporting open source hardware? |
| 24 | + Buy a board from SparkFun! |
| 25 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 26 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 27 | + SAM-M8Q: https://www.sparkfun.com/products/15106 |
| 28 | +
|
| 29 | + Hardware Connections: |
| 30 | + Plug a Qwiic cable into the GPS and a BlackBoard |
| 31 | + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 32 | + Open the serial monitor at 115200 baud to see the output |
| 33 | +*/ |
| 34 | + |
| 35 | +#include <Wire.h> //Needed for I2C to GPS |
| 36 | + |
| 37 | +#include "SparkFun_Ublox_Arduino_Library.h" //http://librarymanager/All#SparkFun_Ublox_GPS |
| 38 | +SFE_UBLOX_GPS myGPS; |
| 39 | + |
| 40 | +long lastTime = 0; //Simple local timer. Limits amount if I2C traffic to Ublox module. |
| 41 | + |
| 42 | +void setup() |
| 43 | +{ |
| 44 | + Serial.begin(115200); |
| 45 | + while (!Serial) |
| 46 | + ; //Wait for user to open terminal |
| 47 | + Serial.println("SparkFun Ublox Example"); |
| 48 | + |
| 49 | + Wire.begin(); |
| 50 | + |
| 51 | + //myGPS.enableDebugging(); // Uncomment this line to enable debug messages |
| 52 | + |
| 53 | + if (myGPS.begin() == false) //Connect to the Ublox module using Wire port |
| 54 | + { |
| 55 | + Serial.println(F("Ublox GPS not detected at default I2C address. Please check wiring. Freezing.")); |
| 56 | + while (1) |
| 57 | + ; |
| 58 | + } |
| 59 | + |
| 60 | + myGPS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 61 | + |
| 62 | + // Let's configure the module's dynamic platform model as if we were using setDynamicModel |
| 63 | + // Possible values are: |
| 64 | + // 0 (PORTABLE), 2 (STATIONARY), 3 (PEDESTRIAN), 4 (AUTOMOTIVE), 5 (SEA), |
| 65 | + // 6 (AIRBORNE1g), 7 (AIRBORNE2g), 8 (AIRBORNE4g), 9 (WRIST), 10 (BIKE) |
| 66 | + |
| 67 | + // Let's create our custom packet |
| 68 | + uint8_t customPayload[MAX_PAYLOAD_SIZE]; // This array holds the payload data bytes |
| 69 | + // The next line creates and initialises the packet information which wraps around the payload |
| 70 | + ubxPacket customCfg = {0, 0, 0, 0, 0, customPayload, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED}; |
| 71 | + |
| 72 | + // The structure of ubxPacket is: |
| 73 | + // uint8_t cls : The message Class |
| 74 | + // uint8_t id : The message ID |
| 75 | + // uint16_t len : Length of the payload. Does not include cls, id, or checksum bytes |
| 76 | + // uint16_t counter : Keeps track of number of overall bytes received. Some responses are larger than 255 bytes. |
| 77 | + // uint16_t startingSpot : The counter value needed to go past before we begin recording into payload array |
| 78 | + // uint8_t *payload : The payload |
| 79 | + // uint8_t checksumA : Given to us by the module. Checked against the rolling calculated A/B checksums. |
| 80 | + // uint8_t checksumB |
| 81 | + // sfe_ublox_packet_validity_e valid : Goes from NOT_DEFINED to VALID or NOT_VALID when checksum is checked |
| 82 | + // sfe_ublox_packet_validity_e classAndIDmatch : Goes from NOT_DEFINED to VALID or NOT_VALID when the Class and ID match the requestedClass and requestedID |
| 83 | + |
| 84 | + // sendCommand will return: |
| 85 | + // SFE_UBLOX_STATUS_DATA_RECEIVED if the data we requested was read / polled successfully |
| 86 | + // SFE_UBLOX_STATUS_DATA_SENT if the data we sent was writted successfully (ACK'd) |
| 87 | + // Other values indicate errors. Please see the sfe_ublox_status_e enum for further details. |
| 88 | + |
| 89 | + // Referring to the u-blox M8 Receiver Description and Protocol Specification we see that |
| 90 | + // the dynamic model is configured using the UBX-CFG-NAV5 message. So let's load our |
| 91 | + // custom packet with the correct information so we can read (poll / get) the current settings. |
| 92 | + |
| 93 | + customCfg.cls = UBX_CLASS_CFG; // This is the message Class |
| 94 | + customCfg.id = UBX_CFG_NAV5; // This is the message ID |
| 95 | + customCfg.len = 0; // Setting the len (length) to zero let's us poll the current settings |
| 96 | + customCfg.startingSpot = 0; // Always set the startingSpot to zero (unless you really know what you are doing) |
| 97 | + |
| 98 | + // We also need to tell sendCommand how long it should wait for a reply |
| 99 | + uint16_t maxWait = 250; // Wait for up to 250ms (Serial may need a lot longer e.g. 1100) |
| 100 | + |
| 101 | + // Now let's read the current navigation model settings. The results will be loaded into customCfg. |
| 102 | + if (myGPS.sendCommand(&customCfg, maxWait) != SFE_UBLOX_STATUS_DATA_RECEIVED) // We are expecting data and an ACK |
| 103 | + { |
| 104 | + Serial.println(F("sendCommand (poll / get) failed! Freezing...")); |
| 105 | + while (1) |
| 106 | + ; |
| 107 | + } |
| 108 | + |
| 109 | + // Referring to the message definition for UBX-CFG-NAV5 we see that we need to change |
| 110 | + // byte 2 to update the dynamic platform model. |
| 111 | + |
| 112 | + // Print the current dynamic model |
| 113 | + Serial.print(F("The current dynamic model is: ")); |
| 114 | + Serial.print(customPayload[2]); |
| 115 | + |
| 116 | + // Let's change it |
| 117 | + if (customPayload[2] != 0x04) // If it is currently not 4, change it to 4 |
| 118 | + { |
| 119 | + Serial.println(F(". Changing it to 4.")); |
| 120 | + customPayload[2] = 0x04; |
| 121 | + } |
| 122 | + else // If it is already 4, change it to 2 |
| 123 | + { |
| 124 | + Serial.println(F(". Changing it to 2.")); |
| 125 | + customPayload[2] = 0x02; |
| 126 | + } |
| 127 | + |
| 128 | + // We don't need to update customCfg.len as it will have been set to 36 (0x24) |
| 129 | + // when sendCommand read the data |
| 130 | + |
| 131 | + // Now we write the custom packet back again to change the setting |
| 132 | + if (myGPS.sendCommand(&customCfg, maxWait) != SFE_UBLOX_STATUS_DATA_SENT) // This time we are only expecting an ACK |
| 133 | + { |
| 134 | + Serial.println(F("sendCommand (set) failed! Freezing.")); |
| 135 | + while (1) |
| 136 | + ; |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + Serial.println(F("Dynamic platform model updated.")); |
| 141 | + } |
| 142 | + |
| 143 | + // Now let's read the navigation model settings again to see if the change was successful. |
| 144 | + |
| 145 | + // We need to reset the packet before we try again as the values could have changed |
| 146 | + customCfg.cls = UBX_CLASS_CFG; |
| 147 | + customCfg.id = UBX_CFG_NAV5; |
| 148 | + customCfg.len = 0; |
| 149 | + customCfg.startingSpot = 0; |
| 150 | + |
| 151 | + if (myGPS.sendCommand(&customCfg, maxWait) != SFE_UBLOX_STATUS_DATA_RECEIVED) // We are expecting data and an ACK |
| 152 | + { |
| 153 | + Serial.println(F("sendCommand (poll) failed! Freezing.")); |
| 154 | + while (1) |
| 155 | + ; |
| 156 | + } |
| 157 | + |
| 158 | + // Print the current dynamic model |
| 159 | + Serial.print(F("The new dynamic model is: ")); |
| 160 | + Serial.println(customPayload[2]); |
| 161 | + |
| 162 | + //myGPS.saveConfigSelective(VAL_CFG_SUBSEC_NAVCONF); //Uncomment this line to save only the NAV settings to flash and BBR |
| 163 | +} |
| 164 | + |
| 165 | +void loop() |
| 166 | +{ |
| 167 | + //Query module only every second. Doing it more often will just cause I2C traffic. |
| 168 | + //The module only responds when a new position is available |
| 169 | + if (millis() - lastTime > 1000) |
| 170 | + { |
| 171 | + lastTime = millis(); //Update the timer |
| 172 | + |
| 173 | + long latitude = myGPS.getLatitude(); |
| 174 | + Serial.print(F("Lat: ")); |
| 175 | + Serial.print(latitude); |
| 176 | + |
| 177 | + long longitude = myGPS.getLongitude(); |
| 178 | + Serial.print(F(" Long: ")); |
| 179 | + Serial.print(longitude); |
| 180 | + Serial.print(F(" (degrees * 10^-7)")); |
| 181 | + |
| 182 | + long altitude = myGPS.getAltitude(); |
| 183 | + Serial.print(F(" Alt: ")); |
| 184 | + Serial.print(altitude); |
| 185 | + Serial.print(F(" (mm)")); |
| 186 | + |
| 187 | + Serial.println(); |
| 188 | + } |
| 189 | +} |
0 commit comments