created a generic "units" module, which should be helpful in keeping certain units Correct.

This commit is contained in:
2026-05-24 10:34:40 -05:00
parent 0846808a4a
commit 1e5f8c15e4
4 changed files with 44 additions and 18 deletions
+2 -1
View File
@@ -6,4 +6,5 @@ pub mod communication;
pub mod logic;
pub mod positional;
pub mod motor;
pub mod motor;
pub mod units;
+25 -14
View File
@@ -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<Current>;
fn get_voltage(&self) -> Option<Voltage>;
fn get_angle(&self) -> Option<Angle>;
+3 -3
View File
@@ -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,
}
+14
View File
@@ -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),
}