motor.py: contains motor functions
from time import sleep
import RPi.GPIO as GPIO
class Servo():
amt = 4.5
dcLow = 2+amt
dcHigh = 13-amt
errLow = -200
errHigh = 200
def __init__(self, pin, high2):
self.pin = pin
if type(high2) == type('string'):
assert high2.lower() in ['nw', 'ne', 'sw', 'se']
high2 = high2.lower() in ['nw','se']
self.high2 = high2
self.servo = None
def setup(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.pin, GPIO.OUT)
self.servo = GPIO.PWM(self.pin, 50)
self.servo.start(0)
def pct2dc(self, pct):
if not self.high2:
return ((self.dcHigh-self.dcLow)/(self.errHigh-self.errLow))*(pct-self.errHigh) + self.dcHigh
else:
return (-1*(self.dcHigh-self.dcLow)/(self.errHigh-self.errLow))*(pct-self.errHigh) + self.dcLow
def set_dc(self, dc):
assert self.servo is not None, "Servo was not initialized. Run servo.setup()"
dc = min(dc, self.dcHigh)
dc = max(dc, self.dcLow)
self.servo.ChangeDutyCycle(dc)