-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpi-servo.py
executable file
·104 lines (89 loc) · 2.71 KB
/
pi-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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Use Raspberry Pi to control Servo Motor motion
# Tutorial URL: http://osoyoo.com/?p=937
import RPi.GPIO as GPIO
import time
import os
GPIO.setwarnings(False)
# Set the layout for the pin declaration
GPIO.setmode(GPIO.BOARD)
# The Raspberry Pi pin 11(GPIO 18) connect to servo signal line(yellow wire)
# Pin 11 send PWM signal to control servo motion
GPIO.setup(11, GPIO.OUT)
# menu info
print "l = move to the left"
print "r = move to the right"
print "m = move to the middle"
print "t = test sequence"
print "q = stop and exit"
while True:
# Now we will start with a PWM signal at 50Hz at pin 18.
# 50Hz should work for many servos very will. If not you can play with the frequency if you like.
Servo = GPIO.PWM(11, 50)
# This command sets the left position of the servo
Servo.start(2.5)
# Now the program asks for the direction the servo should turn.
input = raw_input("Selection: ")
# You can play with the values.
# 7.5 is in most cases the middle position
# 12.5 is the value for a 180 degree move to the right
# 2.5 is the value for a -90 degree move to the left
if(input == "t"):
print "move to the center position:"
Servo.ChangeDutyCycle(7.5)
time.sleep(1)
print "move to the right position:"
Servo.ChangeDutyCycle(12.5)
time.sleep(1)
print "move to the left position:"
Servo.ChangeDutyCycle(2.5)
time.sleep(1)
# this stops the PWM signal
print "Move back to start position."
Servo.stop()
# direction right
if(input == "r"):
# how many steps should the move take.
steps = raw_input("steps (1 - 10): ")
print steps, "steps to the right"
stepslength = 12.5 / int(steps)
for Counter in range(int(steps)):
Servo.ChangeDutyCycle(stepslength * (Counter + 1))
print stepslength * (Counter + 1)
time.sleep(0.5)
time.sleep(1)
# PWM stop
print "Move back to start position."
Servo.stop()
# move to the center position
elif(input == "m"):
print "Move back to the center position."
Servo.start(7.5)
time.sleep(1)
# PWM stop
print "Move back to start position."
Servo.stop()
# move to the left
elif(input == "l"):
print "Move to the max right position and then to the left position."
Servo.start(12.5)
# how many steps...
steps = raw_input("steps (1 - 10): ")
print steps, "steps to the right"
stepslength = 12.5 / int(steps)
for Counter in range(int(steps)):
Servo.ChangeDutyCycle(12.5 - (stepslength * (Counter + 1)))
print (12.5 - (stepslength * (Counter + 1)))
time.sleep(0.5)
time.sleep(1)
# PWM stop
print "Move back to start position."
Servo.stop()
# close program
elif(input == "q"):
print "stop the program and exit......"
os._exit(1)
Servo.stop()
GPIO.cleanup()
# input not valid
else:
print "input not valid!"