|
C Attitude Math Library
Quaternion, Euler, and DCM math utilities for attitude control.
|
#include "attitude/quaternion.h"#include "attitude/attitude_utils.h"#include <math.h>#include <stdio.h>Go to the source code of this file.
Functions | |
| void | axis_angle_rotate (const double axis[3], double angle, const double v_in[3], double v_out[3]) |
| Rotate a vector about an axis by a given angle. | |
| int | quaternion_inverse (const double q[4], double q_inv[4]) |
| Compute the inverse of a quaternion. | |
| void | quaternion_multiply (const double q1[4], const double q2[4], double q_out[4]) |
| Multiply two quaternions using Hamilton product. | |
| void | quaternion_normalize (double q[4]) |
| Normalise a quaternion in-place. | |
| int | quaternion_orientation_error_axis_angle (const double q_current[4], const double q_target[4], double axis[3], double *angle) |
| Compute current-to-target orientation error as an axis-angle command. | |
| int | quaternion_relative (const double q_current[4], const double q_target[4], double q_error[4]) |
| Compute the relative rotation from the current orientation to a target orientation. | |
| void | quaternion_rotate_vector (const double q[4], const double v_in[3], double v_out[3]) |
| Rotate a vector using a quaternion. | |
| void | quaternion_rotate_vector_explicit (const double q[4], const double v_in[3], double v_out[3]) |
| Rotate a vector using the explicit \(q v q^\* \) formulation. | |
| void | quaternion_set_explicit_debug (int enabled) |
| Enable verbose logging for the explicit quaternion rotation implementation. | |
| void | quaternion_slerp (const double q1[4], const double q2[4], double t, double q_out[4]) |
| Interpolate two quaternions using spherical linear interpolation (SLERP). | |
| int | quaternion_to_axis_angle (const double q[4], double axis[3], double *angle) |
| Convert a quaternion to axis-angle representation. | |
| void | quaternion_to_dcm (const double q[4], double dcm[3][3]) |
| Convert a quaternion to a direction cosine matrix (DCM). | |
| void | quaternion_to_euler (const double q[4], double *roll, double *pitch, double *yaw) |
| Convert a quaternion to intrinsic ZYX Euler angles. | |
| void axis_angle_rotate | ( | const double | axis[3], |
| double | angle, | ||
| const double | v_in[3], | ||
| double | v_out[3] | ||
| ) |
Rotate a vector about an axis by a given angle.
| axis | Unit rotation axis. |
| angle | Rotation angle in radians. |
| v_in | Input vector to rotate. |
| v_out | Output rotated vector. |
Definition at line 198 of file quaternion.c.
| int quaternion_inverse | ( | const double | q[4], |
| double | q_inv[4] | ||
| ) |
Compute the inverse of a quaternion.
The inverse is defined as \( q^{-1} = \frac{\bar{q}}{\|q\|^2} \), where \(\bar{q}\) is the conjugate.
| q | Input quaternion. |
| q_inv | Output inverse quaternion. |
Definition at line 85 of file quaternion.c.
| void quaternion_multiply | ( | const double | q1[4], |
| const double | q2[4], | ||
| double | q_out[4] | ||
| ) |
Multiply two quaternions using Hamilton product.
Performs \( q_{\text{out}} = q_1 \otimes q_2 \) which corresponds to applying q2 first, followed by q1.
| q1 | Left operand in \([w, x, y, z]\) order. |
| q2 | Right operand in \([w, x, y, z]\) order. |
| q_out | Output quaternion product. |
Definition at line 75 of file quaternion.c.
| void quaternion_normalize | ( | double | q[4] | ) |
Normalise a quaternion in-place.
If the quaternion norm is very small the vector is left unchanged.
| q | Quaternion to normalise. |
Definition at line 57 of file quaternion.c.
| int quaternion_orientation_error_axis_angle | ( | const double | q_current[4], |
| const double | q_target[4], | ||
| double | axis[3], | ||
| double * | angle | ||
| ) |
Compute current-to-target orientation error as an axis-angle command.
This is useful for controller/debug flows where a camera tracker or planner provides a target attitude and the control loop needs a rotation axis plus angle magnitude.
| q_current | Current orientation quaternion. |
| q_target | Desired target orientation quaternion. |
| axis | Output unit correction axis. |
| angle | Output correction angle in radians. |
Definition at line 117 of file quaternion.c.
| int quaternion_relative | ( | const double | q_current[4], |
| const double | q_target[4], | ||
| double | q_error[4] | ||
| ) |
Compute the relative rotation from the current orientation to a target orientation.
Produces \( q_{\text{error}} = q_{\text{target}} \otimes q_{\text{current}}^{-1} \). With this library's multiplication convention, applying q_error after q_current reconstructs q_target. The result is normalised and sign-adjusted so the scalar component is non-negative, giving the shortest equivalent rotation representation.
| q_current | Current orientation quaternion. |
| q_target | Desired target orientation quaternion. |
| q_error | Output relative rotation from current to target. |
q_current cannot be inverted. Definition at line 98 of file quaternion.c.
| void quaternion_rotate_vector | ( | const double | q[4], |
| const double | v_in[3], | ||
| double | v_out[3] | ||
| ) |
Rotate a vector using a quaternion.
Applies the efficient formulation described by \( v' = q v q^{-1} \) without explicitly forming the matrix.
| q | Rotation quaternion. |
| v_in | Input vector. |
| v_out | Output rotated vector. |
Definition at line 213 of file quaternion.c.
| void quaternion_rotate_vector_explicit | ( | const double | q[4], |
| const double | v_in[3], | ||
| double | v_out[3] | ||
| ) |
Rotate a vector using the explicit \(q v q^\* \) formulation.
Useful for educational tooling and verification of the optimised helper.
| q | Rotation quaternion. |
| v_in | Input vector. |
| v_out | Output rotated vector. |
Definition at line 251 of file quaternion.c.
| void quaternion_set_explicit_debug | ( | int | enabled | ) |
Enable verbose logging for the explicit quaternion rotation implementation.
When enabled, the helper emits intermediate products which is useful for tutorials and debugging.
| enabled | Non-zero value enables logging, zero disables it. |
Definition at line 326 of file quaternion.c.
| void quaternion_slerp | ( | const double | q1[4], |
| const double | q2[4], | ||
| double | t, | ||
| double | q_out[4] | ||
| ) |
Interpolate two quaternions using spherical linear interpolation (SLERP).
Interpolates along the shortest unit-quaternion arc. The interpolation parameter is clamped to \([0, 1]\).
| q1 | Start quaternion. |
| q2 | End quaternion. |
| t | Interpolation parameter in \([0, 1]\). |
| q_out | Resulting interpolated quaternion. |
Definition at line 146 of file quaternion.c.
| int quaternion_to_axis_angle | ( | const double | q[4], |
| double | axis[3], | ||
| double * | angle | ||
| ) |
Convert a quaternion to axis-angle representation.
Produces a unit rotation axis and right-handed rotation angle in radians.
| q | Input quaternion. |
| axis | Output unit axis vector. |
| angle | Output rotation magnitude (rad). |
Definition at line 129 of file quaternion.c.
| void quaternion_to_dcm | ( | const double | q[4], |
| double | dcm[3][3] | ||
| ) |
Convert a quaternion to a direction cosine matrix (DCM).
The resulting matrix rotates a vector from the body frame into the inertial frame: \( v_{\text{world}} = \mathbf{C}_{b}^{w} \, v_{\text{body}} \).
| q | Quaternion in \([w, x, y, z]\) order. |
| dcm | Output \(3\times3\) rotation matrix stored in row-major order. |
Definition at line 8 of file quaternion.c.
| void quaternion_to_euler | ( | const double | q[4], |
| double * | roll, | ||
| double * | pitch, | ||
| double * | yaw | ||
| ) |
Convert a quaternion to intrinsic ZYX Euler angles.
Euler angles follow the aerospace convention (yaw→pitch→roll). Returned angles are in radians.
| q | Quaternion in \([w, x, y, z]\) order. |
| roll | Output rotation about the \(x\) axis (rad). |
| pitch | Output rotation about the \(y\) axis (rad). |
| yaw | Output rotation about the \(z\) axis (rad). |
Definition at line 27 of file quaternion.c.