pyIMU
Python implementation of quaternion/vector math for Attitude and Heading Reference Systems (AHRS), plus motion estimation from IMU data (accelerometer, gyroscope, optional magnetometer).
AHRS based on Madgwick filters.
Authors
| Author | Contribution | Years |
| Urs Utzinger | quaternion, vector, madgwick, motion | 2023 - 2026 |
| Gpt 5.3 | efficiency, cythonization and codereview, fusion | 2026 |
Coordinate Convention
pyIMU currently supports the pilot-style NED frame:
x: forward (North)y: right (East)z: down (Down)
Positive rotations follow the right-hand rule:
- roll: right wing down
- pitch: nose up
- yaw: nose right
Sensor inputs use an aerospace body frame:
x: forwardy: rightz: down
The sensor's accelerometer, gyroscope, and magnetometer need to be transformed so that all three use the body axes defined above. For example for a level, stationary, and NED-aligned body, the accelerometer input should be approximately [0, 0, +1] g
Units
Default units in filters:
- gyroscope:
rad/s(ordeg/sifgyr_in_dps=True) - accelerometer:
g(orm/s^2ifacc_in_g=False) - magnetometer: any consistent unit (direction is normalized internally)
Installation
Install in editable mode:
pip3 install -e .
Or standard install:
pip3 install .
Optional Cython Acceleration
Optional compiled kernel modules for hot functions:
pyIMU._qcorefor quaternionpyIMU._vcorefor vector3DpyIMU._mcorefor madgwickpyIMU._motion_corefor motionpyIMU._fcorefor imporoved madgwick
Build in place:
python3 setup.py build_ext --inplace
If C extensions are unavailable, pyIMU falls back to pure Python.
Quick Start
Madgwick
from pyIMU.madgwick import Madgwick
from pyIMU.quaternion import Vector3D
f = Madgwick(dt=1.0/150.0, gain=0.033, convention="NED")
q = f.update(
gyr=Vector3D(0.01, 0.02, 0.00),
acc=Vector3D(0.0, 0.0, 1.0),
mag=Vector3D(0.3, 0.0, 0.4),
dt=1.0 / 150.0,
)
Fusion (improved Madgwick)
from pyIMU.fusion import Fusion
from pyIMU.quaternion import Vector3D
f = Fusion(k_init=10.0, k_normal=0.5, convention="NED")
q = f.update(
gyr=Vector3D(0.01, 0.02, 0.00),
acc=Vector3D(0.0, 0.0, 1.0),
mag=Vector3D(0.3, 0.0, 0.4),
dt=0.01,
)
Motion
import time
from pyIMU.motion import Motion
from pyIMU.quaternion import Quaternion, Vector3D
m = Motion(latitude=32.253460, altitude=730, convention="NED")
q = Quaternion(1.0, 0.0, 0.0, 0.0)
acc = Vector3D(0.0, 0.0, 1.0)
moving = False
m.update(q=q, acc=acc, moving=moving, timestamp=time.time())
Calibration
import numpy as np
from pyIMU.quaternion import Vector3D
from pyIMU.calibration import InertialCalibration, MagnetometerCalibration
acc_cal = InertialCalibration(
offset=Vector3D(0.0, 0.0, 0.0),
sensitivity=Vector3D(1.0, 1.0, 1.0),
misalignment=np.eye(3),
)
mag_cal = MagnetometerCalibration(
hard_iron=Vector3D(0.0, 0.0, 0.0),
soft_iron=np.eye(3),
)
acc = acc_cal.apply(acc_raw)
mag = mag_cal.apply(mag_raw)
Modules
pyIMU.quaternion
Quaternionmath primitivesVector3Dmath primitives
pyIMU.madgwick
Madgwick gradient-descent AHRS implementation.
References:
pyIMU.fusion
Fusion-style AHRS inspired by Chapter 7 of Madgwick's thesis and x-io Fusion behavior:
- gain ramp initialization
- gyroscope bias compensation
- acceleration rejection/recovery
- magnetic rejection/recovery
- angular-rate recovery
References:
- Thesis chapter 7
- x-io website
pyIMU.motion
IMU motion integration (acceleration/velocity/position) with drift handling.
Note: drift is expected without external aiding.
pyIMU.calibration
InertialCalibration:
$$i_c=M_s(i_u-b)$$
misalignment @ (diag(sensitivity) @ (raw - offset))
MagnetometerCalibration:
$$m_c=S(m_u-h)$$
soft_iron @ (raw - hard_iron)
one-shot helpers:
calibrate_inertial, calibrate_magnetic
Defaults are identity calibration (offset=0, scale=1, identity matrices).
pyIMU.utilities
General helpers and conversion utilities (clip, clamp, q2rpy, rpy2q, accel2q (when still), accelmag2q (when still), gravity and heading helpers, RunningAverage).
Release Helper Script
Use scripts/release.sh to build and optionally install/upload/tag.
Examples:
- build only:
scripts/release.sh --clean - build + install wheel:
scripts/release.sh --clean --install - build + commit + tag:
scripts/release.sh --clean --version 1.0.1 --commit --tag - build + commit + tag + push:
scripts/release.sh --clean --version 1.0.1 --commit --tag --push - build + upload TestPyPI:
scripts/release.sh --clean --upload-testpypi - build + upload PyPI:
scripts/release.sh --clean --upload-pypi
Metadata
Release files for uutzinger-pyimu 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| uutzinger_pyimu-1.0.5.tar.gz | 470.1 kB | Without provenance | Details for uutzinger_pyimu-1.0.5.tar.gz |
Release files / uutzinger_pyimu-1.0.5.tar.gz
| Download URL | uutzinger_pyimu-1.0.5.tar.gz |
|---|---|
| Size | 470.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f163a52fe105c24132ac208e69a6f3b44eb038440549598058a8013ea2c4e01
|
|
BLAKE2b-256 checksum How to use checksums |
4ece8f2079db071766e2c65fe140bcf757472c0e0a4f38aa4ceb369792c07637
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for uutzinger-pyimu 1.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.