### Madgwick begin() Example Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Example of initializing the Madgwick filter in the setup function with a sample rate of 25 Hz. This sets the internal inverse sample frequency for accurate time step calculations. ```cpp void setup() { Madgwick filter; filter.begin(25); // 25 Hz sampling rate (40 ms per sample) } ``` -------------------------------- ### Madgwick Initialization Example Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Demonstrates how to create a Madgwick filter instance and initialize it with a specific sample rate. This setup is required before processing sensor data. ```cpp #include Madgwick filter; // Create filter instance void setup() { Serial.begin(9600); filter.begin(25); // Initialize with 25 Hz sample rate } ``` -------------------------------- ### Madgwick AHRS orientation estimation example Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Example of how to use the `update()` method within a typical Arduino `loop()` function. It reads sensor data and then calls `update()` to process it, followed by retrieving the estimated roll, pitch, and yaw angles. ```cpp void loop() { float ax, ay, az; // Accelerometer in g float gx, gy, gz; // Gyroscope in degrees/sec float mx, my, mz; // Magnetometer (uncalibrated) // Read sensor values from IMU... filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float roll = filter.getRoll(); // Get orientation in degrees float pitch = filter.getPitch(); float yaw = filter.getYaw(); } ``` -------------------------------- ### Basic Setup and 6-DOF IMU Usage Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/README.md Initializes the Madgwick filter and an IMU sensor, then reads and processes data in the loop for orientation estimation. Ensure the filter's sample rate matches the IMU's. ```cpp #include Madgwick filter; void setup() { Serial.begin(9600); // Initialize IMU sensor (example with CurieIMU) CurieIMU.begin(); CurieIMU.setGyroRate(25); // 25 Hz sample rate CurieIMU.setAccelerometerRate(25); // Initialize Madgwick filter with same sample rate filter.begin(25); } void loop() { // Read sensor data int ax_raw, ay_raw, az_raw; int gx_raw, gy_raw, gz_raw; CurieIMU.readMotionSensor(ax_raw, ay_raw, az_raw, gx_raw, gy_raw, gz_raw); // Convert to proper units float ax = convertAccel(ax_raw); // In g (gravity units) float ay = convertAccel(ay_raw); float az = convertAccel(az_raw); float gx = convertGyro(gx_raw); // In degrees/sec float gy = convertGyro(gy_raw); float gz = convertGyro(gz_raw); // Update filter (6-DOF IMU mode) filter.updateIMU(gx, gy, gz, ax, ay, az); // Get orientation float roll = filter.getRoll(); // In degrees float pitch = filter.getPitch(); float yaw = filter.getYaw(); Serial.print("Roll: "); Serial.print(roll); Serial.print(" Pitch: "); Serial.print(pitch); Serial.print(" Yaw: "); Serial.println(yaw); } ``` -------------------------------- ### Madgwick AHRS Library File Structure Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/README.md Overview of the directory structure for the Madgwick AHRS Arduino library, including source files, examples, and metadata. ```text madgwickahrs/ ├── src/ │ ├── MadgwickAHRS.h (Class declaration, inline methods) │ └── MadgwickAHRS.cpp (Implementation) ├── examples/ │ └── Visualize101/ │ └── Visualize101.ino (Usage example) ├── extras/ ├── README.adoc (Project overview) ├── library.properties (Arduino library metadata) └── keywords.txt (Syntax highlighting keywords) ``` -------------------------------- ### Madgwick AHRS Filter Usage Example Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md This snippet demonstrates the typical usage pattern for the Madgwick AHRS filter, including initialization, sensor data reading, filter updates with and without magnetometer, and retrieving computed orientation angles. Ensure your IMU sensor is properly configured and its sample rate is known. ```cpp #include Madgwick filter; void setup() { Serial.begin(9600); // Initialize IMU sensor and get sample rate // Assume sensor is set to 25 Hz filter.begin(25); // Initialize filter with 25 Hz sample rate } void loop() { float ax, ay, az; // Accelerometer (g) float gx, gy, gz; // Gyroscope (degrees/sec) float mx, my, mz; // Magnetometer (uncalibrated) // Read sensor data (implementation depends on IMU hardware) readSensorData(ax, ay, az, gx, gy, gz, mx, my, mz); // Update filter if (hasMagnetometer()) { filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); } else { filter.updateIMU(gx, gy, gz, ax, ay, az); } // Read computed orientation float roll = filter.getRoll(); float pitch = filter.getPitch(); float yaw = filter.getYaw(); Serial.print("Roll: "); Serial.print(roll); Serial.print(" Pitch: "); Serial.print(pitch); Serial.print(" Yaw: "); Serial.println(yaw); } ``` -------------------------------- ### begin() Method Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Initializes the filter with the sample frequency of the IMU sensor readings. This must be called before updating the filter. ```APIDOC ## begin(float sampleFrequency) ### Description Initializes the filter with the sample frequency of the IMU sensor readings. ### Method `void begin(float sampleFrequency)` ### Parameters #### Path Parameters - **sampleFrequency** (float) - Required - Sampling frequency in Hertz (Hz) at which sensor data will be provided to the filter ### Return Type void ### Behavior Sets the inverse sample frequency (`invSampleFreq = 1.0 / sampleFrequency`). This value is used internally to compute the time step for integrating the quaternion derivatives. Must be called before calling `update()` or `updateIMU()`. ### Example ```cpp void setup() { Madgwick filter; filter.begin(25); // 25 Hz sampling rate (40 ms per sample) } ``` ``` -------------------------------- ### begin() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Sets the sample frequency for time integration. This method must be called before `update()` or `updateIMU()` to ensure accurate angle computations. ```APIDOC ## begin(float sampleFrequency) ### Description Sets the sample frequency in Hz used for time integration. This is critical for accurate angle computations and must be called before `update()` or `updateIMU()`. ### Signature `void begin(float sampleFrequency)` ### Parameters - `sampleFrequency` (float): Sampling frequency in Hz. ### Returns void ### Example ```cpp filter.begin(25); // 25 Hz (40 ms per sample) filter.begin(100); // 100 Hz (10 ms per sample) filter.begin(1000); // 1000 Hz (1 ms per sample) ``` ``` -------------------------------- ### Get Roll Angle in Radians Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Retrieves the roll angle in radians after sensor data has been updated. The angle is in the range of -π to +π. ```cpp filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float rollRadians = filter.getRollRadians(); // e.g., 0.27 Serial.println(rollRadians); ``` -------------------------------- ### Madgwick::begin() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Configures the Madgwick AHRS algorithm with the specified sample frequency. ```APIDOC ## begin(float sampleFrequency) ### Description Configures the Madgwick AHRS algorithm with the specified sample frequency. ### Method `begin` ### Parameters #### Path Parameters - **sampleFrequency** (float) - Required - The frequency at which sensor data is sampled (in Hz). ### Request Example ```cpp myAHRS.begin(100.0f); // Set sample frequency to 100 Hz ``` ``` -------------------------------- ### Get Yaw Angle in Degrees Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Retrieves the yaw angle in degrees after sensor data has been updated. The angle is in the range of 0° to 360°. ```cpp filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float yawDegrees = filter.getYaw(); // e.g., 270.0 Serial.println(yawDegrees); ``` -------------------------------- ### AHRS Mode (9-DOF) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Use this method when all three sensors (gyroscope, accelerometer, and magnetometer) are available and reliable to get an absolute heading. ```APIDOC ## update(gx, gy, gz, ax, ay, az, mx, my, mz) ### Description Updates the orientation filter with gyroscope, accelerometer, and magnetometer data to calculate roll, pitch, and yaw (absolute heading). ### Method `update(gx, gy, gz, ax, ay, az, mx, my, mz)` ### Parameters - **gx** (float) - Gyroscope X-axis data in degrees/sec - **gy** (float) - Gyroscope Y-axis data in degrees/sec - **gz** (float) - Gyroscope Z-axis data in degrees/sec - **ax** (float) - Accelerometer X-axis data in g units - **ay** (float) - Accelerometer Y-axis data in g units - **az** (float) - Accelerometer Z-axis data in g units - **mx** (float) - Magnetometer X-axis data in arbitrary units - **my** (float) - Magnetometer Y-axis data in arbitrary units - **mz** (float) - Magnetometer Z-axis data in arbitrary units ### Output - Roll (float) - Pitch (float) - Yaw (float) - Absolute heading ``` -------------------------------- ### Madgwick begin() Method Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Initializes the Madgwick filter with the sensor's sample frequency. This method must be called before updating the filter with sensor data. ```cpp void begin(float sampleFrequency); ``` -------------------------------- ### Get Pitch Angle - Madgwick AHRS Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Retrieves the pitch angle in degrees after the filter has been updated. The angle is cached and updated on subsequent calls to update() or updateIMU(). ```cpp float getPitch(); ``` ```cpp filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float pitchDegrees = filter.getPitch(); // e.g., -5.2 Serial.println(pitchDegrees); ``` -------------------------------- ### Configure Sample Frequency with begin() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/README.md Set the sample frequency in Hz to match your IMU's actual sampling rate. This is crucial for accurate integration of gyroscope data and overall angle computation. ```cpp filter.begin(25); // 25 Hz → 40 ms per sample ``` ```cpp filter.begin(100); // 100 Hz → 10 ms per sample ``` ```cpp filter.begin(1000); // 1000 Hz → 1 ms per sample ``` -------------------------------- ### Get Roll Angle - Madgwick AHRS Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Retrieves the roll angle in degrees after the filter has been updated. The angle is cached and updated on subsequent calls to update() or updateIMU(). ```cpp float getRoll(); ``` ```cpp filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float rollDegrees = filter.getRoll(); // e.g., 15.5 Serial.println(rollDegrees); ``` -------------------------------- ### begin(float sampleFrequency) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MANIFEST.txt Configures the MadgwickAHRS filter with the specified sample frequency. This method must be called before updating the filter with sensor data. ```APIDOC ## begin(float sampleFrequency) ### Description Configures the MadgwickAHRS filter with the specified sample frequency. ### Method `void begin(float sampleFrequency)` ### Parameters #### Path Parameters - **sampleFrequency** (float) - Required - The frequency at which sensor data is sampled, in Hz. ``` -------------------------------- ### Sample Frequency Parameter Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/types.md Details the 'sampleFrequency' parameter used in the `begin()` method, specifying its type, range, default value, and purpose in time integration. ```APIDOC ## Sample Frequency **Type**: `float` (in Hz, hertz) **Range**: Typically 1 to 1000 Hz, depending on IMU hardware and application requirements **Default**: 512 Hz (if not set via `begin()`) **Used by**: Parameter for `begin(float sampleFrequency)` method **Purpose**: Defines the time step for integrating quaternion derivatives. The inverse (1/sampleFrequency) is used internally for time integration: `q_new = q_old + (q_derivative * invSampleFreq)` ``` -------------------------------- ### Get Orientation Angles in Radians Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Retrieves the computed orientation angles in radians. Roll is -π to +π, Pitch is -π/2 to +π/2, and Yaw is 0 to 2π. ```cpp float rollRad = filter.getRollRadians(); float pitchRad = filter.getPitchRadians(); float yawRad = filter.getYawRadians(); ``` -------------------------------- ### Get Orientation Angles in Degrees Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Retrieves the computed orientation angles in degrees. Roll is -180° to +180°, Pitch is -90° to +90°, and Yaw (heading) is 0° to 360°. ```cpp float roll = filter.getRoll(); float pitch = filter.getPitch(); float heading = filter.getYaw(); ``` -------------------------------- ### Create and Use Multiple Madgwick Filter Instances Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md Demonstrates how to instantiate and configure multiple Madgwick AHRS filters for independent sensor data processing. Each filter can be initialized with a different sample rate. ```cpp #include Madgwick filter1; // Filter for IMU #1 Madgwick filter2; // Filter for IMU #2 void setup() { filter1.begin(25); // Configure filter 1 filter2.begin(100); // Configure filter 2 with different sample rate } void loop() { // Update both filters independently filter1.update(gx1, gy1, gz1, ax1, ay1, az1, mx1, my1, mz1); filter2.update(gx2, gy2, gz2, ax2, ay2, az2, mx2, my2, mz2); // Read orientations independently float roll1 = filter1.getRoll(); float roll2 = filter2.getRoll(); } ``` -------------------------------- ### Madgwick Filter Instantiation Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Shows how to create an instance of the Madgwick filter. It can be instantiated globally or as a private member of another class. Ensure the MadgwickAHRS library is included. ```cpp #include Madgwick filter; // Create global instance // Or in a class: class MyFilter { private: Madgwick filter; }; ``` -------------------------------- ### Get Pitch Angle in Radians Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Retrieves the pitch angle (rotation around the Y-axis) in radians. The angle is computed and cached on the first call after an update and subsequent calls return the cached value. ```cpp float getPitchRadians(); ``` ```cpp filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float pitchRadians = filter.getPitchRadians(); // e.g., -0.091 Serial.println(pitchRadians); ``` -------------------------------- ### Initialize Madgwick Filter Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Initializes a new Madgwick filter instance with default settings. Call `begin()` to set the actual IMU sample rate. ```cpp Madgwick filter; filter.begin(25); // Set to actual IMU sample rate ``` -------------------------------- ### Get Yaw Angle in Radians Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Retrieves the yaw angle (rotation around the Z-axis) in radians. An offset of +π radians is applied to shift the angle range. The angle is computed and cached on the first call after an update and subsequent calls return the cached value. ```cpp float getYawRadians(); ``` ```cpp filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float yawRadians = filter.getYawRadians(); // e.g., 4.71 Serial.println(yawRadians); ``` -------------------------------- ### updateIMU() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Performs a 6-DOF IMU update using only accelerometer and gyroscope readings, providing orientation but not absolute heading. ```APIDOC ## updateIMU(float gx, float gy, float gz, float ax, float ay, float az) ### Description Performs a 6-DOF IMU update using accelerometer and gyroscope data only. Provides orientation but not absolute heading information. ### Signature `void updateIMU(float gx, float gy, float gz, float ax, float ay, float az)` ### Parameters - `gx, gy, gz` (float): Gyroscope readings in degrees/sec. - `ax, ay, az` (float): Accelerometer readings in g (gravity units). ### Returns void ### Example ```cpp filter.updateIMU(gx, gy, gz, ax, ay, az); ``` ``` -------------------------------- ### Madgwick AHRS Default Initialization Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md Creates a Madgwick instance with default settings for beta, quaternion, sample frequency, and angle computation status. Ensure to call begin() with your IMU's sample rate. ```cpp Madgwick filter; void setup() { // MUST call begin() with correct sample frequency filter.begin(25); // Your IMU's actual sample rate } ``` -------------------------------- ### MadgwickAHRS Usage Pattern Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Demonstrates the basic usage pattern for the Madgwick filter: creating an instance, configuring it, updating it with sensor data, and retrieving computed angles. ```cpp Madgwick myFilter; myFilter.begin(25); myFilter.update(...); float roll = myFilter.getRoll(); ``` -------------------------------- ### Madgwick() Constructor Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Initializes a new Madgwick filter instance with default settings for quaternion, beta gain, and sample frequency. ```APIDOC ## Madgwick() ### Description Initializes a new Madgwick filter with default state: Quaternion (1, 0, 0, 0), Beta (gain) 0.1f, and Sample frequency 512 Hz. ### Signature `Madgwick(void)` ### Parameters None ### Returns Class instance ``` -------------------------------- ### MadgwickAHRS Include Paths in Arduino IDE Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Shows how the Arduino IDE automatically adds the 'src/' directory to the include path, allowing direct inclusion of the MadgwickAHRS header. ```cpp #include // Finds src/MadgwickAHRS.h ``` -------------------------------- ### Madgwick() Constructor Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Creates and initializes a new Madgwick filter instance. It sets the initial quaternion state, the algorithm gain (beta), and the sample frequency. ```APIDOC ## Madgwick() ### Description Creates and initializes a new Madgwick filter instance. ### Parameters None ### Return Type Madgwick instance ### Behavior Initializes the quaternion state to (q0=1.0, q1=0.0, q2=0.0, q3=0.0) representing no rotation, sets the algorithm gain (beta) to the default value of 0.1, and initializes the sample frequency to the default 512 Hz. ### Example ```cpp #include Madgwick filter; // Create filter instance void setup() { Serial.begin(9600); filter.begin(25); // Initialize with 25 Hz sample rate } ``` ``` -------------------------------- ### Basic MadgwickAHRS Filter Usage Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Initialize and use the MadgwickAHRS filter in an Arduino sketch. Ensure to call begin() with the correct sample frequency and update the filter with sensor data in the loop. ```cpp #include Madgwick filter; void setup() { Serial.begin(9600); filter.begin(25); // 25 Hz IMU } void loop() { // Read sensor data float gx, gy, gz; // Gyroscope in deg/sec float ax, ay, az; // Accelerometer in g readSensors(gx, gy, gz, ax, ay, az); // Assuming readSensors is defined elsewhere // Update filter filter.updateIMU(gx, gy, gz, ax, ay, az); // Get orientation Serial.print(filter.getRoll()); Serial.print(","); Serial.print(filter.getPitch()); Serial.print(","); Serial.println(filter.getYaw()); } ``` -------------------------------- ### Quaternion Update using invSampleFreq Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/types.md Illustrates how invSampleFreq is used to calculate the quaternion delta (q_delta) from the quaternion derivative. This is essential for updating the orientation over the sample time. ```plaintext q_delta = quaternion_derivative * invSampleFreq ``` -------------------------------- ### MadgwickAHRS Constructor Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Initializes the MadgwickAHRS filter with default parameters, including the algorithm gain (beta), identity quaternion, and inverse sample frequency. ```cpp Madgwick::Madgwick() { beta = betaDef; q0 = 1.0f; q1 = 0.0f; q2 = 0.0f; q3 = 0.0f; invSampleFreq = 1.0f / sampleFreqDef; anglesComputed = 0; } ``` -------------------------------- ### Handle Zero Magnetometer Input for Madgwick Update Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md Demonstrates how to check for zero magnetometer readings before calling the Madgwick filter's update function. A zero vector triggers a fallback to IMU-only mode. The library also handles this fallback automatically if the magnetometer input is zero. ```cpp float mx, my, mz; // Read from magnetometer // Zero check - automatic fallback to IMU-only mode if ((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f)) { filter.updateIMU(gx, gy, gz, ax, ay, az); // Fallback } else { filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); // Full 9-DOF } // Or let the library handle this automatically: filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); // Auto-fallback if mag is zero ``` -------------------------------- ### Configure Madgwick Filter Sample Frequency Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md Initialize the Madgwick filter by setting the sensor sampling frequency in Hertz. This affects the internal time step used for quaternion derivative integration. Use this when setting up the filter in your application. ```cpp Madgwick filter; void setup() { // Configure filter for IMU sampled at 25 Hz filter.begin(25); // or for 100 Hz IMU: // filter.begin(100); } ``` -------------------------------- ### Set Default Algorithm Gain (Beta) (Compile-Time) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md Defines the default proportional gain (beta) for the gradient descent algorithm. This constant controls the balance between convergence speed and filter output smoothness. Modify this value in `src/MadgwickAHRS.cpp` and recompile to change the gain. ```cpp #define betaDef 0.1f // 2 * proportional gain ``` -------------------------------- ### Madgwick Constructor Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Initializes a new Madgwick filter instance with default values. The constructor sets the initial quaternion state and default algorithm gain (beta) and sample frequency. ```cpp Madgwick(void); ``` -------------------------------- ### Quaternion Representation Formula Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Illustrates the internal unit quaternion representation used by the library. The sum of the squares of its components must equal 1.0. ```text q = q0 + q1*i + q2*j + q3*k Constraint: q0² + q1² + q2² + q3² = 1.0 ``` -------------------------------- ### invSampleFreq Calculation Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/types.md Shows how invSampleFreq is calculated from the sampleFrequency. This value is used to scale the quaternion derivative for updating the quaternion over a specific time interval. ```plaintext 1.0 / sampleFrequency ``` -------------------------------- ### Madgwick Class Constructor Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Initializes a new Madgwick AHRS object. ```APIDOC ## Madgwick() ### Description Constructs a new Madgwick AHRS object. ### Method Constructor ### Parameters None ### Response Example ```cpp Madgwick myAHRS; ``` ``` -------------------------------- ### Madgwick::updateIMU() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Updates the AHRS algorithm with gyroscope, accelerometer, and magnetometer data. ```APIDOC ## updateIMU() ### Description Updates the AHRS algorithm using gyroscope, accelerometer, and magnetometer data. ### Method `updateIMU` ### Parameters None ### Request Example ```cpp myAHRS.updateIMU(); ``` ``` -------------------------------- ### Set Sample Frequency Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Sets the sample frequency in Hz for time integration. This must be called before `update()` or `updateIMU()`. Incorrect frequency leads to inaccurate angle computations. ```cpp filter.begin(25); // 25 Hz (40 ms per sample) filter.begin(100); // 100 Hz (10 ms per sample) filter.begin(1000); // 1000 Hz (1 ms per sample) ``` -------------------------------- ### update() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Updates the filter state with accelerometer, gyroscope, and magnetometer readings to compute the device's orientation. ```APIDOC ## Method: update() ### Description Updates the filter state with a complete measurement set including accelerometer, gyroscope, and magnetometer readings. ### Method `void update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **gx** (float) - Yes - Gyroscope rotation rate around X-axis in degrees per second - **gy** (float) - Yes - Gyroscope rotation rate around Y-axis in degrees per second - **gz** (float) - Yes - Gyroscope rotation rate around Z-axis in degrees per second - **ax** (float) - Yes - Accelerometer measurement along X-axis in units of gravity (g) - **ay** (float) - Yes - Accelerometer measurement along Y-axis in units of gravity (g) - **az** (float) - Yes - Accelerometer measurement along Z-axis in units of gravity (g) - **mx** (float) - Yes - Magnetometer measurement along X-axis (uncalibrated unit) - **my** (float) - Yes - Magnetometer measurement along Y-axis (uncalibrated unit) - **mz** (float) - Yes - Magnetometer measurement along Z-axis (uncalibrated unit) ### Return Type void ### Behavior Performs a complete AHRS algorithm update using all three sensor types. Converts gyroscope readings from degrees/sec to radians/sec, normalizes accelerometer and magnetometer vectors, and computes the corrective step using gradient descent. Automatically falls back to IMU-only mode if magnetometer readings are zero (0, 0, 0). ### Quaternion Update Updates the internal quaternion representation (q0, q1, q2, q3) that represents the current orientation. The updated quaternion is normalized to maintain unit magnitude. ### Angle Computation Invalidates cached angle computations; subsequent calls to `getRoll()`, `getPitch()`, or `getYaw()` will trigger angle recomputation from the updated quaternion. ### Example ```cpp void loop() { float ax, ay, az; // Accelerometer in g float gx, gy, gz; // Gyroscope in degrees/sec float mx, my, mz; // Magnetometer (uncalibrated) // Read sensor values from IMU... filter.update(gx, gy, gz, ax, ay, az, mx, my, mz); float roll = filter.getRoll(); // Get orientation in degrees float pitch = filter.getPitch(); float yaw = filter.getYaw(); } ``` ``` -------------------------------- ### Madgwick Time Integration Scheme Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md The algorithm uses forward Euler integration for quaternion updates. Higher sample rates improve accuracy, and the scheme is generally stable for typical sample rates. ```c++ q(t + dt) = q(t) + (dq/dt) * dt = q(t) + (quaternion_derivative * invSampleFreq) ``` -------------------------------- ### Coordinate System Definition Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Defines the coordinate system used for sensor input. Ensure all sensors adhere to this convention for accurate results. ```text +X: Forward +Y: Right +Z: Down ``` -------------------------------- ### Update Sensor Fusion with Gyro, Accel, and Magnetometer Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Processes gyroscope, accelerometer, and magnetometer data to update the orientation quaternion. Falls back to IMU mode if magnetometer data is invalid. ```cpp void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) { // ... implementation details ... } ``` -------------------------------- ### Madgwick IMU Update (6-DOF) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md Use this method when a magnetometer is unavailable or in environments with significant magnetic interference. It relies only on accelerometer and gyroscope data. ```c++ updateIMU(gx, gy, gz, ax, ay, az) ``` -------------------------------- ### Set Default Sample Frequency (Compile-Time) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/configuration.md Defines the default sample frequency in Hz. This value is used if `begin()` is not called before using the filter, establishing the initial assumption for the filter's operating frequency. ```cpp #define sampleFreqDef 512.0f // sample frequency in Hz ``` -------------------------------- ### updateIMU() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/api-reference/Madgwick.md Updates the filter state using only accelerometer and gyroscope readings. This method is suitable for scenarios where magnetometer data is unavailable or unreliable, providing a 6-DOF orientation estimation. ```APIDOC ## Method: updateIMU() ### Description Updates the filter state using only accelerometer and gyroscope readings (no magnetometer). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method Signature ```cpp void updateIMU(float gx, float gy, float gz, float ax, float ay, float az); ``` ### Parameters - **gx** (float) - Required - Gyroscope rotation rate around X-axis in degrees per second - **gy** (float) - Required - Gyroscope rotation rate around Y-axis in degrees per second - **gz** (float) - Required - Gyroscope rotation rate around Z-axis in degrees per second - **ax** (float) - Required - Accelerometer measurement along X-axis in units of gravity (g) - **ay** (float) - Required - Accelerometer measurement along Y-axis in units of gravity (g) - **az** (float) - Required - Accelerometer measurement along Z-axis in units of gravity (g) ### Return Type void ### Behavior Performs an IMU-only algorithm update using accelerometer and gyroscope data. Converts gyroscope readings from degrees/sec to radians/sec, normalizes the accelerometer vector, and computes the corrective step. This method is used when magnetometer data is unavailable, unreliable, or produces zero readings. It provides 6-DOF (six degrees of freedom) orientation estimation. Updates the internal quaternion representation (q0, q1, q2, q3). The quaternion is normalized to maintain unit magnitude. Invalidates cached angle computations; subsequent calls to `getRoll()`, `getPitch()`, or `getYaw()` will trigger angle recomputation. ### Example ```cpp void loop() { float ax, ay, az; // Accelerometer in g float gx, gy, gz; // Gyroscope in degrees/sec // Read sensor values... // Use IMU-only update (no magnetometer) filter.updateIMU(gx, gy, gz, ax, ay, az); float roll = filter.getRoll(); float pitch = filter.getPitch(); float yaw = filter.getYaw(); } ``` ``` -------------------------------- ### Include MadgwickAHRS Header Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Include the MadgwickAHRS library header to make the Madgwick class available in your Arduino sketch. ```cpp #include ``` -------------------------------- ### getRoll(), getPitch(), getYaw() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Retrieves the computed orientation angles in degrees. getRoll() ranges from -180° to +180°, getPitch() from -90° to +90°, and getYaw() from 0° to 360°. ```APIDOC ## getRoll(), getPitch(), getYaw() ### Description Query the computed orientation angles in degrees. - `getRoll()`: -180° to +180° (forward/backward tilt) - `getPitch()`: -90° to +90° (left/right tilt) - `getYaw()`: 0° to 360° (compass heading) ### Signatures - `float getRoll()` - `float getPitch()` - `float getYaw()` ### Returns float angle in degrees ### Example ```cpp float roll = filter.getRoll(); float pitch = filter.getPitch(); float heading = filter.getYaw(); ``` ``` -------------------------------- ### Default Sample Frequency (sampleFreqDef) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Defines the default sample frequency for the MadgwickAHRS algorithm. This value is used if the begin() method is not explicitly called. ```cpp #define sampleFreqDef 512.0f ``` -------------------------------- ### update() Method Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Processes gyroscope, accelerometer, and magnetometer data to update the estimated orientation. Falls back to IMU mode if magnetometer data is invalid. ```APIDOC ## update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) ### Description Processes gyroscope, accelerometer, and magnetometer data to update the estimated orientation. Falls back to IMU mode if magnetometer data is invalid. ### Method `void update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz)` ### Parameters #### Input Data - **gx, gy, gz** (float) - Gyroscope readings in degrees/sec. - **ax, ay, az** (float) - Accelerometer readings in g. - **mx, my, mz** (float) - Magnetometer readings in arbitrary units. ### Algorithm Steps 1. Check for zero magnetometer; fallback to IMU mode if detected. 2. Convert gyroscope from deg/sec to rad/sec. 3. Compute quaternion derivative from gyroscope. 4. Normalize accelerometer and magnetometer vectors. 5. Compute magnetic field reference direction. 6. Compute gradient descent correction step. 7. Apply correction to quaternion derivative. 8. Integrate quaternion derivative. 9. Normalize quaternion to unit magnitude. 10. Invalidate cached angles. ``` -------------------------------- ### Angle Return Types Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/types.md Lists the available methods for retrieving orientation angles (roll, pitch, yaw) and their corresponding return types, units, ranges, and meanings. All return values are float precision. ```APIDOC ## Angle Return Types | Method | Return Type | Unit | Range | Meaning | |--------|-------------|------|-------|---------| | getRoll() | float | degrees | -180° to +180° | Rotation around X-axis (forward/backward tilt) | | getPitch() | float | degrees | -90° to +90° | Rotation around Y-axis (left/right tilt) | | getYaw() | float | degrees | 0° to 360° | Rotation around Z-axis (compass heading) | | getRollRadians() | float | radians | -π to +π | Rotation around X-axis | | getPitchRadians() | float | radians | -π/2 to +π/2 | Rotation around Y-axis | | getYawRadians() | float | radians | 0 to 2π | Rotation around Z-axis | All return values are **float** precision (32-bit IEEE 754 single-precision floating-point). ``` -------------------------------- ### Default Algorithm Gain (betaDef) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Sets the default gain parameter for the MadgwickAHRS algorithm's gradient descent correction. Adjust this value to tune the filter's responsiveness and smoothness. ```cpp #define betaDef 0.1f ``` -------------------------------- ### Madgwick Class Methods Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/types.md This section details the public methods available for the Madgwick class, used for initializing and updating the AHRS filter, and retrieving orientation data. ```APIDOC ## Madgwick Class Methods ### Constructor #### Madgwick() - **Purpose**: Initializes the quaternion to identity (no rotation) and sets the default beta value. - **Parameters**: None - **Return Type**: None ### Initialization #### begin(float sampleFrequency) - **Purpose**: Sets the IMU sample frequency in Hz, which is used for time integration within the filter. - **Parameters**: - **sampleFrequency** (float) - The frequency of sensor data samples in Hertz. - **Return Type**: void ### Sensor Data Update #### update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) - **Purpose**: Updates the AHRS filter with full 9-DOF sensor data, including gyroscope, accelerometer, and magnetometer readings. - **Parameters**: - **gx** (float) - Gyroscope reading around the X-axis. - **gy** (float) - Gyroscope reading around the Y-axis. - **gz** (float) - Gyroscope reading around the Z-axis. - **ax** (float) - Accelerometer reading around the X-axis. - **ay** (float) - Accelerometer reading around the Y-axis. - **az** (float) - Accelerometer reading around the Z-axis. - **mx** (float) - Magnetometer reading around the X-axis. - **my** (float) - Magnetometer reading around the Y-axis. - **mz** (float) - Magnetometer reading around the Z-axis. - **Return Type**: void #### updateIMU(float gx, float gy, float gz, float ax, float ay, float az) - **Purpose**: Updates the AHRS filter with 6-DOF sensor data, using only gyroscope and accelerometer readings. - **Parameters**: - **gx** (float) - Gyroscope reading around the X-axis. - **gy** (float) - Gyroscope reading around the Y-axis. - **gz** (float) - Gyroscope reading around the Z-axis. - **ax** (float) - Accelerometer reading around the X-axis. - **ay** (float) - Accelerometer reading around the Y-axis. - **az** (float) - Accelerometer reading around the Z-axis. - **Return Type**: void ### Orientation Retrieval (Degrees) #### getRoll() - **Purpose**: Retrieves the current roll angle in degrees. - **Parameters**: None - **Return Type**: float (Range: -180 to +180) #### getPitch() - **Purpose**: Retrieves the current pitch angle in degrees. - **Parameters**: None - **Return Type**: float (Range: -90 to +90) #### getYaw() - **Purpose**: Retrieves the current yaw angle in degrees. - **Parameters**: None - **Return Type**: float (Range: 0 to 360) ### Orientation Retrieval (Radians) #### getRollRadians() - **Purpose**: Retrieves the current roll angle in radians. - **Parameters**: None - **Return Type**: float (Range: -π to +π) #### getPitchRadians() - **Purpose**: Retrieves the current pitch angle in radians. - **Parameters**: None - **Return Type**: float (Range: -π/2 to +π/2) #### getYawRadians() - **Purpose**: Retrieves the current yaw angle in radians. - **Parameters**: None - **Return Type**: float (Range: 0 to 2π) ``` -------------------------------- ### updateIMU() Method Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Processes gyroscope and accelerometer data to update the estimated orientation. This method does not use magnetometer data. ```APIDOC ## updateIMU(float gx, float gy, float gz, float ax, float ay, float az) ### Description Processes gyroscope and accelerometer data to update the estimated orientation. This method does not use magnetometer data. ### Method `void updateIMU(float gx, float gy, float gz, float ax, float ay, float az)` ### Parameters #### Input Data - **gx, gy, gz** (float) - Gyroscope readings in degrees/sec. - **ax, ay, az** (float) - Accelerometer readings in g. ### Algorithm Steps 1. Convert gyroscope from deg/sec to rad/sec. 2. Compute quaternion derivative from gyroscope. 3. Normalize accelerometer vector. 4. Compute gradient descent correction step (accelerometer only). 5. Apply correction to quaternion derivative. 6. Integrate quaternion derivative. 7. Normalize quaternion to unit magnitude. 8. Invalidate cached angles. ### Difference from update() No magnetometer processing, simpler gradient step computation. ``` -------------------------------- ### Madgwick Instance Memory Layout Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Details the memory allocation for a Madgwick filter instance on a 32-bit system, showing the size of each internal variable. ```text Madgwick instance (32-bit system): ├── beta (float) 4 bytes ├── q0 (float) 4 bytes ├── q1 (float) 4 bytes ├── q2 (float) 4 bytes ├── q3 (float) 4 bytes ├── invSampleFreq (float) 4 bytes ├── roll (float) 4 bytes ├── pitch (float) 4 bytes ├── yaw (float) 4 bytes ├── anglesComputed (char) 1 byte └── padding ~3 bytes Total: ~48 bytes ``` -------------------------------- ### IMU Mode (6-DOF) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Use this method when the magnetometer is unavailable or unreliable. This mode provides roll and pitch, but the yaw will drift over time. ```APIDOC ## updateIMU(gx, gy, gz, ax, ay, az) ### Description Updates the orientation filter with gyroscope and accelerometer data to calculate roll, pitch, and yaw (heading drifts). This mode is automatically triggered if magnetometer data is invalid in AHRS mode. ### Method `updateIMU(gx, gy, gz, ax, ay, az)` ### Parameters - **gx** (float) - Gyroscope X-axis data in degrees/sec - **gy** (float) - Gyroscope Y-axis data in degrees/sec - **gz** (float) - Gyroscope Z-axis data in degrees/sec - **ax** (float) - Accelerometer X-axis data in g units - **ay** (float) - Accelerometer Y-axis data in g units - **az** (float) - Accelerometer Z-axis data in g units ### Output - Roll (float) - Pitch (float) - Yaw (float) - Heading drifts ``` -------------------------------- ### Update Sensor Fusion with Gyro and Accelerometer (IMU Mode) Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/MODULE_INDEX.md Processes gyroscope and accelerometer data to update the orientation quaternion, suitable for environments where magnetometer data is unavailable or unreliable. ```cpp void Madgwick::updateIMU(float gx, float gy, float gz, float ax, float ay, float az) { // ... implementation details ... } ``` -------------------------------- ### getRollRadians(), getPitchRadians(), getYawRadians() Source: https://github.com/arduino-libraries/madgwickahrs/blob/master/_autodocs/INDEX.md Retrieves the computed orientation angles in radians. getRollRadians() ranges from -π to +π, getPitchRadians() from -π/2 to +π/2, and getYawRadians() from 0 to 2π. ```APIDOC ## getRollRadians(), getPitchRadians(), getYawRadians() ### Description Query the computed orientation angles in radians. - `getRollRadians()`: -π to +π - `getPitchRadians()`: -π/2 to +π/2 - `getYawRadians()`: 0 to 2π ### Signatures - `float getRollRadians()` - `float getPitchRadians()` - `float getYawRadians()` ### Returns float angle in radians ### Example ```cpp float rollRad = filter.getRollRadians(); float pitchRad = filter.getPitchRadians(); float yawRad = filter.getYawRadians(); ``` ```