forked from AeroRust/nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsv.rs
211 lines (199 loc) · 5.97 KB
/
gsv.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
use heapless::Vec;
use nom::{
character::complete::char,
combinator::{cond, opt, rest_len},
IResult,
};
use crate::{
parse::NmeaSentence,
sentences::{utils::number, GnssType},
Error, Satellite, SentenceType,
};
/// GSV - Satellites in view
///
/// <https://gpsd.gitlab.io/gpsd/NMEA.html#_gga_global_positioning_system_fix_data>
///
/// ```text
/// 1 2 3 4 5 6 7 n
/// | | | | | | | |
/// $--GSV,x,x,x,x,x,x,x,...*hh<CR><LF>
/// ```
#[derive(Debug, PartialEq)]
pub struct GsvData {
pub gnss_type: GnssType,
pub number_of_sentences: u16,
pub sentence_num: u16,
pub _sats_in_view: u16,
// see SatPack in lib.rs
pub sats_info: Vec<Option<Satellite>, 4>,
}
fn parse_gsv_sat_info(i: &str) -> IResult<&str, Satellite> {
let (i, prn) = number::<u32>(i)?;
let (i, _) = char(',')(i)?;
let (i, elevation) = opt(number::<i32>)(i)?;
let (i, _) = char(',')(i)?;
let (i, azimuth) = opt(number::<i32>)(i)?;
let (i, _) = char(',')(i)?;
let (i, snr) = opt(number::<i32>)(i)?;
let (i, _) = cond(rest_len(i)?.1 > 0, char(','))(i)?;
Ok((
i,
Satellite {
gnss_type: GnssType::Galileo,
prn,
elevation: elevation.map(|v| v as f32),
azimuth: azimuth.map(|v| v as f32),
snr: snr.map(|v| v as f32),
},
))
}
fn do_parse_gsv(i: &str) -> IResult<&str, GsvData> {
let (i, number_of_sentences) = number::<u16>(i)?;
let (i, _) = char(',')(i)?;
let (i, sentence_num) = number::<u16>(i)?;
let (i, _) = char(',')(i)?;
let (i, _sats_in_view) = number::<u16>(i)?;
let (i, _) = char(',')(i)?;
let sats = Vec::<Option<Satellite>, 4>::new();
// We loop through the indices and parse the satellite data
let (i, sats) = (0..4).try_fold((i, sats), |(i, mut sats), sat_index| {
let (i, sat) = opt(parse_gsv_sat_info)(i)?;
sats.insert(sat_index, sat).unwrap();
Ok((i, sats))
})?;
Ok((
i,
GsvData {
gnss_type: GnssType::Galileo,
number_of_sentences,
sentence_num,
_sats_in_view,
sats_info: sats,
},
))
}
/// # Parse one GSV message
///
/// From gpsd/driver_nmea0183.c:
///
/// `$IDGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75`
///
/// 2 Number of sentences for full data
/// 1 Sentence 1 of 2
/// 08 Total number of satellites in view
/// 01 Satellite PRN number
/// 40 Elevation, degrees
/// 083 Azimuth, degrees
/// 46 Signal-to-noise ratio in decibels
/// <repeat for up to 4 satellites per sentence>
///
/// Can occur with talker IDs:
/// - BD (Beidou),
/// - GA (Galileo),
/// - GB (Beidou),
/// - GI (NavIC - India)
/// - GL (GLONASS),
/// - GN (GLONASS, any combination GNSS),
/// - GP (GPS, SBAS, QZSS),
/// - GQ (QZSS)
/// - PQ (QZSS)
/// - QZ (QZSS)
///
/// GL may be (incorrectly) used when GSVs are mixed containing
/// GLONASS, GN may be (incorrectly) used when GSVs contain GLONASS
/// only. Usage is inconsistent.
pub fn parse_gsv(sentence: NmeaSentence) -> Result<GsvData, Error> {
if sentence.message_id != SentenceType::GSV {
Err(Error::WrongSentenceHeader {
expected: SentenceType::GSV,
found: sentence.message_id,
})
} else {
let gnss_type = match sentence.talker_id {
"GA" => GnssType::Galileo,
"GP" => GnssType::Gps,
"GL" => GnssType::Glonass,
"BD" | "GB" => GnssType::Beidou,
"GI" => GnssType::NavIC,
"PQ" | "QZ" => GnssType::Qzss,
_ => return Err(Error::UnknownGnssType(sentence.talker_id)),
};
let mut res = do_parse_gsv(sentence.data)?.1;
res.gnss_type = gnss_type;
for sat in &mut res.sats_info {
if let Some(v) = (*sat).as_mut() {
v.gnss_type = gnss_type;
}
}
Ok(res)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_gsv_full() {
let data = parse_gsv(NmeaSentence {
talker_id: "GP",
message_id: SentenceType::GSV,
data: "2,1,08,01,,083,46,02,17,308,,12,07,344,39,14,22,228,",
checksum: 0,
})
.unwrap();
assert_eq!(data.gnss_type, GnssType::Gps);
assert_eq!(data.number_of_sentences, 2);
assert_eq!(data.sentence_num, 1);
assert_eq!(data._sats_in_view, 8);
assert_eq!(
data.sats_info[0].clone().unwrap(),
Satellite {
gnss_type: data.gnss_type,
prn: 1,
elevation: None,
azimuth: Some(83.),
snr: Some(46.),
}
);
assert_eq!(
data.sats_info[1].clone().unwrap(),
Satellite {
gnss_type: data.gnss_type,
prn: 2,
elevation: Some(17.),
azimuth: Some(308.),
snr: None,
}
);
assert_eq!(
data.sats_info[2].clone().unwrap(),
Satellite {
gnss_type: data.gnss_type,
prn: 12,
elevation: Some(7.),
azimuth: Some(344.),
snr: Some(39.),
}
);
assert_eq!(
data.sats_info[3].clone().unwrap(),
Satellite {
gnss_type: data.gnss_type,
prn: 14,
elevation: Some(22.),
azimuth: Some(228.),
snr: None,
}
);
let data = parse_gsv(NmeaSentence {
talker_id: "GL",
message_id: SentenceType::GSV,
data: "3,3,10,72,40,075,43,87,00,000,",
checksum: 0,
})
.unwrap();
assert_eq!(data.gnss_type, GnssType::Glonass);
assert_eq!(data.number_of_sentences, 3);
assert_eq!(data.sentence_num, 3);
assert_eq!(data._sats_in_view, 10);
}
}