sbp 6.4.0

Rust native implementation of SBP (Swift Binary Protocol) for communicating with devices made by Swift Navigation
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
// Copyright (C) 2015-2021 Swift Navigation Inc.
// Contact: https://support.swiftnav.com
//
// This source is subject to the license found in the file 'LICENSE' which must
// be distributed together with this source. All other rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

//****************************************************************************
// Automatically generated from yaml/swiftnav/sbp/imu.yaml
// with generate.py. Please do not hand edit!
//****************************************************************************/
//! Inertial Measurement Unit (IMU) messages.
pub use msg_imu_aux::MsgImuAux;
pub use msg_imu_comp::MsgImuComp;
pub use msg_imu_raw::MsgImuRaw;

pub mod msg_imu_aux {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Auxiliary IMU data
    ///
    /// Auxiliary data specific to a particular IMU. The `imu_type` field will
    /// always be consistent but the rest of the payload is device specific and
    /// depends on the value of `imu_type`.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[allow(clippy::derive_partial_eq_without_eq)]
    #[derive(Debug, PartialEq, Clone)]
    pub struct MsgImuAux {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing, alias = "sender"))]
        pub sender_id: Option<u16>,
        /// IMU type
        #[cfg_attr(feature = "serde", serde(rename = "imu_type"))]
        pub imu_type: u8,
        /// Raw IMU temperature
        #[cfg_attr(feature = "serde", serde(rename = "temp"))]
        pub temp: i16,
        /// IMU configuration
        #[cfg_attr(feature = "serde", serde(rename = "imu_conf"))]
        pub imu_conf: u8,
    }

    impl MsgImuAux {
        /// Gets the [ImuType][self::ImuType] stored in the `imu_type` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `ImuType` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `ImuType` were added.
        pub fn imu_type(&self) -> Result<ImuType, u8> {
            get_bit_range!(self.imu_type, u8, u8, 7, 0).try_into()
        }

        /// Set the bitrange corresponding to the [ImuType][ImuType] of the `imu_type` bitfield.
        pub fn set_imu_type(&mut self, imu_type: ImuType) {
            set_bit_range!(&mut self.imu_type, imu_type, u8, u8, 7, 0);
        }

        /// Gets the [GyroscopeRange][self::GyroscopeRange] stored in the `imu_conf` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `GyroscopeRange` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `GyroscopeRange` were added.
        pub fn gyroscope_range(&self) -> Result<GyroscopeRange, u8> {
            get_bit_range!(self.imu_conf, u8, u8, 7, 4).try_into()
        }

        /// Set the bitrange corresponding to the [GyroscopeRange][GyroscopeRange] of the `imu_conf` bitfield.
        pub fn set_gyroscope_range(&mut self, gyroscope_range: GyroscopeRange) {
            set_bit_range!(&mut self.imu_conf, gyroscope_range, u8, u8, 7, 4);
        }

        /// Gets the [AccelerometerRange][self::AccelerometerRange] stored in the `imu_conf` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `AccelerometerRange` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `AccelerometerRange` were added.
        pub fn accelerometer_range(&self) -> Result<AccelerometerRange, u8> {
            get_bit_range!(self.imu_conf, u8, u8, 3, 0).try_into()
        }

        /// Set the bitrange corresponding to the [AccelerometerRange][AccelerometerRange] of the `imu_conf` bitfield.
        pub fn set_accelerometer_range(&mut self, accelerometer_range: AccelerometerRange) {
            set_bit_range!(&mut self.imu_conf, accelerometer_range, u8, u8, 3, 0);
        }
    }

    impl ConcreteMessage for MsgImuAux {
        const MESSAGE_TYPE: u16 = 2305;
        const MESSAGE_NAME: &'static str = "MSG_IMU_AUX";
    }

    impl SbpMessage for MsgImuAux {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> Option<u16> {
            Some(<Self as ConcreteMessage>::MESSAGE_TYPE)
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
        fn is_valid(&self) -> bool {
            true
        }
        fn into_valid_msg(self) -> Result<Self, crate::messages::invalid::Invalid> {
            Ok(self)
        }
    }

    impl FriendlyName for MsgImuAux {
        fn friendly_name() -> &'static str {
            "IMU AUX"
        }
    }

    impl TryFrom<Sbp> for MsgImuAux {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgImuAux(m) => Ok(m),
                _ => Err(TryFromSbpError(msg)),
            }
        }
    }

    impl WireFormat for MsgImuAux {
        const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN
            + <i16 as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.imu_type)
                + WireFormat::len(&self.temp)
                + WireFormat::len(&self.imu_conf)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.imu_type, buf);
            WireFormat::write(&self.temp, buf);
            WireFormat::write(&self.imu_conf, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgImuAux {
                sender_id: None,
                imu_type: WireFormat::parse_unchecked(buf),
                temp: WireFormat::parse_unchecked(buf),
                imu_conf: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// IMU Type
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum ImuType {
        /// Bosch BMI160
        BoschBmi160 = 0,

        /// ST Microelectronics ASM330LLH
        StMicroelectronicsAsm330Llh = 1,

        /// TDK ICM-42670
        TdkIcm42670 = 3,

        /// Murata SCHA634-D03
        MurataScha634D03 = 4,

        /// TDK IAM-20680HP
        TdkIam20680Hp = 5,
    }

    impl std::fmt::Display for ImuType {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                ImuType::BoschBmi160 => f.write_str("Bosch BMI160"),
                ImuType::StMicroelectronicsAsm330Llh => {
                    f.write_str("ST Microelectronics ASM330LLH")
                }
                ImuType::TdkIcm42670 => f.write_str("TDK ICM-42670"),
                ImuType::MurataScha634D03 => f.write_str("Murata SCHA634-D03"),
                ImuType::TdkIam20680Hp => f.write_str("TDK IAM-20680HP"),
            }
        }
    }

    impl TryFrom<u8> for ImuType {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, u8> {
            match i {
                0 => Ok(ImuType::BoschBmi160),
                1 => Ok(ImuType::StMicroelectronicsAsm330Llh),
                3 => Ok(ImuType::TdkIcm42670),
                4 => Ok(ImuType::MurataScha634D03),
                5 => Ok(ImuType::TdkIam20680Hp),
                i => Err(i),
            }
        }
    }

    /// Gyroscope Range
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum GyroscopeRange {
        /// +/- 2000 deg / s
        _2000DegS = 0,

        /// +/- 1000 deg / s
        _1000DegS = 1,

        /// +/- 500 deg / s
        _500DegS = 2,

        /// +/- 250 deg / s
        _250DegS = 3,

        /// +/- 125 deg / s
        _125DegS = 4,

        /// +/- 300 deg / s
        _300DegS = 5,
    }

    impl std::fmt::Display for GyroscopeRange {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                GyroscopeRange::_2000DegS => f.write_str("+/- 2000 deg / s"),
                GyroscopeRange::_1000DegS => f.write_str("+/- 1000 deg / s"),
                GyroscopeRange::_500DegS => f.write_str("+/- 500 deg / s"),
                GyroscopeRange::_250DegS => f.write_str("+/- 250 deg / s"),
                GyroscopeRange::_125DegS => f.write_str("+/- 125 deg / s"),
                GyroscopeRange::_300DegS => f.write_str("+/- 300 deg / s"),
            }
        }
    }

    impl TryFrom<u8> for GyroscopeRange {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, u8> {
            match i {
                0 => Ok(GyroscopeRange::_2000DegS),
                1 => Ok(GyroscopeRange::_1000DegS),
                2 => Ok(GyroscopeRange::_500DegS),
                3 => Ok(GyroscopeRange::_250DegS),
                4 => Ok(GyroscopeRange::_125DegS),
                5 => Ok(GyroscopeRange::_300DegS),
                i => Err(i),
            }
        }
    }

    /// Accelerometer Range
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum AccelerometerRange {
        /// +/- 2g
        _2G = 0,

        /// +/- 4g
        _4G = 1,

        /// +/- 8g
        _8G = 2,

        /// +/- 16g
        _16G = 3,

        /// +/- 6g
        _6G = 4,
    }

    impl std::fmt::Display for AccelerometerRange {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                AccelerometerRange::_2G => f.write_str("+/- 2g"),
                AccelerometerRange::_4G => f.write_str("+/- 4g"),
                AccelerometerRange::_8G => f.write_str("+/- 8g"),
                AccelerometerRange::_16G => f.write_str("+/- 16g"),
                AccelerometerRange::_6G => f.write_str("+/- 6g"),
            }
        }
    }

    impl TryFrom<u8> for AccelerometerRange {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, u8> {
            match i {
                0 => Ok(AccelerometerRange::_2G),
                1 => Ok(AccelerometerRange::_4G),
                2 => Ok(AccelerometerRange::_8G),
                3 => Ok(AccelerometerRange::_16G),
                4 => Ok(AccelerometerRange::_6G),
                i => Err(i),
            }
        }
    }
}

pub mod msg_imu_comp {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Compensated IMU data
    ///
    /// Data from the Inertial Measurement Unit, containing accelerometer and
    /// gyroscope readings compensated for estimated errors and constant  physical
    /// effects. The output is valid for inertially referenced center  of
    /// navigation (IMU body frame) represented in vehicle body frame.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[allow(clippy::derive_partial_eq_without_eq)]
    #[derive(Debug, PartialEq, Clone)]
    pub struct MsgImuComp {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing, alias = "sender"))]
        pub sender_id: Option<u16>,
        /// Microseconds since reference epoch
        #[cfg_attr(feature = "serde", serde(rename = "time"))]
        pub time: u64,
        /// Contains the applied compensation parameters and time synchronization
        /// mode
        #[cfg_attr(feature = "serde", serde(rename = "flags"))]
        pub flags: u16,
        /// Compensated acceleration X axis
        #[cfg_attr(feature = "serde", serde(rename = "acc_comp_x"))]
        pub acc_comp_x: i32,
        /// Compensated acceleration Y axis
        #[cfg_attr(feature = "serde", serde(rename = "acc_comp_y"))]
        pub acc_comp_y: i32,
        /// Compensated acceleration Z axis
        #[cfg_attr(feature = "serde", serde(rename = "acc_comp_z"))]
        pub acc_comp_z: i32,
        /// Compensated angular rate X axis
        #[cfg_attr(feature = "serde", serde(rename = "gyr_comp_x"))]
        pub gyr_comp_x: i32,
        /// Compensated angular rate Y axis
        #[cfg_attr(feature = "serde", serde(rename = "gyr_comp_y"))]
        pub gyr_comp_y: i32,
        /// Compensated angular rate Z axis
        #[cfg_attr(feature = "serde", serde(rename = "gyr_comp_z"))]
        pub gyr_comp_z: i32,
    }

    impl MsgImuComp {
        /// Gets the `gyroscopeerrorscompensated` flag.
        #[allow(clippy::identity_op)]
        pub fn gyroscopeerrorscompensated(&self) -> bool {
            ((self.flags >> 6) & 1) == 1
        }

        /// Sets the `gyroscopeerrorscompensated` flag.
        pub fn set_gyroscopeerrorscompensated(&mut self, gyroscopeerrorscompensated: bool) {
            self.flags ^= (!(gyroscopeerrorscompensated as u16)) & (1 << 6)
        }

        /// Gets the `accelerometererrorscompensated` flag.
        #[allow(clippy::identity_op)]
        pub fn accelerometererrorscompensated(&self) -> bool {
            ((self.flags >> 5) & 1) == 1
        }

        /// Sets the `accelerometererrorscompensated` flag.
        pub fn set_accelerometererrorscompensated(&mut self, accelerometererrorscompensated: bool) {
            self.flags ^= (!(accelerometererrorscompensated as u16)) & (1 << 5)
        }

        /// Gets the `corioliseffectcompensated` flag.
        #[allow(clippy::identity_op)]
        pub fn corioliseffectcompensated(&self) -> bool {
            ((self.flags >> 4) & 1) == 1
        }

        /// Sets the `corioliseffectcompensated` flag.
        pub fn set_corioliseffectcompensated(&mut self, corioliseffectcompensated: bool) {
            self.flags ^= (!(corioliseffectcompensated as u16)) & (1 << 4)
        }

        /// Gets the `earthrorationratecompensated` flag.
        #[allow(clippy::identity_op)]
        pub fn earthrorationratecompensated(&self) -> bool {
            ((self.flags >> 3) & 1) == 1
        }

        /// Sets the `earthrorationratecompensated` flag.
        pub fn set_earthrorationratecompensated(&mut self, earthrorationratecompensated: bool) {
            self.flags ^= (!(earthrorationratecompensated as u16)) & (1 << 3)
        }

        /// Gets the `earthgravitycompensated` flag.
        #[allow(clippy::identity_op)]
        pub fn earthgravitycompensated(&self) -> bool {
            ((self.flags >> 2) & 1) == 1
        }

        /// Sets the `earthgravitycompensated` flag.
        pub fn set_earthgravitycompensated(&mut self, earthgravitycompensated: bool) {
            self.flags ^= (!(earthgravitycompensated as u16)) & (1 << 2)
        }

        /// Gets the [TimeStatus][self::TimeStatus] stored in the `flags` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `TimeStatus` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `TimeStatus` were added.
        pub fn time_status(&self) -> Result<TimeStatus, u8> {
            get_bit_range!(self.flags, u16, u8, 1, 0).try_into()
        }

        /// Set the bitrange corresponding to the [TimeStatus][TimeStatus] of the `flags` bitfield.
        pub fn set_time_status(&mut self, time_status: TimeStatus) {
            set_bit_range!(&mut self.flags, time_status, u16, u8, 1, 0);
        }
    }

    impl ConcreteMessage for MsgImuComp {
        const MESSAGE_TYPE: u16 = 2309;
        const MESSAGE_NAME: &'static str = "MSG_IMU_COMP";
    }

    impl SbpMessage for MsgImuComp {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> Option<u16> {
            Some(<Self as ConcreteMessage>::MESSAGE_TYPE)
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
        fn is_valid(&self) -> bool {
            true
        }
        fn into_valid_msg(self) -> Result<Self, crate::messages::invalid::Invalid> {
            Ok(self)
        }
    }

    impl FriendlyName for MsgImuComp {
        fn friendly_name() -> &'static str {
            "IMU COMP"
        }
    }

    impl TryFrom<Sbp> for MsgImuComp {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgImuComp(m) => Ok(m),
                _ => Err(TryFromSbpError(msg)),
            }
        }
    }

    impl WireFormat for MsgImuComp {
        const MIN_LEN: usize = <u64 as WireFormat>::MIN_LEN
            + <u16 as WireFormat>::MIN_LEN
            + <i32 as WireFormat>::MIN_LEN
            + <i32 as WireFormat>::MIN_LEN
            + <i32 as WireFormat>::MIN_LEN
            + <i32 as WireFormat>::MIN_LEN
            + <i32 as WireFormat>::MIN_LEN
            + <i32 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.time)
                + WireFormat::len(&self.flags)
                + WireFormat::len(&self.acc_comp_x)
                + WireFormat::len(&self.acc_comp_y)
                + WireFormat::len(&self.acc_comp_z)
                + WireFormat::len(&self.gyr_comp_x)
                + WireFormat::len(&self.gyr_comp_y)
                + WireFormat::len(&self.gyr_comp_z)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.time, buf);
            WireFormat::write(&self.flags, buf);
            WireFormat::write(&self.acc_comp_x, buf);
            WireFormat::write(&self.acc_comp_y, buf);
            WireFormat::write(&self.acc_comp_z, buf);
            WireFormat::write(&self.gyr_comp_x, buf);
            WireFormat::write(&self.gyr_comp_y, buf);
            WireFormat::write(&self.gyr_comp_z, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgImuComp {
                sender_id: None,
                time: WireFormat::parse_unchecked(buf),
                flags: WireFormat::parse_unchecked(buf),
                acc_comp_x: WireFormat::parse_unchecked(buf),
                acc_comp_y: WireFormat::parse_unchecked(buf),
                acc_comp_z: WireFormat::parse_unchecked(buf),
                gyr_comp_x: WireFormat::parse_unchecked(buf),
                gyr_comp_y: WireFormat::parse_unchecked(buf),
                gyr_comp_z: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Time status
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum TimeStatus {
        /// Reference epoch is start of current GPS week
        ReferenceEpochIsStartOfCurrentGpsWeek = 0,

        /// Reference epoch is time of system startup
        ReferenceEpochIsTimeOfSystemStartup = 1,

        /// Reference epoch is unknown
        ReferenceEpochIsUnknown = 2,

        /// Reference epoch is last PPS
        ReferenceEpochIsLastPps = 3,
    }

    impl std::fmt::Display for TimeStatus {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                TimeStatus::ReferenceEpochIsStartOfCurrentGpsWeek => {
                    f.write_str("Reference epoch is start of current GPS week")
                }
                TimeStatus::ReferenceEpochIsTimeOfSystemStartup => {
                    f.write_str("Reference epoch is time of system startup")
                }
                TimeStatus::ReferenceEpochIsUnknown => f.write_str("Reference epoch is unknown"),
                TimeStatus::ReferenceEpochIsLastPps => f.write_str("Reference epoch is last PPS"),
            }
        }
    }

    impl TryFrom<u8> for TimeStatus {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, u8> {
            match i {
                0 => Ok(TimeStatus::ReferenceEpochIsStartOfCurrentGpsWeek),
                1 => Ok(TimeStatus::ReferenceEpochIsTimeOfSystemStartup),
                2 => Ok(TimeStatus::ReferenceEpochIsUnknown),
                3 => Ok(TimeStatus::ReferenceEpochIsLastPps),
                i => Err(i),
            }
        }
    }
}

pub mod msg_imu_raw {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Raw IMU data
    ///
    /// Raw data from the Inertial Measurement Unit, containing accelerometer and
    /// gyroscope readings. The sense of the measurements are to be aligned with
    /// the indications on the device itself. Measurement units, which are
    /// specific to the device hardware and settings, are communicated via the
    /// MSG_IMU_AUX message. If using "time since startup" local time tags, the
    /// receiving end will expect either a MSG_GNSS_TIME_OFFSET or MSG_PPS_TIME to
    /// establish the relationship between IMU time and GNSS time.
    /// Regardless of the timestamping mode, the timestamp is required to roll
    /// over to zero when reaching one week (604800 seconds, or 604800000
    /// milliseconds).
    /// The time-tagging mode should not change throughout a run.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    #[allow(clippy::derive_partial_eq_without_eq)]
    #[derive(Debug, PartialEq, Clone)]
    pub struct MsgImuRaw {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing, alias = "sender"))]
        pub sender_id: Option<u16>,
        /// Milliseconds since reference epoch and time status.
        #[cfg_attr(feature = "serde", serde(rename = "tow"))]
        pub tow: u32,
        /// Milliseconds since reference epoch, fractional part
        #[cfg_attr(feature = "serde", serde(rename = "tow_f"))]
        pub tow_f: u8,
        /// Acceleration in the IMU frame X axis
        #[cfg_attr(feature = "serde", serde(rename = "acc_x"))]
        pub acc_x: i16,
        /// Acceleration in the IMU frame Y axis
        #[cfg_attr(feature = "serde", serde(rename = "acc_y"))]
        pub acc_y: i16,
        /// Acceleration in the IMU frame Z axis
        #[cfg_attr(feature = "serde", serde(rename = "acc_z"))]
        pub acc_z: i16,
        /// Angular rate around IMU frame X axis
        #[cfg_attr(feature = "serde", serde(rename = "gyr_x"))]
        pub gyr_x: i16,
        /// Angular rate around IMU frame Y axis
        #[cfg_attr(feature = "serde", serde(rename = "gyr_y"))]
        pub gyr_y: i16,
        /// Angular rate around IMU frame Z axis
        #[cfg_attr(feature = "serde", serde(rename = "gyr_z"))]
        pub gyr_z: i16,
    }

    impl MsgImuRaw {
        /// Gets the [TimeStatus][self::TimeStatus] stored in the `tow` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `TimeStatus` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `TimeStatus` were added.
        pub fn time_status(&self) -> Result<TimeStatus, u8> {
            get_bit_range!(self.tow, u32, u8, 31, 30).try_into()
        }

        /// Set the bitrange corresponding to the [TimeStatus][TimeStatus] of the `tow` bitfield.
        pub fn set_time_status(&mut self, time_status: TimeStatus) {
            set_bit_range!(&mut self.tow, time_status, u32, u8, 31, 30);
        }

        /// Gets the `time_since_reference_epoch_in_milliseconds` stored in `tow`.
        pub fn time_since_reference_epoch_in_milliseconds(&self) -> u32 {
            get_bit_range!(self.tow, u32, u32, 29, 0)
        }

        /// Sets the `time_since_reference_epoch_in_milliseconds` bitrange of `tow`.
        pub fn set_time_since_reference_epoch_in_milliseconds(
            &mut self,
            time_since_reference_epoch_in_milliseconds: u32,
        ) {
            set_bit_range!(
                &mut self.tow,
                time_since_reference_epoch_in_milliseconds,
                u32,
                u32,
                29,
                0
            );
        }
    }

    impl ConcreteMessage for MsgImuRaw {
        const MESSAGE_TYPE: u16 = 2304;
        const MESSAGE_NAME: &'static str = "MSG_IMU_RAW";
    }

    impl SbpMessage for MsgImuRaw {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> Option<u16> {
            Some(<Self as ConcreteMessage>::MESSAGE_TYPE)
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
        fn is_valid(&self) -> bool {
            true
        }
        fn into_valid_msg(self) -> Result<Self, crate::messages::invalid::Invalid> {
            Ok(self)
        }

        #[cfg(feature = "swiftnav")]
        fn gps_time(&self) -> Option<std::result::Result<time::MessageTime, time::GpsTimeError>> {
            const IMU_RAW_TIME_STATUS_MASK: u32 = (1 << 30) | (1 << 31);
            if self.tow & IMU_RAW_TIME_STATUS_MASK != 0 {
                return None;
            }
            let tow_s = (self.tow as f64) / 1000.0;
            let gps_time = match time::GpsTime::new(0, tow_s) {
                Ok(gps_time) => gps_time.tow(),
                Err(e) => return Some(Err(e.into())),
            };
            Some(Ok(time::MessageTime::Rover(gps_time.into())))
        }
    }

    impl FriendlyName for MsgImuRaw {
        fn friendly_name() -> &'static str {
            "IMU RAW"
        }
    }

    impl TryFrom<Sbp> for MsgImuRaw {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgImuRaw(m) => Ok(m),
                _ => Err(TryFromSbpError(msg)),
            }
        }
    }

    impl WireFormat for MsgImuRaw {
        const MIN_LEN: usize = <u32 as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN
            + <i16 as WireFormat>::MIN_LEN
            + <i16 as WireFormat>::MIN_LEN
            + <i16 as WireFormat>::MIN_LEN
            + <i16 as WireFormat>::MIN_LEN
            + <i16 as WireFormat>::MIN_LEN
            + <i16 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.tow)
                + WireFormat::len(&self.tow_f)
                + WireFormat::len(&self.acc_x)
                + WireFormat::len(&self.acc_y)
                + WireFormat::len(&self.acc_z)
                + WireFormat::len(&self.gyr_x)
                + WireFormat::len(&self.gyr_y)
                + WireFormat::len(&self.gyr_z)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.tow, buf);
            WireFormat::write(&self.tow_f, buf);
            WireFormat::write(&self.acc_x, buf);
            WireFormat::write(&self.acc_y, buf);
            WireFormat::write(&self.acc_z, buf);
            WireFormat::write(&self.gyr_x, buf);
            WireFormat::write(&self.gyr_y, buf);
            WireFormat::write(&self.gyr_z, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgImuRaw {
                sender_id: None,
                tow: WireFormat::parse_unchecked(buf),
                tow_f: WireFormat::parse_unchecked(buf),
                acc_x: WireFormat::parse_unchecked(buf),
                acc_y: WireFormat::parse_unchecked(buf),
                acc_z: WireFormat::parse_unchecked(buf),
                gyr_x: WireFormat::parse_unchecked(buf),
                gyr_y: WireFormat::parse_unchecked(buf),
                gyr_z: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Time status
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum TimeStatus {
        /// Reference epoch is start of current GPS week
        ReferenceEpochIsStartOfCurrentGpsWeek = 0,

        /// Reference epoch is time of system startup
        ReferenceEpochIsTimeOfSystemStartup = 1,

        /// Reference epoch is unknown
        ReferenceEpochIsUnknown = 2,

        /// Reference epoch is last PPS
        ReferenceEpochIsLastPps = 3,
    }

    impl std::fmt::Display for TimeStatus {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                TimeStatus::ReferenceEpochIsStartOfCurrentGpsWeek => {
                    f.write_str("Reference epoch is start of current GPS week")
                }
                TimeStatus::ReferenceEpochIsTimeOfSystemStartup => {
                    f.write_str("Reference epoch is time of system startup")
                }
                TimeStatus::ReferenceEpochIsUnknown => f.write_str("Reference epoch is unknown"),
                TimeStatus::ReferenceEpochIsLastPps => f.write_str("Reference epoch is last PPS"),
            }
        }
    }

    impl TryFrom<u8> for TimeStatus {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, u8> {
            match i {
                0 => Ok(TimeStatus::ReferenceEpochIsStartOfCurrentGpsWeek),
                1 => Ok(TimeStatus::ReferenceEpochIsTimeOfSystemStartup),
                2 => Ok(TimeStatus::ReferenceEpochIsUnknown),
                3 => Ok(TimeStatus::ReferenceEpochIsLastPps),
                i => Err(i),
            }
        }
    }
}