-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservo.py
More file actions
32 lines (25 loc) · 707 Bytes
/
servo.py
File metadata and controls
32 lines (25 loc) · 707 Bytes
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
from RPIO import PWM
import time
import math
class ServoController:
def __init__(self, gpio):
self.gpioPin = gpio
self.servo = PWM.Servo()
self.angle = 0
self.pulseWidth = 0
def setAngle(self, degrees):
#http://www.raspberrypi.org/forums/viewtopic.php?f=44&t=36572
pulse = 1520 + (degrees * 400) / 45
if degrees > 90:
degrees = 90
elif degrees < -90:
degrees = -90
pulse = int(math.ceil(pulse / 10.0)) * 10 #round to nearest 10
self.servo.set_servo(self.gpioPin, pulse)
time.sleep(0.1)
self.angle = degrees
self.pulseWidth = pulse
def incAngle(self, increment):
self.setAngle(self.angle + increment)
def cleanup(self):
self.servo.stop_servo(self.gpioPin)