diff --git a/src/core/communication/motor.rs b/src/core/communication/motor.rs index d1236a1..f15bf01 100644 --- a/src/core/communication/motor.rs +++ b/src/core/communication/motor.rs @@ -11,6 +11,8 @@ pub struct MotorDriverShaftSpeed { } impl MotorDriverShaftSpeed { + ///This returns the current shaft speed as an RPM Value, which is the default for display. + /// It will be 0.0 if the data is none pub fn speed_as_rpm(&self) -> f32 { match self.shaft_speed { Some(mut speed) => { @@ -34,37 +36,21 @@ impl MotorDriverShaftSpeed { impl Display for MotorDriverShaftSpeed { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self.shaft_speed { - Some(speed) => { - let unit = match self.unit { - Unit::Radians => "rad", - Unit::Rotations => "R", - _ => "", - }; - - - write!(f, "") - } - None => {write!(f, "No Given Speeds")} - } + write!(f, "Motor Shaft Speed: {}", self.speed_as_rpm()) } } #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -#[repr(u8)] pub enum Unit { - Unknown = 0, - Rotations = 1, - Radians = 2, + Rotations, + Radians, } #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] -#[repr(u8)] pub enum TimeScale { - Unknown = 0, - PerSecond = 1, - PerMinute = 2, + PerSecond, + PerMinute, } diff --git a/src/core/mod.rs b/src/core/mod.rs index 8feb5d7..2a97f19 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -5,4 +5,5 @@ pub mod communication; #[cfg(feature = "core_logic")] pub mod logic; -pub mod positional; \ No newline at end of file +pub mod positional; +pub mod motor; \ No newline at end of file diff --git a/src/core/motor.rs b/src/core/motor.rs new file mode 100644 index 0000000..d12348c --- /dev/null +++ b/src/core/motor.rs @@ -0,0 +1,37 @@ +pub enum Direction { + CCW, + CW +} + +pub trait Driver { + ///Spins in the provided direction + ///This is best not called manually, use set_speed_and_direction_raw instead + fn spin(&mut self, dir: Direction); + + ///Commands the motor to stop + fn stop(&mut self); + + ///This method uses a full u16 to set the motor speed. This does not change spin direction, if already spinning + ///Uses a number between 0 and 65535 + ///This is best to not call manually, use set_speed_and_direction_raw instead + fn set_speed_raw(&mut self, speed: u16); + + ///Commands the motors to use the speed and spin in the same command. + ///Uses a number between 0 and 65535 + ///This should be used in most cases, unless the motor has the core::logic::motor::controller Trait, then the the methods from that should be used instead. + fn set_speed_and_direction_raw(&mut self, speed: u16, dir: Direction) { + //Set direction before speed, so it won't start spinning in one direction, then snap to the other. + //Most MCUs should execute these two lines of code so fast, that it should be neglible though. + self.spin(dir); + self.set_speed_raw(speed); + } + +} + +pub trait Sensor { + fn get_speed(&self) -> f32; + 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 bb414c5..5edc6c3 100644 --- a/src/core/positional.rs +++ b/src/core/positional.rs @@ -113,7 +113,6 @@ impl Display for XYZPos { 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) @@ -126,7 +125,6 @@ impl Display for XYZVel { 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) @@ -139,7 +137,6 @@ impl Display for XYZAccel { 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) @@ -241,25 +238,11 @@ impl XYZData for XYZDataBucket { } -///Represents the unit this position 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 +///Represents the unit this position struct is carrying. Combining the Unit + Trait it impls lets you determine what this message contains +/// If you need to send a unit not listed here please use Unit::Custom #[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/embassy/mod.rs b/src/embassy/mod.rs index b7aeffb..b74a073 100644 --- a/src/embassy/mod.rs +++ b/src/embassy/mod.rs @@ -5,4 +5,3 @@ compile_error!( Please use '--no-default-features --features embassy' to build for MCU\n\ or verify that you have not included a different feature that requires'std'" ); -