Skip to content

Added functionality to access the Gravity vector #104

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

Merged
merged 4 commits into from
Jan 23, 2023
Merged
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
81 changes: 81 additions & 0 deletions examples/Example23-Gravity/Example23-Gravity.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Using the BNO085 for finding the direction of gravity.
By: Anant Sharma

Date: January 23rd, 2023
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 outputs a vector pointing towards the ground.

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.enableGravity(10); //Send data update every 50ms

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

}

void loop() {
//Look for reports from the IMU
if (myIMU.dataAvailable() == true)
{
float gravityX = myIMU.getGravityX();
float gravityY = myIMU.getGravityY();
float gravityZ = myIMU.getGravityZ();
float gravityAccuracy = myIMU.getGravityAccuracy();

Serial.print(gravityX, 2);
Serial.print(F(","));
Serial.print(gravityY, 2);
Serial.print(F(","));
Serial.print(gravityZ, 2);
Serial.print(F(","));
Serial.print(gravityAccuracy, 2);
Serial.print(F(","));


Serial.println();
}
}
7 changes: 7 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enableARVRStabilizedRotationVector KEYWORD2
enableARVRStabilizedGameRotationVector KEYWORD2
enableAccelerometer KEYWORD2
enableGyro KEYWORD2
enableGravity KEYWORD2
enableMagnetometer KEYWORD2
enableTapDetector KEYWORD2
enableStepCounter KEYWORD2
Expand Down Expand Up @@ -75,6 +76,12 @@ getGyroY KEYWORD2
getGyroZ KEYWORD2
getGyroAccuracy KEYWORD2

getGravity KEYWORD2
getGravityX KEYWORD2
getGravityY KEYWORD2
getGravityZ KEYWORD2
getGravityAccuracy KEYWORD2

getFastGyro KEYWORD2
getFastGyroX KEYWORD2
getFastGyroY KEYWORD2
Expand Down
50 changes: 49 additions & 1 deletion src/SparkFun_BNO080_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ uint16_t BNO080::parseInputReport(void)
calibrationStatus = shtpData[5 + 5]; //R0 - Status (0 = success, non-zero = fail)
}
}
else if(shtpData[5] == SENSOR_REPORTID_GRAVITY)
{
gravityAccuracy = status;
gravityX = data1;
gravityY = data2;
gravityZ = data3;
}
else
{
//This sensor report ID is unhandled.
Expand Down Expand Up @@ -690,6 +697,41 @@ uint8_t BNO080::getGyroAccuracy()
return (gyroAccuracy);
}

//Gets the full gravity vector
//x,y,z output floats
void BNO080::getGravity(float &x, float &y, float &z, uint8_t &accuracy)
{
x = qToFloat(gravityX, gravity_Q1);
y = qToFloat(gravityX, gravity_Q1);
z = qToFloat(gravityX, gravity_Q1);
accuracy = gravityAccuracy;
}

float BNO080::getGravityX()
{
float x = qToFloat(gravityX, gravity_Q1);
return x;
}

//Return the gravity component
float BNO080::getGravityY()
{
float y = qToFloat(gravityY, gravity_Q1);
return y;
}

//Return the gravity component
float BNO080::getGravityZ()
{
float z = qToFloat(gravityZ, gravity_Q1);
return z;
}

uint8_t BNO080::getGravityAccuracy()
{
return (gravityAccuracy);
}

//Gets the full mag vector
//x,y,z output floats
void BNO080::getMag(float &x, float &y, float &z, uint8_t &accuracy)
Expand Down Expand Up @@ -1112,6 +1154,12 @@ void BNO080::enableLinearAccelerometer(uint16_t timeBetweenReports)
setFeatureCommand(SENSOR_REPORTID_LINEAR_ACCELERATION, timeBetweenReports);
}

//Sends the packet to enable the gravity vector
void BNO080::enableGravity(uint16_t timeBetweenReports)
{
setFeatureCommand(SENSOR_REPORTID_GRAVITY, timeBetweenReports);
}

//Sends the packet to enable the gyro
void BNO080::enableGyro(uint16_t timeBetweenReports)
{
Expand Down Expand Up @@ -1689,4 +1737,4 @@ void BNO080::printHeader(void)
}
_debugPort->println();
}
}
}
11 changes: 10 additions & 1 deletion src/SparkFun_BNO080_Arduino_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class BNO080
void enableARVRStabilizedGameRotationVector(uint16_t timeBetweenReports);
void enableAccelerometer(uint16_t timeBetweenReports);
void enableLinearAccelerometer(uint16_t timeBetweenReports);
void enableGravity(uint16_t timeBetweenReports);
void enableGyro(uint16_t timeBetweenReports);
void enableMagnetometer(uint16_t timeBetweenReports);
void enableTapDetector(uint16_t timeBetweenReports);
Expand Down Expand Up @@ -208,6 +209,12 @@ class BNO080
float getMagZ();
uint8_t getMagAccuracy();

void getGravity(float &x, float &y, float &z, uint8_t &accuracy);
float getGravityX();
float getGravityY();
float getGravityZ();
uint8_t getGravityAccuracy();

void calibrateAccelerometer();
void calibrateGyro();
void calibrateMagnetometer();
Expand Down Expand Up @@ -286,6 +293,7 @@ class BNO080
uint16_t rawMagX, rawMagY, rawMagZ, magAccuracy;
uint16_t rawQuatI, rawQuatJ, rawQuatK, rawQuatReal, rawQuatRadianAccuracy, quatAccuracy;
uint16_t rawFastGyroX, rawFastGyroY, rawFastGyroZ;
uint16_t gravityX, gravityY, gravityZ, gravityAccuracy;
uint8_t tapDetector;
uint16_t stepCount;
uint32_t timeStamp;
Expand All @@ -306,4 +314,5 @@ class BNO080
int16_t gyro_Q1 = 9;
int16_t magnetometer_Q1 = 4;
int16_t angular_velocity_Q1 = 10;
};
int16_t gravity_Q1 = 8;
};