nmea_parser/gnss/
vtg.rs

1/*
2Copyright 2020 Timo Saarinen
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16use super::*;
17
18/// VTG - track made good and speed over ground
19#[derive(Clone, Debug, PartialEq, Serialize)]
20pub struct VtgData {
21    /// Navigation system
22    pub source: NavigationSystem,
23
24    /// Course over ground (CoG), degrees True
25    pub cog_true: Option<f64>,
26
27    /// Course over ground (CoG), degrees Magnetic
28    pub cog_magnetic: Option<f64>,
29
30    /// Speed over ground (SoG), knots
31    pub sog_knots: Option<f64>,
32
33    /// Speed over ground (SoG), km/h
34    pub sog_kph: Option<f64>,
35
36    /// FAA mode indicator
37    pub faa_mode: Option<FaaMode>,
38}
39
40// -------------------------------------------------------------------------------------------------
41
42/// xxVTG: Track Made Good and Ground Speed
43pub(crate) fn handle(
44    sentence: &str,
45    nav_system: NavigationSystem,
46) -> Result<ParsedMessage, ParseError> {
47    let split: Vec<&str> = sentence.split(',').collect();
48
49    Ok(ParsedMessage::Vtg(VtgData {
50        source: nav_system,
51        cog_true: pick_number_field(&split, 1).ok().unwrap_or(None),
52        cog_magnetic: pick_number_field(&split, 3).ok().unwrap_or(None),
53        sog_knots: pick_number_field(&split, 5).ok().unwrap_or(None),
54        sog_kph: pick_number_field(&split, 7).ok().unwrap_or(None),
55        faa_mode: FaaMode::new(split.get(9).unwrap_or(&"")).ok(),
56    }))
57}
58
59// -------------------------------------------------------------------------------------------------
60
61#[cfg(test)]
62mod test {
63    use super::*;
64
65    #[test]
66    fn test_parse_bdvtg() {
67        let mut p = NmeaParser::new();
68        match p.parse_sentence("$BDVTG,054.7,T,034.4,M,005.5,N,010.2,K,D*31") {
69            Ok(ps) => {
70                match ps {
71                    // The expected result
72                    ParsedMessage::Vtg(vtg) => {
73                        assert_eq!(vtg.source, NavigationSystem::Beidou);
74                        assert::close(vtg.cog_true.unwrap_or(0.0), 54.7, 0.1);
75                        assert::close(vtg.cog_magnetic.unwrap_or(0.0), 34.4, 0.1);
76                        assert::close(vtg.sog_knots.unwrap_or(0.0), 5.5, 0.1);
77                        assert::close(vtg.sog_kph.unwrap_or(0.0), 10.2, 0.1);
78                        assert_eq!(vtg.faa_mode, Some(FaaMode::Differential));
79                    }
80                    _ => {
81                        assert!(false);
82                    }
83                }
84            }
85            Err(e) => {
86                assert_eq!(e.to_string(), "OK");
87            }
88        }
89    }
90}