forked from barafael/mpu6050-dmp-rs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgravity.rs
More file actions
55 lines (52 loc) · 1.99 KB
/
gravity.rs
File metadata and controls
55 lines (52 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Gravity Vector Calculations
//!
//! The gravity vector represents the direction of gravitational acceleration
//! relative to the sensor's orientation. This is crucial for:
//! - Determining the sensor's orientation relative to Earth
//! - Separating gravity from linear acceleration
//! - Converting between quaternions and Euler angles
//!
//! The vector components indicate:
//! - x: Forward/backward tilt (pitch)
//! - y: Left/right tilt (roll)
//! - z: Vertical alignment (1.0 when level)
use crate::quaternion::Quaternion;
/// A 3D vector representing the direction of gravity relative to the sensor.
///
/// Properties:
/// - When the sensor is perfectly level:
/// * x = 0 (no forward/backward tilt)
/// * y = 0 (no left/right tilt)
/// * z = 1 (gravity points straight down)
/// - The vector magnitude should always be approximately 1g
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "postcard-experimental", derive(postcard::experimental::max_size::MaxSize))]
pub struct Gravity {
/// Forward/backward tilt component
pub x: f32,
/// Left/right tilt component
pub y: f32,
/// Vertical component
pub z: f32,
}
impl From<Quaternion> for Gravity {
/// Converts a quaternion to a gravity vector.
///
/// This uses the quaternion rotation matrix to transform the unit vector
/// [0, 0, 1] (representing gravity when level) to match the sensor's
/// current orientation.
///
/// The formulas used are derived from the quaternion rotation matrix:
/// - x = 2(qx*qz - qw*qy) [pitch component]
/// - y = 2(qw*qx + qy*qz) [roll component]
/// - z = qw² - qx² - qy² + qz² [vertical component]
fn from(q: Quaternion) -> Self {
Self {
x: 2.0 * (q.x * q.z - q.w * q.y),
y: 2.0 * (q.w * q.x + q.y * q.z),
z: q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z,
}
}
}