Skip to main content

A Python library for attitude estimation using inertial measurement units (IMUs)

Project description

AttiPy

AttiPy is a lightweight Python package for representing and estimating the attitude (orientation) of a moving body using IMU measurements and optional external aiding. It provides a practical Attitude and Heading Reference System (AHRS) implementation based on a multiplicative extended Kalman filter (MEKF), along with a clean, explicit abstraction for attitude representation, with clearly defined reference frames and rotation conventions.

Installation

pip install attipy

Quick start

Convert to/from a variety of attitude representations using the Attitude class:

import attipy as ap
import numpy as np


# From Euler angles to unit quaternion
att = ap.Attitude.from_euler([0.0, 0.0, 0.0])
q = att.as_quaternion()

Estimate attitude using IMU (accelerometer and gyroscope) measurements with the AHRS class:

import attipy as ap
import numpy as np


# Position, velocity, attitude and IMU reference signals
fs = 10.0  # Hz
t, pos, vel, euler, f_b, w_b = ap.pva_sim(fs)

# Add IMU measurement noise
acc_noise_density = 0.001  # (m/s^2) / sqrt(Hz)
gyro_noise_density = 0.0001  # (rad/s) / sqrt(Hz)
bg_b = (0.001, 0.002, 0.003)  # rad/s
rng = np.random.default_rng(42)
f_meas = f_b + acc_noise_density * np.sqrt(fs) * rng.standard_normal(f_b.shape)
w_meas = w_b + bg_b + gyro_noise_density * np.sqrt(fs) * rng.standard_normal(w_b.shape)

# Estimate attitude using AHRS
att0 = ap.Attitude.from_euler(euler[0])
ahrs = ap.AHRS(fs, att0)
euler_est = []
for f_i, w_i in zip(f_meas, w_meas):
    ahrs.update(f_i, w_i)
    euler_est.append(ahrs.attitude.as_euler())
euler_est = np.asarray(euler_est)

To limit integration drift, the AHRS state estimates must be corrected using long-term stable aiding measurements. When no aiding is available (as in the example above), stationarity is assumed to ensure convergence. By default, zero-velocity aiding with a 10 m/s standard deviation is used; this constrains roll and pitch only, as these are the only degrees of freedom observable from specific force and the known direction of gravity.

Under sustained linear acceleration, velocity aiding is recommended to maintain accurate attitude estimates. Heading (yaw) aiding should also be applied to correct yaw drift. The following example shows how to incorporate both.

import attipy as ap
import numpy as np


# Position, velocity, attitude and IMU reference signals
fs = 10.0  # Hz
t, pos, vel, euler, f_b, w_b = ap.pva_sim(fs)
yaw = euler[:, 2]

# Add IMU measurement noise
acc_noise_density = 0.001  # (m/s^2) / sqrt(Hz)
gyro_noise_density = 0.0001  # (rad/s) / sqrt(Hz)
bg_b = (0.001, 0.002, 0.003)  # rad/s
rng = np.random.default_rng(42)
f_meas = f_b + acc_noise_density * np.sqrt(fs) * rng.standard_normal(f_b.shape)
w_meas = w_b + bg_b + gyro_noise_density * np.sqrt(fs) * rng.standard_normal(w_b.shape)

# Add velocity and heading measurement noise
vel_var = 0.01  # (m/s)^2
yaw_var = 0.0001  # rad^2
rng = np.random.default_rng(42)
vel_meas = vel + np.sqrt(vel_var) * rng.standard_normal(vel.shape)
yaw_meas = yaw + np.sqrt(yaw_var) * rng.standard_normal(yaw.shape)

# Estimate attitude using AHRS
att0 = ap.Attitude.from_euler(euler[0])
ahrs = ap.AHRS(fs, att0)
euler_est = []
for f_i, w_i, v_i, y_i in zip(f_meas, w_meas, vel_meas, yaw_meas):
    ahrs.update(f_i, w_i, v_n=v_i, v_var=vel_var*np.ones(3), yaw=y_i, yaw_var=yaw_var)
    euler_est.append(ahrs.attitude.as_euler())
euler_est = np.asarray(euler_est)

Limitations and assumptions

  • Intended for small-area, low-velocity applications; Earth rotation is neglected, and gravitational acceleration is assumed constant.
  • Accelerometer bias is not estimated; a calibrated accelerometer is assumed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

attipy-0.0.7.tar.gz (65.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

attipy-0.0.7-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file attipy-0.0.7.tar.gz.

File metadata

  • Download URL: attipy-0.0.7.tar.gz
  • Upload date:
  • Size: 65.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for attipy-0.0.7.tar.gz
Algorithm Hash digest
SHA256 9fb9df0b19c4e271c926c163e0dda9a2c516504c6949a43cfcefbe61c551a6cf
MD5 712f2524075f10bb8dfa33a806c39e29
BLAKE2b-256 57318d260df21256af2290ebd40197ada7e9aa042580e302c074eb9fa029b501

See more details on using hashes here.

File details

Details for the file attipy-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: attipy-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for attipy-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a9cee768cde6dbd8efef3325dc9cdcee1963f35925123850ebc9a6d23c45a141
MD5 6920ffdf2388dfd691c38cd61f87a4a6
BLAKE2b-256 679eaf4fd5b94a2d2947137a63851486648c069ad07767a448c98c9d5014941d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page