AHRS is a zoo of functions and objects written in Python helping you to estimate the orientation and position of robotic systems.
Orginally, an AHRS is defined as a set of orthogonal sensors providing attitude information about an aircraft. This field is now expanding to smaller devices, like wearables, automated transportation and all kinds of robots in motion.
The module AHRS is developed with a focus on fast prototyping and easy modularity.
AHRS is compatible with Python 3.6 and above.
AHRS may be installed using pip:
pip install ahrs
Or directly from the repository:
git clone https://github.com/Mayitzin/ahrs.git
cd ahrs
python setup.py install
AHRS depends on the most distributed packages of Python. If you don't have them, they will be automatically downloaded and installed.
To play with orientations, for example, we can use the orientation
module.
>>> import ahrs
>>> # Rotation matrix of 30.0 degrees around X-axis
... ahrs.common.orientation.rotation('x', 30.0)
array([[ 1. , 0. , 0. ],
[ 0. , 0.8660254, -0.5 ],
[ 0. , 0.5 , 0.8660254]])
>>> # Rotation sequence of the form: R_y(10.0)@R_x(20.0)@R_y(30.0)
... ahrs.common.orientation.rot_seq('yXy', [10.0, 20.0, 30.0])
array([[ 0.77128058, 0.05939117, 0.63371836],
[ 0.17101007, 0.93969262, -0.29619813],
[-0.61309202, 0.33682409, 0.71461018]])
It also works nicely with Quaternions.
>>> import numpy as np
>>> q = np.random.random(4)
>>> # It automatically normalizes any given vector
... ahrs.common.orientation.q2R(q)
array([[ 0.76811067, 0.3546719 , 0.53311709],
[ 0.55044928, 0.05960693, -0.83273802],
[-0.32712625, 0.93308888, -0.14944417]])
ahrs
also includes a module that simplifies data loading and visualization using matplotlib
.
>>> data = ahrs.utils.io.load("ExampleData.mat")
>>> ahrs.utils.plot_sensors(data.gyr)
It is possible to render more sensors with different subplots, and even titling them.
>>> ahrs.utils.plot_sensors(data.gyr, data.acc, data.mag,
x_axis=data.time, subtitles=["Gyroscopes", "Accelerometers", "Magnetometers"])
To use the sensor data to estimate the attitude, the filters
module includes various (more coming) algorithms for it.
>>> madgwick = ahrs.filters.Madgwick() # Madgwick's attitude estimation using default values
>>> Q = np.tile([1., 0., 0., 0.], (data.num_samples, 1)) # Allocate an array for all quaternions
>>> d2g = ahrs.common.DEG2RAD # Constant to convert degrees to radians
>>> for t in range(1, data.num_samples):
... Q[t] = madgwick.updateMARG(Q[t-1], d2g*data.gyr[t], data.acc[t], data.mag[t])
...
>>> ahrs.utils.plot_quaternions(Q)
Also works by simply passing the data to a desired filter, and it will automatically try to load the sensor information and estimate the quaternions with the given parameters.
>>> orientation = ahrs.filters.Madgwick(data, beta=0.1, frequency=100.0)
>>> orientation.Q.shape
(6959, 4)
A comprehensive documentation, with examples, will soon come to Read the Docs.