semin ahn / Mbed OS zeta_stm_kinetic

Dependencies:   BufferedSerial

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MadgwickAHRS.h Source File

MadgwickAHRS.h

00001 //=============================================================================================
00002 // MadgwickAHRS.h
00003 //=============================================================================================
00004 //
00005 // Implementation of Madgwick's IMU and AHRS algorithms.
00006 // See: http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/
00007 //
00008 // From the x-io website "Open-source resources available on this website are
00009 // provided under the GNU General Public Licence unless an alternative licence
00010 // is provided in source."
00011 //
00012 // Date         Author          Notes
00013 // 29/09/2011   SOH Madgwick    Initial release
00014 // 02/10/2011   SOH Madgwick    Optimised for reduced CPU load
00015 //
00016 //=============================================================================================
00017 #ifndef MadgwickAHRS_h
00018 #define MadgwickAHRS_h
00019 #include <math.h>
00020 
00021 //--------------------------------------------------------------------------------------------
00022 // Variable declaration
00023 class Madgwick{
00024 private:
00025     static float invSqrt(float x);
00026     float beta;             // algorithm gain
00027     float roll;
00028     float pitch;
00029     float yaw;
00030     char anglesComputed;
00031     void computeAngles();
00032 
00033 //-------------------------------------------------------------------------------------------
00034 // Function declarations
00035 public:
00036     float invSampleFreq;
00037 
00038     float q0;
00039     float q1;
00040     float q2;
00041     float q3;   // quaternion of sensor frame relative to auxiliary frame
00042 
00043     Madgwick(void);
00044     void begin(float sampleFrequency) { invSampleFreq = 1.0f / sampleFrequency; }
00045     void update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);
00046     void updateIMU(float gx, float gy, float gz, float ax, float ay, float az);
00047     //float getPitch(){return atan2f(2.0f * q2 * q3 - 2.0f * q0 * q1, 2.0f * q0 * q0 + 2.0f * q3 * q3 - 1.0f);};
00048     //float getRoll(){return -1.0f * asinf(2.0f * q1 * q3 + 2.0f * q0 * q2);};
00049     //float getYaw(){return atan2f(2.0f * q1 * q2 - 2.0f * q0 * q3, 2.0f * q0 * q0 + 2.0f * q1 * q1 - 1.0f);};
00050     float getRoll() {
00051         if (!anglesComputed) computeAngles();
00052         return roll * 57.29578f;
00053     }
00054     float getPitch() {
00055         if (!anglesComputed) computeAngles();
00056         return pitch * 57.29578f;
00057     }
00058     float getYaw() {
00059         if (!anglesComputed) computeAngles();
00060         return yaw * 57.29578f + 180.0f;
00061     }
00062     float getRollRadians() {
00063         if (!anglesComputed) computeAngles();
00064         return roll;
00065     }
00066     float getPitchRadians() {
00067         if (!anglesComputed) computeAngles();
00068         return pitch;
00069     }
00070     float getYawRadians() {
00071         if (!anglesComputed) computeAngles();
00072         return yaw;
00073     }
00074 };
00075 #endif
00076 
00077