Skip to content

Commit 92d6dbb

Browse files
committed
Improve RawReadings example
-Data was printing at inconsistent times and not updating correctly -changed data types to correct signed ints -switched back to 100KHz I2C com speed -decreased report cycle times to 1ms for all enable functions for all desired reports -added user-defined debug report time interval functionality. This sets a minimum/desired debug print interval. Note, sometimes this can be longer due to the sensor report length and the driver processing the incoming report. -removed serial print for decoding sensor event error
1 parent a78e3b4 commit 92d6dbb

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

examples/Example_11_RawReadings/Example_11_RawReadings.ino

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,24 @@ BNO08x myIMU;
6464
// variables to store all our incoming values
6565

6666
// raw accel
67-
uint16_t x;
68-
uint16_t y;
69-
uint16_t z;
67+
int16_t x;
68+
int16_t y;
69+
int16_t z;
7070

7171
// raw gyros
72-
uint16_t gx;
73-
uint16_t gy;
74-
uint16_t gz;
72+
int16_t gx;
73+
int16_t gy;
74+
int16_t gz;
7575

7676
// raw mags
77-
uint16_t mx;
78-
uint16_t my;
79-
uint16_t mz;
77+
int16_t mx;
78+
int16_t my;
79+
int16_t mz;
80+
81+
unsigned long previousMicros = 0;
82+
83+
unsigned long previousDebugMillis = 0;
84+
#define DEBUG_INTERVAL_MILLISECONDS 30
8085

8186
void setup() {
8287
Serial.begin(115200);
@@ -99,7 +104,7 @@ void setup() {
99104
}
100105
Serial.println("BNO08x found!");
101106

102-
Wire.setClock(400000); //Increase I2C data rate to 400kHz
107+
//Wire.setClock(400000); //Increase I2C data rate to 400kHz
103108

104109
setReports();
105110

@@ -111,37 +116,37 @@ void setup() {
111116
void setReports(void) {
112117
Serial.println("Setting desired reports");
113118

114-
if (myIMU.enableAccelerometer() == true) {
119+
if (myIMU.enableAccelerometer(1) == true) {
115120
Serial.println(F("Accelerometer enabled"));
116121
} else {
117122
Serial.println("Could not enable accelerometer");
118123
}
119124

120-
if (myIMU.enableRawAccelerometer() == true) {
125+
if (myIMU.enableRawAccelerometer(1) == true) {
121126
Serial.println(F("Raw Accelerometer enabled"));
122127
} else {
123128
Serial.println("Could not enable raw accelerometer");
124129
}
125130

126-
if (myIMU.enableGyro() == true) {
131+
if (myIMU.enableGyro(1) == true) {
127132
Serial.println(F("Gyro enabled"));
128133
} else {
129134
Serial.println("Could not enable gyro");
130135
}
131136

132-
if (myIMU.enableRawGyro() == true) {
137+
if (myIMU.enableRawGyro(1) == true) {
133138
Serial.println(F("Raw Gyro enabled"));
134139
} else {
135140
Serial.println("Could not enable raw gyro");
136141
}
137142

138-
if (myIMU.enableMagnetometer() == true) {
143+
if (myIMU.enableMagnetometer(1) == true) {
139144
Serial.println(F("Magnetometer enabled"));
140145
} else {
141146
Serial.println("Could not enable Magnetometer");
142147
}
143148

144-
if (myIMU.enableRawMagnetometer() == true) {
149+
if (myIMU.enableRawMagnetometer(1) == true) {
145150
Serial.println(F("Raw Magnetometer enabled"));
146151
} else {
147152
Serial.println("Could not enable Raw Magnetometer");
@@ -152,7 +157,9 @@ void setReports(void) {
152157
}
153158

154159
void loop() {
155-
delay(10);
160+
unsigned long currentMicros = micros();
161+
162+
delayMicroseconds(10);
156163

157164
if (myIMU.wasReset()) {
158165
Serial.print("sensor was reset ");
@@ -188,11 +195,19 @@ void loop() {
188195
break;
189196
}
190197

191-
// Only print data to terminal if one of the report IDs we want came
192-
// in on the bus
193-
if( (reportID == SENSOR_REPORTID_RAW_ACCELEROMETER) ||
194-
(reportID == SENSOR_REPORTID_RAW_GYROSCOPE) ||
195-
(reportID == SENSOR_REPORTID_RAW_MAGNETOMETER))
198+
// Only print data to the terminal at a user defined interval
199+
// Each data type (accel or gyro or mag) is reported from the
200+
// BNO086 as separate messages.
201+
// To allow for all these separate messages to arrive, and thus
202+
// have updated data on all axis/types,
203+
// The report intervals for each datatype must be much faster
204+
// than our debug interval.
205+
206+
// time since last debug data printed to terminal
207+
int timeSinceLastSerialPrint = (millis() - previousDebugMillis);
208+
209+
// Only print data to the terminal at a user deficed interval
210+
if(timeSinceLastSerialPrint > DEBUG_INTERVAL_MILLISECONDS)
196211
{
197212
Serial.print(x);
198213
Serial.print("\t");
@@ -215,7 +230,12 @@ void loop() {
215230
Serial.print(mz);
216231
Serial.print("\t");
217232

233+
Serial.print(timeSinceLastSerialPrint);
234+
218235
Serial.println();
236+
237+
previousDebugMillis = millis();
238+
219239
}
220240
}
221241
}

src/SparkFun_BNO08x_Arduino_Library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ static void sensorHandler(void *cookie, sh2_SensorEvent_t *event) {
13731373

13741374
rc = sh2_decodeSensorEvent(_sensor_value, event);
13751375
if (rc != SH2_OK) {
1376-
Serial.println("BNO08x - Error decoding sensor event");
1376+
//Serial.println("BNO08x - Error decoding sensor event");
13771377
_sensor_value->timestamp = 0;
13781378
return;
13791379
}

0 commit comments

Comments
 (0)