From 1e5f8c15e4f41558747c4aa25a31b5bd5dd4e74b Mon Sep 17 00:00:00 2001 From: melfey Date: Sun, 24 May 2026 10:34:40 -0500 Subject: [PATCH] created a generic "units" module, which should be helpful in keeping certain units Correct. --- src/core/mod.rs | 3 ++- src/core/motor.rs | 39 +++++++++++++++++++++++++-------------- src/core/positional.rs | 6 +++--- src/core/units.rs | 14 ++++++++++++++ 4 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 src/core/units.rs diff --git a/src/core/mod.rs b/src/core/mod.rs index 2a97f19..1ac48bf 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -6,4 +6,5 @@ pub mod communication; pub mod logic; pub mod positional; -pub mod motor; \ No newline at end of file +pub mod motor; +pub mod units; \ No newline at end of file diff --git a/src/core/motor.rs b/src/core/motor.rs index 77d9da4..5ddfb06 100644 --- a/src/core/motor.rs +++ b/src/core/motor.rs @@ -1,5 +1,13 @@ +use core::fmt::Display; + use serde::{Deserialize, Serialize}; +use crate::core::units::{ + Current, + Voltage, + Angle, +}; + ///A simple enum for Counterclockwise or Clockwise direction semantics. Makes it easier much clear than a true/false bool #[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum Direction { @@ -10,6 +18,19 @@ pub enum Direction { } +impl Direction { + ///Inverts the direction currently stored in this. + /// Helps with a simple direction change without knowing the current direction. + pub fn inv_dir(&mut self) { + match *self { + Self::CCW => {*self = Self::CW;} + Self::CW => {*self = Self::CCW;} + } + } + + +} + ///A enum for containerizing speeds easily /// Lets the backend take in a Rotations per minute value from a user Sensor implementation and convert it to a Radians per second value ///Pay attention to the units of each term @@ -47,19 +68,10 @@ impl Speed { } } -pub enum Current { - MilliAmps(u32), - MicroAmps(u32), -} - -pub enum Voltage { - MilliVolts(u32), - MicroVolts(u32), -} - -pub enum Angle { - Degrees(f32), - Radians(f32), +impl Display for Speed { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "") + } } ///This trait is implemented on a motor struct to implement the motor DRIVER (like an ESC) capabalities in how it should control the motor. @@ -109,7 +121,6 @@ pub trait Sensor { /// IF properly returned in the correct Enum Data field, all other systems will use the value propely fn get_speed(&self) -> Speed; - fn get_current(&self) -> Option; fn get_voltage(&self) -> Option; fn get_angle(&self) -> Option; diff --git a/src/core/positional.rs b/src/core/positional.rs index 5edc6c3..83e4a43 100644 --- a/src/core/positional.rs +++ b/src/core/positional.rs @@ -242,7 +242,7 @@ impl XYZData for XYZDataBucket { /// If you need to send a unit not listed here please use Unit::Custom #[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug, )] pub enum Unit { - Meters = 1, - Radians = 2, - Custom = 3, + Meters, + Radians, + Custom, } diff --git a/src/core/units.rs b/src/core/units.rs new file mode 100644 index 0000000..7de34d2 --- /dev/null +++ b/src/core/units.rs @@ -0,0 +1,14 @@ +pub enum Current { + MilliAmps(u32), + MicroAmps(u32), +} + +pub enum Voltage { + MilliVolts(u32), + MicroVolts(u32), +} + +pub enum Angle { + Degrees(f32), + Radians(f32), +} \ No newline at end of file