ublox 0.10.0

A crate to communicate with u-blox GPS devices using the UBX protocol
Documentation
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#![cfg(any(
    feature = "ubx_proto23",
    feature = "ubx_proto27",
    feature = "ubx_proto31",
    feature = "ubx_proto33",
))]
#[allow(unused_imports, reason = "It is only unused in some feature sets")]
use crate::FieldIter;
use bitflags::bitflags;
use core::fmt;
#[cfg(feature = "serde")]
use {super::SerializeUbxPacketFields, crate::serde::ser::SerializeMap};

use crate::{error::ParserError, UbxPacketMeta};
use ublox_derive::{ubx_extend, ubx_extend_bitflags, ubx_packet_recv};

#[ubx_packet_recv]
#[ubx(class = 0x10, id = 0x10, max_payload_len = 1240)]
struct EsfStatus {
    itow: u32,
    /// Version is 2 for M8L spec
    version: u8,

    #[ubx(map_type = EsfInitStatus1, from = EsfInitStatus1)]
    init_status1: u8,

    #[ubx(map_type = EsfInitStatus2, from = EsfInitStatus2)]
    init_status2: u8,

    reserved1: [u8; 5],

    #[ubx(map_type = EsfStatusFusionMode)]
    fusion_mode: u8,

    reserved2: [u8; 2],

    num_sens: u8,

    #[ubx(
        map_type = EsfSensorStatusIter,
        from = EsfSensorStatusIter::new,
        is_valid = EsfSensorStatusIter::is_valid,
        may_fail,
    )]
    data: [u8; 0],
}

#[ubx_extend]
#[ubx(from, rest_reserved)]
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum EsfStatusFusionMode {
    Initializing = 0,
    Fusion = 1,
    Suspended = 2,
    Disabled = 3,
}

#[repr(transparent)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct EsfInitStatus1(u8);

impl EsfInitStatus1 {
    const WHEEL_TICK_MASK: u8 = 0x03;
    const MOUNTING_ANGLE_STATUS_MASK: u8 = 0x07;
    const INS_STATUS_MASK: u8 = 0x03;

    pub fn wheel_tick_init_status(self) -> EsfStatusWheelTickInit {
        let bits = (self.0) & Self::WHEEL_TICK_MASK;
        match bits {
            0 => EsfStatusWheelTickInit::Off,
            1 => EsfStatusWheelTickInit::Initializing,
            2 => EsfStatusWheelTickInit::Initialized,
            _ => EsfStatusWheelTickInit::Invalid,
        }
    }

    pub fn wheel_tick_init_status_raw(self) -> u8 {
        self.wheel_tick_init_status() as u8
    }

    pub fn mounting_angle_status(self) -> EsfStatusMountAngle {
        let bits = (self.0 >> 2) & Self::MOUNTING_ANGLE_STATUS_MASK;
        match bits {
            0 => EsfStatusMountAngle::Off,
            1 => EsfStatusMountAngle::Initializing,
            2 | 3 => EsfStatusMountAngle::Initialized,
            _ => EsfStatusMountAngle::Invalid,
        }
    }

    pub fn mount_angle_status_raw(self) -> u8 {
        self.mounting_angle_status() as u8
    }

    pub fn ins_initialization_status(self) -> EsfStatusInsInit {
        let bits = (self.0 >> 5) & Self::INS_STATUS_MASK;
        match bits {
            0 => EsfStatusInsInit::Off,
            1 => EsfStatusInsInit::Initializing,
            2 => EsfStatusInsInit::Initialized,
            _ => EsfStatusInsInit::Invalid,
        }
    }

    pub fn ins_initialization_status_raw(self) -> u8 {
        self.ins_initialization_status() as u8
    }
}

impl fmt::Debug for EsfInitStatus1 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("initStatus1")
            .field("wtInitStatus", &self.wheel_tick_init_status())
            .field("mntAlgStatus", &self.mounting_angle_status())
            .field("insInitStatus", &self.ins_initialization_status())
            .finish()
    }
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EsfStatusWheelTickInit {
    Off = 0,
    Initializing = 1,
    Initialized = 2,
    /// Only two bits are reserved for the init status
    Invalid = 3,
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EsfStatusMountAngle {
    Off = 0,
    Initializing = 1,
    Initialized = 2,
    /// Only two bits are reserved for the init status
    Invalid = 3,
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EsfStatusInsInit {
    Off = 0,
    Initializing = 1,
    Initialized = 2,
    /// Only two bits are reserved for the init status
    Invalid = 3,
}

#[repr(transparent)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct EsfInitStatus2(u8);

impl EsfInitStatus2 {
    pub fn imu_init_status_raw(self) -> u8 {
        self.imu_init_status() as u8
    }

    pub fn imu_init_status(self) -> EsfStatusImuInit {
        let bits = (self.0) & 0x02;
        match bits {
            0 => EsfStatusImuInit::Off,
            1 => EsfStatusImuInit::Initializing,
            2 => EsfStatusImuInit::Initialized,
            _ => EsfStatusImuInit::Invalid,
        }
    }
}

impl fmt::Debug for EsfInitStatus2 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("initStatus2")
            .field("imuInitStatus", &self.imu_init_status())
            .finish()
    }
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EsfStatusImuInit {
    Off = 0,
    Initializing = 1,
    Initialized = 2,
    /// Only two bits are reserved for the init status
    Invalid = 3,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct EsfSensorStatus {
    sens_status1: SensorStatus1,
    sens_status2: SensorStatus2,
    freq: u16,
    faults: EsfSensorFaults,
}

impl EsfSensorStatus {
    pub fn freq(&self) -> u16 {
        self.freq
    }

    pub fn faults(&self) -> EsfSensorFaults {
        self.faults
    }

    pub fn faults_raw(&self) -> u8 {
        self.faults().into_raw()
    }

    pub fn sensor_type(&self) -> EsfSensorType {
        self.sens_status1.sensor_type
    }

    pub fn sensor_type_raw(&self) -> u8 {
        self.sensor_type() as u8
    }

    pub fn sensor_used(&self) -> bool {
        self.sens_status1.used
    }

    pub fn sensor_ready(&self) -> bool {
        self.sens_status1.ready
    }

    pub fn calibration_status(&self) -> EsfSensorStatusCalibration {
        self.sens_status2.calibration_status
    }

    pub fn calibration_status_raw(&self) -> u8 {
        self.calibration_status() as u8
    }

    pub fn time_status(&self) -> EsfSensorStatusTime {
        self.sens_status2.time_status
    }

    pub fn time_status_raw(&self) -> u8 {
        self.time_status() as u8
    }
}

#[derive(Clone, Debug)]
pub struct EsfSensorStatusIter<'a>(core::slice::ChunksExact<'a, u8>);

impl<'a> EsfSensorStatusIter<'a> {
    const BLOCK_SIZE: usize = 4;
    fn new(bytes: &'a [u8]) -> Self {
        Self(bytes.chunks_exact(Self::BLOCK_SIZE))
    }

    fn is_valid(bytes: &[u8]) -> bool {
        bytes.len().is_multiple_of(Self::BLOCK_SIZE)
    }
}

impl core::iter::Iterator for EsfSensorStatusIter<'_> {
    type Item = EsfSensorStatus;

    fn next(&mut self) -> Option<Self::Item> {
        let chunk = self.0.next()?;
        let data = u32::from_le_bytes(chunk[0..Self::BLOCK_SIZE].try_into().unwrap());
        Some(EsfSensorStatus {
            sens_status1: ((data & 0xFF) as u8).into(),
            sens_status2: (((data >> 8) & 0xFF) as u8).into(),
            freq: ((data >> 16) & 0xFF).try_into().unwrap(),
            faults: (((data >> 24) & 0xFF) as u8).into(),
        })
    }
}

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SensorStatus1 {
    sensor_type: EsfSensorType,
    used: bool,
    ready: bool,
}

impl From<u8> for SensorStatus1 {
    fn from(s: u8) -> Self {
        let sensor_type: EsfSensorType = (s & 0x3F).into();
        Self {
            sensor_type,
            used: (s >> 6) != 0,
            ready: (s >> 7) != 0,
        }
    }
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum EsfSensorType {
    None = 0,
    /// Angular acceleration in [deg/s]
    GyroZ = 5,
    /// Unitless (counter)
    FrontLeftWheelTicks = 6,
    /// Unitless (counter)
    FrontRightWheelTicks = 7,
    /// Unitless (counter)
    RearLeftWheelTicks = 8,
    /// Unitless (counter)
    RearRightWheelTicks = 9,
    /// Unitless (counter)
    SpeedTick = 10,
    /// Speed in [m/s]
    Speed = 11,
    /// Temperature Celsius \[deg\]
    GyroTemp = 12,
    /// Angular acceleration in [deg/s]
    GyroY = 13,
    /// Angular acceleration in [deg/s]
    GyroX = 14,
    /// Specific force in [m/s^2]
    AccX = 16,
    /// Specific force in [m/s^2]
    AccY = 17,
    /// Specific force in [m/s^2]
    AccZ = 18,
    Invalid = 19,
}

impl From<u8> for EsfSensorType {
    fn from(orig: u8) -> Self {
        match orig {
            0 => EsfSensorType::None,
            5 => EsfSensorType::GyroZ,
            6 => EsfSensorType::FrontLeftWheelTicks,
            7 => EsfSensorType::FrontRightWheelTicks,
            8 => EsfSensorType::RearLeftWheelTicks,
            9 => EsfSensorType::RearRightWheelTicks,
            10 => EsfSensorType::SpeedTick,
            11 => EsfSensorType::Speed,
            12 => EsfSensorType::GyroTemp,
            13 => EsfSensorType::GyroY,
            14 => EsfSensorType::GyroX,
            16 => EsfSensorType::AccX,
            17 => EsfSensorType::AccY,
            18 => EsfSensorType::AccZ,
            _ => EsfSensorType::Invalid,
        }
    }
}

#[allow(dead_code)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SensorStatus2 {
    pub(crate) calibration_status: EsfSensorStatusCalibration,
    pub(crate) time_status: EsfSensorStatusTime,
}

impl From<u8> for SensorStatus2 {
    fn from(s: u8) -> Self {
        let calibration_status: EsfSensorStatusCalibration = (s & 0x03).into();
        let time_status: EsfSensorStatusTime = ((s >> 2) & 0x03).into();
        Self {
            calibration_status,
            time_status,
        }
    }
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum EsfSensorStatusCalibration {
    NotCalibrated = 0,
    Calibrating = 1,
    Calibrated = 2,
    Invalid = 4,
}

impl From<u8> for EsfSensorStatusCalibration {
    fn from(orig: u8) -> Self {
        match orig {
            0 => EsfSensorStatusCalibration::NotCalibrated,
            1 => EsfSensorStatusCalibration::Calibrating,
            2 | 3 => EsfSensorStatusCalibration::Calibrated,
            _ => EsfSensorStatusCalibration::Invalid,
        }
    }
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum EsfSensorStatusTime {
    NoData = 0,
    OnReceptionFirstByte = 1,
    OnEventInput = 2,
    TimeTagFromData = 3,
    Invalid = 4,
}

impl From<u8> for EsfSensorStatusTime {
    fn from(orig: u8) -> Self {
        match orig {
            0 => EsfSensorStatusTime::NoData,
            1 => EsfSensorStatusTime::OnReceptionFirstByte,
            2 => EsfSensorStatusTime::OnEventInput,
            3 => EsfSensorStatusTime::TimeTagFromData,
            _ => EsfSensorStatusTime::Invalid,
        }
    }
}

#[ubx_extend_bitflags]
#[ubx(from, into_raw, rest_reserved)]
bitflags! {
#[derive(Debug, Default, Clone, Copy)]
pub struct EsfSensorFaults: u8 {
    const BAD_MEASUREMENT = 1;
    const BAD_TIME_TAG = 2;
    const MISSING_MEASUREMENT = 4;
    const NOISY_MEASUREMENT = 8;
}
}

impl From<u8> for EsfSensorFaults {
    fn from(s: u8) -> Self {
        Self::from_bits(s).unwrap_or(EsfSensorFaults::empty())
    }
}