-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo_controller.py
executable file
·64 lines (46 loc) · 1.39 KB
/
servo_controller.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
from time import sleep, time
from sys import stdout, stdin, argv
class Servo:
def __init__(self, gpio, pin):
self.pin = pin
self.gpio = gpio
self.gpio.setup(pin, self.gpio.OUT)
# 20 ms
self.period = 2000
self.pwm = gpio.PWM(self.pin, 100)
self.rot = 0
self.pwm.start(self.rot)
self.duty = 0
self.set_rotation(90)
def __del__(self):
self.pwm.stop()
def set_rotation(self, rotation):
self.rot = min(max(rotation, 0.0), 180.0)
print 'Rotation:', self.rot
# normalize 180 degrees to a 0-20ms duty cycle
duty = self.rot / 180.0 * 20.0 + 1
print 'Duty:', duty
# rotate asynchronously
self._set_duty(duty)
sleep(.1 * abs(self.duty - duty) / 2)
self.duty = duty
self._idle()
# Thread(target=rotate).start()
def _set_duty(self, duty):
self.pwm.ChangeDutyCycle(duty)
def _idle(self):
self._set_duty(0)
def rotate_up(self):
self.set_rotation(self.rot + 15)
def rotate_down(self):
self.set_rotation(self.rot - 15)
if __name__ == '__main__':
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
servo = Servo(gpio, 18)
servo.set_rotation(0)
for pulse in xrange(0, 12):
servo.rotate_up()
servo.set_rotation(90)
gpio.cleanup()