# AHRS Library

**GyroLib** is a **free, open-source** Attitude and Heading Reference System (AHRS) library for the Matlab. It includes basic algorithms that allow to determine the orientation of the device equipped with the inertial sensors - accelerometers and gyroscopes and with vector magnetometer.

**Axes definitions.** It is assumed that the device is equipped with three-axis accelerometer, gyroscope and vector magnetometer with mutually orthogonal axes: **x** axis is looking “forward”; **z** axis is looking “downwards”; **y** axis completes the right orthogonal system of axes.

**Sensors measurements.** Gyroscopes measure positive angular rates being rotated counter-clockwise (if one watches from the end of the corresponding axis). The magnetometer is assumed to measure the Earth’s magnetic field vector. Accelerometer “measures” the gravity force (it actually doesn’t - it can only measure active forces, and when it sits on the table it measures the force with which the table acts on the accelerometer body preventing it from falling right through the lid of the table).

**Order of rotations.** The standard ZYX order is used - first we rotate counter-clockwise around the **z** axis, then around the new **y** axis and finally around the new **x** axis.

**Attitude determination.** What AHRS is basically doing is that it finds the attitude of the device, equipped with sensors, relative to some other predefined frame. We call the first frame “body” frame and latter “navigation” frame. We can define the attitude of the body using two noncollinear vectors attached to it.

**TRIAD** and **QUEST. **These two algorithms determine the attitude using information from two sensors that measure the projections of two noncollinear vectors on the axes of the body frame. We can use Earth’s magnetic field vector and gravity vector, or two direction vectors to the distant stars, etc. We also need to know the projections of the same two vectors on the axes of the navigation frame. TRIAD and QUEST algorithms employ the described principle and return the estimations of the direction cosine matrix and attitude quaternion respectively.

**AHRS** algorithms. The problem with attitude determination using magnetic field vector and “gravity” vector is that these two vectors can easily be disturbed (accelerated motion, magnetic field sources nearby, etc.), which leads to erroneous attitude estimates. AHRS algorithm uses the information from the gyroscopes (integrates angular rates) to determine the attitude and periodically employs information from the accelerometer and magnetometers to correct the attitude. GyroLib's AHRS algorithm was built using classical scheme, where the complimentary Kalman filter estimates the errors of calculated attitude and gyroscopes biases. Two types of AHRS were implemented: one that uses the direction cosine matrix (DCM) to represent the attitude, another uses the quaternion.

For the Matlab code please go to the File Exchange at Matlabcentral:

https://www.mathworks.com/matlabcentral/fileexchange/63250-gyrolib-ahrs-library