Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
refactor: Direction enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Tropix126 committed Mar 6, 2024
1 parent a88c9c4 commit a70fa58
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions packages/pros-devices/src/smart/motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,45 @@ pub enum MotorControl {
/// Motor is braking using a [`BrakeMode`].
Brake(BrakeMode),

/// Motor is attempting to hold a velocity using internal PID control.
Velocity(i32),

/// Motor is outputting a raw voltage.
Voltage(f64),

/// Motor is attempting to reach a position.
/// Motor is attempting to hold a velocity using internal PID control.
Velocity(i32),

/// Motor is attempting to reach a position using internal PID control.
Position(Position, i32),
}

/// Represents a possible direction that a motor can be configured as.
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
pub enum Direction {
/// Motor rotates in the forward direction.
#[default]
Forward,

/// Motor rotates in the reverse direction.
Reverse
}

impl Direction {
/// Returns `true` if the level is [`Forward`].
pub const fn is_forward(&self) -> bool {
match self {
Self::Forward => true,
Self::Reverse => false,
}
}

/// Returns `true` if the level is [`Reverse`].
pub const fn is_reverse(&self) -> bool {
match self {
Self::Forward => false,
Self::Reverse => true,
}
}
}

impl Motor {
/// The maximum voltage value that can be sent to a [`Motor`].
pub const MAX_VOLTAGE: f64 = 12.0;
Expand All @@ -44,7 +73,7 @@ impl Motor {
pub const DATA_WRITE_RATE: Duration = Duration::from_millis(5);

/// Create a new motor from a smart port index.
pub fn new(port: SmartPort, gearset: Gearset, reversed: bool) -> Result<Self, MotorError> {
pub fn new(port: SmartPort, gearset: Gearset, direction: Direction) -> Result<Self, MotorError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::motor_set_encoder_units(port.index(), pros_sys::E_MOTOR_ENCODER_DEGREES)
});
Expand All @@ -55,7 +84,7 @@ impl Motor {
};

motor.set_gearset(gearset)?;
motor.set_reversed(reversed)?;
motor.set_direction(direction)?;

Ok(motor)
}
Expand Down Expand Up @@ -146,7 +175,7 @@ impl Motor {
Ok(())
}

/// Get the current [`MotorControl`] value that the motor is attempting to reach.
/// Get the current [`MotorControl`] value that the motor is attempting to use.
pub fn target(&self) -> MotorControl {
self.target
}
Expand Down Expand Up @@ -335,19 +364,24 @@ impl Motor {
Ok(self.faults()?.contains(MotorFaults::OVER_CURRENT))
}

/// Set whether or not this motor's ouput should be reversed.
pub fn set_reversed(&mut self, reversed: bool) -> Result<(), MotorError> {
/// Set the [`Direction`] of this motor.
pub fn set_direction(&mut self, direction: Direction) -> Result<(), MotorError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::motor_set_reversed(self.port.index(), reversed)
pros_sys::motor_set_reversed(self.port.index(), direction.is_reverse())
});
Ok(())
}

/// Check if this motor has been reversed.
pub fn is_reversed(&self) -> Result<bool, MotorError> {
Ok(bail_on!(PROS_ERR, unsafe {
/// Get the [`Direction`] of this motor.
pub fn direction(&self) -> Result<Direction, MotorError> {
let reversed = bail_on!(PROS_ERR, unsafe {
pros_sys::motor_is_reversed(self.port.index())
}) == 1)
}) == 1;

Ok(match reversed {
false => Direction::Forward,
true => Direction::Reverse,
})
}

/// Adjusts the internal tuning constants of the motor when using velocity control.
Expand Down

0 comments on commit a70fa58

Please sign in to comment.