marg_orientation/
gyroscope_bias.rs

1use crate::impl_standard_traits;
2use core::fmt::{Debug, Formatter};
3use core::ops::{Mul, Sub};
4use uniform_array_derive::UniformArray;
5
6#[derive(UniformArray)]
7#[cfg_attr(test, ensure_uniform_type::ensure_uniform_type)]
8#[repr(C)]
9pub struct GyroscopeBias<T> {
10    /// The angular rate around the x-axis, in radians per second.
11    pub omega_x: T,
12    /// The angular rate around the y-axis, in radians per second.
13    pub omega_y: T,
14    /// The angular rate around the z-axis, in radians per second.
15    pub omega_z: T,
16}
17
18impl<T> GyroscopeBias<T> {
19    /// Initializes a new [`GyroscopeBias`] instance.
20    #[inline(always)]
21    pub const fn new(omega_x: T, omega_y: T, omega_z: T) -> Self {
22        Self {
23            omega_x,
24            omega_y,
25            omega_z,
26        }
27    }
28}
29
30impl<T> Default for GyroscopeBias<T>
31where
32    T: Default,
33{
34    #[inline]
35    fn default() -> Self {
36        Self::new(Default::default(), Default::default(), Default::default())
37    }
38}
39
40impl<T> Clone for GyroscopeBias<T>
41where
42    T: Clone,
43{
44    fn clone(&self) -> Self {
45        Self {
46            omega_x: self.omega_x.clone(),
47            omega_y: self.omega_y.clone(),
48            omega_z: self.omega_z.clone(),
49        }
50    }
51}
52
53impl<T> Debug for GyroscopeBias<T>
54where
55    T: Debug,
56{
57    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
58        f.debug_tuple("GyroscopeBias")
59            .field(&self.omega_x)
60            .field(&self.omega_y)
61            .field(&self.omega_z)
62            .finish()
63    }
64}
65
66impl<T> Mul<T> for GyroscopeBias<T>
67where
68    T: Mul<T, Output = T> + Clone,
69{
70    type Output = GyroscopeBias<T>;
71
72    fn mul(self, rhs: T) -> Self::Output {
73        Self {
74            omega_x: self.omega_x * rhs.clone(),
75            omega_y: self.omega_y * rhs.clone(),
76            omega_z: self.omega_z * rhs.clone(),
77        }
78    }
79}
80
81impl<T> Sub<T> for GyroscopeBias<T>
82where
83    T: Sub<T, Output = T> + Clone,
84{
85    type Output = GyroscopeBias<T>;
86
87    fn sub(self, rhs: T) -> Self::Output {
88        Self {
89            omega_x: self.omega_x - rhs.clone(),
90            omega_y: self.omega_y - rhs.clone(),
91            omega_z: self.omega_z - rhs.clone(),
92        }
93    }
94}
95
96impl_standard_traits!(GyroscopeBias, T);
97
98#[cfg(test)]
99mod test {
100    use super::*;
101
102    #[test]
103    fn test_len() {
104        let reading = GyroscopeBias::<f32>::default();
105        assert_eq!(reading.len(), 3);
106    }
107
108    #[test]
109    fn test_index() {
110        let reading = GyroscopeBias::<f32> {
111            omega_x: 1.0,
112            omega_y: 2.0,
113            omega_z: 3.0,
114        };
115
116        assert_eq!(reading[0], 1.0);
117        assert_eq!(reading[1], 2.0);
118        assert_eq!(reading[2], 3.0);
119    }
120}