Skip to content

Commit 6f1944b

Browse files
committed
Add tare function and example
1 parent 24603cc commit 6f1944b

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Using the BNO080 IMU
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: December 21st, 2017
6+
SparkFun code, firmware, and software is released under the MIT License.
7+
Please see LICENSE.md for further details.
8+
9+
Feel like supporting our work? Buy a board from SparkFun!
10+
https://www.sparkfun.com/products/14586
11+
12+
This example shows how to output the i/j/k/real parts of the rotation vector.
13+
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
14+
15+
It takes about 1ms at 400kHz I2C to read a record from the sensor, but we are polling the sensor continually
16+
between updates from the sensor. Use the interrupt pin on the BNO080 breakout to avoid polling.
17+
18+
Hardware Connections:
19+
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
20+
Plug the sensor onto the shield
21+
Serial.print it out at 9600 baud to serial monitor.
22+
*/
23+
24+
#include <Wire.h>
25+
26+
#include "SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080
27+
BNO080 myIMU;
28+
29+
void setup()
30+
{
31+
Serial.begin(9600);
32+
Serial.println();
33+
Serial.println("BNO080 Read Example");
34+
35+
Wire.begin();
36+
37+
//Are you using a ESP? Check this issue for more information: https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/issues/16
38+
// //=================================
39+
// delay(100); // Wait for BNO to boot
40+
// // Start i2c and BNO080
41+
// Wire.flush(); // Reset I2C
42+
// IMU.begin(BNO080_DEFAULT_ADDRESS, Wire);
43+
// Wire.begin(4, 5);
44+
// Wire.setClockStretchLimit(4000);
45+
// //=================================
46+
47+
if (myIMU.begin() == false)
48+
{
49+
Serial.println("BNO080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
50+
while (1);
51+
}
52+
53+
Wire.setClock(400000); //Increase I2C data rate to 400kHz
54+
55+
myIMU.enableRotationVector(50); //Send data update every 50ms
56+
57+
Serial.println(F("Rotation vector enabled"));
58+
Serial.println(F("Output in form i, j, k, real, accuracy"));
59+
}
60+
61+
void loop()
62+
{
63+
if(Serial.available())
64+
{
65+
byte incoming = Serial.read();
66+
67+
if(incoming == 't')
68+
{
69+
myIMU.tareAllAxes(); //tare x,y,z axes to calibrate device with default manufacturing
70+
myIMU.saveTare(); //save tare
71+
delay(1);
72+
}
73+
}
74+
75+
//Look for reports from the IMU
76+
if (myIMU.dataAvailable() == true)
77+
{
78+
float quatI = myIMU.getQuatI();
79+
float quatJ = myIMU.getQuatJ();
80+
float quatK = myIMU.getQuatK();
81+
float quatReal = myIMU.getQuatReal();
82+
float quatRadianAccuracy = myIMU.getQuatRadianAccuracy();
83+
84+
/* See page 5 of Tare
85+
* Determine North in your current environment
86+
* a. You can either look at a physical compass, or look at the Rotation Vector to
87+
* determine where North is.
88+
* When the Rotation Vector reads W=1, X=0, Y=0, Z=0 the device is pointed North
89+
90+
* w = real
91+
* x = i
92+
* y = j
93+
* z = k
94+
*/
95+
96+
Serial.print(F(", W : "));
97+
Serial.print(quatReal, 2);
98+
Serial.print(F(", x : "));
99+
Serial.print(quatI, 2);
100+
Serial.print(F(", y: "));
101+
Serial.print(quatJ, 2);
102+
Serial.print(F(", z: "));
103+
Serial.print(quatK, 2);
104+
Serial.print(F(","));
105+
Serial.print(quatRadianAccuracy, 2);
106+
Serial.print(F(","));
107+
108+
Serial.println();
109+
}
110+
}

src/SparkFun_BNO080_Arduino_Library.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,53 @@ boolean BNO080::calibrationComplete()
12211221
return (false);
12221222
}
12231223

1224+
1225+
//See Tare Procedure Tare Function Usage Guide
1226+
//https://www.ceva-dsp.com/wp-content/uploads/2019/09/BNO080-BNO085-Tare-Function-Usage-Guide.pdf
1227+
void BNO080::tareAllAxes()
1228+
{
1229+
/*shtpData[3] = 0; //P0 - P0 (Subcommand)
1230+
shtpData[4] = 0; //P1 - All 3 Axes(x,y,z)
1231+
shtpData[5] = 0; //P2 - Rotation vector
1232+
shtpData[6] = 0; //P3 - Reserved
1233+
shtpData[7] = 0; //P4 - Reserved
1234+
shtpData[8] = 0; //P5 - Reserved
1235+
shtpData[9] = 0; //P6 - Reserved
1236+
shtpData[10] = 0; //P7 - Reserved
1237+
shtpData[11] = 0; //P8 - Reserved*/
1238+
1239+
for (uint8_t x = 3; x < 12; x++) //Clear this section of the shtpData array
1240+
shtpData[x] = 0;
1241+
1242+
shtpData[4] = 0x07;
1243+
1244+
//Using this shtpData packet, send a command
1245+
sendCommand(COMMAND_TARE); //Save TARE command
1246+
}
1247+
1248+
//See Tare Procedure Tare Function Usage Guide
1249+
//Run the Persist Tare
1250+
//https://www.ceva-dsp.com/wp-content/uploads/2019/09/BNO080-BNO085-Tare-Function-Usage-Guide.pdf
1251+
void BNO080::saveTare()
1252+
{
1253+
/*shtpData[3] = 0; //P0 (Subcommand)
1254+
shtpData[4] = 0; //Reserved
1255+
shtpData[5] = 0; //Reserved
1256+
shtpData[6] = 0; //P3 - Reserved
1257+
shtpData[7] = 0; //P4 - Reserved
1258+
shtpData[8] = 0; //P5 - Reserved
1259+
shtpData[9] = 0; //P6 - Reserved
1260+
shtpData[10] = 0; //P7 - Reserved
1261+
shtpData[11] = 0; //P8 - Reserved*/
1262+
1263+
for (uint8_t x = 3; x < 12; x++) //Clear this section of the shtpData array
1264+
shtpData[x] = 0;
1265+
1266+
shtpData[3] = 0x01;
1267+
1268+
sendCommand(COMMAND_TARE); //Run TARE command
1269+
}
1270+
12241271
//Given a sensor's report ID, this tells the BNO080 to begin reporting the values
12251272
void BNO080::setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports)
12261273
{

src/SparkFun_BNO080_Arduino_Library.h

+3
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ class BNO080
240240
float getPitch();
241241
float getYaw();
242242

243+
void BNO080::tareAllAxes();
244+
void BNO080::saveTare();
245+
243246
void setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports);
244247
void setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports, uint32_t specificConfig);
245248
void sendCommand(uint8_t command);

0 commit comments

Comments
 (0)