Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active August 17, 2023 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bboyho/070ced658b78314a269508ddeb8a246a to your computer and use it in GitHub Desktop.
Save bboyho/070ced658b78314a269508ddeb8a246a to your computer and use it in GitHub Desktop.
/*
Using the BNO08x IMU
This example shows how to output the i/j/k/real parts of the rotation vector.
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
Note that this is a modified example which comments out unnecessary Serial Prints
so that we can simply output the comma separated values of the rotation vector
for the Processing demo "Serial_Cube_Rotate.pde".
By: Nathan Seidle
SparkFun Electronics
Date: December 21st, 2017
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Originally written by Nathan Seidle @ SparkFun Electronics, December 28th, 2017
Adjusted by Pete Lewis @ SparkFun Electronics, June 2023 to incorporate the
CEVA Sensor Hub Driver, found here:
https://github.com/ceva-dsp/sh2
Also, utilizing code from the Adafruit BNO08x Arduino Library by Bryan Siepert
for Adafruit Industries. Found here:
https://github.com/adafruit/Adafruit_BNO08x
Also, utilizing I2C and SPI read/write functions and code from the Adafruit
BusIO library found here:
https://github.com/adafruit/Adafruit_BusIO
Hardware Connections:
Plug the sensor into IoT RedBoard via QWIIC cable.
Serial.print it out at 115200 baud to serial monitor.
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/22857
*/
#include <Wire.h>
#include "SparkFun_BNO08x_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO08x
BNO08x myIMU;
void setup() {
Serial.begin(9600); //115200 baud seems to be too much for Processing?... 9600 baud seems better
//Serial.println();
//Serial.println("BNO08x Read Example");
Wire.begin();
if (myIMU.begin() == false) {
//Serial.println("BNO08x not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
while (1)
;
}
//Serial.println("BNO08x found!");
// Wire.setClock(400000); //Increase I2C data rate to 400kHz
setReports();
//Serial.println("Reading events");
delay(100);
}
// Here is where you define the sensor outputs you want to receive
void setReports(void) {
//Serial.println("Setting desired reports");
if (myIMU.enableRotationVector() == true) {
//Serial.println(F("Rotation vector enabled"));
//Serial.println(F("Output in form i, j, k, real, accuracy"));
} else {
//Serial.println("Could not enable rotation vector");
}
}
void loop() {
delay(10);
if (myIMU.wasReset()) {
//Serial.print("sensor was reset ");
setReports();
}
// Has a new event come in on the Sensor Hub Bus?
if (myIMU.getSensorEvent() == true) {
// is it the correct sensor data we want?
if (myIMU.getSensorEventID() == SENSOR_REPORTID_ROTATION_VECTOR) {
float quatI = myIMU.getQuatI();
float quatJ = myIMU.getQuatJ();
float quatK = myIMU.getQuatK();
float quatReal = myIMU.getQuatReal();
float quatRadianAccuracy = myIMU.getQuatRadianAccuracy();
Serial.print(quatReal, 2); //<==move these two lines of code
Serial.print(F(",")); // for Processing
Serial.print(quatI, 2);
Serial.print(F(","));
Serial.print(quatJ, 2);
Serial.print(F(","));
Serial.print(quatK, 2);
// Serial.print(F(","));
//Serial.print(quatRadianAccuracy, 2);
Serial.println();
}
}
}
/**
* <p>The ToxiclibsSupport class of the toxi.processing package provides various
* shortcuts to directly use toxiclibs geometry datatypes with Processing style
* drawing operations. Most of these are demonstrated in this example.</p>
*
* <p>UPDATES:
* <ul>
* <li>2010-12-30: added sphere/cylinder resolution modulation</li>
* </ul></p>
*/
/*
* Copyright (c) 2010 Karsten Schmidt
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* http://creativecommons.org/licenses/LGPL/2.1/
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//Download and manually install "toxiclibs-complete-0021.zip" => https://github.com/postspectacular/toxiclibs/releases/tag/0021
//Make sure to install in the correct path using Processing 3 => https://github.com/processing/processing/wiki/How-to-Install-a-Contributed-Library#manual-install
import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.math.waves.*;
import toxi.processing.*;
import processing.serial.*;
String myString = null;
Serial myPort; // The serial port
ToxiclibsSupport gfx;
boolean waitFlag = true;
void setup() {
size(600,600,P3D);
gfx=new ToxiclibsSupport(this);
background(255);
noStroke();
fill(200,200,200);
// Print a list of connected serial devices in the console
printArray(Serial.list());
// Depending on where your BNO086 falls on this list, you
// may need to change Serial.list()[0] to a different number
myPort = new Serial(this, Serial.list()[6], 9600); //115200 baud seems to be too much for Processing?... 9600 baud seems better
delay(50); //small delay as we wait for the ESP32 to initialize since it outputs serial on boot, this seems to help
myPort.clear();
// Throw out the first chunk in case we caught it in the
// middle of a frame
myString = myPort.readStringUntil(13);
myString = null;
}
void draw() {
Quaternion RotQ = new Quaternion(1,0,0,0);
float qMatrix[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
PMatrix M1 = getMatrix();
// When there is a sizeable amount of data on the serial port
// read everything up to the first linefeed
if(myPort.available() > 30){
myString = myPort.readStringUntil(13);
// generate an array of strings that contains each of the commas
// separated values
String inQuat[] = splitTokens(myString, ",");
// make sure that inQuat has a length of 4 before proceeding
if (inQuat.length == 4){
// build a Quaternion from inQuat[] array
RotQ = new Quaternion(float(inQuat[0]), float(inQuat[1]), float(inQuat[2]),float(inQuat[3]));
RotQ.toMatrix4x4().toFloatArray(qMatrix);
M1.set(
qMatrix[0],
qMatrix[1],
qMatrix[2],
qMatrix[3],
qMatrix[4],
qMatrix[5],
qMatrix[6],
qMatrix[7],
qMatrix[8],
qMatrix[9],
qMatrix[10],
qMatrix[11],
qMatrix[12],
qMatrix[13],
qMatrix[14],
qMatrix[15]
);
AABB cube;
background(255);
// Set some mood lighting
ambientLight(128, 128, 128);
directionalLight(128, 128, 128, 0, 0, 1);
lightFalloff(1, 0, 0);
lightSpecular(0, 0, 0);
// Get to the middle of the screen
translate(width/2,height/2,0);
// Do some rotates to get oriented "behind" the device
rotateX(-PI/2);
// Apply the Matrix that we generated from our IMU Quaternion
applyMatrix(M1);
// Draw the Cube from a 3D Bounding Box
cube=new AABB(new Vec3D(0,0,0),new Vec3D(100,100,100));
gfx.box(cube);
}
}else{
if(waitFlag){
textSize(32);
text("Waiting for quaternions to chew on...", 10, 30);
waitFlag = false;}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment