From 611911db6cc62959cf84dac2d7d25a414909c061 Mon Sep 17 00:00:00 2001 From: Anant Sharma Date: Mon, 23 Jan 2023 15:17:17 +0530 Subject: [PATCH 1/3] Added functionality to retrieve gravity packets from the device. - Added functionality on the BNO080::parseInputReport(void) to interpret gravity packets. - Added getGravityX, getGravityY and getGravityZ in the same format as the others reports. - Added the function BNO080::enableGravity() which also functions like the other reports. Tested with Teensy 4.0, Adafruit ItsyBitsy and ESP32. --- src/SparkFun_BNO080_Arduino_Library.cpp | 35 ++++++++++++++++++++++++- src/SparkFun_BNO080_Arduino_Library.h | 9 ++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/SparkFun_BNO080_Arduino_Library.cpp b/src/SparkFun_BNO080_Arduino_Library.cpp index 1ebdfe7..6623d90 100644 --- a/src/SparkFun_BNO080_Arduino_Library.cpp +++ b/src/SparkFun_BNO080_Arduino_Library.cpp @@ -401,6 +401,12 @@ uint16_t BNO080::parseInputReport(void) calibrationStatus = shtpData[5 + 5]; //R0 - Status (0 = success, non-zero = fail) } } + else if(shtpData[5] == SENSOR_REPORTID_GRAVITY) + { + gravityX = data1; + gravityY = data2; + gravityZ = data3; + } else { //This sensor report ID is unhandled. @@ -663,6 +669,27 @@ void BNO080::getGyro(float &x, float &y, float &z, uint8_t &accuracy) accuracy = gyroAccuracy; } +//Return the gravity component +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; +} + //Return the gyro component float BNO080::getGyroX() { @@ -1112,6 +1139,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) { @@ -1689,4 +1722,4 @@ void BNO080::printHeader(void) } _debugPort->println(); } -} +} \ No newline at end of file diff --git a/src/SparkFun_BNO080_Arduino_Library.h b/src/SparkFun_BNO080_Arduino_Library.h index 0b1c811..1e70326 100644 --- a/src/SparkFun_BNO080_Arduino_Library.h +++ b/src/SparkFun_BNO080_Arduino_Library.h @@ -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); @@ -208,6 +209,10 @@ class BNO080 float getMagZ(); uint8_t getMagAccuracy(); + float getGravityX(); + float getGravityY(); + float getGravityZ(); + void calibrateAccelerometer(); void calibrateGyro(); void calibrateMagnetometer(); @@ -286,6 +291,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; uint8_t tapDetector; uint16_t stepCount; uint32_t timeStamp; @@ -306,4 +312,5 @@ class BNO080 int16_t gyro_Q1 = 9; int16_t magnetometer_Q1 = 4; int16_t angular_velocity_Q1 = 10; -}; + int16_t gravity_Q1 = 8; +}; \ No newline at end of file From 1c1d069f7a9181c9430ff1cd17e7b6acaf9d453d Mon Sep 17 00:00:00 2001 From: Anant Sharma Date: Mon, 23 Jan 2023 15:39:09 +0530 Subject: [PATCH 2/3] Added an example for checking gravity packets --- .../Example23-Gravity/Example23-Gravity.ino | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/Example23-Gravity/Example23-Gravity.ino diff --git a/examples/Example23-Gravity/Example23-Gravity.ino b/examples/Example23-Gravity/Example23-Gravity.ino new file mode 100644 index 0000000..cd0f3d9 --- /dev/null +++ b/examples/Example23-Gravity/Example23-Gravity.ino @@ -0,0 +1,77 @@ +/* + 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 +#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(); + + Serial.print(gravityX, 2); + Serial.print(F(",")); + Serial.print(gravityY, 2); + Serial.print(F(",")); + Serial.print(gravityZ, 2); + Serial.print(F(",")); + + Serial.println(); + } +} From e1f2aaa38b9164bd040e8892e1f35efb016f46d6 Mon Sep 17 00:00:00 2001 From: Anant Sharma Date: Mon, 23 Jan 2023 15:56:37 +0530 Subject: [PATCH 3/3] Added gravity accuracy tests --- .../Example23-Gravity/Example23-Gravity.ino | 4 ++ keywords.txt | 7 +++ src/SparkFun_BNO080_Arduino_Library.cpp | 57 ++++++++++++------- src/SparkFun_BNO080_Arduino_Library.h | 4 +- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/examples/Example23-Gravity/Example23-Gravity.ino b/examples/Example23-Gravity/Example23-Gravity.ino index cd0f3d9..3c96e12 100644 --- a/examples/Example23-Gravity/Example23-Gravity.ino +++ b/examples/Example23-Gravity/Example23-Gravity.ino @@ -64,6 +64,7 @@ void loop() { float gravityX = myIMU.getGravityX(); float gravityY = myIMU.getGravityY(); float gravityZ = myIMU.getGravityZ(); + float gravityAccuracy = myIMU.getGravityAccuracy(); Serial.print(gravityX, 2); Serial.print(F(",")); @@ -71,6 +72,9 @@ void loop() { Serial.print(F(",")); Serial.print(gravityZ, 2); Serial.print(F(",")); + Serial.print(gravityAccuracy, 2); + Serial.print(F(",")); + Serial.println(); } diff --git a/keywords.txt b/keywords.txt index 318ed33..277eb0d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -39,6 +39,7 @@ enableARVRStabilizedRotationVector KEYWORD2 enableARVRStabilizedGameRotationVector KEYWORD2 enableAccelerometer KEYWORD2 enableGyro KEYWORD2 +enableGravity KEYWORD2 enableMagnetometer KEYWORD2 enableTapDetector KEYWORD2 enableStepCounter KEYWORD2 @@ -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 diff --git a/src/SparkFun_BNO080_Arduino_Library.cpp b/src/SparkFun_BNO080_Arduino_Library.cpp index 6623d90..43bc429 100644 --- a/src/SparkFun_BNO080_Arduino_Library.cpp +++ b/src/SparkFun_BNO080_Arduino_Library.cpp @@ -403,6 +403,7 @@ uint16_t BNO080::parseInputReport(void) } else if(shtpData[5] == SENSOR_REPORTID_GRAVITY) { + gravityAccuracy = status; gravityX = data1; gravityY = data2; gravityZ = data3; @@ -669,27 +670,6 @@ void BNO080::getGyro(float &x, float &y, float &z, uint8_t &accuracy) accuracy = gyroAccuracy; } -//Return the gravity component -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; -} - //Return the gyro component float BNO080::getGyroX() { @@ -717,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) diff --git a/src/SparkFun_BNO080_Arduino_Library.h b/src/SparkFun_BNO080_Arduino_Library.h index 1e70326..ae7a550 100644 --- a/src/SparkFun_BNO080_Arduino_Library.h +++ b/src/SparkFun_BNO080_Arduino_Library.h @@ -209,9 +209,11 @@ 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(); @@ -291,7 +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; + uint16_t gravityX, gravityY, gravityZ, gravityAccuracy; uint8_t tapDetector; uint16_t stepCount; uint32_t timeStamp;