ev3_drivebase/drivebase/
ramping.rs

1use super::DriveBase;
2use ev3dev_lang_rust::Ev3Error;
3
4impl DriveBase {
5    /// Sets the acceleration rate for both motors.
6    ///
7    /// Acceleration determines how quickly the motors ramp up to the target speed.
8    /// Higher values result in faster acceleration but may cause wheel slipping or
9    /// mechanical stress. Lower values provide smoother, more controlled starts.
10    ///
11    /// # Parameters
12    ///
13    /// - `acceleration`: The acceleration rate in degrees per second squared (deg/s²)
14    ///
15    /// # Errors
16    ///
17    /// Returns an error if:
18    /// - The acceleration value is negative
19    /// - The value cannot be set on either motor
20    ///
21    /// # Examples
22    ///
23    /// ```no_run
24    /// use ev3_drivebase::{DriveBase, Motor, Direction};
25    /// use ev3_drivebase::ev3dev_lang_rust::{Ev3Error, motors::MotorPort};
26    ///
27    /// fn main() -> Result<(), Ev3Error> {
28    ///     let left = Motor::new(MotorPort::OutA, Direction::Clockwise);
29    ///     let right = Motor::new(MotorPort::OutB, Direction::CounterClockwise);
30    ///     let drivebase = DriveBase::new(left, right, 43.2, 185.0)?;
31    ///
32    ///     // Set moderate acceleration for smooth starts
33    ///     drivebase.set_acceleration(2000)?;
34    ///
35    ///     // Set high acceleration for quick response
36    ///     drivebase.set_acceleration(5000)?;
37    ///
38    ///     Ok(())
39    /// }
40    /// ```
41    pub fn set_acceleration(&self, acceleration: i32) -> Result<&Self, Ev3Error> {
42        self.left.set_ramp_up_sp(acceleration)?;
43        self.right.set_ramp_up_sp(acceleration)?;
44        Ok(self)
45    }
46
47    /// Sets the deceleration rate for both motors.
48    ///
49    /// Deceleration determines how quickly the motors slow down when stopping.
50    /// Higher values result in faster stops but may cause the robot to jerk or tip.
51    /// Lower values provide smoother, more controlled stops.
52    ///
53    /// # Parameters
54    ///
55    /// - `deceleration`: The deceleration rate in degrees per second squared (deg/s²)
56    ///
57    /// # Errors
58    ///
59    /// Returns an error if:
60    /// - The deceleration value is negative
61    /// - The value cannot be set on either motor
62    ///
63    /// # Examples
64    ///
65    /// ```no_run
66    /// use ev3_drivebase::{DriveBase, Motor, Direction};
67    /// use ev3_drivebase::ev3dev_lang_rust::{Ev3Error, motors::MotorPort};
68    ///
69    /// fn main() -> Result<(), Ev3Error> {
70    ///     let left = Motor::new(MotorPort::OutA, Direction::Clockwise);
71    ///     let right = Motor::new(MotorPort::OutB, Direction::CounterClockwise);
72    ///     let drivebase = DriveBase::new(left, right, 43.2, 185.0)?;
73    ///
74    ///     // Set moderate deceleration for smooth stops
75    ///     drivebase.set_deceleration(2000)?;
76    ///
77    ///     // Set high deceleration for quick stops
78    ///     drivebase.set_deceleration(5000)?;
79    ///
80    ///     Ok(())
81    /// }
82    /// ```
83    ///
84    /// # Note
85    ///
86    /// Deceleration works in combination with the brake mode:
87    /// - With `BrakeMode::Coast`: Deceleration has limited effect
88    /// - With `BrakeMode::Brake` or `BrakeMode::Hold`: Full effect
89    pub fn set_deceleration(&self, deceleration: i32) -> Result<&Self, Ev3Error> {
90        self.left.set_ramp_down_sp(deceleration)?;
91        self.right.set_ramp_down_sp(deceleration)?;
92        Ok(self)
93    }
94}