ev3_drivebase/drivebase/
brake_mode.rs1use super::DriveBase;
2use ev3dev_lang_rust::{Ev3Error, motors::TachoMotor};
3
4impl DriveBase {
5 pub fn set_brake_mode(&self, brake_mode: BrakeMode) -> Result<&Self, Ev3Error> {
11 self.left.set_stop_action(brake_mode.as_ref())?;
12 self.right.set_stop_action(brake_mode.as_ref())?;
13 Ok(self)
14 }
15
16 pub fn get_brake_mode(&self) -> Result<BrakeMode, Ev3Error> {
23 let left_brake_mode: BrakeMode = self.left.get_stop_action()?.into();
24 let right_brake_mode: BrakeMode = self.right.get_stop_action()?.into();
25
26 if right_brake_mode != left_brake_mode {
27 self.right.set_stop_action(left_brake_mode.as_ref())?;
28 }
29
30 Ok(left_brake_mode)
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
36pub enum BrakeMode {
37 Coast,
39 Brake,
42 Hold,
45}
46
47impl AsRef<str> for BrakeMode {
48 fn as_ref(&self) -> &str {
49 match self {
50 Self::Coast => TachoMotor::STOP_ACTION_COAST,
51 Self::Brake => TachoMotor::STOP_ACTION_BRAKE,
52 Self::Hold => TachoMotor::STOP_ACTION_HOLD,
53 }
54 }
55}
56
57impl From<&str> for BrakeMode {
58 fn from(value: &str) -> Self {
59 match value {
60 TachoMotor::STOP_ACTION_BRAKE => Self::Brake,
61 TachoMotor::STOP_ACTION_HOLD => Self::Hold,
62 _ => Self::Coast,
63 }
64 }
65}
66
67impl From<String> for BrakeMode {
68 fn from(value: String) -> Self {
69 Self::from(value.as_str())
70 }
71}