Reefwing AHRS Arduino Library for Drones — Part 2

David Such
12 min readAug 9

The Reefwing AHRS library provides an Attitude and Heading Reference System (AHRS) class for use with Arduino compatible boards. In Part 2 of this article we will explain how to use the library and provide hardware specific examples for the Arduino Nano, Nano 33 BLE, Nano 33 BLE SENSE (REV 1 and REV 2), Nano 33 IoT, MKR Vidor 4000, Portenta H7 and the Seeed XIAO nRF52840 Sense boards. These will use the following IMUs:

  • LSM9DS1
  • LSM6DS3
  • BMI270 & BMM150
  • MPU6000/MPU6500/MPU6050

Using the Reefwing AHRS Library

In order to get the correct angles out of the AHRS, we need to take the following steps:

  1. Calibrate the IMU (if available) and assign the appropriate offsets. This is normally possible in the IMU library.
  2. In setup(), instantiate the ReefwingAHRS class and call begin() to detect the board type, and assign the default free parameters, fusion algorithm (Madgwick) and declination (Sydney, Australia). If you want to use something other than the defaults, then change those now.
  3. On each iteration of loop():
  • If required adjust the sensor data to the correct format. Accelerometer — g, Gyroscope — DPS (degrees per second), and Magnetometer — can be anything (e.g., gauss or uT). The other thing to check is that the axis orientation is consistent for the three sensors. On the LSM9DS1, the magnetometer x-axis is inverted.
  • Pass the latest formatted sensor data from the IMU to the AHRS class.

4. Update the AHRS model (call update()).

5. If the selected AHRS model uses quaternions (i.e., COMPLEMENTARY, MADGWICK, and MAHONY), convert the quaternion to a Euler Angle.

The best way to illustrate this process is with the provided examples.

Examples — boardType.ino

This simple example attempts to detect the board type using the methodology explained in our article on the nRF52840 boards.

David Such

Reefwing Software · Embedded Systems Engineer · iOS & Android Development · Robotics · Drones · Arduino · Raspberry Pi · Flight Control

Recommended from Medium