Skip to content

Commit

Permalink
Visualize101.ino: Simplify sensor reading
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eriknyquist committed Feb 22, 2017
1 parent 70fd2c9 commit 076ce2b
Showing 1 changed file with 3 additions and 41 deletions.
44 changes: 3 additions & 41 deletions examples/Visualize101/Visualize101.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <MadgwickAHRS.h>

Madgwick filter;
unsigned long microsPerReading, microsPrevious;
float accelScale, gyroScale;

void setup() {
Expand All @@ -18,34 +17,18 @@ void setup() {
CurieIMU.setAccelerometerRange(2);
// Set the gyroscope range to 250 degrees/second
CurieIMU.setGyroRange(250);

// initialize variables to pace updates to correct rate
microsPerReading = 1000000 / 25;
microsPrevious = micros();
}

void loop() {
int aix, aiy, aiz;
int gix, giy, giz;
float ax, ay, az;
float gx, gy, gz;
float roll, pitch, heading;
unsigned long microsNow;

// check if it's time to read data and update the filter
microsNow = micros();
if (microsNow - microsPrevious >= microsPerReading) {

// read raw data from CurieIMU
CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
if (CurieIMU.dataReady()) {

// convert from raw data to gravity and degrees/second units
ax = convertRawAcceleration(aix);
ay = convertRawAcceleration(aiy);
az = convertRawAcceleration(aiz);
gx = convertRawGyro(gix);
gy = convertRawGyro(giy);
gz = convertRawGyro(giz);
// read scaled data from CurieIMU
CurieIMU.readMotionSensorScaled(ax, ay, az, gx, gy, gz);

// update the filter, which computes orientation
filter.updateIMU(gx, gy, gz, ax, ay, az);
Expand All @@ -60,26 +43,5 @@ void loop() {
Serial.print(pitch);
Serial.print(" ");
Serial.println(roll);

// increment previous time, so we keep proper pace
microsPrevious = microsPrevious + microsPerReading;
}
}

float convertRawAcceleration(int aRaw) {
// since we are using 2G range
// -2g maps to a raw value of -32768
// +2g maps to a raw value of 32767

float a = (aRaw * 2.0) / 32768.0;
return a;
}

float convertRawGyro(int gRaw) {
// since we are using 250 degrees/seconds range
// -250 maps to a raw value of -32768
// +250 maps to a raw value of 32767

float g = (gRaw * 250.0) / 32768.0;
return g;
}

0 comments on commit 076ce2b

Please sign in to comment.