Skip to main content

Servo

Struct Servo 

Source
pub struct Servo<'d> { /* private fields */ }
Available on 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>

Source

pub const DEFAULT_MAX_DEGREES: u16 = 180

Default maximum rotation range in degrees (180°).

Source

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.

Source

pub fn relax(&mut self)

Stop sending control signals to the servo.

This allows the servo to relax and move freely, reducing power consumption and mechanical stress.

See the Servo example for usage.

Source

pub fn hold(&mut self)

Resume sending control signals to the servo.

The servo will move back to its last commanded position.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.