10#ifndef M5_UNIT_IMU_MATH_QUATERNION_HPP
11#define M5_UNIT_IMU_MATH_QUATERNION_HPP
40 constexpr Quaternion(
const float w_,
const float x_,
const float y_,
const float z_) :
w(w_),
x(x_),
y(y_),
z(z_)
54 w * rhs.
y -
x * rhs.
z +
y * rhs.
w +
z * rhs.
x,
w * rhs.
z +
x * rhs.
y -
y * rhs.
x +
z * rhs.
w);
66 return w *
w +
x *
x +
y *
y +
z *
z;
71 return std::sqrt(
normSq());
79 const float n =
norm();
83 const float inv = 1.0f / n;
107 return v + t *
w + qv.
cross(t);
122 const float half = angle_rad * 0.5f;
123 const float s = std::sin(half);
124 return Quaternion(std::cos(half), a.
x * s, a.
y * s, a.
z * s);
137 const float roll = roll_deg * deg2rad;
138 const float pitch = pitch_deg * deg2rad;
139 const float yaw = yaw_deg * deg2rad;
141 const float cr = std::cos(roll * 0.5f);
142 const float sr = std::sin(roll * 0.5f);
143 const float cp = std::cos(pitch * 0.5f);
144 const float sp = std::sin(pitch * 0.5f);
145 const float cy = std::cos(yaw * 0.5f);
146 const float sy = std::sin(yaw * 0.5f);
148 return Quaternion(cr * cp * cy + sr * sp * sy, sr * cp * cy - cr * sp * sy, cr * sp * cy + sr * cp * sy,
149 cr * cp * sy - sr * sp * cy);
160 inline void toEuler(
float& roll_deg,
float& pitch_deg,
float& yaw_deg)
const
164 const float sinr_cosp = 2.0f * (
w *
x +
y *
z);
165 const float cosr_cosp = 1.0f - 2.0f * (
x *
x +
y *
y);
166 const float roll = std::atan2(sinr_cosp, cosr_cosp);
168 float sinp = 2.0f * (
w *
y -
z *
x);
171 }
else if (sinp < -1.0f) {
174 const float pitch = std::asin(sinp);
176 const float siny_cosp = 2.0f * (
w *
z +
x *
y);
177 const float cosy_cosp = 1.0f - 2.0f * (
y *
y +
z *
z);
178 const float yaw = std::atan2(siny_cosp, cosy_cosp);
180 roll_deg = roll * rad2deg;
181 pitch_deg = pitch * rad2deg;
182 yaw_deg = yaw * rad2deg;
194 const Quaternion omega(0.0f, gyro_rad_s.
x, gyro_rad_s.
y, gyro_rad_s.
z);
196 const float half_dt = 0.5f * dt;
197 w += half_dt * qdot.
w;
198 x += half_dt * qdot.
x;
199 y += half_dt * qdot.
y;
200 z += half_dt * qdot.
z;
Top level namespace of M5Stack.
Definition ahrs_controller.cpp:26
constexpr float QUATERNION_ZERO_EPSILON
Quaternions with a norm below this are treated as zero (normalize safety)
Definition quaternion.hpp:21
constexpr float QUATERNION_PI
PI (float)
Definition quaternion.hpp:23
Plain Hamilton quaternion (float) used to represent attitude.
Definition quaternion.hpp:31
float norm() const
Norm (magnitude)
Definition quaternion.hpp:69
static Quaternion fromEuler(const float roll_deg, const float pitch_deg, const float yaw_deg)
Build a rotation quaternion from aerospace ZYX Euler angles.
Definition quaternion.hpp:134
static Quaternion identity()
Identity quaternion (no rotation)
Definition quaternion.hpp:45
constexpr Quaternion(const float w_, const float x_, const float y_, const float z_)
Construct from components.
Definition quaternion.hpp:40
float x
Vector part X.
Definition quaternion.hpp:33
Quaternion operator*(const Quaternion &rhs) const
Hamilton product (this rotation applied after rhs)
Definition quaternion.hpp:51
float normSq() const
Squared norm.
Definition quaternion.hpp:64
float w
Scalar part.
Definition quaternion.hpp:32
Quaternion conjugated() const
Conjugate (equals the inverse for a unit quaternion)
Definition quaternion.hpp:58
float z
Vector part Z.
Definition quaternion.hpp:35
static Quaternion fromAxisAngle(const Vector3 &axis, const float angle_rad)
Build a rotation quaternion from an axis and angle.
Definition quaternion.hpp:116
bool normalize()
Normalize this quaternion in place.
Definition quaternion.hpp:77
Quaternion()=default
Default-construct as identity (no rotation)
Quaternion normalized() const
Return a normalized copy.
Definition quaternion.hpp:91
void toEuler(float &roll_deg, float &pitch_deg, float &yaw_deg) const
Decompose this quaternion into aerospace ZYX Euler angles.
Definition quaternion.hpp:160
Vector3 rotate(const Vector3 &v) const
Rotate a vector by this quaternion (active rotation, right-handed)
Definition quaternion.hpp:103
void integrate(const Vector3 &gyro_rad_s, const float dt)
Integrate body-frame angular velocity over dt (gyro kinematics)
Definition quaternion.hpp:192
float y
Vector part Y.
Definition quaternion.hpp:34
Plain 3D vector (float)
Definition vector3.hpp:25
Vector3 normalized() const
Return a normalized copy (zero vector maps to zero vector)
Definition vector3.hpp:83
Vector3 cross(const Vector3 &rhs) const
Cross product (right-handed)
Definition vector3.hpp:57
float z
Z component.
Definition vector3.hpp:28
float norm() const
Euclidean norm (magnitude)
Definition vector3.hpp:62
float x
X component.
Definition vector3.hpp:26
float y
Y component.
Definition vector3.hpp:27
Minimal 3D vector arithmetic (framework independent)