ev3_drivebase/drivebase/
drive.rs

1use super::DriveBase;
2use crate::Direction;
3use ev3dev_lang_rust::Ev3Error;
4
5impl DriveBase {
6    /// Drives the robot forward or backward at the specified speed.
7    ///
8    /// # Parameters
9    ///
10    /// - `speed`: The speed at which to drive the robot, in tacho counts per second (positive for forward, negative for backward).
11    /// - `distance`: Optional distance to drive, in millimeters.
12    ///     - If `Some(mm)`, the robot drives that distance and then stops.
13    ///     - If `None`, the robot will drive indefinitely until another command stops it.
14    ///
15    /// # Errors
16    ///
17    /// Returns an `Ev3Error` if the motor cannot be started or a command fails.
18    ///
19    /// # Example
20    ///
21    /// Drive forward 500 mm at speed 200:
22    /// ```rust
23    /// robot.drive(200, 500)?;
24    /// ```
25    pub fn drive(
26        &mut self,
27        speed: i32,
28        distance: impl Into<i32>,
29        stop: bool,
30    ) -> Result<&Self, Ev3Error> {
31        let distance: i32 = distance.into();
32        if distance == 0 {
33            return Ok(self);
34        }
35
36        let direction = if distance < 0 {
37            Direction::CounterClockwise
38        } else {
39            Direction::Clockwise
40        };
41
42        self.set_speed(speed, speed)?;
43
44        let mut counts = self.calculate_counts(distance, distance)?;
45
46        counts.0 = counts.0.saturating_mul(direction.sign());
47        counts.1 = counts.1.saturating_mul(direction.sign());
48
49        self.run_to_rel_pos(counts.0, counts.1)?;
50        self.wait_until_not_moving(None);
51
52        if !stop {
53            self.run_forever()?;
54        }
55
56        Ok(self)
57    }
58}