Skip to content

Commit d2a757f

Browse files
authored
Merge pull request #16 from sparkfun/geomagnetic_rotation_vector
Geomagnetic rotation vector
2 parents 67e2941 + 4961d98 commit d2a757f

File tree

5 files changed

+158
-1
lines changed

5 files changed

+158
-1
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ In addition the BNO08x IMU provides a built-in step counter, tap detector, activ
1515

1616
This Arduino Library was built off the [original library](https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library) written by Nathan Seidle ([SparkFun](http://www.sparkfun.com)).
1717

18-
Thanks to all those who have helped improve the original library:
18+
Thanks to all those who have helped improve this library!
1919

20+
This BNO086 Library - CEVA-DSP Sh2 Driver-based improvements:
21+
* SFSailor for providing code for Geomagnetic Rotation Vector [issue #10](https://github.com/sparkfun/SparkFun_BNO08x_Arduino_Library/issues/10)
22+
* LazaroFilm for adding setReorientation() [PR15](https://github.com/sparkfun/SparkFun_BNO08x_Arduino_Library/pull/15)
23+
* rah2501 for fixing an INT/RST pin bug [PR13](https://github.com/sparkfun/SparkFun_BNO08x_Arduino_Library/pull/13)
24+
25+
Original BNO080 Library improvements:
2026
* blendmaster for adding [Linear Accel report](https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/pull/4)
2127
* per1234 for fixing our [keywords file](https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/pull/12)
2228
* fm4dd for typo - [PR 19](https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/pull/19)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
/*
3+
Using the BNO08x IMU
4+
5+
Example : Geomagnetic Rotation Vector to Euler Angles
6+
7+
This example was initially created by github user SFSailor November 2023
8+
https://github.com/sparkfun/SparkFun_BNO08x_Arduino_Library/issues/10
9+
https://forum.sparkfun.com/viewtopic.php?f=83&t=60523&p=245145#p245145
10+
11+
Modified from original example of Euler Angles
12+
By: Paul Clark
13+
Date: April 28th, 2020
14+
15+
Using the Geomagnetic Rotation vectors,
16+
This example shows how to output the Euler angles: roll, pitch and yaw.
17+
The yaw (compass heading) is tilt-compensated, which is nice.
18+
https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
19+
https://github.com/sparkfun/SparkFun_MPU-9250-DMP_Arduino_Library/issues/5#issuecomment-306509440
20+
21+
By: Nathan Seidle
22+
SparkFun Electronics
23+
Date: December 21st, 2017
24+
SparkFun code, firmware, and software is released under the MIT License.
25+
Please see LICENSE.md for further details.
26+
27+
Originally written by Nathan Seidle @ SparkFun Electronics, December 28th, 2017
28+
29+
Adjusted by Pete Lewis @ SparkFun Electronics, June 2023 to incorporate the
30+
CEVA Sensor Hub Driver, found here:
31+
https://github.com/ceva-dsp/sh2
32+
33+
Also, utilizing code from the Adafruit BNO08x Arduino Library by Bryan Siepert
34+
for Adafruit Industries. Found here:
35+
https://github.com/adafruit/Adafruit_BNO08x
36+
37+
Also, utilizing I2C and SPI read/write functions and code from the Adafruit
38+
BusIO library found here:
39+
https://github.com/adafruit/Adafruit_BusIO
40+
41+
Hardware Connections:
42+
IoT RedBoard --> BNO08x
43+
QWIIC --> QWIIC
44+
A4 --> INT
45+
A5 --> RST
46+
47+
BNO08x "mode" jumpers set for I2C (default):
48+
PSO: OPEN
49+
PS1: OPEN
50+
51+
Serial.print it out at 115200 baud to serial monitor.
52+
53+
Feel like supporting our work? Buy a board from SparkFun!
54+
https://www.sparkfun.com/products/22857
55+
*/
56+
57+
#include <Wire.h>
58+
59+
#include "SparkFun_BNO08x_Arduino_Library.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_BNO08x
60+
BNO08x myIMU;
61+
62+
// For the most reliable interaction with the SHTP bus, we need
63+
// to use hardware reset control, and to monitor the H_INT pin.
64+
// The H_INT pin will go low when its okay to talk on the SHTP bus.
65+
// Note, these can be other GPIO if you like.
66+
// Define as -1 to disable these features.
67+
#define BNO08X_INT A4
68+
//#define BNO08X_INT -1
69+
#define BNO08X_RST A5
70+
//#define BNO08X_RST -1
71+
72+
#define BNO08X_ADDR 0x4B // SparkFun BNO08x Breakout (Qwiic) defaults to 0x4B
73+
//#define BNO08X_ADDR 0x4A // Alternate address if ADR jumper is closed
74+
75+
void setup() {
76+
Serial.begin(115200);
77+
78+
while(!Serial) delay(10); // Wait for Serial to become available.
79+
// Necessary for boards with native USB (like the SAMD51 Thing+).
80+
// For a final version of a project that does not need serial debug (or a USB cable plugged in),
81+
// Comment out this while loop, or it will prevent the remaining code from running.
82+
83+
Serial.println();
84+
Serial.println("BNO08x Read Example");
85+
86+
Wire.begin();
87+
88+
//if (myIMU.begin() == false) { // Setup without INT/RST control (Not Recommended)
89+
if (myIMU.begin(BNO08X_ADDR, Wire, BNO08X_INT, BNO08X_RST) == false) {
90+
Serial.println("BNO08x not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
91+
while (1)
92+
;
93+
}
94+
Serial.println("BNO08x found!");
95+
96+
// Wire.setClock(400000); //Increase I2C data rate to 400kHz
97+
98+
setReports();
99+
100+
Serial.println("Reading events");
101+
delay(100);
102+
}
103+
104+
// Here is where you define the sensor outputs you want to receive
105+
void setReports(void) {
106+
Serial.println("Setting desired reports");
107+
if (myIMU.enableGeomagneticRotationVector() == true) {
108+
Serial.println(F("Geomagnetic Rotation vector enabled"));
109+
Serial.println(F("Output in form roll, pitch, yaw"));
110+
} else {
111+
Serial.println("Could not enable geomagnetic rotation vector");
112+
}
113+
}
114+
115+
void loop() {
116+
delay(10);
117+
118+
if (myIMU.wasReset()) {
119+
Serial.print("sensor was reset ");
120+
setReports();
121+
}
122+
123+
// Has a new event come in on the Sensor Hub Bus?
124+
if (myIMU.getSensorEvent() == true) {
125+
126+
// is it the correct sensor data we want?
127+
if (myIMU.getSensorEventID() == SENSOR_REPORTID_GEOMAGNETIC_ROTATION_VECTOR) {
128+
129+
float roll = (myIMU.getRoll()) * 180.0 / PI; // Convert roll to degrees
130+
float pitch = (myIMU.getPitch()) * 180.0 / PI; // Convert pitch to degrees
131+
float yaw = (myIMU.getYaw()) * 180.0 / PI; // Convert yaw / heading to degrees
132+
133+
Serial.print(roll, 1);
134+
Serial.print(F(","));
135+
Serial.print(pitch, 1);
136+
Serial.print(F(","));
137+
Serial.print(yaw, 1);
138+
139+
Serial.println();
140+
}
141+
}
142+
}

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ modeSleep KEYWORD2
2626
qToFloat KEYWORD2
2727

2828
enableRotationVector KEYWORD2
29+
enableGeomagneticRotationVector KEYWORD2
2930
enableGameRotationVector KEYWORD2
3031
enableARVRStabilizedRotationVector KEYWORD2
3132
enableARVRStabilizedGameRotationVector KEYWORD2

src/SparkFun_BNO08x_Arduino_Library.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,13 @@ bool BNO08x::enableRotationVector(uint16_t timeBetweenReports)
796796
return enableReport(SH2_ROTATION_VECTOR, timeBetweenReports);
797797
}
798798

799+
//Sends the packet to enable the geomagnetic rotation vector
800+
bool BNO08x::enableGeomagneticRotationVector(uint16_t timeBetweenReports)
801+
{
802+
timeBetweenReports = timeBetweenReports * 1000; // ms to us
803+
return enableReport(SH2_GEOMAGNETIC_ROTATION_VECTOR, timeBetweenReports);
804+
}
805+
799806
//Sends the packet to enable the ar/vr stabilized rotation vector
800807
bool BNO08x::enableARVRStabilizedRotationVector(uint16_t timeBetweenReports)
801808
{

src/SparkFun_BNO08x_Arduino_Library.h

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class BNO08x
171171
float qToFloat(int16_t fixedPointValue, uint8_t qPoint); //Given a Q value, converts fixed point floating to regular floating point number
172172

173173
bool enableRotationVector(uint16_t timeBetweenReports = 10);
174+
bool enableGeomagneticRotationVector(uint16_t timeBetweenReports = 10);
174175
bool enableGameRotationVector(uint16_t timeBetweenReports = 10);
175176
bool enableARVRStabilizedRotationVector(uint16_t timeBetweenReports);
176177
bool enableARVRStabilizedGameRotationVector(uint16_t timeBetweenReports);

0 commit comments

Comments
 (0)