Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit d420820

Browse files
committed
Update Example20_SendCustomCommand.ino
1 parent 66ee2b3 commit d420820

File tree

1 file changed

+32
-60
lines changed

1 file changed

+32
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Send Custom Command
33
By: Paul Clark (PaulZC)
4-
Date: April 18th, 2020
4+
Date: April 20th, 2020
55
66
License: MIT. See license file for more information but you can
77
basically do whatever you want with this code.
@@ -32,16 +32,16 @@
3232
Open the serial monitor at 115200 baud to see the output
3333
*/
3434

35+
#define NAV_RATE 20 // The new navigation rate in Hz (measurements per second)
36+
3537
#include <Wire.h> //Needed for I2C to GPS
3638

3739
#include "SparkFun_Ublox_Arduino_Library.h" //http://librarymanager/All#SparkFun_Ublox_GPS
3840
SFE_UBLOX_GPS myGPS;
3941

40-
long lastTime = 0; //Simple local timer. Limits amount if I2C traffic to Ublox module.
41-
4242
void setup()
4343
{
44-
Serial.begin(115200);
44+
Serial.begin(115200); // You may need to increase this for high navigation rates!
4545
while (!Serial)
4646
; //Wait for user to open terminal
4747
Serial.println("SparkFun Ublox Example");
@@ -59,10 +59,7 @@ void setup()
5959

6060
myGPS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
6161

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)
62+
// Let's configure the module's navigation rate as if we were using setNavigationFrequency
6663

6764
// Let's create our custom packet
6865
uint8_t customPayload[MAX_PAYLOAD_SIZE]; // This array holds the payload data bytes
@@ -87,45 +84,45 @@ void setup()
8784
// Other values indicate errors. Please see the sfe_ublox_status_e enum for further details.
8885

8986
// 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
87+
// the navigation rate is configured using the UBX-CFG-RATE message. So let's load our
9188
// custom packet with the correct information so we can read (poll / get) the current settings.
9289

9390
customCfg.cls = UBX_CLASS_CFG; // This is the message Class
94-
customCfg.id = UBX_CFG_NAV5; // This is the message ID
91+
customCfg.id = UBX_CFG_RATE; // This is the message ID
9592
customCfg.len = 0; // Setting the len (length) to zero let's us poll the current settings
9693
customCfg.startingSpot = 0; // Always set the startingSpot to zero (unless you really know what you are doing)
9794

9895
// We also need to tell sendCommand how long it should wait for a reply
9996
uint16_t maxWait = 250; // Wait for up to 250ms (Serial may need a lot longer e.g. 1100)
10097

101-
// Now let's read the current navigation model settings. The results will be loaded into customCfg.
98+
// Now let's read the current navigation rate. The results will be loaded into customCfg.
10299
if (myGPS.sendCommand(&customCfg, maxWait) != SFE_UBLOX_STATUS_DATA_RECEIVED) // We are expecting data and an ACK
103100
{
104101
Serial.println(F("sendCommand (poll / get) failed! Freezing..."));
105102
while (1)
106103
;
107104
}
108105

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.
106+
// Referring to the message definition for UBX-CFG-RATE we see that the measurement rate
107+
// is stored in payload bytes 0 and 1 as a uint16_t in LSB-first (little endian) format
108+
109+
uint16_t rate = (customPayload[1] << 8) | customPayload[0]; // Extract the current rate (ms)
110+
float f_rate = 1000.0 / ((float)rate); // Convert the navigation rate to Hz (measurements per second)
111111

112-
// Print the current dynamic model
113-
Serial.print(F("The current dynamic model is: "));
114-
Serial.print(customPayload[2]);
112+
// Print the current measurement rate
113+
Serial.print(F("The current measurement rate is: "));
114+
Serial.println(f_rate, 1);
115115

116116
// 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-
}
117+
rate = 1000 / NAV_RATE; // Load the new value into rate
118+
customPayload[0] = rate & 0xFF; // Store it in the payload
119+
customPayload[1] = rate >> 8;
120+
121+
// Print the new measurement rate
122+
Serial.print(F("The new measurement rate will be: "));
123+
Serial.println(NAV_RATE);
127124

128-
// We don't need to update customCfg.len as it will have been set to 36 (0x24)
125+
// We don't need to update customCfg.len as it will have been set to 6
129126
// when sendCommand read the data
130127

131128
// Now we write the custom packet back again to change the setting
@@ -137,53 +134,28 @@ void setup()
137134
}
138135
else
139136
{
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-
;
137+
Serial.println(F("Navigation rate updated. Here we go..."));
156138
}
157139

158-
// Print the current dynamic model
159-
Serial.print(F("The new dynamic model is: "));
160-
Serial.println(customPayload[2]);
140+
myGPS.setAutoPVT(true); // Enable AutoPVT. The module will generate measurements automatically without being polled.
161141

162142
//myGPS.saveConfigSelective(VAL_CFG_SUBSEC_NAVCONF); //Uncomment this line to save only the NAV settings to flash and BBR
163143
}
164144

165145
void loop()
166146
{
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();
147+
//Query the module as fast as possible
148+
int32_t latitude = myGPS.getLatitude();
174149
Serial.print(F("Lat: "));
175150
Serial.print(latitude);
176151

177-
long longitude = myGPS.getLongitude();
178-
Serial.print(F(" Long: "));
152+
int32_t longitude = myGPS.getLongitude();
153+
Serial.print(F(" Lon: "));
179154
Serial.print(longitude);
180155
Serial.print(F(" (degrees * 10^-7)"));
181156

182-
long altitude = myGPS.getAltitude();
157+
int32_t altitude = myGPS.getAltitude();
183158
Serial.print(F(" Alt: "));
184159
Serial.print(altitude);
185-
Serial.print(F(" (mm)"));
186-
187-
Serial.println();
188-
}
160+
Serial.println(F(" (mm)"));
189161
}

0 commit comments

Comments
 (0)