Skip to content

Add tare function and example #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Using the BNO080 IMU
By: Nathan Seidle
SparkFun Electronics
Date: December 21st, 2017
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.

Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/14586

This example shows how to output the i/j/k/real parts of the rotation vector.
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

It takes about 1ms at 400kHz I2C to read a record from the sensor, but we are polling the sensor continually
between updates from the sensor. Use the interrupt pin on the BNO080 breakout to avoid polling.

Hardware Connections:
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
Plug the sensor onto the shield
Serial.print it out at 9600 baud to serial monitor.
*/

#include <Wire.h>

#include "SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080
BNO080 myIMU;

void setup()
{
Serial.begin(9600);
Serial.println();
Serial.println("BNO080 Read Example");

Wire.begin();

//Are you using a ESP? Check this issue for more information: https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/issues/16
// //=================================
// delay(100); // Wait for BNO to boot
// // Start i2c and BNO080
// Wire.flush(); // Reset I2C
// IMU.begin(BNO080_DEFAULT_ADDRESS, Wire);
// Wire.begin(4, 5);
// Wire.setClockStretchLimit(4000);
// //=================================

if (myIMU.begin() == false)
{
Serial.println("BNO080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
while (1);
}

Wire.setClock(400000); //Increase I2C data rate to 400kHz

myIMU.enableRotationVector(50); //Send data update every 50ms

Serial.println(F("Rotation vector enabled"));
Serial.println(F("Output in form i, j, k, real, accuracy"));
}

void loop()
{
if(Serial.available())
{
byte incoming = Serial.read();

if(incoming == 't')
{
myIMU.tareAllAxes(); //tare x,y,z axes to calibrate device with default manufacturing
myIMU.saveTare(); //save tare
delay(1);
}
}

//Look for reports from the IMU
if (myIMU.dataAvailable() == true)
{
float quatI = myIMU.getQuatI();
float quatJ = myIMU.getQuatJ();
float quatK = myIMU.getQuatK();
float quatReal = myIMU.getQuatReal();
float quatRadianAccuracy = myIMU.getQuatRadianAccuracy();

/* See page 5 of Tare
* Determine North in your current environment
* a. You can either look at a physical compass, or look at the Rotation Vector to
* determine where North is.
* When the Rotation Vector reads W=1, X=0, Y=0, Z=0 the device is pointed North

* w = real
* x = i
* y = j
* z = k
*/

Serial.print(F(", W : "));
Serial.print(quatReal, 2);
Serial.print(F(", x : "));
Serial.print(quatI, 2);
Serial.print(F(", y: "));
Serial.print(quatJ, 2);
Serial.print(F(", z: "));
Serial.print(quatK, 2);
Serial.print(F(","));
Serial.print(quatRadianAccuracy, 2);
Serial.print(F(","));

Serial.println();
}
}
47 changes: 47 additions & 0 deletions src/SparkFun_BNO080_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,53 @@ boolean BNO080::calibrationComplete()
return (false);
}


//See Tare Procedure Tare Function Usage Guide
//https://www.ceva-dsp.com/wp-content/uploads/2019/09/BNO080-BNO085-Tare-Function-Usage-Guide.pdf
void BNO080::tareAllAxes()
{
/*shtpData[3] = 0; //P0 - P0 (Subcommand)
shtpData[4] = 0; //P1 - All 3 Axes(x,y,z)
shtpData[5] = 0; //P2 - Rotation vector
shtpData[6] = 0; //P3 - Reserved
shtpData[7] = 0; //P4 - Reserved
shtpData[8] = 0; //P5 - Reserved
shtpData[9] = 0; //P6 - Reserved
shtpData[10] = 0; //P7 - Reserved
shtpData[11] = 0; //P8 - Reserved*/

for (uint8_t x = 3; x < 12; x++) //Clear this section of the shtpData array
shtpData[x] = 0;

shtpData[4] = 0x07;

//Using this shtpData packet, send a command
sendCommand(COMMAND_TARE); //Save TARE command
}

//See Tare Procedure Tare Function Usage Guide
//Run the Persist Tare
//https://www.ceva-dsp.com/wp-content/uploads/2019/09/BNO080-BNO085-Tare-Function-Usage-Guide.pdf
void BNO080::saveTare()
{
/*shtpData[3] = 0; //P0 (Subcommand)
shtpData[4] = 0; //Reserved
shtpData[5] = 0; //Reserved
shtpData[6] = 0; //P3 - Reserved
shtpData[7] = 0; //P4 - Reserved
shtpData[8] = 0; //P5 - Reserved
shtpData[9] = 0; //P6 - Reserved
shtpData[10] = 0; //P7 - Reserved
shtpData[11] = 0; //P8 - Reserved*/

for (uint8_t x = 3; x < 12; x++) //Clear this section of the shtpData array
shtpData[x] = 0;

shtpData[3] = 0x01;

sendCommand(COMMAND_TARE); //Run TARE command
}

//Given a sensor's report ID, this tells the BNO080 to begin reporting the values
void BNO080::setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports)
{
Expand Down
3 changes: 3 additions & 0 deletions src/SparkFun_BNO080_Arduino_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ class BNO080
float getPitch();
float getYaw();

void BNO080::tareAllAxes();
void BNO080::saveTare();

void setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports);
void setFeatureCommand(uint8_t reportID, uint16_t timeBetweenReports, uint32_t specificConfig);
void sendCommand(uint8_t command);
Expand Down