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