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

Commit c5b2754

Browse files
committed
Fixed config packet counter reset bug. Added print for debug.
1 parent 8edc8f2 commit c5b2754

File tree

3 files changed

+170
-23
lines changed

3 files changed

+170
-23
lines changed

examples/Example3_GetPosition/Example3_GetPosition.ino

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,30 @@ void setup()
4343
while (1);
4444
}
4545

46-
Wire.setClock(400000); //Increase I2C clock speed to 400kHz
46+
//Wire.setClock(400000); //Increase I2C clock speed to 400kHz
47+
48+
/*byte response;
49+
response = myGPS.getVal(VAL_GROUP_I2C, VAL_ID_I2C_ADDRESS, VAL_GROUP_I2C_SIZE, VAL_LAYER_RAM);
50+
Serial.print("res: 0x");
51+
Serial.println(response, HEX);
52+
53+
delay(100);
54+
55+
response = myGPS.getVal(VAL_GROUP_I2COUTPROT, VAL_ID_I2COUTPROT_NMEA, VAL_GROUP_I2COUTPROT_SIZE, VAL_LAYER_RAM);
56+
Serial.print("res: 0x");
57+
Serial.print(response, HEX);
58+
while(1);*/
59+
4760

4861
myGPS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
49-
50-
myGPS.setUSBOutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
51-
5262
myGPS.setNavigationFrequency(4); //Set output to 4 times a second
5363

64+
byte rate = myGPS.getNavigationFrequency(); //Get the update rate of this module
65+
Serial.print("Update rate:");
66+
Serial.println(rate);
67+
68+
while(1);
69+
5470
/*long pos = myGPS.getPositionAccuracy();
5571
Serial.print("pos: ");
5672
Serial.println(pos);

src/SparkFun_Ublox_Arduino_Library.cpp

Lines changed: 120 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ boolean SFE_UBLOX_GPS::checkUbloxSerial()
158158
//Take a given byte and file it into the proper array
159159
void SFE_UBLOX_GPS::process(uint8_t incoming)
160160
{
161+
162+
#ifdef DEBUG
163+
//if (currentSentence == NONE && incoming == 0xB5) //UBX binary frames start with 0xB5, aka μ
164+
// debug.println(); //Show new packet start
165+
166+
//debug.print(" ");
167+
//debug.print(incoming, HEX);
168+
#endif
169+
161170
if (currentSentence == NONE || currentSentence == NMEA)
162171
{
163172
if (incoming == 0xB5) //UBX binary frames start with 0xB5, aka μ
@@ -196,26 +205,23 @@ void SFE_UBLOX_GPS::process(uint8_t incoming)
196205
currentSentence = NONE; //Something went wrong. Reset.
197206
else if (ubxFrameCounter == 2) //Class
198207
{
208+
packetAck.counter = 0;
209+
packetAck.valid = false;
210+
packetCfg.counter = 0;
211+
packetCfg.valid = false;
212+
199213
//We can now identify the type of response
200214
if (incoming == UBX_CLASS_ACK)
201-
{
202215
ubxFrameClass = CLASS_ACK;
203-
packetAck.counter = 0;
204-
packetAck.valid = false;
205-
}
206216
else
207-
{
208217
ubxFrameClass = CLASS_NOT_AN_ACK;
209-
packetCfg.counter = 0;
210-
packetCfg.valid = false;
211-
}
212218
}
213219

214220
ubxFrameCounter++;
215221

216222
//Depending on this frame's class, pass different structs and payload arrays
217223
if (ubxFrameClass == CLASS_ACK)
218-
processUBX(incoming, &packetAck);
224+
processUBX(incoming, &packetAck);
219225
else if (ubxFrameClass == CLASS_NOT_AN_ACK)
220226
processUBX(incoming, &packetCfg);
221227
}
@@ -343,8 +349,8 @@ void SFE_UBLOX_GPS::processUBX(uint8_t incoming, ubxPacket *incomingUBX)
343349
if (incomingUBX->checksumA == rollingChecksumA && incomingUBX->checksumB == rollingChecksumB)
344350
{
345351
#ifdef DEBUG
346-
debug.println("Checksum/Frame Good");
347-
//printFrame(incomingUBX);
352+
debug.print("Received: ");
353+
printPacket(incomingUBX);
348354
#endif
349355
incomingUBX->valid = true;
350356
processUBXpacket(incomingUBX); //We've got a valid packet, now do something with it
@@ -404,6 +410,11 @@ boolean SFE_UBLOX_GPS::sendCommand(ubxPacket outgoingUBX, uint16_t maxWait)
404410

405411
calcChecksum(&outgoingUBX); //Sets checksum A and B bytes of the packet
406412

413+
#ifdef DEBUG
414+
debug.print("Sending: ");
415+
printPacket(&outgoingUBX);
416+
#endif
417+
407418
//Point at 0xFF data register
408419
_i2cPort->beginTransmission((uint8_t)_gpsI2Caddress); //There is no register to write to, we just begin writing data bytes
409420
_i2cPort->write(0xFF);
@@ -508,8 +519,32 @@ void SFE_UBLOX_GPS::addToChecksum(uint8_t incoming)
508519
rollingChecksumB += rollingChecksumA;
509520
}
510521

522+
//Pretty prints the current ubxPacket
523+
void SFE_UBLOX_GPS::printPacket(ubxPacket *packet)
524+
{
525+
debug.print("CLS:");
526+
debug.print(packet->cls, HEX);
527+
528+
debug.print(" ID:");
529+
debug.print(packet->id, HEX);
530+
531+
//debug.print(" Len: 0x");
532+
//debug.print(packet->len, HEX);
533+
534+
debug.print(" Payload:");
535+
536+
for(int x = 0 ; x < packet->len ; x++)
537+
{
538+
debug.print(" ");
539+
debug.print(packet->payload[x], HEX);
540+
}
541+
debug.println();
542+
}
543+
544+
545+
//=-=-=-=-=-=-=-= Specific commands =-=-=-=-=-=-=-==-=-=-=-=-=-=-=
546+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
511547

512-
//=-=-=-=-=-=-=-= Specific commands =-=-=-=-=-=-=-=
513548

514549
//Poll the module until and ack is received
515550
boolean SFE_UBLOX_GPS::waitForResponse(uint16_t maxTime)
@@ -522,10 +557,8 @@ boolean SFE_UBLOX_GPS::waitForResponse(uint16_t maxTime)
522557
{
523558
checkUblox(); //See if new data is available. Process bytes as they come in.
524559

525-
//If the packet we just sent was a CFG packet then we'll get an ACK
526-
//If the packet we just sent was a NAV packet then we'll just get data back
527-
if (commandAck == true) return (true);
528-
if (packetCfg.valid == true) return (true);
560+
if (commandAck == true) return (true); //If the packet we just sent was a CFG packet then we'll get an ACK
561+
if (packetCfg.valid == true) return (true); //If the packet we just sent was a NAV packet then we'll just get data back
529562
}
530563

531564
#ifdef DEBUG
@@ -535,6 +568,48 @@ boolean SFE_UBLOX_GPS::waitForResponse(uint16_t maxTime)
535568
return (false);
536569
}
537570

571+
//Given a key, return its value
572+
//This is how the new Ublox modules are communicating, ie protocol v27 and above found on ZED-F9P
573+
uint8_t SFE_UBLOX_GPS::getVal(uint16_t group, uint16_t id, uint8_t size, uint8_t layer, uint16_t maxWait)
574+
{
575+
packetCfg.cls = UBX_CLASS_CFG;
576+
packetCfg.id = UBX_CFG_VALGET;
577+
packetCfg.len = 4 + 4*1; //Only one key at a time
578+
packetCfg.startingSpot = 0;
579+
580+
//Clear packet payload
581+
for(uint8_t x = 0 ; x < packetCfg.len ; x++)
582+
packetCfg.payload[x] = 0;
583+
584+
payloadCfg[0] = 0; //Message Version - set to 0
585+
payloadCfg[1] = layer;
586+
587+
//Create key
588+
uint32_t key = 0;
589+
key |= (uint32_t)id;
590+
key |= (uint32_t)group << 16;
591+
key |= (uint32_t)size << 28;
592+
593+
#ifdef DEBUG
594+
Serial.print("key: 0x");
595+
Serial.print(key, HEX);
596+
Serial.println();
597+
#endif
598+
599+
//Load key into outgoing payload
600+
payloadCfg[4] = key >> 8*0; //Key LSB
601+
payloadCfg[5] = key >> 8*1;
602+
payloadCfg[6] = key >> 8*2;
603+
payloadCfg[7] = key >> 8*3;
604+
605+
//Send VALGET command with this key
606+
if(sendCommand(packetCfg, maxWait) == false)
607+
return(false); //If command send fails then bail
608+
609+
//Pull the requested value from the response
610+
return(extractByte(8)); //Look for our response value at location 4+4*N
611+
}
612+
538613
//Get the current TimeMode3 settings - these contain survey in statuses
539614
boolean SFE_UBLOX_GPS::getSurveyMode(uint16_t maxWait)
540615
{
@@ -672,7 +747,8 @@ boolean SFE_UBLOX_GPS::getPortSettings(uint8_t portID, uint16_t maxWait)
672747
boolean SFE_UBLOX_GPS::setPortOutput(uint8_t portID, uint8_t outStreamSettings, uint16_t maxWait)
673748
{
674749
//Get the current config values for this port ID
675-
getPortSettings(portID); //This will load the payloadCfg array with current port settings
750+
if(getPortSettings(portID) == false)
751+
return(false); //Something went wrong. Bail.
676752

677753
//Yes, this is the depreciated way to do it but it's still supported on v27 so it
678754
//covers both ZED-F9P (v27) and SAM-M8Q (v18)
@@ -694,7 +770,9 @@ boolean SFE_UBLOX_GPS::setPortOutput(uint8_t portID, uint8_t outStreamSettings,
694770
boolean SFE_UBLOX_GPS::setPortInput(uint8_t portID, uint8_t inStreamSettings, uint16_t maxWait)
695771
{
696772
//Get the current config values for this port ID
697-
getPortSettings(portID); //This will load the payloadCfg array with current port settings
773+
//This will load the payloadCfg array with current port settings
774+
if(getPortSettings(portID) == false)
775+
return(false); //Something went wrong. Bail.
698776

699777
packetCfg.cls = UBX_CLASS_CFG;
700778
packetCfg.id = UBX_CFG_PRT;
@@ -745,6 +823,9 @@ boolean SFE_UBLOX_GPS::setNavigationFrequency(uint8_t navFreq, uint16_t maxWait)
745823
if(sendCommand(packetCfg, maxWait) == false) //This will load the payloadCfg array with current settings of the given register
746824
return(false); //If command send fails then bail
747825

826+
Serial.print("Len: ");
827+
Serial.print(packetCfg.len);
828+
748829
uint16_t measurementRate = 1000 / navFreq ;
749830

750831
//payloadCfg is now loaded with current bytes. Change only the ones we need to
@@ -754,6 +835,27 @@ boolean SFE_UBLOX_GPS::setNavigationFrequency(uint8_t navFreq, uint16_t maxWait)
754835
return ( sendCommand(packetCfg, maxWait) );
755836
}
756837

838+
//Get the rate at which the module is outputting nav solutions
839+
uint8_t SFE_UBLOX_GPS::getNavigationFrequency(uint16_t maxWait)
840+
{
841+
//Query the module for the latest lat/long
842+
packetCfg.cls = UBX_CLASS_CFG;
843+
packetCfg.id = UBX_CFG_RATE;
844+
packetCfg.len = 0;
845+
packetCfg.startingSpot = 0;
846+
847+
if(sendCommand(packetCfg, maxWait) == false) //This will load the payloadCfg array with current settings of the given register
848+
return(0); //If command send fails then bail
849+
850+
uint16_t measurementRate = 0;
851+
852+
//payloadCfg is now loaded with current bytes. Get what we need
853+
measurementRate = extractInt(0); //Pull from payloadCfg at measRate LSB
854+
855+
measurementRate = 1000 / measurementRate; //This may return an int when it's a float, but I'd rather not return 4 bytes
856+
return (measurementRate);
857+
}
858+
757859
//Given a spot in the payload array, extract four bytes and build a long
758860
uint32_t SFE_UBLOX_GPS::extractLong(uint8_t spotToStart)
759861
{

src/SparkFun_Ublox_Arduino_Library.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ const uint8_t UBX_CLASS_HNR = 0x28;
9494

9595
const uint8_t UBX_CFG_PRT = 0x00; //Used to configure port specifics
9696
const uint8_t UBX_CFG_RATE = 0x08; //Used to set port baud rates
97+
const uint8_t UBX_CFG_VALSET = 0x8A; //Used for config of higher version Ublox modules (ie protocol v27 and above)
98+
const uint8_t UBX_CFG_VALGET = 0x8B; //Used for config of higher version Ublox modules (ie protocol v27 and above)
99+
const uint8_t UBX_CFG_VALDEL = 0x8C; //Used for config of higher version Ublox modules (ie protocol v27 and above)
97100

98101
const uint8_t UBX_CFG_TMODE3 = 0x71; //Used to enable Survey In Mode
99102
const uint8_t SVIN_MODE_DISABLE = 0x00;
@@ -117,7 +120,7 @@ const uint8_t UBX_RTCM_1230 = 0xE6; //GLONASS code-phase biases, set to once eve
117120
const uint8_t UBX_ACK_NACK = 0x00;
118121
const uint8_t UBX_ACK_ACK = 0x01;
119122

120-
//The folloing consts are used to configure the various ports and streams for those ports. See -CFG-PRT.
123+
//The following consts are used to configure the various ports and streams for those ports. See -CFG-PRT.
121124
const uint8_t COM_PORT_I2C = 0;
122125
const uint8_t COM_PORT_UART1 = 1;
123126
const uint8_t COM_PORT_UART2 = 2;
@@ -128,6 +131,26 @@ const uint8_t COM_TYPE_UBX = (1<<0);
128131
const uint8_t COM_TYPE_NMEA = (1<<1);
129132
const uint8_t COM_TYPE_RTCM3 = (1<<5);
130133

134+
//The following consts are used to generate KEY values for the advanced protocol functions of VELGET/SET/DEL
135+
const uint8_t VAL_SIZE_BIT = 0x01;
136+
const uint8_t VAL_SIZE_BYTE = 0x02;
137+
138+
const uint8_t VAL_LAYER_RAM = 0;
139+
const uint8_t VAL_LAYER_BBR = 1;
140+
const uint8_t VAL_LAYER_FLASH = 2;
141+
const uint8_t VAL_LAYER_DEFAULT = 7;
142+
143+
const uint8_t VAL_GROUP_I2COUTPROT_SIZE = VAL_SIZE_BIT; //All fields in I2C group are currently 1 bit
144+
const uint8_t VAL_GROUP_I2COUTPROT = 0x72;
145+
146+
const uint8_t VAL_ID_I2COUTPROT_UBX = 0x01;
147+
const uint8_t VAL_ID_I2COUTPROT_NMEA = 0x02;
148+
const uint8_t VAL_ID_I2COUTPROT_RTCM3 = 0x03;
149+
150+
const uint8_t VAL_GROUP_I2C_SIZE = VAL_SIZE_BYTE; //All fields in I2C group are currently 1 byte
151+
const uint8_t VAL_GROUP_I2C = 0x51;
152+
153+
const uint8_t VAL_ID_I2C_ADDRESS = 0x01;
131154

132155
#define MAX_PAYLOAD_SIZE 64 //Some commands are larger than 64 bytes but this covers most
133156

@@ -171,10 +194,13 @@ class SFE_UBLOX_GPS
171194
void calcChecksum(ubxPacket *msg); //Sets the checksumA and checksumB of a given messages
172195
boolean sendCommand(ubxPacket outgoingUBX, uint16_t maxWait = 250); //Given a packet and payload, send everything including CRC bytes
173196

197+
void printPacket(ubxPacket *packet); //Useful for debugging
198+
174199
boolean setI2CAddress(uint8_t deviceAddress, uint16_t maxTime = 250); //Changes the I2C address of the Ublox module
175200
void setNMEAOutputPort(Stream &nmeaOutputPort); //Sets the internal variable for the port to direct NMEA characters to
176201

177202
boolean setNavigationFrequency(uint8_t navFreq, uint16_t maxWait = 250); //Set the number of nav solutions sent per second
203+
uint8_t getNavigationFrequency(uint16_t maxWait = 250); //Get the number of nav solutions sent per second currently being output by module
178204

179205
boolean waitForResponse(uint16_t maxTime = 250); //Poll the module until and ack is received
180206

@@ -200,6 +226,9 @@ class SFE_UBLOX_GPS
200226
boolean setUSBOutput(uint8_t comSettings, uint16_t maxWait = 250); //Configure USB port to output UBX, NMEA, RTCM3 or a combination thereof
201227
boolean setSPIOutput(uint8_t comSettings, uint16_t maxWait = 250); //Configure SPI port to output UBX, NMEA, RTCM3 or a combination thereof
202228

229+
//General configuration (used only on protocol v27 and higher - ie, ZED-F9P)
230+
uint8_t getVal(uint16_t group, uint16_t id, uint8_t size, uint8_t layer, uint16_t maxWait = 250); //Returns the value at a given group/id/size location
231+
203232
//Functions used for RTK and base station setup
204233
boolean getSurveyMode(uint16_t maxWait = 250); //Get the current TimeMode3 settings
205234
boolean setSurveyMode(uint8_t mode, uint16_t observationTime, float requiredAccuracy, uint16_t maxWait = 250); //Control survey in mode

0 commit comments

Comments
 (0)