MadgwickAHRS Arduino Library

repository·master·Indexed 20 days ago

https://github.com/arduino-libraries/madgwickahrs

An implementation of the MadgwickAHRS algorithm for Arduino environments to estimate object orientation. It supports 9-axis orientation estimation using gyroscope, accelerometer, and magnetometer data via the update method, as well as 6-axis estimation using only gyroscope and accelerometer data via the updateIMU method.

Tokens
692
Snippets
5
Records
6
Agent score
20%

What's inside MadgwickAHRS

  1. Overview of the Madgwick Library

    master
    The Madgwick library is an implementation of the MadgwickAHRS algorithm. It is used to estimate the orientation (attitude) of an object by processing data from an accelerometer and a gyroscope. This library wraps the official implementation of the algorithm for use in Arduino environments.
  2. Retrieve orientation in degrees

    master

    The following methods return the estimated Roll, Pitch, and Yaw in degrees.

    • getRoll(): Returns roll in degrees.
    • getPitch(): Returns pitch in degrees.
    • getYaw(): Returns yaw in degrees (offset by 180.0 degrees).
    float roll = filter.getRoll();
    float pitch = filter.getPitch();
    float yaw = filter.getYaw();
  3. Initialize the Madgwick class

    master

    To use the Madgwick algorithm, instantiate the Madgwick class and call the begin(float sampleFrequency) method. The sampleFrequency parameter should be the rate (in Hz) at which you intend to call the update or updateIMU methods. This value is used to calculate the inverse sample frequency for the algorithm's internal integration.

    Madgwick filter;
    // Set sample frequency to 100Hz
    filter.begin(100.0f);
  4. Update orientation using AHRS (9-axis)

    master

    Use the update method to perform orientation estimation using a 9-axis sensor suite (Gyroscope, Accelerometer, and Magnetometer). This method integrates the sensor data to maintain an accurate orientation estimate.

    Parameters:

    • gx, gy, gz: Gyroscope readings (rad/s)
    • ax, ay, az: Accelerometer readings (g)
    • mx, my, mz: Magnetometer readings (uT)
    // Example 9-axis update
    filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
  5. Retrieve orientation in radians

    master

    The following methods return the estimated Roll, Pitch, and Yaw in radians.

    • getRollRadians(): Returns roll in radians.
    • getPitchRadians(): Returns pitch in radians.
    • getYawRadians(): Returns yaw in radians.
    float rollRad = filter.getRollRadians();
    float pitchRad = filter.getPitchRadians();
    float yawRad = filter.getYawRadians();
  6. Update orientation using IMU (6-axis)

    master

    Use the updateIMU method to perform orientation estimation using a 6-axis sensor suite (Gyroscope and Accelerometer only). This is suitable for applications where a magnetometer is unavailable or not required.

    Parameters:

    • gx, gy, gz: Gyroscope readings (rad/s)
    • ax, ay, az: Accelerometer readings (g)
    // Example 6-axis update
    filter.updateIMU(gx, gy, gz, ax, ay, az);