Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions examples/Example16-AccessMultiple/Example16-AccessMultiple.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @guihomework ,
Thank you for submitting this PR.
Some changes are required...
We already have an Example16. Please change the title of this file to Example18-AccessMultiple.ino.
We have separate examples for I2C and SPI. Please change this example so it uses I2C. You can include a separate SPI example in the SPI\examples folder.
Please comment out the Teensy-specific setMOSI commands etc.. Please see the other SPI examples and use the same format - using generic SPI. It is OK to leave the Teensy code in - so long as it is commented.
I think you missed an '&'? (See comment below.
Thank you!
Paul

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice PR. Great work!
Please make the suggested changes and I will be happy to merge it.
Best wishes,
Paul

Using the BNO080 IMU with helper methods
By: Guillaume Walck
Date: September 08th, 2020
License: This code is public domain.
This example shows how to output several data types using helper methods.
Using SPI1 on Teensy4.0
*/


#include <SPI.h>
#include <Wire.h>

#include "SparkFun_BNO080_Arduino_Library.h"
BNO080 myIMU;

const byte imuMOSIPin = 26;
const byte imuMISOPin = 1;
const byte imuSCKPin = 27;
const byte imuCSPin = 0;
//Further IMU pins
const byte imuWAKPin = 24; //PS0
const byte imuINTPin = 25; //INT
const byte imuRSTPin = 2; //RST

bool imu_initialized = false;
bool imu_calibrated = false;

// internal copies of the IMU data
float ax, ay, az, gx, gy, gz, mx, my, mz, qx, qy, qz, qw; // (qx, qy, qz, qw = to i,j,k, real)

bool timer_started = false;

void setup() {
// set the Slave Select Pins as outputs:
pinMode (imuCSPin, OUTPUT);

// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
Serial.setTimeout(500); //timeout of 500 ms
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}

// set up the SPI pins utilized on Teensy 4.0
SPI1.setMOSI(imuMOSIPin);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please comment these lines.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please use the new SPI terminology:
We don't use the terms Master and Slave any more. We now use Controller and Peripheral. We would like all new code to use the new terms: CIPO and COPI.
https://www.oshwa.org/2020/06/29/a-resolution-to-redefine-spi-pin-names/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(You cannot do anything about setMOSI but you can change imuMOSIPin to imuCOPIPin etc.)

SPI1.setMISO(imuMISOPin);
SPI1.setSCK(imuSCKPin);
// initialize SPI1:
SPI1.begin();

//Setup BNO080 to use SPI1 interface with max BNO080 clk speed of 3MHz
imu_initialized = myIMU.beginSPI(imuCSPin, imuWAKPin, imuINTPin, imuRSTPin, 3000000, SPI1); //changed from slaveCPin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please comment this line and add a line without the 3000000, SPI1 to make the example generic to SPI.


// Default periodicity (IMU_REFRESH_PERIOD ms)
if (imu_initialized)
{
myIMU.enableLinearAccelerometer(50); // m/s^2 no gravity
myIMU.enableRotationVector(50); // quat
myIMU.enableGyro(50); // rad/s
//myIMU.enableMagnetometer(50); // cannot be enabled at the same time as RotationVector (will not produce data)

Serial.println(F("LinearAccelerometer enabled, Output in form x, y, z, in m/s^2"));
Serial.println(F("Gyro enabled, Output in form x, y, z, in radians per second"));
Serial.println(F("Rotation vector, Output in form i, j, k, real, accuracy"));
//Serial.println(F("Magnetometer enabled, Output in form x, y, z, in uTesla"));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be best to add a warning here that the IMU did not initialize (instead of doing it in the main loop?)?

}


void loop() {

//Look for data from the IMU
if (imu_initialized)
{
if(!imu_calibrated)
{
myIMU.requestCalibrationStatus();
if(myIMU.calibrationComplete() == true)
{
Serial.println("Device is calibrated");
imu_calibrated = true;
}
}

if (myIMU.dataAvailable() == true)
{
byte linAccuracy = 0;
byte gyroAccuracy = 0;
byte magAccuracy = 0;
float quatRadianAccuracy = 0;
byte quatAccuracy = 0;

myIMU.getLinAccel(ax, ay, az, linAccuracy);
myIMU.getGyro(gx, gy, gz, gyroAccuracy);
myIMU.getQuat(qx, qy, qz, qw, quatRadianAccuracy, quatAccuracy);
//myIMU.getMag(mx, my, mz, magAccuracy);


if (imu_calibrated)
{
Serial.print(F("acc :"));
Serial.print(ax, 2);
Serial.print(F(","));
Serial.print(ay, 2);
Serial.print(F(","));
Serial.print(az, 2);
Serial.print(F(","));
Serial.print(az, 2);
Serial.print(F(","));
printAccuracyLevel(linAccuracy);

Serial.print(F("gyro:"));
Serial.print(gx, 2);
Serial.print(F(","));
Serial.print(gy, 2);
Serial.print(F(","));
Serial.print(gz, 2);
Serial.print(F(","));
printAccuracyLevel(gyroAccuracy);
/*
Serial.print(F("mag :"));
Serial.print(mx, 2);
Serial.print(F(","));
Serial.print(my, 2);
Serial.print(F(","));
Serial.print(mz, 2);
Serial.print(F(","));
printAccuracyLevel(magAccuracy);
*/

Serial.print(F("quat:"));
Serial.print(qx, 2);
Serial.print(F(","));
Serial.print(qy, 2);
Serial.print(F(","));
Serial.print(qz, 2);
Serial.print(F(","));
Serial.print(qw, 2);
Serial.print(F(","));
printAccuracyLevel(quatAccuracy);

}
}
}
else
{
Serial.println("BNO080 not detected. Check your jumpers and the hookup guide. Freezing...");
while (1);
}
}

//Given a accuracy number, print what it means
void printAccuracyLevel(byte accuracyNumber)
{
if (accuracyNumber == 0) Serial.println(F("Unreliable"));
else if (accuracyNumber == 1) Serial.println(F("Low"));
else if (accuracyNumber == 2) Serial.println(F("Medium"));
else if (accuracyNumber == 3) Serial.println(F("High"));
}
7 changes: 6 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,38 @@ dataAvailable KEYWORD2
parseInputReport KEYWORD2
parseCommandReport KEYWORD2

getQuat KEYWORD2
getQuatI KEYWORD2
getQuatJ KEYWORD2
getQuatK KEYWORD2
getQuatReal KEYWORD2
getQuatRadianAccuracy KEYWORD2
getQuatAccuracy KEYWORD2

getAccel KEYWORD2
getAccelX KEYWORD2
getAccelY KEYWORD2
getAccelZ KEYWORD2
getAccelAccuracy KEYWORD2

getGyro KEYWORD2
getGyroX KEYWORD2
getGyroY KEYWORD2
getGyroZ KEYWORD2
getGyroAccuracy KEYWORD2

getFastGyro KEYWORD2
getFastGyroX KEYWORD2
getFastGyroY KEYWORD2
getFastGyroZ KEYWORD2


getMag KEYWORD2
getMagX KEYWORD2
getMagY KEYWORD2
getMagZ KEYWORD2
getMagAccuracy KEYWORD2

getLinAccel KEYWORD2
getLinAccelX KEYWORD2
getLinAccelY KEYWORD2
getLinAccelZ KEYWORD2
Expand Down
61 changes: 61 additions & 0 deletions src/SparkFun_BNO080_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,18 @@ float BNO080::getYaw()
return (yaw);
}

//Gets the full quaternion
//i,j,k,real output floats
void BNO080::getQuat(float &i, float &j, float &k, float &real, float radaccuracy, uint8_t &accuracy)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot the & and capital A for &radAccuracy?

{
i = qToFloat(rawQuatI, rotationVector_Q1);
j = qToFloat(rawQuatJ, rotationVector_Q1);
k = qToFloat(rawQuatK, rotationVector_Q1);
real = qToFloat(rawQuatReal, rotationVector_Q1);
radaccuracy = qToFloat(rawQuatRadianAccuracy, rotationVector_Q1);
accuracy = quatAccuracy;
}

//Return the rotation vector quaternion I
float BNO080::getQuatI()
{
Expand Down Expand Up @@ -526,6 +538,16 @@ uint8_t BNO080::getQuatAccuracy()
return (quatAccuracy);
}

//Gets the full acceleration
//x,y,z output floats
void BNO080::getAccel(float &x, float &y, float &z, uint8_t &accuracy)
{
x = qToFloat(rawAccelX, accelerometer_Q1);
y = qToFloat(rawAccelY, accelerometer_Q1);
z = qToFloat(rawAccelZ, accelerometer_Q1);
accuracy = accelAccuracy;
}

//Return the acceleration component
float BNO080::getAccelX()
{
Expand Down Expand Up @@ -555,6 +577,16 @@ uint8_t BNO080::getAccelAccuracy()

// linear acceleration, i.e. minus gravity

//Gets the full lin acceleration
//x,y,z output floats
void BNO080::getLinAccel(float &x, float &y, float &z, uint8_t &accuracy)
{
x = qToFloat(rawLinAccelX, linear_accelerometer_Q1);
y = qToFloat(rawLinAccelY, linear_accelerometer_Q1);
z = qToFloat(rawLinAccelZ, linear_accelerometer_Q1);
accuracy = accelLinAccuracy;
}

//Return the acceleration component
float BNO080::getLinAccelX()
{
Expand Down Expand Up @@ -582,6 +614,16 @@ uint8_t BNO080::getLinAccelAccuracy()
return (accelLinAccuracy);
}

//Gets the full gyro vector
//x,y,z output floats
void BNO080::getGyro(float &x, float &y, float &z, uint8_t &accuracy)
{
x = qToFloat(rawGyroX, gyro_Q1);
y = qToFloat(rawGyroY, gyro_Q1);
z = qToFloat(rawGyroZ, gyro_Q1);
accuracy = gyroAccuracy;
}

//Return the gyro component
float BNO080::getGyroX()
{
Expand Down Expand Up @@ -609,6 +651,16 @@ uint8_t BNO080::getGyroAccuracy()
return (gyroAccuracy);
}

//Gets the full mag vector
//x,y,z output floats
void BNO080::getMag(float &x, float &y, float &z, uint8_t &accuracy)
{
x = qToFloat(rawMagX, magnetometer_Q1);
y = qToFloat(rawMagY, magnetometer_Q1);
z = qToFloat(rawMagZ, magnetometer_Q1);
accuracy = magAccuracy;
}

//Return the magnetometer component
float BNO080::getMagX()
{
Expand Down Expand Up @@ -636,6 +688,15 @@ uint8_t BNO080::getMagAccuracy()
return (magAccuracy);
}

//Gets the full high rate gyro vector
//x,y,z output floats
void BNO080::getFastGyro(float &x, float &y, float &z)
{
x = qToFloat(rawFastGyroX, angular_velocity_Q1);
y = qToFloat(rawFastGyroY, angular_velocity_Q1);
z = qToFloat(rawFastGyroZ, angular_velocity_Q1);
}

// Return the high refresh rate gyro component
float BNO080::getFastGyroX()
{
Expand Down
6 changes: 6 additions & 0 deletions src/SparkFun_BNO080_Arduino_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,38 @@ class BNO080
uint16_t parseInputReport(void); //Parse sensor readings out of report
uint16_t parseCommandReport(void); //Parse command responses out of report

void getQuat(float &i, float &j, float &k, float &real, float radAccuracy, uint8_t &accuracy);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment: I think you forgot the & for radAccuracy?

float getQuatI();
float getQuatJ();
float getQuatK();
float getQuatReal();
float getQuatRadianAccuracy();
uint8_t getQuatAccuracy();

void getAccel(float &x, float &y, float &z, uint8_t &accuracy);
float getAccelX();
float getAccelY();
float getAccelZ();
uint8_t getAccelAccuracy();

void getLinAccel(float &x, float &y, float &z, uint8_t &accuracy);
float getLinAccelX();
float getLinAccelY();
float getLinAccelZ();
uint8_t getLinAccelAccuracy();

void getGyro(float &x, float &y, float &z, uint8_t &accuracy);
float getGyroX();
float getGyroY();
float getGyroZ();
uint8_t getGyroAccuracy();

void getFastGyro(float &x, float &y, float &z);
float getFastGyroX();
float getFastGyroY();
float getFastGyroZ();

void getMag(float &x, float &y, float &z, uint8_t &accuracy);
float getMagX();
float getMagY();
float getMagZ();
Expand Down