From eb29b97f9ba2352c27d7e855c2f7de32d048cb7a Mon Sep 17 00:00:00 2001 From: melfey Date: Sat, 23 May 2026 13:59:32 -0500 Subject: [PATCH] changed the structure of core::communication. I think the implementation of the positional.rs is good --- Cargo.lock | 68 ++++++++ Cargo.toml | 3 +- src/core/communication/drivetrain.rs | 2 + src/core/communication/general.rs | 24 +++ src/core/communication/impls.rs | 0 src/core/communication/mod.rs | 9 +- src/core/communication/motor.rs | 4 + src/core/communication/positional.rs | 244 +++++++++++++++++++++++++++ src/core/communication/sensing.rs | 5 + src/core/communication/structs.rs | 0 src/core/communication/traits.rs | 0 src/core/communication/wheel.rs | 5 + src/core/mod.rs | 4 + src/lib.rs | 10 +- 14 files changed, 371 insertions(+), 7 deletions(-) create mode 100644 src/core/communication/drivetrain.rs create mode 100644 src/core/communication/general.rs delete mode 100644 src/core/communication/impls.rs create mode 100644 src/core/communication/motor.rs create mode 100644 src/core/communication/positional.rs create mode 100644 src/core/communication/sensing.rs delete mode 100644 src/core/communication/structs.rs delete mode 100644 src/core/communication/traits.rs create mode 100644 src/core/communication/wheel.rs diff --git a/Cargo.lock b/Cargo.lock index a58eeda..2ce9676 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,74 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rnavp" version = "0.1.0" +dependencies = [ + "serde", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" diff --git a/Cargo.toml b/Cargo.toml index 27556b3..d48a6cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2024" [features] -default= ["host"] +default= ["embassy"] std = [] embassy = ["core_full"] core = [] @@ -16,3 +16,4 @@ host = ["std", "core_full"] [dependencies] +serde = { version = "1.0.228", features = ["derive"], default-features = false } diff --git a/src/core/communication/drivetrain.rs b/src/core/communication/drivetrain.rs new file mode 100644 index 0000000..d205997 --- /dev/null +++ b/src/core/communication/drivetrain.rs @@ -0,0 +1,2 @@ +use serde::{Deserialize, Serialize}; + diff --git a/src/core/communication/general.rs b/src/core/communication/general.rs new file mode 100644 index 0000000..9a69f7d --- /dev/null +++ b/src/core/communication/general.rs @@ -0,0 +1,24 @@ +use serde::{Deserialize, Serialize}; + +///Represents the unit this struct is carrying. Combining the Unit + Trait it impls lets you determine what this message contatains +/// A value of 0 or Unknown means the data is malformed or something is was wrong on the sending side, it should be treated a failure case. +/// If you need to send a unit not listed here please use Unit::Custom instead of Unit::Unknown +#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug, )] +#[repr(u8)] +pub enum Unit { + Unknown = 0, + Meters = 1, + Radians = 2, + Custom = 3, +} + +impl From for Unit { + fn from(value: u8) -> Self { + match value { + 1 => {Self::Meters} + 2 => {Self::Radians} + 3 => {Self::Custom} + _ => {Self::Unknown} + } + } +} diff --git a/src/core/communication/impls.rs b/src/core/communication/impls.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/core/communication/mod.rs b/src/core/communication/mod.rs index 8ee67db..400aa4d 100644 --- a/src/core/communication/mod.rs +++ b/src/core/communication/mod.rs @@ -1,3 +1,6 @@ -pub mod impls; -pub mod structs; -pub mod traits; \ No newline at end of file +pub mod sensing; +pub mod motor; +pub mod positional; +pub mod general; +pub mod drivetrain; +pub mod wheel; \ No newline at end of file diff --git a/src/core/communication/motor.rs b/src/core/communication/motor.rs new file mode 100644 index 0000000..9e01730 --- /dev/null +++ b/src/core/communication/motor.rs @@ -0,0 +1,4 @@ +use serde::{Deserialize, Serialize}; +pub struct MotorRPM{ + +} \ No newline at end of file diff --git a/src/core/communication/positional.rs b/src/core/communication/positional.rs new file mode 100644 index 0000000..468f8e4 --- /dev/null +++ b/src/core/communication/positional.rs @@ -0,0 +1,244 @@ +use core::{fmt::{Display}, u8}; + +use serde::{Deserialize, Serialize}; + +use crate::core::communication::general::Unit; + +///Generic Positional Data Struct, This can contain XYZ positions in meters or Radians +#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug, )] +pub struct XYZPos { + x: Option, + y: Option, + z: Option, + unit: Unit, +} + +///Generic Velocity Data Struct, This can contain XYZ velocities in Meters Per Second or Radians Per Second +#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug, )] +pub struct XYZVel { + x: Option, + y: Option, + z: Option, + unit: Unit, +} + +///Generic Acceleration Data Struct, This can contain XYZ accelerations in Meters Per Second Squared or Radians Per Second +#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug, )] +pub struct XYZAccel{ + x: Option, + y: Option, + z: Option, + unit: Unit, +} + +impl XYZData for XYZPos { + fn x(&self) -> Option { + self.x + } + + fn y(&self) -> Option { + self.y + } + + fn z(&self) -> Option { + self.z + } + + fn unit(&self) -> Unit { + self.unit + } + + fn is_pos(&self) -> bool { + true + } +} + +impl XYZData for XYZVel { + fn x(&self) -> Option { + self.x + } + + fn y(&self) -> Option { + self.y + } + + fn z(&self) -> Option { + self.z + } + + fn unit(&self) -> Unit { + self.unit + } + + fn is_vel(&self) -> bool { + true + } +} + +impl XYZData for XYZAccel { + fn x(&self) -> Option { + self.x + } + + fn y(&self) -> Option { + self.y + } + + fn z(&self) -> Option { + self.z + } + + fn unit(&self) -> Unit { + self.unit + } + + fn is_accel(&self) -> bool { + true + } +} + +impl Position for XYZPos { + +} + +impl Velocity for XYZVel { + +} + +impl Acceleration for XYZAccel { + +} + +impl Display for XYZPos { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let unit_str = match self.unit { + Unit::Meters => {"m"} + Unit::Radians => {"rad"} + Unit::Custom => {"custom"} + _ => {return write!(f, "Unknown Data");} + }; + + write!(f, "[X: {:?}{unit_str} | Y: {:?}{unit_str} | Z: {:?}{unit_str}]", self.x, self.y, self.z) + } +} + +impl Display for XYZVel { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let unit_str = match self.unit { + Unit::Meters => {"m"} + Unit::Radians => {"rad"} + Unit::Custom => {"custom"} + _ => {return write!(f, "Unknown Data");} + }; + + write!(f, "[X: {:?}{unit_str}/s | Y: {:?}{unit_str}/s | Z: {:?}{unit_str}/s]", self.x, self.y, self.z) + } +} + +impl Display for XYZAccel { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let unit_str = match self.unit { + Unit::Meters => {"m"} + Unit::Radians => {"rad"} + Unit::Custom => {"custom"} + _ => {return write!(f, "Unknown Data");} + }; + + write!(f, "[X: {:?}{unit_str}/s^2 | Y: {:?}{unit_str}/s^2 | Z: {:?}{unit_str}/s^2]", self.x, self.y, self.z) + } +} + +///Structs that impl this trait, mean they are positions. Determine what type of position by reading the unit. +trait Position : XYZData { + +} + +///Structs that impl this trait mean they are Velocities. Determine the type of velocity by reading the unit. +trait Velocity : XYZData{ + +} + +///Structs that impl this trait mean they are Accelerations. Determine the type of Acceleration by reading the unit. +trait Acceleration : XYZData{ + +} + +///This is the data backbone for the XYZ Position, Velocity, and Acceleration Data structs. +///They MUST implmenent this so it possible to easily and quickly get the data from the struct. +///Provides methods to check what type this is +trait XYZData { + fn x(&self) -> Option; + fn y(&self) -> Option; + fn z(&self) -> Option; + fn unit(&self) -> Unit; + + ///Returns an array of f32s that are the XYZ position, velocity or accel, based on the implemented traits. + ///Any option field in the array that are None are returned as 0.0 here. + ///Do not forget to check the unit, this may be in radians or meters. + fn xyz_array(&self) -> [f32; 3] { + let arr = [self.x().unwrap_or(0.0), self.y().unwrap_or(0.0), self.z().unwrap_or(0.0)]; + arr + } + + fn is_pos(&self) -> bool { + false + } + fn is_vel(&self) -> bool { + false + } + fn is_accel(&self) -> bool { + false + } +} + + +#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, )] +pub enum XYZDataBucket { + Pos(XYZPos), + Vel(XYZVel), + Accel(XYZAccel), +} + +impl XYZData for XYZDataBucket { + fn is_pos(&self) -> bool { + matches!(self, Self::Pos(_)) + } + fn is_vel(&self) -> bool { + matches!(self, Self::Vel(_)) + } + fn is_accel(&self) -> bool { + matches!(self, Self::Accel(_)) + } + + fn x(&self) -> Option { + match self { + Self::Pos(p) => p.x(), + Self::Vel(v) => v.x(), + Self::Accel(a) => a.x(), + } + } + + fn y(&self) -> Option { + match self { + Self::Pos(p) => p.y(), + Self::Vel(v) => v.y(), + Self::Accel(a) => a.y(), + } + } + + fn z(&self) -> Option { + match self { + Self::Pos(p) => p.z(), + Self::Vel(v) => v.z(), + Self::Accel(a) => a.z(), + } + } + + fn unit(&self) -> Unit { + match self { + Self::Pos(p) => p.unit(), + Self::Vel(v) => v.unit(), + Self::Accel(a) => a.unit(), + } + } +} \ No newline at end of file diff --git a/src/core/communication/sensing.rs b/src/core/communication/sensing.rs new file mode 100644 index 0000000..57da2c6 --- /dev/null +++ b/src/core/communication/sensing.rs @@ -0,0 +1,5 @@ +use serde::{Deserialize, Serialize}; + +pub struct SensorData{ + +} \ No newline at end of file diff --git a/src/core/communication/structs.rs b/src/core/communication/structs.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/core/communication/traits.rs b/src/core/communication/traits.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/core/communication/wheel.rs b/src/core/communication/wheel.rs new file mode 100644 index 0000000..cbd540c --- /dev/null +++ b/src/core/communication/wheel.rs @@ -0,0 +1,5 @@ +use serde::{Deserialize, Serialize}; + +pub struct WheelSpeed{ + +} \ No newline at end of file diff --git a/src/core/mod.rs b/src/core/mod.rs index e0748aa..654adc7 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,2 +1,6 @@ + +#[cfg(feature = "core_communication")] pub mod communication; + +#[cfg(feature = "core_logic")] pub mod logic; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 07e02e3..9cd3fdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,11 @@ #[cfg(feature = "std")] extern crate std; +#[cfg(feature = "core")] +pub mod core; -mod core; -mod embassy; -mod host; +#[cfg(feature = "embassy")] +pub mod embassy; + +#[cfg(feature = "host")] +pub mod host;