ev3_drivebase/
drivebase.rs

1mod brake_mode;
2mod drive;
3mod ramping;
4mod run;
5mod speed;
6mod turn;
7mod utils;
8mod wait;
9
10pub use brake_mode::BrakeMode;
11
12use crate::Motor;
13use ev3dev_lang_rust::{Ev3Error, motors::TachoMotor, sensors::ColorSensor};
14use std::f64::consts::PI;
15
16/// The `DriveBase` struct which holds all the needed fields
17#[derive(Debug, Clone)]
18pub struct DriveBase {
19    /// The left motor of the `DriveBase`
20    pub left: TachoMotor,
21    /// The right motor of the `DriveBase`
22    pub right: TachoMotor,
23    /// Current speed of the robot
24    pub current_speed: i32,
25    /// left color sensor if specified, useful for line following methods
26    pub left_sensor: Option<ColorSensor>,
27    /// right color sensor if specified, useful for line following methods
28    pub right_sensor: Option<ColorSensor>,
29    /// Metadata of the left motor
30    pub left_meta: Motor,
31    /// Metadata of the right motor
32    pub right_meta: Motor,
33    /// The circumference of the wheels in mm
34    pub circumference: f64,
35    /// The distance between the points where both wheels touch the ground.
36    pub axle_track: f64,
37}
38
39impl DriveBase {
40    /// Creates a new `DriveBase` using the provided `Motor` structs for the left and right motor.
41    ///
42    /// # Errors
43    ///
44    /// Errors if the port is not used or used by another device.
45    pub fn new(
46        left_meta: Motor,
47        right_meta: Motor,
48        wheel_diameter: f64,
49        axle_track: f64,
50    ) -> Result<Self, Ev3Error> {
51        let left = TachoMotor::get(left_meta.port)?;
52        let right = TachoMotor::get(right_meta.port)?;
53        let circumference = PI * wheel_diameter;
54        let drivebase = Self {
55            left,
56            right,
57            current_speed: 0,
58            left_sensor: None,
59            right_sensor: None,
60            left_meta,
61            right_meta,
62            circumference,
63            axle_track,
64        };
65        drivebase.reset()?;
66        Ok(drivebase)
67    }
68
69    /// Add left and right colorsensors
70    pub fn add_colorsensor(
71        &mut self,
72        left_sensor: ColorSensor,
73        right_sensor: ColorSensor,
74    ) -> &Self {
75        self.left_sensor = Some(left_sensor);
76        self.right_sensor = Some(right_sensor);
77        self
78    }
79}