Files at this revision

API Documentation at this revision

Comitter:
nimbusgb
Date:
Mon Feb 07 22:02:32 2011 +0000
Parent:
0:6a5296aa7dbf
Child:
2:202906c5fadd
Commit message:
added ability to adjust gyro errors for tuning purposes

Changed in this revision

IMUfilter.cpp Show annotated file Show diff for this revision Revisions of this file
IMUfilter.h Show annotated file Show diff for this revision Revisions of this file
--- a/IMUfilter.cpp	Sat Feb 05 18:57:55 2011 +0000
+++ b/IMUfilter.cpp	Mon Feb 07 22:02:32 2011 +0000
@@ -173,7 +173,7 @@
     double ASq_1, ASq_2, ASq_3, ASq_4;    
                               
     //Compute the quaternion conjugate.
-    ESq_1 = SEq_1;
+    ESq_1 =  SEq_1;
     ESq_2 = -SEq_2;
     ESq_3 = -SEq_3;
     ESq_4 = -SEq_4;
@@ -185,9 +185,9 @@
     ASq_4 = ESq_1 * AEq_4 + ESq_2 * AEq_3 - ESq_3 * AEq_2 + ESq_4 * AEq_1;
 
     //Compute the Euler angles from the quaternion.
-    phi = atan2(2 * ASq_3 * ASq_4 - 2 * ASq_1 * ASq_2, 2 * ASq_1 * ASq_1 + 2 * ASq_4 * ASq_4 - 1);
-    theta = asin(2 * ASq_2 * ASq_3 - 2 * ASq_1 * ASq_3);
-    psi = atan2(2 * ASq_2 * ASq_3 - 2 * ASq_1 * ASq_4, 2 * ASq_1 * ASq_1 + 2 * ASq_2 * ASq_2 - 1);
+    phi   = atan2(2 * ASq_3 * ASq_4 - 2 * ASq_1 * ASq_2, 2 * ASq_1 * ASq_1 + 2 * ASq_4 * ASq_4 - 1);
+    theta = asin (2 * ASq_2 * ASq_3 - 2 * ASq_1 * ASq_3);
+    psi   = atan2(2 * ASq_2 * ASq_3 - 2 * ASq_1 * ASq_4, 2 * ASq_1 * ASq_1 + 2 * ASq_2 * ASq_2 - 1);
 
 }
 
@@ -209,6 +209,17 @@
 
 }
 
+void IMUfilter::setGyroError(double gyroscopeMeasurementError)
+    {
+    gyroMeasError = gyroscopeMeasurementError;
+    reset();
+    }
+    
+double IMUfilter::getGyroError(void)
+    {
+    return gyroMeasError;
+    }
+    
 void IMUfilter::reset(void) {
 
     firstUpdate = 0;
@@ -224,5 +235,7 @@
     SEq_2 = 0;
     SEq_3 = 0;
     SEq_4 = 0;
-
+    
+    //Compute beta.
+    beta = sqrt(3.0 / 4.0) * (PI * (gyroMeasError / 180.0));
 }
--- a/IMUfilter.h	Sat Feb 05 18:57:55 2011 +0000
+++ b/IMUfilter.h	Mon Feb 07 22:02:32 2011 +0000
@@ -1,143 +1,156 @@
-/**
- * @author Aaron Berk
- *
- * @section LICENSE
- *
- * Copyright (c) 2010 ARM Limited
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @section DESCRIPTION
- *
- * IMU orientation filter developed by Sebastian Madgwick.
- *
- * Find more details about his paper here:
- *
- * http://code.google.com/p/imumargalgorithm30042010sohm/
- */
-
-#ifndef IMU_FILTER_H
-#define IMU_FILTER_H
-
-/**
- * Includes
- */
-#include "mbed.h"
-
-/**
- * Defines
- */
-#define PI 3.1415926536
-
-/**
- * IMU orientation filter.
- */
-class IMUfilter {
-
-public:
-
-    /**
-     * Constructor.
-     *
-     * Initializes filter variables.
-     *
-     * @param rate The rate at which the filter should be updated.
-     * @param gyroscopeMeasurementError The error of the gyroscope in degrees
-     *  per second. This used to calculate a tuning constant for the filter.
-     *  Try changing this value if there are jittery readings, or they change
-     *  too much or too fast when rotating the IMU.
-     */
-    IMUfilter(double rate, double gyroscopeMeasurementError);
-
-    /**
-     * Update the filter variables.
-     *
-     * @param w_x X-axis gyroscope reading in rad/s.
-     * @param w_y Y-axis gyroscope reading in rad/s.
-     * @param w_z Z-axis gyroscope reading in rad/s.
-     * @param a_x X-axis accelerometer reading in m/s/s.
-     * @param a_y Y-axis accelerometer reading in m/s/s.
-     * @param a_z Z-axis accelerometer reading in m/s/s.
-     */
-    void updateFilter(double w_x, double w_y, double w_z,
-                      double a_x, double a_y, double a_z);
-
-    /**
-     * Compute the Euler angles based on the current filter data.
-     */
-    void computeEuler(void);
-
-    /**
-     * Get the current roll.
-     *
-     * @return The current roll angle in radians.
-     */
-    double getRoll(void);
-
-    /**
-     * Get the current pitch.
-     *
-     * @return The current pitch angle in radians.
-     */
-    double getPitch(void);
-
-    /**
-     * Get the current yaw.
-     *
-     * @return The current yaw angle in radians.
-     */
-    double getYaw(void);
-
-    /**
-     * Reset the filter.
-     */
-    void reset(void);
-
-private:
-
-    int firstUpdate;
-
-    //Quaternion orientation of earth frame relative to auxiliary frame.
-    double AEq_1;
-    double AEq_2;
-    double AEq_3;
-    double AEq_4;
-
-    //Estimated orientation quaternion elements with initial conditions.
-    double SEq_1;
-    double SEq_2;
-    double SEq_3;
-    double SEq_4;
-
-    //Sampling period
-    double deltat;
-
-    //Gyroscope measurement error (in degrees per second).
-    double gyroMeasError;
-
-    //Compute beta (filter tuning constant..
-    double beta;
-
-    double phi;
-    double theta;
-    double psi;
-
-};
-
-#endif /* IMU_FILTER_H */
+/**
+ * @author Aaron Berk
+ *
+ * @section LICENSE
+ *
+ * Copyright (c) 2010 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * IMU orientation filter developed by Sebastian Madgwick.
+ *
+ * Find more details about his paper here:
+ *
+ * http://code.google.com/p/imumargalgorithm30042010sohm/
+ */
+
+#ifndef IMU_FILTER_H
+#define IMU_FILTER_H
+
+/**
+ * Includes
+ */
+#include "mbed.h"
+
+/**
+ * Defines
+ */
+#define PI 3.1415926536
+
+/**
+ * IMU orientation filter.
+ */
+class IMUfilter {
+
+public:
+
+    /**
+     * Constructor.
+     *
+     * Initializes filter variables.
+     *
+     * @param rate The rate at which the filter should be updated.
+     * @param gyroscopeMeasurementError The error of the gyroscope in degrees
+     *  per second. This used to calculate a tuning constant for the filter.
+     *  Try changing this value if there are jittery readings, or they change
+     *  too much or too fast when rotating the IMU.
+     */
+    IMUfilter(double rate, double gyroscopeMeasurementError);
+
+    /**
+     * Update the filter variables.
+     *
+     * @param w_x X-axis gyroscope reading in rad/s.
+     * @param w_y Y-axis gyroscope reading in rad/s.
+     * @param w_z Z-axis gyroscope reading in rad/s.
+     * @param a_x X-axis accelerometer reading in m/s/s.
+     * @param a_y Y-axis accelerometer reading in m/s/s.
+     * @param a_z Z-axis accelerometer reading in m/s/s.
+     */
+    void updateFilter(double w_x, double w_y, double w_z,
+                      double a_x, double a_y, double a_z);
+
+    /**
+     * Compute the Euler angles based on the current filter data.
+     */
+    void computeEuler(void);
+
+    /**
+     * Get the current roll.
+     *
+     * @return The current roll angle in radians.
+     */
+    double getRoll(void);
+
+    /**
+     * Get the current pitch.
+     *
+     * @return The current pitch angle in radians.
+     */
+    double getPitch(void);
+
+    /**
+     * Get the current yaw.
+     *
+     * @return The current yaw angle in radians.
+     */
+    double getYaw(void);
+    
+    /**
+     * Get the current Gyro error rate.
+     *
+     * @return The Gyro error rate setting.
+     */
+    double getGyroError(void);
+    
+    /**
+     * Set the Gyro error rate. 
+     * The filter resets during calling this routine.
+     */
+    void setGyroError(double gyroscopeMeasurementError);
+
+    /**
+     * Reset the filter.
+     */
+    void reset(void);
+
+private:
+
+    int firstUpdate;
+
+    //Quaternion orientation of earth frame relative to auxiliary frame.
+    double AEq_1;
+    double AEq_2;
+    double AEq_3;
+    double AEq_4;
+
+    //Estimated orientation quaternion elements with initial conditions.
+    double SEq_1;
+    double SEq_2;
+    double SEq_3;
+    double SEq_4;
+
+    //Sampling period
+    double deltat;
+
+    //Gyroscope measurement error (in degrees per second).
+    double gyroMeasError;
+
+    //Compute beta (filter tuning constant..
+    double beta;
+
+    double phi;
+    double theta;
+    double psi;
+
+};
+
+#endif /* IMU_FILTER_H */