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
use super::packets::*;
use crate::error::DateTimeError;
use chrono::prelude::*;
use core::convert::TryFrom;
#[derive(Debug, Clone, Copy)]
pub struct Position {
pub lon: f64,
pub lat: f64,
pub alt: f64,
}
#[derive(Debug, Clone, Copy)]
pub struct Velocity {
pub speed: f64,
pub heading: f64,
}
impl<'a> From<&NavPosLlhRef<'a>> for Position {
fn from(packet: &NavPosLlhRef<'a>) -> Self {
Position {
lon: packet.lon_degrees(),
lat: packet.lat_degrees(),
alt: packet.height_msl(),
}
}
}
impl<'a> From<&NavVelNedRef<'a>> for Velocity {
fn from(packet: &NavVelNedRef<'a>) -> Self {
Velocity {
speed: packet.ground_speed(),
heading: packet.heading_degrees(),
}
}
}
impl<'a> From<&NavPosVelTimeRef<'a>> for Position {
fn from(packet: &NavPosVelTimeRef<'a>) -> Self {
Position {
lon: packet.lon_degrees(),
lat: packet.lat_degrees(),
alt: packet.height_msl(),
}
}
}
impl<'a> From<&NavPosVelTimeRef<'a>> for Velocity {
fn from(packet: &NavPosVelTimeRef<'a>) -> Self {
Velocity {
speed: packet.ground_speed(),
heading: packet.heading_degrees(),
}
}
}
impl<'a> TryFrom<&NavPosVelTimeRef<'a>> for DateTime<Utc> {
type Error = DateTimeError;
fn try_from(sol: &NavPosVelTimeRef<'a>) -> Result<Self, Self::Error> {
let date = NaiveDate::from_ymd_opt(
i32::from(sol.year()),
u32::from(sol.month()),
u32::from(sol.day()),
)
.ok_or(DateTimeError::InvalidDate)?;
let time = NaiveTime::from_hms_opt(
u32::from(sol.hour()),
u32::from(sol.min()),
u32::from(sol.sec()),
)
.ok_or(DateTimeError::InvalidTime)?;
const NANOS_LIM: u32 = 1_000_000_000;
if (sol.nanosecond().wrapping_abs() as u32) >= NANOS_LIM {
return Err(DateTimeError::InvalidNanoseconds);
}
let dt = NaiveDateTime::new(date, time)
+ chrono::Duration::nanoseconds(i64::from(sol.nanosecond()));
Ok(DateTime::from_utc(dt, Utc))
}
}