Skip to content

Instantly share code, notes, and snippets.

@piersstorey
Created October 29, 2023 12:09
Show Gist options
  • Save piersstorey/f57123c8e9a5289306d64f290ba6a6f2 to your computer and use it in GitHub Desktop.
Save piersstorey/f57123c8e9a5289306d64f290ba6a6f2 to your computer and use it in GitHub Desktop.
MeArm Python
import os
import math
import pigpio
pi = pigpio.pi()
class Servo:
def __init__(self, config):
self.pin = config['pin']
self.min = config['min']
self.max = config['max']
self.minAngle = config['minAngle']
self.maxAngle = config['maxAngle']
def moveTo(self, angle):
self.moveToAngle(angle)
def moveBy(self, angle):
newAngle = self.currentAngle + angle
self.moveToAngle(newAngle)
def moveToCentre(self):
centre = self.minAngle + (self.maxAngle - self.minAngle)/2
self.moveToAngle(centre)
def moveToAngle(self, angle):
if angle > self.maxAngle:
angle = self.maxAngle
if angle < self.minAngle:
angle = self.minAngle
self.currentAngle = angle
self.updateServo()
def updateServo(self):
pulseWidth = math.floor(self.min + ((float(self.currentAngle - self.minAngle) / float(self.maxAngle - self.minAngle)) * (self.max - self.min)));
pi.set_servo_pulsewidth(self.pin, pulseWidth)
class MeArm:
def __init__(self):
self.base = Servo({'pin': 4, 'min': 530, 'max': 2400, 'minAngle': -90, 'maxAngle': 90});
self.lower = Servo({'pin': 17, 'min': 530, 'max': 1450, 'minAngle': 0, 'maxAngle': 90});
self.upper = Servo({'pin': 22, 'min': 530, 'max': 2000, 'minAngle': 0, 'maxAngle': 135});
self.grip = Servo({'pin': 10, 'min': 1400, 'max': 2400, 'minAngle': 0, 'maxAngle': 90});
def moveToCentres(self):
self.base.moveToCentre()
self.lower.moveToCentre()
self.upper.moveToCentre()
self.grip.moveToCentre()
myMeArm = MeArm()
myMeArm.moveToCentres()
myMeArm.grip.moveTo(0)
myMeArm.base.moveTo(-90)
myMeArm.base.moveTo(90)
myMeArm.grip.moveTo(90)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment