forked from AeroRust/nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsa.rs
246 lines (228 loc) · 7.4 KB
/
gsa.rs
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
use heapless::Vec;
use nom::{
branch::alt,
bytes::complete::take_while1,
character::complete::{char, one_of},
combinator::{all_consuming, opt, value},
error::{ErrorKind, ParseError},
number::complete::float,
sequence::terminated,
Err, IResult, InputLength, Parser,
};
use crate::{parse::NmeaSentence, sentences::utils::number, Error, SentenceType};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GsaMode1 {
Manual,
Automatic,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GsaMode2 {
NoFix,
Fix2D,
Fix3D,
}
/// GSA - GPS DOP and active satellites
///
/// <https://gpsd.gitlab.io/gpsd/NMEA.html#_bod_bearing_waypoint_to_waypoint>
///
/// ```text
/// 1 2 3 14 15 16 17 18
/// | | | | | | | |
/// $--GSA,a,a,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x.x,x.x,x.x*hh<CR><LF>
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct GsaData {
pub mode1: GsaMode1,
pub mode2: GsaMode2,
pub fix_sats_prn: Vec<u32, 12>,
pub pdop: Option<f32>,
pub hdop: Option<f32>,
pub vdop: Option<f32>,
}
/// This function is take from `nom`, see `nom::multi::many0`
/// with one difference - we use a [`heapless::Vec`]
/// because we want `no_std` & no `alloc`
fn many0<I, O, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, Vec<O, 12>, E>
where
I: Clone + InputLength,
F: Parser<I, O, E>,
E: ParseError<I>,
O: core::fmt::Debug,
{
move |mut i: I| {
let mut acc = Vec::<_, 12>::new();
loop {
let len = i.input_len();
match f.parse(i.clone()) {
Err(Err::Error(_)) => return Ok((i, acc)),
Err(e) => return Err(e),
Ok((i1, o)) => {
// infinite loop check: the parser must always consume
if i1.input_len() == len {
return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many0)));
}
i = i1;
acc.push(o).unwrap();
}
}
}
}
}
fn gsa_prn_fields_parse(i: &str) -> IResult<&str, Vec<Option<u32>, 12>> {
many0(terminated(opt(number::<u32>), char(',')))(i)
}
type GsaTail = (Vec<Option<u32>, 12>, Option<f32>, Option<f32>, Option<f32>);
fn do_parse_gsa_tail(i: &str) -> IResult<&str, GsaTail> {
let (i, prns) = gsa_prn_fields_parse(i)?;
let (i, pdop) = float(i)?;
let (i, _) = char(',')(i)?;
let (i, hdop) = float(i)?;
let (i, _) = char(',')(i)?;
let (i, vdop) = float(i)?;
Ok((i, (prns, Some(pdop), Some(hdop), Some(vdop))))
}
fn is_comma(x: char) -> bool {
x == ','
}
fn do_parse_empty_gsa_tail(i: &str) -> IResult<&str, GsaTail> {
value(
(Vec::new(), None, None, None),
all_consuming(take_while1(is_comma)),
)(i)
}
fn do_parse_gsa(i: &str) -> IResult<&str, GsaData> {
let (i, mode1) = one_of("MA")(i)?;
let (i, _) = char(',')(i)?;
let (i, mode2) = one_of("123")(i)?;
let (i, _) = char(',')(i)?;
let (i, mut tail) = alt((do_parse_empty_gsa_tail, do_parse_gsa_tail))(i)?;
Ok((
i,
GsaData {
mode1: match mode1 {
'M' => GsaMode1::Manual,
'A' => GsaMode1::Automatic,
_ => unreachable!(),
},
mode2: match mode2 {
'1' => GsaMode2::NoFix,
'2' => GsaMode2::Fix2D,
'3' => GsaMode2::Fix3D,
_ => unreachable!(),
},
fix_sats_prn: {
let mut fix_sats_prn = Vec::<u32, 12>::new();
for sat in tail.0.iter().flatten() {
fix_sats_prn.push(*sat).unwrap()
}
// now that we don't have `drain()` from `std::Vec`,
// we clear the `heapless::Vec`'s tail manually
tail.0.clear();
fix_sats_prn
},
pdop: tail.1,
hdop: tail.2,
vdop: tail.3,
},
))
}
/// # Parse GSA message
///
/// From gpsd:
///
/// eg1. `$GPGSA,A,3,,,,,,16,18,,22,24,,,3.6,2.1,2.2*3C`
/// eg2. `$GPGSA,A,3,19,28,14,18,27,22,31,39,,,,,1.7,1.0,1.3*35`
/// 1 = Mode:
/// M=Manual, forced to operate in 2D or 3D
/// A=Automatic, 3D/2D
/// 2 = Mode: 1=Fix not available, 2=2D, 3=3D
/// 3-14 = PRNs of satellites used in position fix (null for unused fields)
/// 15 = PDOP
/// 16 = HDOP
/// 17 = VDOP
///
/// Not all documentation specifies the number of PRN fields, it
/// may be variable. Most doc that specifies says 12 PRNs.
///
/// The CH-4701 outputs 24 PRNs!
///
/// The Skytraq S2525F8-BD-RTK output both GPGSA and BDGSA in the
/// same cycle:
/// $GPGSA,A,3,23,31,22,16,03,07,,,,,,,1.8,1.1,1.4*3E
/// $BDGSA,A,3,214,,,,,,,,,,,,1.8,1.1,1.4*18
/// These need to be combined like GPGSV and BDGSV
///
/// Some GPS emit GNGSA. So far we have not seen a GPS emit GNGSA
/// and then another flavor of xxGSA
///
/// Some Skytraq will emit all GPS in one GNGSA, Then follow with
/// another GNGSA with the BeiDou birds.
///
/// SEANEXX and others also do it:
///
/// ```text
/// $GNGSA,A,3,31,26,21,,,,,,,,,,3.77,2.55,2.77*1A
/// $GNGSA,A,3,75,86,87,,,,,,,,,,3.77,2.55,2.77*1C
/// ```
/// seems like the first is GNSS and the second GLONASS
///
/// One chipset called the i.Trek M3 issues GPGSA lines that look like
/// this: "$GPGSA,A,1,,,,*32" when it has no fix. This is broken
/// in at least two ways:
/// - It's got the wrong number of fields
/// - it claims to be a valid sentence (A flag) when it isn't
///
/// Alarmingly, it's possible this error may be generic to SiRFstarIII
pub fn parse_gsa(sentence: NmeaSentence) -> Result<GsaData, Error> {
if sentence.message_id != SentenceType::GSA {
Err(Error::WrongSentenceHeader {
expected: SentenceType::GSA,
found: sentence.message_id,
})
} else {
Ok(do_parse_gsa(sentence.data)?.1)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parse::parse_nmea_sentence;
#[test]
fn test_gsa_prn_fields_parse() {
let (_, ret) = gsa_prn_fields_parse("5,").unwrap();
assert_eq!(ret, &[Some(5)]);
let (_, ret) = gsa_prn_fields_parse(",").unwrap();
assert_eq!(ret, &[None]);
let (_, ret) = gsa_prn_fields_parse(",,5,6,").unwrap();
assert_eq!(ret, &[None, None, Some(5), Some(6)],);
}
#[test]
fn smoke_test_parse_gsa() {
let s = parse_nmea_sentence("$GPGSA,A,3,,,,,,16,18,,22,24,,,3.6,2.1,2.2*3C").unwrap();
let gsa = parse_gsa(s).unwrap();
assert_eq!(
GsaData {
mode1: GsaMode1::Automatic,
mode2: GsaMode2::Fix3D,
fix_sats_prn: Vec::<_, 12>::from_slice(&[16, 18, 22, 24]).unwrap(),
pdop: Some(3.6),
hdop: Some(2.1),
vdop: Some(2.2),
},
gsa
);
let gsa_examples = [
"$GPGSA,A,3,19,28,14,18,27,22,31,39,,,,,1.7,1.0,1.3*35",
"$GPGSA,A,3,23,31,22,16,03,07,,,,,,,1.8,1.1,1.4*3E",
"$BDGSA,A,3,214,,,,,,,,,,,,1.8,1.1,1.4*18",
"$GNGSA,A,3,31,26,21,,,,,,,,,,3.77,2.55,2.77*1A",
"$GNGSA,A,3,75,86,87,,,,,,,,,,3.77,2.55,2.77*1C",
"$GPGSA,A,1,,,,*32",
];
for line in &gsa_examples {
println!("we parse line '{}'", line);
let s = parse_nmea_sentence(line).unwrap();
parse_gsa(s).unwrap();
}
}
}