MadgwickAHRS Arduino Library
repository·master·Indexed 20 days ago
https://github.com/arduino-libraries/madgwickahrsAn 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.
What's inside MadgwickAHRS
- 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.
Retrieve orientation in degrees
masterThe 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();Initialize the Madgwick class
masterTo use the Madgwick algorithm, instantiate the
Madgwickclass and call thebegin(float sampleFrequency)method. ThesampleFrequencyparameter should be the rate (in Hz) at which you intend to call theupdateorupdateIMUmethods. 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);Update orientation using AHRS (9-axis)
masterUse the
updatemethod 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);Retrieve orientation in radians
masterThe 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();Update orientation using IMU (6-axis)
masterUse the
updateIMUmethod 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);