AP_AHRS Detailed Reference

Class Documentation

class AP_AHRS

Detailed Description

AP_AHRS (Attitude and Heading Reference System) is the central navigation system for ArduPilot. It uses sensor fusion (Extended Kalman Filter - EKF) to combine data from GPS, compass, gyroscopes, and accelerometers to provide accurate attitude estimation.

Key Features:

  • Extended Kalman Filter (EKF) for attitude estimation

  • GPS fusion for position

  • Compass fusion for heading

  • Sensor redundancy handling

  • Multiple EKF lanes for safety

  • Airspeed estimation

  • Wind estimation

Inheritance

This class does not inherit from any other class. It is a top-level class.

Related Classes:

Public Types

enum AP_AHRS::Status

Navigation/AHRS status.

Noindex:

NO_FIX = 0,         // No position fix
FIX_2D = 1,         // 2D position fix
FIX_3D = 2,         // 3D position fix
DR = 3,             // Dead reckoning
INTERNAL = 4,       // Internal navigation
EXTERNAL = 5        // External navigation
enum AP_AHRS::EKFType

EKF type selection.

Noindex:

NONE = 0,           // DCM (simple filter)
EKF2 = 1,           // EKF2
EKF3 = 2,           // EKF3

Public Member Functions

Singleton

static AP_AHRS *AP_AHRS::get_singleton()

Get singleton instance of AHRS.

Returns:

Pointer to AP_AHRS instance, or nullptr if not available

AP_AHRS &ahrs = AP_AHRS::get_singleton();

Initialization & Update

void AP_AHRS::init(void)

Initialize AHRS. Must be called once at startup.

void AP_AHRS::update(bool skip_ins_update = false)

Update AHRS estimates. Must be called in main loop (typically 100Hz).

Parameters:

skip_ins_update – Skip INS update if true

void AP_AHRS::reset()

Reset AHRS to current sensor values.

Attitude (Euler Angles)

float AP_AHRS::get_roll() const

Get current roll angle.

Returns:

Roll angle in radians

float AP_AHRS::get_pitch() const

Get current pitch angle.

Returns:

Pitch angle in radians

float AP_AHRS::get_yaw() const

Get current yaw angle.

Returns:

Yaw angle in radians

float AP_AHRS::get_roll_deg() const

Get current roll angle in degrees.

Returns:

Roll angle in degrees

float AP_AHRS::get_pitch_deg() const

Get current pitch angle in degrees.

Returns:

Pitch angle in degrees

float AP_AHRS::get_yaw_deg() const

Get current yaw angle in degrees.

Returns:

Yaw angle in degrees

Vector3f get_roll_pitch_yaw() const

Get all euler angles as a vector.

Returns:

Vector3f containing roll, pitch, yaw in radians

Quaternion

bool AP_AHRS::get_quaternion(Quaternion &quat) const

Get current attitude as quaternion.

Parameters:

quat – Output quaternion (w, x, y, z)

Returns:

true if quaternion is available

Rotation Matrices

const Matrix3f &AP_AHRS::get_rotation_body_to_ned(void) const

Get rotation matrix from body to NED frame.

Returns:

3x3 rotation matrix

Gyro & Accelerometer

const Vector3f &AP_AHRS::get_gyro(void) const

Get estimated gyro values.

Returns:

Gyro vector in rad/s

const Vector3f &AP_AHRS::get_accel(void) const

Get estimated accelerometer values.

Returns:

Acceleration vector in m/s^2

const Vector3f &get_accel_ef() const

Get acceleration in earth frame.

Returns:

Earth-frame acceleration

Position

bool AP_AHRS::get_location(Location &loc) const

Get current GPS location.

Parameters:

loc – Output Location struct

Returns:

true if location is available

bool AP_AHRS::get_origin(Location &ret) const

Get navigation origin location.

Parameters:

loc – Output Location struct

Returns:

true if origin is set

bool AP_AHRS::get_velocity_NED(Vector3f &vec) const

Get velocity in NED frame.

Parameters:

vec – Output velocity vector (m/s)

Returns:

true if velocity is available

Velocity

float AP_AHRS::groundspeed(void) const

Get ground speed magnitude.

Returns:

Ground speed in m/s

const Vector2f &AP_AHRS::groundspeed_vector() const

Get ground speed as 2D vector.

Returns:

N/E velocity in m/s

Airspeed

bool AP_AHRS::airspeed_EAS(float &airspeed_ret) const

Get Equivalent Airspeed (EAS).

Parameters:

airspeed_ret – Output airspeed in m/s

Returns:

true if airspeed is available

bool AP_AHRS::airspeed_TAS(float &airspeed_ret) const

Get True Airspeed (TAS).

Parameters:

airspeed_ret – Output airspeed in m/s

Returns:

true if airspeed is available

float AP_AHRS::get_EAS2TAS(void) const

Get EAS to TAS conversion factor.

Returns:

Conversion factor

Wind Estimation

const Vector3f &AP_AHRS::wind_estimate() const

Get estimated wind velocity.

Returns:

Wind vector in NED frame (m/s)

bool AP_AHRS::wind_estimate(Vector3f &wind) const

Get estimated wind velocity.

Parameters:

wind – Output wind vector

Returns:

true if wind estimate is available

float AP_AHRS::head_wind(void) const

Get head wind component.

Returns:

Head wind speed in m/s

Health & Status

bool AP_AHRS::healthy() const

Check if AHRS is healthy.

Returns:

true if healthy

bool AP_AHRS::initialised() const

Check if AHRS is initialized.

Returns:

true if initialized

bool AP_AHRS::get_filter_status(nav_filter_status &status) const

Get EKF filter status.

Parameters:

status – Output status structure

Returns:

true if status available

Home Position

bool AP_AHRS::home_is_set(void) const

Check if home position is set.

Returns:

true if home is set

bool AP_AHRS::set_home(const Location &loc)

Set home position.

Parameters:

loc – Home location

Returns:

true if successful

const Location &AP_AHRS::get_home(void) const

Get home position.

Returns:

Home location

Trim Values

const Vector3f &AP_AHRS::get_trim() const

Get trim values.

Returns:

Trim vector (roll, pitch, yaw) in radians

void AP_AHRS::set_trim(const Vector3f &new_trim)

Set trim values.

Parameters:

new_trim – Trim vector

void AP_AHRS::add_trim(float roll_in_radians, float pitch_in_radians, bool save_to_eeprom = true)

Add trim offset.

Parameters:
  • roll_in_radians – Roll offset

  • pitch_in_radians – Pitch offset

  • save_to_eeprom – Whether to save

EKF Control

void AP_AHRS::check_lane_switch(void)

Check and perform EKF lane switch if needed.

void AP_AHRS::request_yaw_reset(void)

Request EKF yaw reset.

EKFType AP_AHRS::active_EKF_type(void) const

Get currently active EKF type.

Returns:

Active EKF type

Usage Example

#include <AP_AHRS.h>

AP_AHRS &ahrs = AP_AHRS::get_singleton();

void setup() {
    // Initialize AHRS
    ahrs.init();
}

void loop() {
    // Update AHRS (typically 100Hz)
    ahrs.update();

    // Get attitude
    float roll = ahrs.get_roll();      // radians
    float pitch = ahrs.get_pitch();    // radians
    float yaw = ahrs.get_yaw();        // radians

    // Get as degrees
    float roll_deg = ahrs.get_roll_deg();
    float pitch_deg = ahrs.get_pitch_deg();
    float yaw_deg = ahrs.get_yaw_deg();

    Serial.printf("Attitude: R=%.1f, P=%.1f, Y=%.1f\n",
                roll_deg, pitch_deg, yaw_deg);

    // Get ground speed
    float ground_speed = ahrs.groundspeed();
    Serial.printf("Ground Speed: %.2f m/s\n", ground_speed);

    // Get position
    Location loc;
    if (ahrs.get_location(loc)) {
        Serial.printf("Position: %.6f, %.6f, %.0f\n",
                    loc.lat / 1e7, loc.lng / 1e7, loc.alt / 100.0);
    }

    // Get velocity
    Vector3f vel;
    if (ahrs.get_velocity_NED(vel)) {
        Serial.printf("Velocity: N=%.2f, E=%.2f, D=%.2f m/s\n",
                    vel.x, vel.y, vel.z);
    }

    // Get wind estimate
    Vector3f wind = ahrs.wind_estimate();
    Serial.printf("Wind: N=%.2f, E=%.2f, D=%.2f m/s\n",
                wind.x, wind.y, wind.z);
}

See Also

  • Sensor Libraries - Overview of sensor libraries

  • api/ap_gps - GPS sensor for position

  • api/ap_compass - Compass for heading

  • api/ap_baro - Barometer for altitude

  • Control Libraries - Control libraries (uses AHRS)