From 4a6662eccf563742401e1b457c05ace374201021 Mon Sep 17 00:00:00 2001 From: Noah Sprenger <69169402+NoahSprenger@users.noreply.github.com> Date: Sun, 17 Nov 2024 15:32:27 -0500 Subject: [PATCH] Add GPS message types, consolidate SBG messages, create common message type. (#6) --- Cargo.toml | 3 +- src/command.rs | 18 +---- src/lib.rs | 87 ++++++++++++++-------- src/sensor.rs | 198 +++++++++++++++++++++++++++++-------------------- 4 files changed, 180 insertions(+), 126 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8eb0a2c..7c974f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,8 @@ bitflags = { version = "2.3.1", features = ["serde"] } proptest = { version = "1.2.0", optional = true } proptest-derive = { version = "0.3.0", optional = true } messages-proc-macros-lib = { path = "messages-proc-macros-lib" } -chrono = {git = "https://github.com/uorocketry/chrono", features = ["serde"], default-features = false} +chrono = { git = "https://github.com/uorocketry/chrono", features = ["serde", "arbitrary"], default-features = false} +ublox = { git = "https://github.com/uorocketry/ublox", features = ["serde"], default-features = false } [dev-dependencies] proptest = "1.2.0" diff --git a/src/command.rs b/src/command.rs index 32b2703..519f045 100644 --- a/src/command.rs +++ b/src/command.rs @@ -2,14 +2,9 @@ use crate::node::Node; use derive_more::From; use messages_proc_macros_lib::common_derives; -#[common_derives] -pub struct Command { - pub data: CommandData, -} - #[common_derives] #[derive(From)] -pub enum CommandData { +pub enum Command{ DeployDrogue(DeployDrogue), DeployMain(DeployMain), PowerDown(PowerDown), @@ -17,7 +12,6 @@ pub enum CommandData { Online(Online), } -// not really a command but this decreases the complexity of the change on ground station. #[common_derives] pub struct Online { pub online: bool, @@ -39,7 +33,7 @@ pub struct DeployMain { #[common_derives] #[derive(From)] pub struct PowerDown { - pub board: Node, // This isn't proper naming !! This is the board to be powered down. Changes name of sender.rs to board.rs. + pub board: Node, } #[common_derives] @@ -53,10 +47,4 @@ pub struct RadioRateChange { pub enum RadioRate { Fast, Slow, -} - -impl Command { - pub fn new(data: impl Into) -> Self { - Command { data: data.into() } - } -} +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 513b35a..38eba31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,13 +5,11 @@ //! This crate contains all the message definitions that will be used for inter-board communication //! and ground-station communication. -use crate::command::Command; use crate::node::Node; -use crate::sensor::Sensor; use crate::state::State; use chrono::NaiveDateTime; use derive_more::From; -/// This is to help control versions. + pub use mavlink; use messages_proc_macros_lib::common_derives; @@ -42,31 +40,73 @@ impl Format for FormattedNaiveDateTime { /// Topmost message. Encloses all the other possible messages, and is the only thing that should /// be sent over the wire. #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] -#[derive(Format)] +pub struct RadioMessage<'a> { + pub timestamp: FormattedNaiveDateTime, + + /// The original sender of this message. + pub node: Node, + + /// The data contained in this message. + #[serde(borrow)] + pub data: RadioData<'a>, +} + +/// Topmost message. Encloses all the other possible messages, and is the only thing that should +/// be sent over the wire. +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[cfg_attr(all(feature = "std", test), derive(proptest_derive::Arbitrary))] -pub struct Message { +pub struct CanMessage { pub timestamp: FormattedNaiveDateTime, /// The original sender of this message. pub node: Node, /// The data contained in this message. - pub data: Data, + pub data: CanData, +} + + +#[common_derives] +#[derive(From)] +pub enum Common { + ResetReason(sensor::ResetReason), + Command(command::Command), + Log(Log), } #[common_derives] +#[serde(rename_all = "lowercase")] +pub enum CanData { + Common(Common), + Temperature(u8, f32), // sensor id, temperature + Pressure(u8, f32), + Strain(u8, f32), +} + +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(From)] #[serde(rename_all = "lowercase")] -pub enum Data { +pub enum RadioData<'a> { + Common(Common), State(State), - Sensor(Sensor), - Log(Log), - Command(Command), + Sbg(sensor::SbgData), + #[serde(borrow)] + Gps(sensor::Gps<'a>), +} + +impl CanMessage { + pub fn new(timestamp: FormattedNaiveDateTime, node: Node, data: impl Into) -> Self { + CanMessage { + timestamp, + node, + data: data.into(), + } + } } -impl Message { - pub fn new(timestamp: FormattedNaiveDateTime, node: Node, data: impl Into) -> Self { - Message { +impl<'a> RadioMessage<'a> { + pub fn new(timestamp: FormattedNaiveDateTime, node: Node, data: impl Into>) -> Self { + RadioMessage { timestamp, node, data: data.into(), @@ -76,32 +116,17 @@ impl Message { #[cfg(all(test, feature = "std"))] mod test { - use crate::{Message, MAX_SIZE_CAN, MAX_SIZE_RADIO}; + use crate::{CanMessage, MAX_SIZE_CAN, MAX_SIZE_RADIO}; use proptest::prelude::*; proptest! { #[test] - fn message_size(msg: Message) { + fn can_message_size(msg: CanMessage) { let bytes = postcard::to_allocvec(&msg).unwrap(); dbg!(msg.clone()); - match msg.data { - crate::Data::Sensor(sensor) => { - match sensor.data { - crate::sensor::SensorData::SbgData(_) => { - assert!(bytes.len() <= MAX_SIZE_RADIO); - } - _ => { - assert!(bytes.len() <= MAX_SIZE_CAN); - } - } - } - _ => { - assert!(bytes.len() <= MAX_SIZE_CAN); - } - - } + assert!(bytes.len() <= MAX_SIZE_CAN); } } } diff --git a/src/sensor.rs b/src/sensor.rs index e931dea..3b7d38a 100644 --- a/src/sensor.rs +++ b/src/sensor.rs @@ -4,33 +4,119 @@ use crate::sensor_status::{ use derive_more::From; use messages_proc_macros_lib::common_derives; -#[common_derives] -pub struct Sensor { - /// Used to differentiate between multiple components on the same sender. Unused right now. - pub component_id: u8, - pub data: SensorData, -} - -#[common_derives] +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(From)] -pub enum SensorData { - // UtcTime(UtcTime), - // Air(Air), - // EkfQuat(EkfQuat), - // EkfNav1(EkfNav1), - // EkfNav2(EkfNav2), - // EkfNavAcc(EkfNavAcc), - // Imu1(Imu1), - // Imu2(Imu2), - NavPosLlh(NavPosLlh), - // GpsVel(GpsVel), - // GpsVelAcc(GpsVelAcc), - // GpsPos1(GpsPos1), - // GpsPos2(GpsPos2), - // GpsPosAcc(GpsPosAcc), - ResetReason(ResetReason), - RecoverySensing(RecoverySensing), - SbgData(SbgData), +pub enum Gps<'a> { + #[serde(borrow)] + NavPosLlh(ublox::NavPosLlhRef<'a>), + #[serde(borrow)] + NavStatus(ublox::NavStatusRef<'a>), + #[serde(borrow)] + NavDop(ublox::NavDopRef<'a>), + #[serde(borrow)] + NavPvt(ublox::NavPvtRef<'a>), + #[serde(borrow)] + NavSolution(ublox::NavSolutionRef<'a>), + #[serde(borrow)] + NavVelNed(ublox::NavVelNedRef<'a>), + #[serde(borrow)] + NavHpPosLlh(ublox::NavHpPosLlhRef<'a>), + #[serde(borrow)] + NavHpPosEcef(ublox::NavHpPosEcefRef<'a>), + #[serde(borrow)] + NavTimeUTC(ublox::NavTimeUTCRef<'a>), + #[serde(borrow)] + NavTimeLs(ublox::NavTimeLsRef<'a>), + #[serde(borrow)] + NavSat(ublox::NavSatRef<'a>), + #[serde(borrow)] + NavEoe(ublox::NavEoeRef<'a>), + #[serde(borrow)] + NavOdo(ublox::NavOdoRef<'a>), + #[serde(borrow)] + CfgOdo(ublox::CfgOdoRef<'a>), + #[serde(borrow)] + MgaAck(ublox::MgaAckRef<'a>), + #[serde(borrow)] + MgaGpsIono(ublox::MgaGpsIonoRef<'a>), + #[serde(borrow)] + MgaGpsEph(ublox::MgaGpsEphRef<'a>), + #[serde(borrow)] + MgaGloEph(ublox::MgaGloEphRef<'a>), + #[serde(borrow)] + AlpSrv(ublox::AlpSrvRef<'a>), + #[serde(borrow)] + AckAck(ublox::AckAckRef<'a>), + #[serde(borrow)] + AckNak(ublox::AckNakRef<'a>), + #[serde(borrow)] + CfgItfm(ublox::CfgItfmRef<'a>), + #[serde(borrow)] + CfgPrtI2c(ublox::CfgPrtI2cRef<'a>), + #[serde(borrow)] + CfgPrtSpi(ublox::CfgPrtSpiRef<'a>), + #[serde(borrow)] + CfgPrtUart(ublox::CfgPrtUartRef<'a>), + #[serde(borrow)] + CfgNav5(ublox::CfgNav5Ref<'a>), + #[serde(borrow)] + CfgAnt(ublox::CfgAntRef<'a>), + #[serde(borrow)] + CfgTmode2(ublox::CfgTmode2Ref<'a>), + #[serde(borrow)] + CfgTmode3(ublox::CfgTmode3Ref<'a>), + #[serde(borrow)] + CfgTp5(ublox::CfgTp5Ref<'a>), + #[serde(borrow)] + InfError(ublox::InfErrorRef<'a>), + #[serde(borrow)] + InfWarning(ublox::InfWarningRef<'a>), + #[serde(borrow)] + InfNotice(ublox::InfNoticeRef<'a>), + #[serde(borrow)] + InfTest(ublox::InfTestRef<'a>), + #[serde(borrow)] + InfDebug(ublox::InfDebugRef<'a>), + #[serde(borrow)] + RxmRawx(ublox::RxmRawxRef<'a>), + #[serde(borrow)] + TimTp(ublox::TimTpRef<'a>), + #[serde(borrow)] + TimTm2(ublox::TimTm2Ref<'a>), + #[serde(borrow)] + MonVer(ublox::MonVerRef<'a>), + #[serde(borrow)] + MonGnss(ublox::MonGnssRef<'a>), + #[serde(borrow)] + MonHw(ublox::MonHwRef<'a>), + #[serde(borrow)] + RxmRtcm(ublox::RxmRtcmRef<'a>), + #[serde(borrow)] + EsfMeas(ublox::EsfMeasRef<'a>), + #[serde(borrow)] + EsfIns(ublox::EsfInsRef<'a>), + #[serde(borrow)] + HnrAtt(ublox::HnrAttRef<'a>), + #[serde(borrow)] + HnrIns(ublox::HnrInsRef<'a>), + #[serde(borrow)] + HnrPvt(ublox::HnrPvtRef<'a>), + #[serde(borrow)] + NavAtt(ublox::NavAttRef<'a>), + #[serde(borrow)] + NavClock(ublox::NavClockRef<'a>), + #[serde(borrow)] + NavVelECEF(ublox::NavVelECEFRef<'a>), + #[serde(borrow)] + MgaGpsEPH(ublox::MgaGpsEPHRef<'a>), + #[serde(borrow)] + RxmSfrbx(ublox::RxmSfrbxRef<'a>), + #[serde(borrow)] + EsfRaw(ublox::EsfRawRef<'a>), + #[serde(borrow)] + TimSvin(ublox::TimSvinRef<'a>), + #[serde(borrow)] + SecUniqId(ublox::SecUniqIdRef<'a>), } #[common_derives] @@ -39,23 +125,10 @@ pub enum SbgData { UtcTime(UtcTime), Air(Air), EkfQuat(EkfQuat), - EkfNav1(EkfNav1), - EkfNav2(EkfNav2), - EkfNavAcc(EkfNavAcc), - Imu1(Imu1), - Imu2(Imu2), + EkfNav(EkfNav), + Imu(Imu), GpsVel(GpsVel), - GpsVelAcc(GpsVelAcc), - GpsPos1(GpsPos1), - GpsPos2(GpsPos2), - GpsPosAcc(GpsPosAcc), -} - -#[common_derives] -pub struct NavPosLlh { - pub height_msl: f64, - pub longitude: f64, - pub latitude: f64, + GpsPos(GpsPos), } /* Replace with new health monitor */ @@ -92,25 +165,17 @@ pub enum ResetReason { } #[common_derives] -pub struct GpsPos1 { +pub struct GpsPos { #[doc = "< Latitude in degrees, positive north."] pub latitude: Option, #[doc = "< Longitude in degrees, positive east."] pub longitude: Option, -} - -#[common_derives] -pub struct GpsPos2 { #[doc = "< GPS time of week in ms."] pub time_of_week: Option, #[doc = "< Altitude difference between the geoid and the Ellipsoid in meters (Height above Ellipsoid = altitude + undulation)."] pub undulation: Option, #[doc = "< Altitude above Mean Sea Level in meters."] pub altitude: Option, -} - -#[common_derives] -pub struct GpsPosAcc { #[doc = "< Time in us since the sensor power up."] pub time_stamp: u32, #[doc = "< GPS position status, type and bitmask."] @@ -184,25 +249,17 @@ pub struct EkfQuat { } #[common_derives] -pub struct EkfNavAcc { +pub struct EkfNav { #[doc = "< EKF solution status bitmask and enum."] pub status: EkfStatus, #[doc = "< North, East, Down velocity 1 sigma standard deviation in m.s^-1."] pub velocity_std_dev: Option<[f32; 3usize]>, #[doc = "< Latitude, longitude and altitude 1 sigma standard deviation in meters."] pub position_std_dev: Option<[f32; 3usize]>, -} - -#[common_derives] -pub struct EkfNav1 { #[doc = "< Time in us since the sensor power up."] pub time_stamp: u32, #[doc = "< North, East, Down velocity in m.s^-1."] pub velocity: Option<[f32; 3usize]>, -} - -#[common_derives] -pub struct EkfNav2 { #[doc = "< Latitude, Longitude in degrees positive North and East.\nAltitude above Mean Sea Level in meters."] pub position: Option<[f64; 3usize]>, #[doc = "< Altitude difference between the geoid and the Ellipsoid in meters (Height above Ellipsoid = altitude + undulation)."] @@ -210,7 +267,7 @@ pub struct EkfNav2 { } #[common_derives] -pub struct Imu1 { +pub struct Imu { #[doc = "< Time in us since the sensor power up."] pub time_stamp: u32, #[doc = "< IMU status bitmask."] @@ -219,10 +276,6 @@ pub struct Imu1 { pub accelerometers: Option<[f32; 3usize]>, #[doc = "< X, Y, Z gyroscopes in rad.s^-1."] pub gyroscopes: Option<[f32; 3usize]>, -} - -#[common_derives] -pub struct Imu2 { #[doc = "< Internal temperature in °C."] pub temperature: Option, #[doc = "< X, Y, Z delta velocity in m.s^-2."] @@ -243,10 +296,6 @@ pub struct GpsVel { pub velocity: Option<[f32; 3usize]>, #[doc = "< Track ground course in degrees."] pub course: Option, -} - -#[common_derives] -pub struct GpsVelAcc { #[doc = "< Course accuracy in degrees."] pub course_acc: Option, #[doc = "< GPS North, East, Down velocity 1 sigma accuracy in m.s^-1."] @@ -259,13 +308,4 @@ pub struct RecoverySensing { pub main_current: u16, pub drogue_voltage: u16, pub main_voltage: u16, -} - -impl Sensor { - pub fn new(data: impl Into) -> Self { - Sensor { - component_id: 0, - data: data.into(), - } - } -} +} \ No newline at end of file