diff --git a/src/core/communication/drivetrain.rs b/src/core/communication/drivetrain.rs index d205997..e69de29 100644 --- a/src/core/communication/drivetrain.rs +++ b/src/core/communication/drivetrain.rs @@ -1,2 +0,0 @@ -use serde::{Deserialize, Serialize}; - diff --git a/src/core/communication/motor.rs b/src/core/communication/motor.rs index f15bf01..e69de29 100644 --- a/src/core/communication/motor.rs +++ b/src/core/communication/motor.rs @@ -1,69 +0,0 @@ -use core::fmt::Display; - -use serde::{Deserialize, Serialize}; - -///Motor Drivers reported shaft speed. This can be in any of the given unit types by the Unit enum and timescalse given by the TimeScales enum -#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] -pub struct MotorDriverShaftSpeed { - shaft_speed: Option, - unit: Unit, - time_scale: TimeScale, -} - -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) => { - match self.unit { - Unit::Radians => {speed /= core::f32::consts::PI * 2.0;} - _ => {} - } - - match self.time_scale { - TimeScale::PerSecond => {speed *= 60.0;} - _ => {} - } - speed - } - None => { - 0.0 - } - } - } -} - -impl Display for MotorDriverShaftSpeed { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "Motor Shaft Speed: {}", self.speed_as_rpm()) - } -} - -#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -pub enum Unit { - Rotations, - Radians, - -} - -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] -pub enum TimeScale { - PerSecond, - PerMinute, - -} - -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] -pub struct MotorDriverDiagData { - ///Current draw of the motor if readable, reported in microAmps - current: Option, - ///Voltage of the motor if readable, reported in microVolts - voltage: Option, - ///Shaft speed of the motor if readable. - shaft_speed: Option, - ///Status of the hardware brake if present, none if not present. - brake_status: Option, - -} - diff --git a/src/core/communication/sensing.rs b/src/core/communication/sensing.rs index 57da2c6..e69de29 100644 --- a/src/core/communication/sensing.rs +++ b/src/core/communication/sensing.rs @@ -1,5 +0,0 @@ -use serde::{Deserialize, Serialize}; - -pub struct SensorData{ - -} \ No newline at end of file diff --git a/src/core/communication/wheel.rs b/src/core/communication/wheel.rs index cbd540c..e69de29 100644 --- a/src/core/communication/wheel.rs +++ b/src/core/communication/wheel.rs @@ -1,5 +0,0 @@ -use serde::{Deserialize, Serialize}; - -pub struct WheelSpeed{ - -} \ No newline at end of file diff --git a/src/core/motor.rs b/src/core/motor.rs index d12348c..77d9da4 100644 --- a/src/core/motor.rs +++ b/src/core/motor.rs @@ -1,18 +1,86 @@ +use serde::{Deserialize, Serialize}; + +///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 { + ///CounterClockWise CCW, + ///ClockWise CW + } +///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 +#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] +pub enum Speed { + ///Rotations per minute + RPM(f32), + ///Radians per second + RADS(f32), +} +///Used to convert from Rotations per minute to radians per second +const RPM_TO_RADS: f32 = core::f32::consts::TAU / 60.0; +///Used to convert from Radians per second to Rotations per minute +const RADS_TO_RPM: f32 = 60.0 / core::f32::consts::TAU; + +impl Speed { + ///Returns the value as Radians Per Second regardless of what the enum contains + /// Uses no runtime division + #[inline] + pub fn rads(&self) -> f32 { + match *self { + Self::RPM(rpm) => rpm * RADS_TO_RPM, + Self::RADS(rps) => rps, + } + } + + ///Returns the value as Rotations Per minute regardles of what the enum contains + /// Uses no runtime division + #[inline] + pub fn rpm(&self) -> f32{ + match *self { + Self::RPM(rpm) => rpm, + Self::RADS(rps) => rps * RPM_TO_RADS, + } + } +} + +pub enum Current { + MilliAmps(u32), + MicroAmps(u32), +} + +pub enum Voltage { + MilliVolts(u32), + MicroVolts(u32), +} + +pub enum Angle { + Degrees(f32), + Radians(f32), +} + +///This trait is implemented on a motor struct to implement the motor DRIVER (like an ESC) capabalities in how it should control the motor. +/// This will then let other systems use this generic trait and system to control your motor without needing to understand the hardware as much. pub trait Driver { ///Spins in the provided direction + /// Properly map each direction to CounterClockWise and Clockwise when LOOKING AT THE MOTOR SHAFT. This is VITAL for the way the systems within core::logic will work. ///This is best not called manually, use set_speed_and_direction_raw instead fn spin(&mut self, dir: Direction); ///Commands the motor to stop + /// This should stop the motor in what ever way you see fit. + /// Some hardware may have a proper "Stop" or "Off" Signal, some will not. + /// If there is no proper hardware stop, verify that 0 speed is actually a stopping command and you can easily just set speed to 0 via set_speed_raw in this method. + /// No default implementation (though this is easily something I could have done) because it SHOULD be something you notice and implement PROPERLY if applicable 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 method uses a full u16 to set the motor speed. This does not change spin direction. + /// You need to map 0 to 65535 properly to your specific hardware range, just because PWM is commonly 0 to 65535 do not assume that ALL ways of controlling a motor cleanly takes a value of 0 to 65535, + /// you need to verify and map properly. + /// 65535 should be the max possible speed, while 0 should be stopped. ///This is best to not call manually, use set_speed_and_direction_raw instead fn set_speed_raw(&mut self, speed: u16); @@ -21,17 +89,29 @@ pub trait Driver { ///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. + //Most MCUs should execute these two lines of code so fast, that it should be neglible regardless. self.spin(dir); self.set_speed_raw(speed); } } +///This trait is implemented on motors that have some form of sensory feedback. Note that this does not require the motor ITSELF provide feedback, just that the setup the motor is in supports it. IE, a Current draw based ESC +/// Speed Feedback is REQUIRED, if your device does not provide speed feedback directly, +/// Implement the other conditions and use them to then implement the speed method. +/// +/// Some motors/sensors might have a direct "Current Angle" being reported, the option is there in case yours can, so the communication system can sync it to the host for you. +/// Some sensors may also provide current and voltage feedback, if implemented the communication system can sync them to the host you for. +/// Can also be used for other custom logic. They are unused by RNavP, entirely optional. pub trait Sensor { - fn get_speed(&self) -> f32; - fn get_current(&self) -> Option; - fn get_voltage(&self) -> Option; - fn get_angle(&self) -> Option; + ///Gets a speed struct value from the motors sensor + /// This should be non-blocking, there should be some form of cached value ready to go as soon as the method is called + /// 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; }