1
1
/*
2
2
Send Custom Command
3
3
By: Paul Clark (PaulZC)
4
- Date: April 18th , 2020
4
+ Date: April 20th , 2020
5
5
6
6
License: MIT. See license file for more information but you can
7
7
basically do whatever you want with this code.
32
32
Open the serial monitor at 115200 baud to see the output
33
33
*/
34
34
35
+ #define NAV_RATE 20 // The new navigation rate in Hz (measurements per second)
36
+
35
37
#include < Wire.h> // Needed for I2C to GPS
36
38
37
39
#include " SparkFun_Ublox_Arduino_Library.h" // http://librarymanager/All#SparkFun_Ublox_GPS
38
40
SFE_UBLOX_GPS myGPS;
39
41
40
- long lastTime = 0 ; // Simple local timer. Limits amount if I2C traffic to Ublox module.
41
-
42
42
void setup ()
43
43
{
44
- Serial.begin (115200 );
44
+ Serial.begin (115200 ); // You may need to increase this for high navigation rates!
45
45
while (!Serial)
46
46
; // Wait for user to open terminal
47
47
Serial.println (" SparkFun Ublox Example" );
@@ -59,10 +59,7 @@ void setup()
59
59
60
60
myGPS.setI2COutput (COM_TYPE_UBX); // Set the I2C port to output UBX only (turn off NMEA noise)
61
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)
62
+ // Let's configure the module's navigation rate as if we were using setNavigationFrequency
66
63
67
64
// Let's create our custom packet
68
65
uint8_t customPayload[MAX_PAYLOAD_SIZE]; // This array holds the payload data bytes
@@ -87,45 +84,45 @@ void setup()
87
84
// Other values indicate errors. Please see the sfe_ublox_status_e enum for further details.
88
85
89
86
// 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
91
88
// custom packet with the correct information so we can read (poll / get) the current settings.
92
89
93
90
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
95
92
customCfg.len = 0 ; // Setting the len (length) to zero let's us poll the current settings
96
93
customCfg.startingSpot = 0 ; // Always set the startingSpot to zero (unless you really know what you are doing)
97
94
98
95
// We also need to tell sendCommand how long it should wait for a reply
99
96
uint16_t maxWait = 250 ; // Wait for up to 250ms (Serial may need a lot longer e.g. 1100)
100
97
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.
102
99
if (myGPS.sendCommand (&customCfg, maxWait) != SFE_UBLOX_STATUS_DATA_RECEIVED) // We are expecting data and an ACK
103
100
{
104
101
Serial.println (F (" sendCommand (poll / get) failed! Freezing..." ));
105
102
while (1 )
106
103
;
107
104
}
108
105
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)
111
111
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 );
115
115
116
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
- }
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);
127
124
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
129
126
// when sendCommand read the data
130
127
131
128
// Now we write the custom packet back again to change the setting
@@ -137,53 +134,28 @@ void setup()
137
134
}
138
135
else
139
136
{
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..." ));
156
138
}
157
139
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.
161
141
162
142
// myGPS.saveConfigSelective(VAL_CFG_SUBSEC_NAVCONF); //Uncomment this line to save only the NAV settings to flash and BBR
163
143
}
164
144
165
145
void loop ()
166
146
{
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 ();
174
149
Serial.print (F (" Lat: " ));
175
150
Serial.print (latitude);
176
151
177
- long longitude = myGPS.getLongitude ();
178
- Serial.print (F (" Long : " ));
152
+ int32_t longitude = myGPS.getLongitude ();
153
+ Serial.print (F (" Lon : " ));
179
154
Serial.print (longitude);
180
155
Serial.print (F (" (degrees * 10^-7)" ));
181
156
182
- long altitude = myGPS.getAltitude ();
157
+ int32_t altitude = myGPS.getAltitude ();
183
158
Serial.print (F (" Alt: " ));
184
159
Serial.print (altitude);
185
- Serial.print (F (" (mm)" ));
186
-
187
- Serial.println ();
188
- }
160
+ Serial.println (F (" (mm)" ));
189
161
}
0 commit comments