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.6.tar.gz (64.1 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.6-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: attipy-0.0.6.tar.gz
  • Upload date:
  • Size: 64.1 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.6.tar.gz
Algorithm Hash digest
SHA256 f62c087dba61e50d1aae77ff9a31ed2585a0436764d233460b9621d0e056af97
MD5 c22ca4523b3e4693233af700a6651898
BLAKE2b-256 017ab0dbedc5f5949431f12f2d2c30ae84a0251e52966ac8ba8bf35b01b21107

See more details on using hashes here.

File details

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

File metadata

  • Download URL: attipy-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 20.2 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9bbe49178d3c21f11a53cb2e3c659d700707bdc2292120e3ad9442be1336b655
MD5 9c052fa59ca1d0a373c6ba867dc70be7
BLAKE2b-256 ee079be961b38a769e6fd46c62e2209b6dc7707b9b5877bd7a44f446c9b39292

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