Skip to content

Instantly share code, notes, and snippets.

@piersstorey
Created November 29, 2023 22:22
Show Gist options
  • Save piersstorey/3e84cc80920f70a71241566a18cb59fb to your computer and use it in GitHub Desktop.
Save piersstorey/3e84cc80920f70a71241566a18cb59fb to your computer and use it in GitHub Desktop.
MeArm Servo testing
from servo import Servo
my_servo = Servo(pin_id=0, freq=50)
my_servo.write(10)
import machine
import math
class Servo:
def __init__(self,pin_id,min_us=544.0,max_us=2400.0,min_deg=0.0,max_deg=180.0,freq=50):
self.pwm = machine.PWM(machine.Pin(pin_id))
self.pwm.freq(freq)
self.current_us = 0.0
self._slope = (min_us-max_us)/(math.radians(min_deg)-math.radians(max_deg))
self._offset = min_us
def write(self,deg):
self.write_rad(math.radians(deg))
def read(self):
return math.degrees(self.read_rad())
def write_rad(self,rad):
self.write_us(rad*self._slope+self._offset)
def read_rad(self):
return (self.current_us-self._offset)/self._slope
def write_us(self,us):
self.current_us=us
self.pwm.duty_ns(int(self.current_us*1000.0))
def read_us(self):
return self.current_us
def off(self):
self.pwm.duty_ns(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment