|
| 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 | +} |
0 commit comments