cargo fmt

This commit is contained in:
2026-06-07 00:36:22 -05:00
parent 2eae22cb9b
commit 35fd1e8532
10 changed files with 98 additions and 94 deletions
+34 -36
View File
@@ -2,9 +2,10 @@ use core::fmt::Display;
use serde::{Deserialize, Serialize};
use crate::core::{motor::Direction::{CCW, CW}, units::{
Angle, Current, Voltage
}};
use crate::core::{
motor::Direction::{CCW, CW},
units::{Angle, Current, Voltage},
};
///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)]
@@ -12,17 +13,20 @@ pub enum Direction {
///CounterClockWise
CCW,
///ClockWise
CW
CW,
}
impl Direction {
///Inverts the direction currently stored in this.
/// Helps with a simple direction change without knowing the current 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;}
Self::CCW => {
*self = Self::CW;
}
Self::CW => {
*self = Self::CCW;
}
}
}
@@ -30,28 +34,28 @@ impl Direction {
///Recommended to use this over the "From" impl for directions that already exist, to prevent werid 0.0 case and NaN problems
/// This handles 0.0 and NaN by just doing nothing. The "From" impl defualts to CCW for both
pub fn dir_from_f32(&mut self, value: f32) {
if value == 0.0 || value.is_nan() { return; }
if value == 0.0 || value.is_nan() {
return;
}
*self = value.into();
}
///Changes the current direction based on the sign of the int.
///Changes the current direction based on the sign of the int.
///Recommended to use this over the "From" impl for directions that already exist, to prevent weird 0 case problems
///This handles 0 by just doing nothing. The "From" impl defaults to CCW
///This handles 0 by just doing nothing. The "From" impl defaults to CCW
pub fn dir_from_i32(&mut self, value: i32) {
if value == 0 { return; }
if value == 0 {
return;
}
*self = value.into();
}
}
///Converts F32 to direction using the number's sign.
///Converts F32 to direction using the number's sign.
/// This defaults to CCW for 0.0 and NaN.
impl From<f32> for Direction {
fn from(value: f32) -> Self {
if value < 0.0 {
CW
} else {
CCW
}
if value < 0.0 { CW } else { CCW }
}
}
@@ -59,11 +63,7 @@ impl From<f32> for Direction {
/// This defaults to CCW for 0.
impl From<i32> for Direction {
fn from(value: i32) -> Self {
if value < 0 {
CW
} else {
CCW
}
if value < 0 { CW } else { CCW }
}
}
@@ -72,15 +72,15 @@ impl From<i32> for Direction {
///Pay attention to the units of each term
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
pub enum Speed {
///Rotations per minute
///Rotations per minute
RPM(f32),
///Radians per second
RADS(f32),
}
///Used to convert from Rotations per minute to radians per second
///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;
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
@@ -96,7 +96,7 @@ impl Speed {
///Returns the value as Rotations Per minute regardles of what the enum contains
/// Uses no runtime division
#[inline]
pub fn rpm(&self) -> f32{
pub fn rpm(&self) -> f32 {
match *self {
Self::RPM(rpm) => rpm,
Self::RADS(rps) => rps * RPM_TO_RADS,
@@ -110,7 +110,7 @@ impl Display for Speed {
}
}
///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 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
@@ -127,7 +127,7 @@ pub trait Driver {
///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.
/// 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(&mut self, speed: u16);
@@ -138,23 +138,22 @@ pub trait Driver {
fn set_speed_and_direction(&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 regardless.
self.spin(dir);
self.spin(dir);
self.set_speed(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.
/// 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 {
///Returns the 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, being unit agnostic later.
fn get_speed(&self) -> Speed;
@@ -169,5 +168,4 @@ pub trait Sensor {
///Returns the current angle from the sensor if it is capable.
/// This should be non-blocking. Keep a cached value ready.
fn get_angle(&self) -> Option<Angle>;
}