Skip to content

Commit 308d0ca

Browse files
authored
Merge pull request #104 from anant3110/main
Added functionality to access the Gravity vector
2 parents 33c2091 + e1f2aaa commit 308d0ca

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Using the BNO085 for finding the direction of gravity.
3+
By: Anant Sharma
4+
5+
Date: January 23rd, 2023
6+
SparkFun code, firmware, and software is released under the MIT License.
7+
Please see LICENSE.md for further details.
8+
Feel like supporting our work? Buy a board from SparkFun!
9+
https://www.sparkfun.com/products/14586
10+
This example outputs a vector pointing towards the ground.
11+
12+
It takes about 1ms at 400kHz I2C to read a record from the sensor, but we are polling the sensor continually
13+
between updates from the sensor. Use the interrupt pin on the BNO080 breakout to avoid polling.
14+
15+
Hardware Connections:
16+
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
17+
Plug the sensor onto the shield
18+
19+
Serial.print it out at 9600 baud to serial monitor.
20+
*/
21+
22+
#include <Wire.h>
23+
#include "SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080
24+
BNO080 myIMU;
25+
26+
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
Serial.println();
31+
Serial.println("BNO080 Read Example");
32+
33+
Wire.begin();
34+
35+
//Are you using a ESP? Check this issue for more information: https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/issues/16
36+
// //=================================
37+
// delay(100); // Wait for BNO to boot
38+
// // Start i2c and BNO080
39+
// Wire.flush(); // Reset I2C
40+
// IMU.begin(BNO080_DEFAULT_ADDRESS, Wire);
41+
// Wire.begin(4, 5);
42+
// Wire.setClockStretchLimit(4000);
43+
// //=================================
44+
45+
if (myIMU.begin() == false)
46+
{
47+
Serial.println("BNO080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
48+
while (1);
49+
}
50+
51+
Wire.setClock(400000); //Increase I2C data rate to 400kHz
52+
53+
myIMU.enableGravity(10); //Send data update every 50ms
54+
55+
Serial.println(F("Rotation vector enabled"));
56+
Serial.println(F("Output in form i, j, k, real, accuracy"));
57+
58+
}
59+
60+
void loop() {
61+
//Look for reports from the IMU
62+
if (myIMU.dataAvailable() == true)
63+
{
64+
float gravityX = myIMU.getGravityX();
65+
float gravityY = myIMU.getGravityY();
66+
float gravityZ = myIMU.getGravityZ();
67+
float gravityAccuracy = myIMU.getGravityAccuracy();
68+
69+
Serial.print(gravityX, 2);
70+
Serial.print(F(","));
71+
Serial.print(gravityY, 2);
72+
Serial.print(F(","));
73+
Serial.print(gravityZ, 2);
74+
Serial.print(F(","));
75+
Serial.print(gravityAccuracy, 2);
76+
Serial.print(F(","));
77+
78+
79+
Serial.println();
80+
}
81+
}

keywords.txt

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ enableARVRStabilizedRotationVector KEYWORD2
3939
enableARVRStabilizedGameRotationVector KEYWORD2
4040
enableAccelerometer KEYWORD2
4141
enableGyro KEYWORD2
42+
enableGravity KEYWORD2
4243
enableMagnetometer KEYWORD2
4344
enableTapDetector KEYWORD2
4445
enableStepCounter KEYWORD2
@@ -75,6 +76,12 @@ getGyroY KEYWORD2
7576
getGyroZ KEYWORD2
7677
getGyroAccuracy KEYWORD2
7778

79+
getGravity KEYWORD2
80+
getGravityX KEYWORD2
81+
getGravityY KEYWORD2
82+
getGravityZ KEYWORD2
83+
getGravityAccuracy KEYWORD2
84+
7885
getFastGyro KEYWORD2
7986
getFastGyroX KEYWORD2
8087
getFastGyroY KEYWORD2

src/SparkFun_BNO080_Arduino_Library.cpp

+49-1
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ uint16_t BNO080::parseInputReport(void)
401401
calibrationStatus = shtpData[5 + 5]; //R0 - Status (0 = success, non-zero = fail)
402402
}
403403
}
404+
else if(shtpData[5] == SENSOR_REPORTID_GRAVITY)
405+
{
406+
gravityAccuracy = status;
407+
gravityX = data1;
408+
gravityY = data2;
409+
gravityZ = data3;
410+
}
404411
else
405412
{
406413
//This sensor report ID is unhandled.
@@ -690,6 +697,41 @@ uint8_t BNO080::getGyroAccuracy()
690697
return (gyroAccuracy);
691698
}
692699

700+
//Gets the full gravity vector
701+
//x,y,z output floats
702+
void BNO080::getGravity(float &x, float &y, float &z, uint8_t &accuracy)
703+
{
704+
x = qToFloat(gravityX, gravity_Q1);
705+
y = qToFloat(gravityX, gravity_Q1);
706+
z = qToFloat(gravityX, gravity_Q1);
707+
accuracy = gravityAccuracy;
708+
}
709+
710+
float BNO080::getGravityX()
711+
{
712+
float x = qToFloat(gravityX, gravity_Q1);
713+
return x;
714+
}
715+
716+
//Return the gravity component
717+
float BNO080::getGravityY()
718+
{
719+
float y = qToFloat(gravityY, gravity_Q1);
720+
return y;
721+
}
722+
723+
//Return the gravity component
724+
float BNO080::getGravityZ()
725+
{
726+
float z = qToFloat(gravityZ, gravity_Q1);
727+
return z;
728+
}
729+
730+
uint8_t BNO080::getGravityAccuracy()
731+
{
732+
return (gravityAccuracy);
733+
}
734+
693735
//Gets the full mag vector
694736
//x,y,z output floats
695737
void BNO080::getMag(float &x, float &y, float &z, uint8_t &accuracy)
@@ -1112,6 +1154,12 @@ void BNO080::enableLinearAccelerometer(uint16_t timeBetweenReports)
11121154
setFeatureCommand(SENSOR_REPORTID_LINEAR_ACCELERATION, timeBetweenReports);
11131155
}
11141156

1157+
//Sends the packet to enable the gravity vector
1158+
void BNO080::enableGravity(uint16_t timeBetweenReports)
1159+
{
1160+
setFeatureCommand(SENSOR_REPORTID_GRAVITY, timeBetweenReports);
1161+
}
1162+
11151163
//Sends the packet to enable the gyro
11161164
void BNO080::enableGyro(uint16_t timeBetweenReports)
11171165
{
@@ -1689,4 +1737,4 @@ void BNO080::printHeader(void)
16891737
}
16901738
_debugPort->println();
16911739
}
1692-
}
1740+
}

src/SparkFun_BNO080_Arduino_Library.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class BNO080
155155
void enableARVRStabilizedGameRotationVector(uint16_t timeBetweenReports);
156156
void enableAccelerometer(uint16_t timeBetweenReports);
157157
void enableLinearAccelerometer(uint16_t timeBetweenReports);
158+
void enableGravity(uint16_t timeBetweenReports);
158159
void enableGyro(uint16_t timeBetweenReports);
159160
void enableMagnetometer(uint16_t timeBetweenReports);
160161
void enableTapDetector(uint16_t timeBetweenReports);
@@ -208,6 +209,12 @@ class BNO080
208209
float getMagZ();
209210
uint8_t getMagAccuracy();
210211

212+
void getGravity(float &x, float &y, float &z, uint8_t &accuracy);
213+
float getGravityX();
214+
float getGravityY();
215+
float getGravityZ();
216+
uint8_t getGravityAccuracy();
217+
211218
void calibrateAccelerometer();
212219
void calibrateGyro();
213220
void calibrateMagnetometer();
@@ -286,6 +293,7 @@ class BNO080
286293
uint16_t rawMagX, rawMagY, rawMagZ, magAccuracy;
287294
uint16_t rawQuatI, rawQuatJ, rawQuatK, rawQuatReal, rawQuatRadianAccuracy, quatAccuracy;
288295
uint16_t rawFastGyroX, rawFastGyroY, rawFastGyroZ;
296+
uint16_t gravityX, gravityY, gravityZ, gravityAccuracy;
289297
uint8_t tapDetector;
290298
uint16_t stepCount;
291299
uint32_t timeStamp;
@@ -306,4 +314,5 @@ class BNO080
306314
int16_t gyro_Q1 = 9;
307315
int16_t magnetometer_Q1 = 4;
308316
int16_t angular_velocity_Q1 = 10;
309-
};
317+
int16_t gravity_Q1 = 8;
318+
};

0 commit comments

Comments
 (0)