Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/output_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,10 +737,11 @@ impl Servo {
/// Set servo to any position between min and max.
/// value must be between -1 (the minimun position) and +1 (the maximum position).
pub fn set_position(&mut self, value: f64) {
if value >= -1.0 && value <= 1.0 {
if (-1.0..=1.0).contains(&value) {
// Map value form [-1, 1] to [min_pulse_width, max_pulse_width] linearly
let range: f64 = (self.max_pulse_width - self.min_pulse_width) as f64;
let pulse_width: u64 = self.min_pulse_width + (((value + 1.0)/2.0) * range).round() as u64;
let pulse_width: u64 =
self.min_pulse_width + (((value + 1.0) / 2.0) * range).round() as u64;
if self
.pin
.set_pwm(
Expand All @@ -751,12 +752,11 @@ impl Servo {
{
println!("Failed to set servo to a new position");
}
}
else {
} else {
println!("set_position value must be between -1 and 1");
}
}

/// Set the servo's minimum pulse width
pub fn set_min_pulse_width(&mut self, value: u64) {
if value >= self.max_pulse_width {
Expand Down Expand Up @@ -796,12 +796,8 @@ impl Servo {
}

pub fn detach(&mut self) {
if self
.pin
.clear_pwm()
.is_err()
{
if self.pin.clear_pwm().is_err() {
println!("Failed to detach servo")
}
}
}
}