Skip to content

Commit 8c9831a

Browse files
committed
Visualize101.ino: Simplify sensor reading
CurieIMU now has dataReady() functions to check for newly available accel/gyro data, and also readAccelerometer/GyroScaled(), which reads then sensor and returns values scaled according to the configured range. Using these functions in the Visualize101 example makes the source much simpler
1 parent 70fd2c9 commit 8c9831a

File tree

1 file changed

+3
-41
lines changed

1 file changed

+3
-41
lines changed

examples/Visualize101/Visualize101.ino

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <MadgwickAHRS.h>
33

44
Madgwick filter;
5-
unsigned long microsPerReading, microsPrevious;
65
float accelScale, gyroScale;
76

87
void setup() {
@@ -18,34 +17,18 @@ void setup() {
1817
CurieIMU.setAccelerometerRange(2);
1918
// Set the gyroscope range to 250 degrees/second
2019
CurieIMU.setGyroRange(250);
21-
22-
// initialize variables to pace updates to correct rate
23-
microsPerReading = 1000000 / 25;
24-
microsPrevious = micros();
2520
}
2621

2722
void loop() {
28-
int aix, aiy, aiz;
29-
int gix, giy, giz;
3023
float ax, ay, az;
3124
float gx, gy, gz;
3225
float roll, pitch, heading;
33-
unsigned long microsNow;
3426

3527
// check if it's time to read data and update the filter
36-
microsNow = micros();
37-
if (microsNow - microsPrevious >= microsPerReading) {
38-
39-
// read raw data from CurieIMU
40-
CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
28+
if (CurieIMU.accelDataReady() && CurieIMU.gyroDataReady()) {
4129

42-
// convert from raw data to gravity and degrees/second units
43-
ax = convertRawAcceleration(aix);
44-
ay = convertRawAcceleration(aiy);
45-
az = convertRawAcceleration(aiz);
46-
gx = convertRawGyro(gix);
47-
gy = convertRawGyro(giy);
48-
gz = convertRawGyro(giz);
30+
// read scaled data from CurieIMU
31+
CurieIMU.readMotionSensorScaled(ax, ay, az, gx, gy, gz);
4932

5033
// update the filter, which computes orientation
5134
filter.updateIMU(gx, gy, gz, ax, ay, az);
@@ -60,26 +43,5 @@ void loop() {
6043
Serial.print(pitch);
6144
Serial.print(" ");
6245
Serial.println(roll);
63-
64-
// increment previous time, so we keep proper pace
65-
microsPrevious = microsPrevious + microsPerReading;
6646
}
6747
}
68-
69-
float convertRawAcceleration(int aRaw) {
70-
// since we are using 2G range
71-
// -2g maps to a raw value of -32768
72-
// +2g maps to a raw value of 32767
73-
74-
float a = (aRaw * 2.0) / 32768.0;
75-
return a;
76-
}
77-
78-
float convertRawGyro(int gRaw) {
79-
// since we are using 250 degrees/seconds range
80-
// -250 maps to a raw value of -32768
81-
// +250 maps to a raw value of 32767
82-
83-
float g = (gRaw * 250.0) / 32768.0;
84-
return g;
85-
}

0 commit comments

Comments
 (0)