ev3_drivebase/motor.rs
1//! Motor configuration and direction types for the drivebase.
2
3use ev3dev_lang_rust::motors::MotorPort;
4
5/// Required metadata for each motor of the drivebase.
6///
7/// This struct holds the physical configuration of a motor, including which port
8/// it's connected to and which direction it needs to turn to make the robot move forward.
9///
10/// # Examples
11///
12/// ```
13/// use ev3_drivebase::{Motor, Direction};
14/// use ev3_drivebase::ev3dev_lang_rust::motors::MotorPort;
15///
16/// // Left motor with shaft pointing forward
17/// let left = Motor::new(MotorPort::OutA, Direction::Clockwise);
18///
19/// // Right motor with shaft pointing backward
20/// let right = Motor::new(MotorPort::OutB, Direction::CounterClockwise);
21/// ```
22#[derive(Debug, Clone, Copy)]
23pub struct Motor {
24 /// The port of the motor (`OutA`, `OutB`, `OutC`, or `OutD`).
25 pub port: MotorPort,
26 /// The direction the motor needs to turn to make the robot go forward.
27 pub direction: Direction,
28}
29
30impl Motor {
31 /// Creates a new `Motor` configuration.
32 ///
33 /// # Parameters
34 ///
35 /// - `port`: The EV3 output port this motor is connected to
36 /// - `direction`: The direction this motor should turn to move the robot forward
37 ///
38 /// # Examples
39 ///
40 /// ```
41 /// use ev3_drivebase::{Motor, Direction};
42 /// use ev3_drivebase::ev3dev_lang_rust::motors::MotorPort;
43 ///
44 /// let motor = Motor::new(MotorPort::OutA, Direction::Clockwise);
45 /// ```
46 #[must_use]
47 pub const fn new(port: MotorPort, direction: Direction) -> Self {
48 Self { port, direction }
49 }
50}
51
52#[expect(clippy::doc_markdown)]
53/// The direction a motor needs to turn to make the robot move forward.
54///
55/// This depends on how the motor is physically mounted on your robot:
56///
57/// - **Clockwise**: Use this if the motor shaft points toward the front of the robot
58/// - **CounterClockwise**: Use this if the motor shaft points toward the back of the robot
59///
60/// # Examples
61///
62/// For a typical two-wheeled robot with motors on opposite sides:
63///
64/// ```
65/// use ev3_drivebase::{Motor, Direction};
66/// use ev3_drivebase::ev3dev_lang_rust::motors::MotorPort;
67///
68/// // Left motor (shaft points forward)
69/// let left = Motor::new(MotorPort::OutA, Direction::Clockwise);
70///
71/// // Right motor (shaft points backward due to mirrored mounting)
72/// let right = Motor::new(MotorPort::OutB, Direction::CounterClockwise);
73/// ```
74#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
75#[repr(i8)]
76pub enum Direction {
77 /// Motor needs to turn clockwise to move the robot forward.
78 Clockwise = 1,
79 /// Motor needs to turn counter-clockwise to move the robot forward.
80 CounterClockwise = -1,
81}
82
83impl Direction {
84 /// Returns the sign multiplier for this direction.
85 ///
86 /// - `Clockwise` returns `1`
87 /// - `CounterClockwise` returns `-1`
88 ///
89 /// This is used internally for calculating motor encoder counts.
90 ///
91 /// # Examples
92 ///
93 /// ```
94 /// use ev3_drivebase::Direction;
95 ///
96 /// assert_eq!(Direction::Clockwise.sign(), 1);
97 /// assert_eq!(Direction::CounterClockwise.sign(), -1);
98 /// ```
99 #[inline]
100 #[must_use]
101 pub const fn sign(self) -> i32 {
102 self as i8 as i32
103 }
104}