Skip to content

Commit f84b7c5

Browse files
committed
Investigating Issue #42 with printPacket
1 parent 8037323 commit f84b7c5

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// This example demonstrates what happens if you set the
2+
// Rotation Vector Report Interval to 10ms but then
3+
// deliberately only call dataAvailable() every 15ms
4+
// to retrieve the SPI data
5+
6+
#include "SparkFun_BNO080_Arduino_Library.h"
7+
BNO080 myIMU;
8+
9+
//These pins can be any GPIO
10+
byte imuCSPin = 10;
11+
byte imuWAKPin = 9;
12+
byte imuINTPin = 8;
13+
byte imuRSTPin = 7;
14+
15+
long measurements = 0;
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println();
21+
Serial.println("BNO080 SPI Read Example");
22+
Serial.println("Press any key to begin...");
23+
while (!Serial.available()) // Wait for a 'key press'
24+
;
25+
while (Serial.available()) Serial.read(); // Clear the buffer
26+
27+
myIMU.enableDebugging(); // Enable debug messages so we can use printPacket
28+
29+
if(myIMU.beginSPI(imuCSPin, imuWAKPin, imuINTPin, imuRSTPin) == false)
30+
{
31+
Serial.println("BNO080 over SPI not detected. Are you sure you have all 7 connections? Freezing...");
32+
while(1);
33+
}
34+
35+
myIMU.enableRotationVector(10); //Send data update every 10ms
36+
}
37+
38+
void loop()
39+
{
40+
delay(15); // Deliberately wait 15ms between calls to dataAvailable
41+
42+
//Look for reports from the IMU
43+
if (myIMU.dataAvailable() == true)
44+
{
45+
measurements++;
46+
}
47+
48+
// Pressing any key (sending one or more serial bytes)
49+
// prints the measurement tally
50+
if (Serial.available())
51+
{
52+
Serial.println();
53+
Serial.print(F("Measurements: "));
54+
Serial.println(measurements);
55+
while (Serial.available()) Serial.read();
56+
}
57+
}

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ receivePacket KEYWORD2
2828
getData KEYWORD2
2929
sendPacket KEYWORD2
3030
printPacket KEYWORD2
31+
printHeader KEYWORD2
3132

3233
enableRotationVector KEYWORD2
3334
enableGameRotationVector KEYWORD2

src/SparkFun_BNO080_Arduino_Library.cpp

+60-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ boolean BNO080::begin(uint8_t deviceAddress, TwoWire &wirePort, uint8_t intPin)
5656
{
5757
if (shtpData[0] == SHTP_REPORT_PRODUCT_ID_RESPONSE)
5858
{
59+
if (_printDebug == true)
60+
{
61+
_debugPort->print(F("SW Version Major: 0x"));
62+
_debugPort->print(shtpData[2], HEX);
63+
_debugPort->print(F(" SW Version Minor: 0x"));
64+
_debugPort->print(shtpData[3], HEX);
65+
uint32_t SW_Part_Number = ((uint32_t)shtpData[7] << 24) | ((uint32_t)shtpData[6] << 16) | ((uint32_t)shtpData[5] << 8) | ((uint32_t)shtpData[4]);
66+
_debugPort->print(F(" SW Part Number: 0x"));
67+
_debugPort->print(SW_Part_Number, HEX);
68+
uint32_t SW_Build_Number = ((uint32_t)shtpData[11] << 24) | ((uint32_t)shtpData[10] << 16) | ((uint32_t)shtpData[9] << 8) | ((uint32_t)shtpData[8]);
69+
_debugPort->print(F(" SW Build Number: 0x"));
70+
_debugPort->print(SW_Build_Number, HEX);
71+
uint16_t SW_Version_Patch = ((uint16_t)shtpData[13] << 8) | ((uint16_t)shtpData[12]);
72+
_debugPort->print(F(" SW Version Patch: 0x"));
73+
_debugPort->println(SW_Version_Patch, HEX);
74+
}
5975
return (true);
6076
}
6177
}
@@ -123,6 +139,22 @@ boolean BNO080::beginSPI(uint8_t user_CSPin, uint8_t user_WAKPin, uint8_t user_I
123139
if (receivePacket() == true)
124140
{
125141
if (shtpData[0] == SHTP_REPORT_PRODUCT_ID_RESPONSE)
142+
if (_printDebug == true)
143+
{
144+
_debugPort->print(F("SW Version Major: 0x"));
145+
_debugPort->print(shtpData[2], HEX);
146+
_debugPort->print(F(" SW Version Minor: 0x"));
147+
_debugPort->print(shtpData[3], HEX);
148+
uint32_t SW_Part_Number = ((uint32_t)shtpData[7] << 24) | ((uint32_t)shtpData[6] << 16) | ((uint32_t)shtpData[5] << 8) | ((uint32_t)shtpData[4]);
149+
_debugPort->print(F(" SW Part Number: 0x"));
150+
_debugPort->print(SW_Part_Number, HEX);
151+
uint32_t SW_Build_Number = ((uint32_t)shtpData[11] << 24) | ((uint32_t)shtpData[10] << 16) | ((uint32_t)shtpData[9] << 8) | ((uint32_t)shtpData[8]);
152+
_debugPort->print(F(" SW Build Number: 0x"));
153+
_debugPort->print(SW_Build_Number, HEX);
154+
uint16_t SW_Version_Patch = ((uint16_t)shtpData[13] << 8) | ((uint16_t)shtpData[12]);
155+
_debugPort->print(F(" SW Version Patch: 0x"));
156+
_debugPort->println(SW_Version_Patch, HEX);
157+
}
126158
return (true);
127159
}
128160

@@ -346,13 +378,19 @@ void BNO080::parseInputReport(void)
346378
}
347379
else if (shtpData[5] == SHTP_REPORT_COMMAND_RESPONSE)
348380
{
349-
Serial.println("!");
381+
if (_printDebug == true)
382+
{
383+
_debugPort->println(F("!"));
384+
}
350385
//The BNO080 responds with this report to command requests. It's up to use to remember which command we issued.
351386
uint8_t command = shtpData[5 + 2]; //This is the Command byte of the response
352387

353388
if (command == COMMAND_ME_CALIBRATE)
354389
{
355-
Serial.println("ME Cal report found!");
390+
if (_printDebug == true)
391+
{
392+
_debugPort->println(F("ME Cal report found!"));
393+
}
356394
calibrationStatus = shtpData[5 + 5]; //R0 - Status (0 = success, non-zero = fail)
357395
}
358396
}
@@ -1231,6 +1269,7 @@ boolean BNO080::receivePacket(void)
12311269
if (dataLength == 0)
12321270
{
12331271
//Packet is empty
1272+
printHeader();
12341273
return (false); //All done
12351274
}
12361275
dataLength -= 4; //Remove the header bytes from the data count
@@ -1246,7 +1285,7 @@ boolean BNO080::receivePacket(void)
12461285
digitalWrite(_cs, HIGH); //Release BNO080
12471286

12481287
_spiPort->endTransaction();
1249-
printPacket();
1288+
printPacket();
12501289
}
12511290
else //Do I2C
12521291
{
@@ -1443,3 +1482,21 @@ void BNO080::printPacket(void)
14431482
_debugPort->println();
14441483
}
14451484
}
1485+
1486+
//Pretty prints the contents of the current shtp header (only)
1487+
void BNO080::printHeader(void)
1488+
{
1489+
if (_printDebug == true)
1490+
{
1491+
//Print the four byte header
1492+
_debugPort->print(F("Header:"));
1493+
for (uint8_t x = 0; x < 4; x++)
1494+
{
1495+
_debugPort->print(F(" "));
1496+
if (shtpHeader[x] < 0x10)
1497+
_debugPort->print(F("0"));
1498+
_debugPort->print(shtpHeader[x], HEX);
1499+
}
1500+
_debugPort->println();
1501+
}
1502+
}

src/SparkFun_BNO080_Arduino_Library.h

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class BNO080
146146
boolean getData(uint16_t bytesRemaining); //Given a number of bytes, send the requests in I2C_BUFFER_LENGTH chunks
147147
boolean sendPacket(uint8_t channelNumber, uint8_t dataLength);
148148
void printPacket(void); //Prints the current shtp header and data packets
149+
void printHeader(void); //Prints the current shtp header (only)
149150

150151
void enableRotationVector(uint16_t timeBetweenReports);
151152
void enableGameRotationVector(uint16_t timeBetweenReports);

0 commit comments

Comments
 (0)