-
Notifications
You must be signed in to change notification settings - Fork 4
/
servo.py
executable file
·41 lines (33 loc) · 812 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
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
import RPi.GPIO as GPIO
from time import sleep
import atexit
class Servo():
def start(self):
GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.OUT)
self.pwm = GPIO.PWM(14, 50)
self.pwm.start(0)
def set_angle(self, angle):
duty = angle / 18 + 2
GPIO.output(14, True)
self.pwm.ChangeDutyCycle(duty)
sleep(0.3)
GPIO.output(14, False)
self.pwm.ChangeDutyCycle(0)
def stop(self):
self.pwm.stop()
GPIO.cleanup()
def turn_180(self):
while True:
self.set_angle(0)
self.set_angle(90)
self.set_angle(180)
self.set_angle(90)
s = Servo()
s.start()
s.turn_180()
def exit_handler():
print 'Stoping servo!'
s.stop()
atexit.register(exit_handler)