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
14 changes: 11 additions & 3 deletions servo.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,22 @@ def pwm(self, pwm):
"""Set servo pulse width in µs for a 2ms frame.

The pulse value is clamped between configured min and max pulse widths.
Use None to turn off the servo.
"""
pwm = min(max(pwm, self.min_pulse), self.max_pulse)
self._ensure_hw()
if self.servo is None:
# No hardware available; nothing to do.
return
# Convert microseconds in ~2ms frame to 16-bit duty (Raspberry Pi Pico style)
self.servo.duty_u16(int(pwm * 3.2768))
if pwm is None:
self.servo.duty_u16(0)
else:
pwm = min(max(pwm, self.min_pulse), self.max_pulse)
# Convert microseconds in ~2ms frame to 16-bit duty (Raspberry Pi Pico style)
self.servo.duty_u16(int(pwm * 3.2768))

def off(self):
"""Turn off the servo."""
self.pwm(None)

def angle(self, angle: float) -> None:
"""Set servo angle. Values are capped between min_angle and max_angle."""
Expand Down