-
Notifications
You must be signed in to change notification settings - Fork 3
/
servo.py
32 lines (28 loc) · 869 Bytes
/
servo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import RPi.GPIO as GPIO
import time
import random
import math
class Servo:
def __init__(self, pin):
self.pin = pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
self.servo = GPIO.PWM(pin, 50)
self.servo.start(0)
self.angle = random.random()
def move(self, angle, prev_servo):
self.angle = angle
angle = math.degrees(angle)
angle = angle + 90.0 - math.degrees(prev_servo)
if angle > 180:
self.angle = math.pi / 2. + prev_servo
angle = 180.0
if angle < 0:
self.angle = prev_servo - math.pi / 2.
angle = 0
if self.pin == 22: #if you ask, the arm is bullshit
angle = 180. - angle
angle = angle / 18. + 2.
self.servo.ChangeDutyCycle(angle)
def close(self):
self.servo.stop()