pub struct Servo<'d> { /* private fields */ }target_os=none only.Expand description
A device abstraction for hobby servos.
Use Servo for direct, immediate control when you want to manually manage servo
positioning. Use servo_player instead when you need
background animation sequences or want motion to continue while your code does other work.
§How Servos Work
When you call set_degrees(45), the Pico starts sending a PWM control signal to the
servo, telling it to move to and hold at 45 degrees. The Pico’s hardware generates this control
signal automatically in the background, taking no CPU time. The control signal remains active until
you change it or call relax().
The servo itself has no idea what angle it is currently at. It simply moves as fast as it can
to match whatever angle the current control signal specifies. The library does not—and cannot—wait
for the servo to reach a position. This is why you must wait (for example, using Timer::after())
after calling set_degrees() to give the servo time to physically reach the target position.
§Examples
use device_envoy::{servo, servo::Servo};
use embassy_time::{Duration, Timer};
async fn example(p: embassy_rp::Peripherals) {
// Create a servo on GPIO 11.
// GPIO 11 → (11/2) % 8 = 5 → PWM_SLICE5
let mut servo = servo! {
pin: p.PIN_11,
slice: p.PWM_SLICE5,
};
servo.set_degrees(45); // Move to 45 degrees and hold.
Timer::after(Duration::from_secs(1)).await; // Give servo reasonable time to reach position
servo.set_degrees(90); // Move to 90 degrees and hold.
Timer::after(Duration::from_secs(1)).await; // Give servo reasonable time to reach position
servo.relax(); // Let the servo relax. It will re-enable on next set_degrees()
}Implementations§
Source§impl<'d> Servo<'d>
impl<'d> Servo<'d>
Sourcepub const DEFAULT_MAX_DEGREES: u16 = 180
pub const DEFAULT_MAX_DEGREES: u16 = 180
Default maximum rotation range in degrees (180°).
Sourcepub fn set_degrees(&mut self, degrees: u16)
pub fn set_degrees(&mut self, degrees: u16)
Set position in degrees 0..=max_degrees mapped into [min_us, max_us].
Automatically enables the servo if it was disabled.
See the Servo example for usage.
Auto Trait Implementations§
impl<'d> Freeze for Servo<'d>
impl<'d> RefUnwindSafe for Servo<'d>
impl<'d> Send for Servo<'d>
impl<'d> Sync for Servo<'d>
impl<'d> Unpin for Servo<'d>
impl<'d> !UnwindSafe for Servo<'d>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more