Skip to main content

ev3dev_lang_rust/motors/
servo_motor_macro.rs

1//! The ServoMotor provides a uniform interface for using hobby type servo motors.
2
3/// The ServoMotor provides a uniform interface for using hobby type servo motors.
4#[macro_export]
5macro_rules! servo_motor {
6    () => {
7        /// Remove power from the motor.
8        pub const COMMAND_RUN: &'static str = "run";
9
10        /// Drive servo to the position set in the position_sp attribute.
11        pub const COMMAND_FLOAT: &'static str = "float";
12
13        /// With normal polarity, a positive duty cycle will cause the motor to rotate clockwise.
14        pub const POLARITY_NORMAL: &'static str = "normal";
15
16        /// With inversed polarity, a positive duty cycle will cause the motor to rotate counter-clockwise.
17        pub const POLARITY_INVERSED: &'static str = "inversed";
18
19        /// Power is being sent to the motor.
20        pub const STATE_RUNNING: &'static str = "running";
21
22        /// Returns the current polarity of the motor.
23        pub fn get_polarity(&self) -> Ev3Result<String> {
24            self.get_attribute("polarity").get()
25        }
26
27        /// Sets the polarity of the motor.
28        pub fn set_polarity(&self, polarity: &str) -> Ev3Result<()> {
29            self.get_attribute("polarity").set_str_slice(polarity)
30        }
31
32        /// Returns the current max pulse setpoint.
33        /// Used to set the pulse size in milliseconds for the signal
34        /// that tells the servo to drive to the maximum (clockwise) position_sp.
35        /// Default value is 2400. Valid values are 2300 to 2700.
36        /// You must write to the position_sp attribute for changes to this attribute to take effect.
37        pub fn get_max_pulse_sp(&self) -> Ev3Result<i32> {
38            self.get_attribute("max_pulse_sp").get()
39        }
40
41        /// Sets the max pulse setpoint.
42        /// Used to set the pulse size in milliseconds for the signal
43        /// that tells the servo to drive to the maximum (clockwise) position_sp.
44        /// Default value is 2400. Valid values are 2300 to 2700.
45        /// You must write to the position_sp attribute for changes to this attribute to take effect.
46        pub fn set_max_pulse_sp(&self, max_pulse_sp: i32) -> Ev3Result<()> {
47            self.get_attribute("max_pulse_sp").set(max_pulse_sp)
48        }
49
50        /// Returns the current mid pulse setpoint.
51        /// Used to set the pulse size in milliseconds for the signal
52        /// that tells the servo to drive to the minimum (counter-clockwise) position_sp.
53        /// Default value is 600.
54        /// Valid values are 300 to 700.
55        ///  You must write to the position_sp attribute for changes to this attribute to take effect.
56        pub fn get_mid_pulse_sp(&self) -> Ev3Result<i32> {
57            self.get_attribute("mid_pulse_sp").get()
58        }
59
60        /// Sets the mid pulse setpoint.
61        /// Used to set the pulse size in milliseconds for the signal
62        /// that tells the servo to drive to the minimum (counter-clockwise) position_sp.
63        /// Default value is 600.
64        /// Valid values are 300 to 700.
65        ///  You must write to the position_sp attribute for changes to this attribute to take effect.
66        pub fn set_mid_pulse_sp(&self, max_pulse_sp: i32) -> Ev3Result<()> {
67            self.get_attribute("mid_pulse_sp").set(max_pulse_sp)
68        }
69
70        /// Returns the current min pulse setpoint.
71        /// Used to set the pulse size in milliseconds for the signal
72        /// that tells the servo to drive to the minimum (counter-clockwise) position_sp.
73        /// Default value is 600. Valid values are 300 to 700.
74        /// You must write to the position_sp attribute for changes to this attribute to take effect.
75        pub fn get_min_pulse_sp(&self) -> Ev3Result<i32> {
76            self.get_attribute("min_pulse_sp").get()
77        }
78        /// Sets the min pulse setpoint.
79        /// Used to set the pulse size in milliseconds for the signal
80        /// that tells the servo to drive to the minimum (counter-clockwise) position_sp.
81        /// Default value is 600. Valid values are 300 to 700.
82        /// You must write to the position_sp attribute for changes to this attribute to take effect.
83        pub fn set_min_pulse_sp(&self, min_pulse_sp: i32) -> Ev3Result<()> {
84            self.get_attribute("min_pulse_sp").set(min_pulse_sp)
85        }
86
87        /// Returns the current target position for the `run-to-abs-pos` and `run-to-rel-pos` commands. Units are in tacho counts.
88        /// You can use the value returned by `counts_per_rot` to convert tacho counts to/from rotations or degrees.
89        /// The range is -2,147,483,648 and +2,147,483,647 tachometer counts (32-bit signed integer).
90        pub fn get_position_sp(&self) -> Ev3Result<i32> {
91            self.get_attribute("position_sp").get()
92        }
93
94        /// Sets the target position for the `run-to-abs-pos` and `run-to-rel-pos` commands.
95        /// Units are in tacho counts.
96        /// You can use the value returned by `counts_per_rot` to convert tacho counts to/from rotations or degrees.
97        /// The range is -2,147,483,648 and +2,147,483,647 tachometer counts (32-bit signed integer).
98        pub fn set_position_sp(&self, position_sp: i32) -> Ev3Result<()> {
99            self.get_attribute("position_sp").set(position_sp)
100        }
101
102        /// Returns the current the rate_sp at which the servo travels from 0 to 100.0%
103        /// (half of the full range of the servo).
104        /// Units are in milliseconds.
105        ///
106        /// ## Example:
107        ///
108        /// Setting the rate_sp to 1000 means that it will take a 180
109        /// degree servo 2 second to move from 0 to 180 degrees.
110        ///
111        /// ## Note:
112        ///
113        /// Some servo controllers may not support this in which case
114        /// reading and writing will fail with -EOPNOTSUPP.
115        /// In continuous rotation servos, this value will affect the
116        /// rate_sp at which the speed ramps up or down.
117        pub fn get_rate_sp(&self) -> Ev3Result<i32> {
118            self.get_attribute("rate_sp").get()
119        }
120
121        /// Sets the rate_sp at which the servo travels from 0 to 100.0%
122        /// (half of the full range of the servo).
123        /// Units are in milliseconds.
124        ///
125        /// ## Example:
126        ///
127        /// Setting the rate_sp to 1000 means that it will take a 180
128        /// degree servo 2 second to move from 0 to 180 degrees.
129        ///
130        /// ## Note:
131        ///
132        /// Some servo controllers may not support this in which case
133        /// reading and writing will fail with -EOPNOTSUPP.
134        /// In continuous rotation servos, this value will affect the
135        /// rate_sp at which the speed ramps up or down.
136        pub fn set_rate_sp(&self, rate_sp: i32) -> Ev3Result<()> {
137            self.get_attribute("rate_sp").set(rate_sp)
138        }
139
140        /// Returns a list of state flags.
141        pub fn get_state(&self) -> Ev3Result<Vec<String>> {
142            self.get_attribute("state").get_vec()
143        }
144
145        /// Power is being sent to the motor.
146        pub fn is_running(&self) -> Ev3Result<bool> {
147            Ok(self
148                .get_state()?
149                .iter()
150                .any(|state| state == Self::STATE_RUNNING))
151        }
152
153        /// Drive servo to the position set in the `position_sp` attribute.
154        pub fn run(&self) -> Ev3Result<()> {
155            self.set_command(Self::COMMAND_RUN)
156        }
157
158        /// Remove power from the motor.
159        pub fn float(&self) -> Ev3Result<()> {
160            self.set_command(Self::COMMAND_FLOAT)
161        }
162    };
163}