-
Notifications
You must be signed in to change notification settings - Fork 66
Added getters for multiple variables at the same time #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| 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); | ||
|
||
| 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 | ||
|
||
|
|
||
| // 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")); | ||
| } | ||
|
||
| } | ||
|
|
||
|
|
||
| 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")); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
||
| { | ||
| 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() | ||
| { | ||
|
|
@@ -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() | ||
| { | ||
|
|
@@ -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() | ||
| { | ||
|
|
@@ -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() | ||
| { | ||
|
|
@@ -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() | ||
| { | ||
|
|
@@ -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() | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
||
| 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(); | ||
|
|
||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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