Skip to content

Commit

Permalink
Add GPS message types, consolidate SBG messages, create common messag…
Browse files Browse the repository at this point in the history
…e type. (#6)
  • Loading branch information
NoahSprenger authored Nov 17, 2024
1 parent 5b9b567 commit 4a6662e
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 126 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 3 additions & 15 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ 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),
RadioRateChange(RadioRateChange),
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,
Expand All @@ -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]
Expand All @@ -53,10 +47,4 @@ pub struct RadioRateChange {
pub enum RadioRate {
Fast,
Slow,
}

impl Command {
pub fn new(data: impl Into<CommandData>) -> Self {
Command { data: data.into() }
}
}
}
87 changes: 56 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CanData>) -> Self {
CanMessage {
timestamp,
node,
data: data.into(),
}
}
}

impl Message {
pub fn new(timestamp: FormattedNaiveDateTime, node: Node, data: impl Into<Data>) -> Self {
Message {
impl<'a> RadioMessage<'a> {
pub fn new(timestamp: FormattedNaiveDateTime, node: Node, data: impl Into<RadioData<'a>>) -> Self {
RadioMessage {
timestamp,
node,
data: data.into(),
Expand All @@ -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};

Check warning on line 119 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test

unused import: `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);
}
}
}
Loading

0 comments on commit 4a6662e

Please sign in to comment.