Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GST sentence parser #121

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmt-03 = ["dep:defmt", "heapless/defmt-03"]

all-sentences = ["GNSS", "waypoint", "maritime", "water", "vendor-specific", "other"]
# GNSS specific sentences related to the possition or speed relative to the ground
GNSS = ["ALM", "GBS", "GGA", "GLL", "GNS", "GSA", "GSV", "RMC", "VTG"]
GNSS = ["ALM", "GBS", "GGA", "GLL", "GNS", "GSA", "GST", "GSV", "RMC", "VTG"]

waypoint = ["AAM", "BOD", "BWC", "BWW", "WNC", "ZFO", "ZTG"]
maritime = ["waypoint", "water"]
Expand Down Expand Up @@ -116,6 +116,10 @@ GNS = []
# feature: GNSS
GSA = []

# GST - GPS Pseudorange Noise Statistics
# feature: GNSS
GST = []

# GSV - Satellites in view
# feature: GNSS
GSV = []
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//! - GLL *
//! - GNS *
//! - GSA *
//! - GST
//! - GSV *
//! - HDT
//! - MDA
Expand Down
11 changes: 11 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub enum ParseResult {
GLL(GllData),
GNS(GnsData),
GSA(GsaData),
GST(GstData),
GSV(GsvData),
HDT(HdtData),
MDA(MdaData),
Expand Down Expand Up @@ -150,6 +151,7 @@ impl From<&ParseResult> for SentenceType {
ParseResult::GLL(_) => SentenceType::GLL,
ParseResult::GNS(_) => SentenceType::GNS,
ParseResult::GSA(_) => SentenceType::GSA,
ParseResult::GST(_) => SentenceType::GST,
ParseResult::GSV(_) => SentenceType::GSV,
ParseResult::HDT(_) => SentenceType::HDT,
ParseResult::MDA(_) => SentenceType::MDA,
Expand Down Expand Up @@ -298,6 +300,15 @@ pub fn parse_str(sentence_input: &str) -> Result<ParseResult, Error> {
}
}
}
SentenceType::GST => {
cfg_if! {
if #[cfg(feature = "GST")] {
parse_gst(nmea_sentence).map(ParseResult::GST)
} else {
return Err(Error::DisabledSentence);
}
}
}
SentenceType::GSV => {
cfg_if! {
if #[cfg(feature = "GSV")] {
Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ impl<'a> Nmea {
| ParseResult::BOD(_)
| ParseResult::DBK(_)
| ParseResult::GBS(_)
| ParseResult::GST(_)
| ParseResult::AAM(_)
| ParseResult::ALM(_)
| ParseResult::HDT(_)
Expand Down Expand Up @@ -678,6 +679,7 @@ define_sentence_type_enum! {
/// - [`SentenceType::GGA`]
/// - [`SentenceType::GNS`]
/// - [`SentenceType::GSA`]
/// - [`SentenceType::GST`]
/// - [`SentenceType::GSV`]
///
/// ### Course
Expand Down
130 changes: 130 additions & 0 deletions src/sentences/gst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use chrono::NaiveTime;
use nom::{character::complete::char, number::complete::float, combinator::opt, IResult};
use crate::{parse::NmeaSentence, sentences::utils::parse_hms, Error, SentenceType};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// GST - GPS Pseudorange Noise Statistics
/// ```text
/// 1 2 3 4 5 6 7 8 9
/// | | | | | | | | |
/// $ --GST,hhmmss.ss,x,x,x,x,x,x,x*hh<CR><LF>
/// ```
/// Example: `$GPGST,182141.000,15.5,15.3,7.2,21.8,0.9,0.5,0.8*54`
///
/// 1. UTC time of associated GGA fix
/// 2. Total RMS standard deviation of ranges inputs to the navigation solution
/// 3. Standard deviation (meters) of semi-major axis of error ellipse
/// 4. Standard deviation (meters) of semi-minor axis of error ellipse
/// 5. Orientation of semi-major axis of error ellipse (true north degrees)
/// 6. Standard deviation (meters) of latitude error
/// 7. Standard deviation (meters) of longitude error
/// 8. Standard deviation (meters) of altitude error
/// 9. Checksum

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[derive(Debug, PartialEq)]
pub struct GstData {
#[cfg_attr(feature = "defmt-03", defmt(Debug2Format))]
pub time: Option<NaiveTime>,
pub rms_sd: Option<f32>,
pub ellipse_semi_major_sd: Option<f32>,
pub ellipse_semi_minor_sd: Option<f32>,
pub err_ellipse_orientation: Option<f32>,
pub lat_sd: Option<f32>,
pub long_sd: Option<f32>,
pub alt_sd: Option<f32>,
}

fn do_parse_gst(i: &str) -> IResult<&str, GstData> {
let (i, time) = opt(parse_hms)(i)?;
let (i, _) = char(',')(i)?;

let (i, rms_sd) = opt(float)(i)?;
let (i, _) = char(',')(i)?;

let (i, ellipse_semi_major_sd) = opt(float)(i)?;
let (i, _) = char(',')(i)?;

let (i, ellipse_semi_minor_sd) = opt(float)(i)?;
let (i, _) = char(',')(i)?;

let (i, err_ellipse_orientation) = opt(float)(i)?;
let (i, _) = char(',')(i)?;

let (i, lat_sd) = opt(float)(i)?;
let (i, _) = char(',')(i)?;

let (i, long_sd) = opt(float)(i)?;
let (i, _) = char(',')(i)?;

let (i, alt_sd) = opt(float)(i)?;

Ok((
i,
GstData {
time,
rms_sd,
ellipse_semi_major_sd,
ellipse_semi_minor_sd,
err_ellipse_orientation,
lat_sd,
long_sd,
alt_sd,
},
))
}
pub fn parse_gst(sentence: NmeaSentence) -> Result<GstData, Error> {
if sentence.message_id != SentenceType::GST {
Err(Error::WrongSentenceHeader {
expected: SentenceType::GST,
found: sentence.message_id,
})
} else {
Ok(do_parse_gst(sentence.data)?.1)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{parse::parse_nmea_sentence, Error};

fn run_parse_gst(line: &str) -> Result<GstData, Error> {
let s = parse_nmea_sentence(line).expect("GST sentence initial parse failed");
assert_eq!(s.checksum, s.calc_checksum());
parse_gst(s)
}

#[test]
fn test_parse_gst() {
assert_eq!(
GstData {
time: NaiveTime::from_hms_micro_opt(18, 21, 41, 00),
rms_sd: Some(15.5),
ellipse_semi_major_sd: Some(15.3),
ellipse_semi_minor_sd: Some(7.2),
err_ellipse_orientation: Some(21.8),
lat_sd: Some(0.9),
long_sd: Some(0.5),
alt_sd: Some(0.8),
},
run_parse_gst("$GPGST,182141.000,15.5,15.3,7.2,21.8,0.9,0.5,0.8*54").unwrap()
);
assert_eq!(
GstData {
time: None,
rms_sd: None,
ellipse_semi_major_sd: None,
ellipse_semi_minor_sd: None,
err_ellipse_orientation: None,
lat_sd: None,
long_sd: None,
alt_sd: None,
},
run_parse_gst("$GPGST,,,,,,,,*57").unwrap()
);
}
}
2 changes: 2 additions & 0 deletions src/sentences/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod gga;
pub mod gll;
pub mod gns;
pub mod gsa;
pub mod gst;
pub mod gsv;
pub mod hdt;
pub mod mda;
Expand Down Expand Up @@ -47,6 +48,7 @@ pub use {
gns::{parse_gns, GnsData},
gnss_type::GnssType,
gsa::{parse_gsa, GsaData},
gst::{parse_gst, GstData},
gsv::{parse_gsv, GsvData},
hdt::{parse_hdt, HdtData},
mda::{parse_mda, MdaData},
Expand Down
2 changes: 2 additions & 0 deletions tests/all_supported_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fn test_all_supported_messages() {
(SentenceType::GNS, "$GPGNS,224749.00,3333.4268304,N,11153.3538273,W,D,19,0.6,406.110,-26.294,6.0,0138,S,*46"),
// GSA
(SentenceType::GSA, "$GPGSA,A,3,23,31,22,16,03,07,,,,,,,1.8,1.1,1.4*3E"),
// GST
(SentenceType::GST, "$GPGST,182141.000,15.5,15.3,7.2,21.8,0.9,0.5,0.8*54"),
// GSV
(SentenceType::GSV, "$GPGSV,3,1,12,01,49,196,41,03,71,278,32,06,02,323,27,11,21,196,39*72"),
// HDT
Expand Down