From 076ce2bb09b0404e55795c3b687791957393215e Mon Sep 17 00:00:00 2001 From: Erik Nyquist Date: Tue, 24 Jan 2017 11:14:08 -0800 Subject: [PATCH] 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 --- examples/Visualize101/Visualize101.ino | 44 ++------------------------ 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/examples/Visualize101/Visualize101.ino b/examples/Visualize101/Visualize101.ino index bdd4500..664d571 100644 --- a/examples/Visualize101/Visualize101.ino +++ b/examples/Visualize101/Visualize101.ino @@ -2,7 +2,6 @@ #include Madgwick filter; -unsigned long microsPerReading, microsPrevious; float accelScale, gyroScale; void setup() { @@ -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); @@ -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; -}