removed all current communication code. Upper level main core code should be made first.

Upper level Core code, will also all be serde compatible anyways.

Added in alot of comments to many methods and structs for core::motors

Added in a Enum for each major data which will also support conversions from and to more useful dataTypes
This commit is contained in:
2026-05-23 20:59:36 -05:00
parent 450ea8da91
commit 0846808a4a
5 changed files with 87 additions and 88 deletions
-2
View File
@@ -1,2 +0,0 @@
use serde::{Deserialize, Serialize};
-69
View File
@@ -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<f32>,
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<i32>,
///Voltage of the motor if readable, reported in microVolts
voltage: Option<i32>,
///Shaft speed of the motor if readable.
shaft_speed: Option<MotorDriverShaftSpeed>,
///Status of the hardware brake if present, none if not present.
brake_status: Option<bool>,
}
-5
View File
@@ -1,5 +0,0 @@
use serde::{Deserialize, Serialize};
pub struct SensorData{
}
-5
View File
@@ -1,5 +0,0 @@
use serde::{Deserialize, Serialize};
pub struct WheelSpeed{
}
+87 -7
View File
@@ -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 { pub enum Direction {
///CounterClockWise
CCW, CCW,
///ClockWise
CW 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 { pub trait Driver {
///Spins in the provided direction ///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 ///This is best not called manually, use set_speed_and_direction_raw instead
fn spin(&mut self, dir: Direction); fn spin(&mut self, dir: Direction);
///Commands the motor to stop ///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); fn stop(&mut self);
///This method uses a full u16 to set the motor speed. This does not change spin direction, if already spinning ///This method uses a full u16 to set the motor speed. This does not change spin direction.
///Uses a number between 0 and 65535 /// 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 ///This is best to not call manually, use set_speed_and_direction_raw instead
fn set_speed_raw(&mut self, speed: u16); 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. ///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) { 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. //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.spin(dir);
self.set_speed_raw(speed); 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 { pub trait Sensor {
fn get_speed(&self) -> f32; ///Gets a speed struct value from the motors sensor
fn get_current(&self) -> Option<u32>; /// This should be non-blocking, there should be some form of cached value ready to go as soon as the method is called
fn get_voltage(&self) -> Option<u32>; /// IF properly returned in the correct Enum Data field, all other systems will use the value propely
fn get_angle(&self) -> Option<f32>; fn get_speed(&self) -> Speed;
fn get_current(&self) -> Option<Current>;
fn get_voltage(&self) -> Option<Voltage>;
fn get_angle(&self) -> Option<Angle>;
} }