-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServoControl.py
75 lines (57 loc) · 1.11 KB
/
ServoControl.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
65
66
67
68
69
70
71
72
73
74
75
import RPi.GPIO as GPIO
from time import sleep
def setup_servo(pins):
servo = []
for pin in pins:
GPIO.setup(pin,GPIO.OUT)
pwm = GPIO.PWM(pin,50)
pwm.start(0)
servo.append(pwm)
return servo
def cleanup(servos):
for servo in servos:
servo.stop()
GPIO.cleanup()
def set_angle(servo,angle):
duty_cycle = 2 + (angle/18)
servo.ChangeDutyCycle(duty_cycle)
sleep(0.5)
def reset_servo(servos):
for servo in servos:
servo.ChangeDutyCycle(2+(90/18))
sleep(0.5)
def catch():
pin = [38,40,5]
GPIO.setmode(GPIO.BOARD)
servos = setup_servo(pin)
set_angle(servos[0],125)
set_angle(servos[1],55)
set_angle(servos[2],55)
def release():
pin = [38,40,5]
GPIO.setmode(GPIO.BOARD)
servos = setup_servo(pin)
reset_servo(servos)
cleanup(servos)
def cam_back():
pin = [7]
GPIO.setmode(GPIO.BOARD)
servos = setup_servo(pin)
set_angle(servos[0],0)
cleanup(servos)
def cam_front():
pin = [7]
GPIO.setmode(GPIO.BOARD)
servos = setup_servo(pin)
set_angle(servos[0],180)
cleanup(servos)
def main():
catch()
sleep(1)
cam_back()
sleep(1)
release()
sleep(1)
cam_front()
if __name__=="__main__":
main()