mirror of
https://github.com/m5stack/M5Cloud.git
synced 2026-05-20 10:07:21 -07:00
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from machine import PWM
|
|
import math
|
|
|
|
|
|
class Servo:
|
|
"""
|
|
A simple class for controlling hobby servos.
|
|
|
|
Args:
|
|
pin (machine.Pin): The pin where servo is connected. Must support PWM.
|
|
freq (int): The frequency of the signal, in hertz.
|
|
min_us (int): The minimum signal length supported by the servo.
|
|
max_us (int): The maximum signal length supported by the servo.
|
|
angle (int): The angle between the minimum and maximum positions.
|
|
|
|
"""
|
|
def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):
|
|
self.min_us = min_us
|
|
self.max_us = max_us
|
|
self.us = 0
|
|
self.freq = freq
|
|
self.angle = angle
|
|
self.pwm = PWM(pin, freq=freq, duty=0)
|
|
|
|
def write_us(self, us):
|
|
"""Set the signal to be ``us`` microseconds long. Zero disables it."""
|
|
if us == 0:
|
|
self.pwm.duty(0)
|
|
return
|
|
us = min(self.max_us, max(self.min_us, us))
|
|
duty = us * 100 * self.freq // 1000000
|
|
self.pwm.duty(duty)
|
|
|
|
def write_angle(self, degrees=None, radians=None):
|
|
"""Move to the specified angle in ``degrees`` or ``radians``."""
|
|
if degrees is None:
|
|
degrees = math.degrees(radians)
|
|
degrees = degrees % 360
|
|
total_range = self.max_us - self.min_us
|
|
us = self.min_us + total_range * degrees // self.angle
|
|
self.write_us(us) |