Skip to content

Commit 0857fcc

Browse files
committed
Rewrote examples-18 to separate I2C and SPI
1 parent 00741ae commit 0857fcc

File tree

2 files changed

+258
-127
lines changed

2 files changed

+258
-127
lines changed

examples/Example18-AccessMultiple/Example18-AccessMultiple.ino

Lines changed: 96 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -5,155 +5,124 @@
55
Date: September 08th, 2020
66
License: This code is public domain.
77
8-
This example shows how to output several data types using helper methods.
8+
This example shows how to access multiple data of one type using helper methods.
99
10-
Using SPI1 on Teensy4.0
11-
*/
10+
It takes about 1ms at 400kHz I2C to read a record from the sensor, but we are polling the sensor continually
11+
between updates from the sensor. Use the interrupt pin on the BNO080 breakout to avoid polling.
1212
13+
Hardware Connections:
14+
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
15+
Plug the sensor onto the shield
16+
Serial.print it out at 9600 baud to serial monitor.
17+
*/
1318

14-
#include <SPI.h>
1519
#include <Wire.h>
1620

17-
#include "SparkFun_BNO080_Arduino_Library.h"
21+
#include "SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080
1822
BNO080 myIMU;
1923

20-
const byte imuMOSIPin = 26;
21-
const byte imuMISOPin = 1;
22-
const byte imuSCKPin = 27;
23-
const byte imuCSPin = 0;
24-
//Further IMU pins
25-
const byte imuWAKPin = 24; //PS0
26-
const byte imuINTPin = 25; //INT
27-
const byte imuRSTPin = 2; //RST
28-
29-
bool imu_initialized = false;
30-
bool imu_calibrated = false;
31-
32-
// internal copies of the IMU data
33-
float ax, ay, az, gx, gy, gz, mx, my, mz, qx, qy, qz, qw; // (qx, qy, qz, qw = to i,j,k, real)
34-
35-
bool timer_started = false;
36-
37-
void setup() {
38-
// set the Slave Select Pins as outputs:
39-
pinMode (imuCSPin, OUTPUT);
40-
41-
// initialize serial communication at 115200 bits per second:
42-
Serial.begin(115200);
43-
Serial.setTimeout(500); //timeout of 500 ms
44-
while (!Serial) {
45-
; // wait for serial port to connect. Needed for native USB
24+
void setup()
25+
{
26+
Serial.begin(9600);
27+
Serial.println();
28+
Serial.println("BNO080 Multiple Read Example");
29+
30+
Wire.begin();
31+
32+
//Are you using a ESP? Check this issue for more information: https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/issues/16
33+
// //=================================
34+
// delay(100); // Wait for BNO to boot
35+
// // Start i2c and BNO080
36+
// Wire.flush(); // Reset I2C
37+
// IMU.begin(BNO080_DEFAULT_ADDRESS, Wire);
38+
// Wire.begin(4, 5);
39+
// Wire.setClockStretchLimit(4000);
40+
// //=================================
41+
42+
if (myIMU.begin() == false)
43+
{
44+
Serial.println("BNO080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
45+
while (1);
4646
}
4747

48-
// set up the SPI pins utilized on Teensy 4.0
49-
SPI1.setMOSI(imuMOSIPin);
50-
SPI1.setMISO(imuMISOPin);
51-
SPI1.setSCK(imuSCKPin);
52-
// initialize SPI1:
53-
SPI1.begin();
48+
Wire.setClock(400000); //Increase I2C data rate to 400kHz
5449

55-
//Setup BNO080 to use SPI1 interface with max BNO080 clk speed of 3MHz
56-
imu_initialized = myIMU.beginSPI(imuCSPin, imuWAKPin, imuINTPin, imuRSTPin, 3000000, SPI1); //changed from slaveCPin
50+
myIMU.enableLinearAccelerometer(50); // m/s^2 no gravity
51+
myIMU.enableRotationVector(50); // quat
52+
myIMU.enableGyro(50); // rad/s
53+
//myIMU.enableMagnetometer(50); // cannot be enabled at the same time as RotationVector (will not produce data)
5754

58-
// Default periodicity (IMU_REFRESH_PERIOD ms)
59-
if (imu_initialized)
60-
{
61-
myIMU.enableLinearAccelerometer(50); // m/s^2 no gravity
62-
myIMU.enableRotationVector(50); // quat
63-
myIMU.enableGyro(50); // rad/s
64-
//myIMU.enableMagnetometer(50); // cannot be enabled at the same time as RotationVector (will not produce data)
65-
66-
Serial.println(F("LinearAccelerometer enabled, Output in form x, y, z, in m/s^2"));
67-
Serial.println(F("Gyro enabled, Output in form x, y, z, in radians per second"));
68-
Serial.println(F("Rotation vector, Output in form i, j, k, real, accuracy"));
69-
//Serial.println(F("Magnetometer enabled, Output in form x, y, z, in uTesla"));
70-
}
55+
Serial.println(F("LinearAccelerometer enabled, Output in form x, y, z, accuracy, in m/s^2"));
56+
Serial.println(F("Gyro enabled, Output in form x, y, z, accuracy, in radians per second"));
57+
Serial.println(F("Rotation vector, Output in form i, j, k, real, accuracy"));
58+
//Serial.println(F("Magnetometer enabled, Output in form x, y, z, accuracy, in uTesla"));
7159
}
7260

7361

7462
void loop() {
7563

7664
//Look for data from the IMU
77-
if (imu_initialized)
65+
if (myIMU.dataAvailable() == true)
7866
{
79-
if(!imu_calibrated)
80-
{
81-
myIMU.requestCalibrationStatus();
82-
if(myIMU.calibrationComplete() == true)
83-
{
84-
Serial.println("Device is calibrated");
85-
imu_calibrated = true;
86-
}
87-
}
88-
89-
if (myIMU.dataAvailable() == true)
90-
{
91-
byte linAccuracy = 0;
92-
byte gyroAccuracy = 0;
93-
byte magAccuracy = 0;
94-
float quatRadianAccuracy = 0;
95-
byte quatAccuracy = 0;
96-
97-
myIMU.getLinAccel(ax, ay, az, linAccuracy);
98-
myIMU.getGyro(gx, gy, gz, gyroAccuracy);
99-
myIMU.getQuat(qx, qy, qz, qw, quatRadianAccuracy, quatAccuracy);
100-
//myIMU.getMag(mx, my, mz, magAccuracy);
101-
102-
103-
if (imu_calibrated)
104-
{
105-
Serial.print(F("acc :"));
106-
Serial.print(ax, 2);
107-
Serial.print(F(","));
108-
Serial.print(ay, 2);
109-
Serial.print(F(","));
110-
Serial.print(az, 2);
111-
Serial.print(F(","));
112-
Serial.print(az, 2);
113-
Serial.print(F(","));
114-
printAccuracyLevel(linAccuracy);
115-
116-
Serial.print(F("gyro:"));
117-
Serial.print(gx, 2);
118-
Serial.print(F(","));
119-
Serial.print(gy, 2);
120-
Serial.print(F(","));
121-
Serial.print(gz, 2);
122-
Serial.print(F(","));
123-
printAccuracyLevel(gyroAccuracy);
124-
/*
125-
Serial.print(F("mag :"));
126-
Serial.print(mx, 2);
127-
Serial.print(F(","));
128-
Serial.print(my, 2);
129-
Serial.print(F(","));
130-
Serial.print(mz, 2);
131-
Serial.print(F(","));
132-
printAccuracyLevel(magAccuracy);
67+
// internal copies of the IMU data
68+
float ax, ay, az, gx, gy, gz, qx, qy, qz, qw; // mx, my, mz, (qx, qy, qz, qw = i,j,k, real)
69+
byte linAccuracy = 0;
70+
byte gyroAccuracy = 0;
71+
//byte magAccuracy = 0;
72+
float quatRadianAccuracy = 0;
73+
byte quatAccuracy = 0;
74+
75+
// get IMU data in one go for each sensor type
76+
myIMU.getLinAccel(ax, ay, az, linAccuracy);
77+
myIMU.getGyro(gx, gy, gz, gyroAccuracy);
78+
myIMU.getQuat(qx, qy, qz, qw, quatRadianAccuracy, quatAccuracy);
79+
//myIMU.getMag(mx, my, mz, magAccuracy);
80+
81+
82+
Serial.print(F("acc :"));
83+
Serial.print(ax, 2);
84+
Serial.print(F(","));
85+
Serial.print(ay, 2);
86+
Serial.print(F(","));
87+
Serial.print(az, 2);
88+
Serial.print(F(","));
89+
Serial.print(az, 2);
90+
Serial.print(F(","));
91+
printAccuracyLevel(linAccuracy);
92+
93+
Serial.print(F("gyro:"));
94+
Serial.print(gx, 2);
95+
Serial.print(F(","));
96+
Serial.print(gy, 2);
97+
Serial.print(F(","));
98+
Serial.print(gz, 2);
99+
Serial.print(F(","));
100+
printAccuracyLevel(gyroAccuracy);
101+
/*
102+
Serial.print(F("mag :"));
103+
Serial.print(mx, 2);
104+
Serial.print(F(","));
105+
Serial.print(my, 2);
106+
Serial.print(F(","));
107+
Serial.print(mz, 2);
108+
Serial.print(F(","));
109+
printAccuracyLevel(magAccuracy);
133110
*/
134-
135-
Serial.print(F("quat:"));
136-
Serial.print(qx, 2);
137-
Serial.print(F(","));
138-
Serial.print(qy, 2);
139-
Serial.print(F(","));
140-
Serial.print(qz, 2);
141-
Serial.print(F(","));
142-
Serial.print(qw, 2);
143-
Serial.print(F(","));
144-
printAccuracyLevel(quatAccuracy);
145-
146-
}
147-
}
148-
}
149-
else
150-
{
151-
Serial.println("BNO080 not detected. Check your jumpers and the hookup guide. Freezing...");
152-
while (1);
111+
112+
Serial.print(F("quat:"));
113+
Serial.print(qx, 2);
114+
Serial.print(F(","));
115+
Serial.print(qy, 2);
116+
Serial.print(F(","));
117+
Serial.print(qz, 2);
118+
Serial.print(F(","));
119+
Serial.print(qw, 2);
120+
Serial.print(F(","));
121+
printAccuracyLevel(quatAccuracy);
153122
}
154123
}
155124

156-
//Given a accuracy number, print what it means
125+
//Given an accuracy number, print what it means
157126
void printAccuracyLevel(byte accuracyNumber)
158127
{
159128
if (accuracyNumber == 0) Serial.println(F("Unreliable"));

0 commit comments

Comments
 (0)