Class DCM#
- class ahrs.common.dcm.DCM(array: ndarray | None = None, **kwargs)#
Direction Cosine Matrix in SO(3)
Class to represent a Direction Cosine Matrix. It is built from a 3-by-3 array, but it can also be built from 3-dimensional vectors representing the roll-pitch-yaw angles, a quaternion, or an axis-angle pair representation.
- Parameters:
array (array-like, default: None) – Array to build the DCM with.
q (array-like, default: None) – Quaternion to convert to DCM.
rpy (array-like, default: None) – Array with roll->pitch->yaw angles.
euler (tuple, default: None) – Dictionary with a set of angles as a pair of string and array.
axang (tuple, default: None) – Tuple with an array and a float of the axis and the angle representation.
- Variables:
A (numpy.ndarray) – Array with the 3-by-3 direction cosine matrix.
Examples
All DCM are created as an identity matrix, which means no rotation.
>>> from ahrs import DCM >>> DCM() DCM([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
A rotation around a single axis can be defined by giving the desired axis and its value, in degrees.
>>> DCM(x=10.0) DCM([[ 1. , 0. , 0. ], [ 0. , 0.98480775, -0.17364818], [ 0. , 0.17364818, 0.98480775]]) >>> DCM(y=20.0) DCM([[ 0.93969262, 0. , 0.34202014], [ 0. , 1. , 0. ], [-0.34202014, 0. , 0.93969262]]) >>> DCM(z=30.0) DCM([[ 0.8660254, -0.5 , 0. ], [ 0.5 , 0.8660254, 0. ], [ 0. , 0. , 1. ]])
If we want a rotation conforming the roll-pitch-yaw sequence, we can give the corresponding angles.
>>> DCM(rpy=[30.0, 20.0, 10.0]) DCM([[ 0.81379768, -0.44096961, 0.37852231], [ 0.46984631, 0.88256412, 0.01802831], [-0.34202014, 0.16317591, 0.92541658]])
Note
Notice the angles are given in reverse order, as it is the way the matrices are multiplied.
>>> DCM(z=30.0) @ DCM(y=20.0) @ DCM(x=10.0) DCM([[ 0.81379768, -0.44096961, 0.37852231], [ 0.46984631, 0.88256412, 0.01802831], [-0.34202014, 0.16317591, 0.92541658]])
But also a different sequence can be defined, if given as a tuple with two elements: the order of the axis to rotate about, and the value of the rotation angles (again in reverse order)
>>> DCM(euler=('zyz', [40.0, 50.0, 60.0])) DCM([[-0.31046846, -0.74782807, 0.58682409], [ 0.8700019 , 0.02520139, 0.49240388], [-0.38302222, 0.66341395, 0.64278761]]) >>> DCM(z=40.0) @ DCM(y=50.0) @ DCM(z=60.0) DCM([[-0.31046846, -0.74782807, 0.58682409], [ 0.8700019 , 0.02520139, 0.49240388], [-0.38302222, 0.66341395, 0.64278761]])
Another option is to build the rotation matrix from a quaternion:
>>> DCM(q=[1., 2., 3., 4.]) DCM([[-0.66666667, 0.13333333, 0.73333333], [ 0.66666667, -0.33333333, 0.66666667], [ 0.33333333, 0.93333333, 0.13333333]])
The quaternions are automatically normalized to make them versors and be used as rotation operators.
Finally, we can also build the rotation matrix from an axis-angle representation:
>>> DCM(axang=([1., 2., 3.], 60.0)) DCM([[-0.81295491, 0.52330834, 0.25544608], [ 0.03452394, -0.3945807 , 0.91821249], [ 0.58130234, 0.75528436, 0.30270965]])
The axis of rotation is also normalized to be used as part of the rotation operator.
Attributes
Methods