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
+1
View File
@@ -0,0 +1 @@
+3 -3
View File
@@ -1,4 +1,4 @@
pub mod sensing;
pub mod motor;
pub mod drivetrain;
pub mod wheel;
pub mod motor;
pub mod sensing;
pub mod wheel;
+1
View File
@@ -0,0 +1 @@
+1
View File
@@ -0,0 +1 @@
+1
View File
@@ -0,0 +1 @@
+2 -3
View File
@@ -1,10 +1,9 @@
#[cfg(feature = "core_communication")]
pub mod communication;
#[cfg(feature = "core_logic")]
pub mod logic;
pub mod positional;
pub mod motor;
pub mod units;
pub mod positional;
pub mod units;
+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>;
}
+44 -42
View File
@@ -1,9 +1,9 @@
use core::{fmt::{Display}};
use core::fmt::Display;
use serde::{Deserialize, Serialize};
///Generic Positional Data Struct, This can contain XYZ positions in meters or Radians
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug, )]
///Generic Positional Data Struct, This can contain XYZ positions in meters or Radians
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug)]
pub struct XYZPos {
x: Option<f32>,
y: Option<f32>,
@@ -12,7 +12,7 @@ pub struct XYZPos {
}
///Generic Velocity Data Struct, This can contain XYZ velocities in Meters Per Second or Radians Per Second
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug, )]
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug)]
pub struct XYZVel {
x: Option<f32>,
y: Option<f32>,
@@ -21,8 +21,8 @@ pub struct XYZVel {
}
///Generic Acceleration Data Struct, This can contain XYZ accelerations in Meters Per Second Squared or Radians Per Second
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug, )]
pub struct XYZAccel{
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug)]
pub struct XYZAccel {
x: Option<f32>,
y: Option<f32>,
z: Option<f32>,
@@ -95,70 +95,70 @@ impl XYZData for XYZAccel {
}
}
impl Position for XYZPos {
impl Position for XYZPos {}
}
impl Velocity for XYZVel {}
impl Velocity for XYZVel {
}
impl Acceleration for XYZAccel {
}
impl Acceleration for XYZAccel {}
impl Display for XYZPos {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let unit_str = match self.unit {
Unit::Meters => {"m"}
Unit::Radians => {"rad"}
Unit::Custom => {"custom"}
Unit::Meters => "m",
Unit::Radians => "rad",
Unit::Custom => "custom",
};
write!(f, "[X: {:?}{unit_str} | Y: {:?}{unit_str} | Z: {:?}{unit_str}]", self.x, self.y, self.z)
write!(
f,
"[X: {:?}{unit_str} | Y: {:?}{unit_str} | Z: {:?}{unit_str}]",
self.x, self.y, self.z
)
}
}
impl Display for XYZVel {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let unit_str = match self.unit {
Unit::Meters => {"m"}
Unit::Radians => {"rad"}
Unit::Custom => {"custom"}
Unit::Meters => "m",
Unit::Radians => "rad",
Unit::Custom => "custom",
};
write!(f, "[X: {:?}{unit_str}/s | Y: {:?}{unit_str}/s | Z: {:?}{unit_str}/s]", self.x, self.y, self.z)
write!(
f,
"[X: {:?}{unit_str}/s | Y: {:?}{unit_str}/s | Z: {:?}{unit_str}/s]",
self.x, self.y, self.z
)
}
}
impl Display for XYZAccel {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let unit_str = match self.unit {
Unit::Meters => {"m"}
Unit::Radians => {"rad"}
Unit::Custom => {"custom"}
Unit::Meters => "m",
Unit::Radians => "rad",
Unit::Custom => "custom",
};
write!(f, "[X: {:?}{unit_str}/s^2 | Y: {:?}{unit_str}/s^2 | Z: {:?}{unit_str}/s^2]", self.x, self.y, self.z)
write!(
f,
"[X: {:?}{unit_str}/s^2 | Y: {:?}{unit_str}/s^2 | Z: {:?}{unit_str}/s^2]",
self.x, self.y, self.z
)
}
}
///Structs that impl this trait, mean they are positions. Determine what type of position by reading the unit.
pub trait Position : XYZData {
}
pub trait Position: XYZData {}
///Structs that impl this trait mean they are Velocities. Determine what type of velocity by reading the unit.
pub trait Velocity : XYZData{
}
pub trait Velocity: XYZData {}
///Structs that impl this trait mean they are Accelerations. Determine what type of Acceleration by reading the unit.
pub trait Acceleration : XYZData{
}
pub trait Acceleration: XYZData {}
///This is the data backbone for the XYZ Position, Velocity, and Acceleration Data structs.
///This is the data backbone for the XYZ Position, Velocity, and Acceleration Data structs.
///They MUST implmenent this so it possible to easily and quickly get the data from the struct.
///Provides methods to check what type this is
pub trait XYZData {
@@ -171,7 +171,11 @@ pub trait XYZData {
///Any option field in the array that are None are returned as 0.0 here.
///Do not forget to check the unit, this may be in radians or meters.
fn xyz_array(&self) -> [f32; 3] {
[self.x().unwrap_or(0.0), self.y().unwrap_or(0.0), self.z().unwrap_or(0.0)]
[
self.x().unwrap_or(0.0),
self.y().unwrap_or(0.0),
self.z().unwrap_or(0.0),
]
}
fn is_pos(&self) -> bool {
@@ -185,8 +189,7 @@ pub trait XYZData {
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, )]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub enum XYZDataBucket {
Pos(XYZPos),
Vel(XYZVel),
@@ -237,10 +240,9 @@ impl XYZData for XYZDataBucket {
}
}
///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, )]
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
pub enum Unit {
Meters,
Radians,
+10 -10
View File
@@ -11,7 +11,7 @@ pub const FROM_MILLI: f32 = 1.0 / 1_000.0;
///Standard way for the RNavP system to transfer around Current measurements.
/// This system by default uses Amps as f32.
/// Offers MilliAmps and MicroAmps for utilizing higher precision raw data for when needed.
/// Offers MilliAmps and MicroAmps for utilizing higher precision raw data for when needed.
/// Implements all needed conversions for handling f32 into and from this data type.
/// Uses Amps by default, please use MicroAmps and MilliVolts when needed for higher precision.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -23,22 +23,22 @@ pub enum Current {
impl From<f32> for Current {
fn from(value: f32) -> Self {
Self::Amps(value)
Self::Amps(value)
}
}
impl Into<f32> for Current {
fn into(self) -> f32 {
match self {
Self::MicroAmps(micro) => {micro as f32 * FROM_MICRO}
Self::MilliAmps(milli) => {milli as f32 * FROM_MILLI}
Self::Amps(amps) => {amps}
Self::MicroAmps(micro) => micro as f32 * FROM_MICRO,
Self::MilliAmps(milli) => milli as f32 * FROM_MILLI,
Self::Amps(amps) => amps,
}
}
}
///Standard way for the RNavP system to transfer around voltage measurements.
/// This system by default uses Volts as f32.
/// This system by default uses Volts as f32.
/// Offers MilliVolts and MicroVolts for utilizing higher precison raw data for when needed.
/// Implements all needed conversions for handling f32 into and from this dataType.
/// Uses Volts by default, please use MicroVolts and MilliVolts when needed for higher precision.
@@ -58,9 +58,9 @@ impl From<f32> for Voltage {
impl Into<f32> for Voltage {
fn into(self) -> f32 {
match self {
Self::MicroVolts(micro) => {micro as f32 * FROM_MICRO}
Self::MilliVolts(milli) => {milli as f32 * FROM_MILLI}
Self::Volts(volts) => {volts}
Self::MicroVolts(micro) => micro as f32 * FROM_MICRO,
Self::MilliVolts(milli) => milli as f32 * FROM_MILLI,
Self::Volts(volts) => volts,
}
}
}
@@ -72,4 +72,4 @@ impl Into<f32> for Voltage {
pub enum Angle {
Degrees(f32),
Radians(f32),
}
}
+1
View File
@@ -0,0 +1 @@