-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo.py
71 lines (55 loc) · 1.6 KB
/
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
#!/usr/bin/python
import wiringpi
import time
import math
#use BCM pin numbers
wiringpi.wiringPiSetupGpio()
# setup WiringPi PWM
SERVO_PIN = 18
PWM_DIVISOR = 384 # clock at 50kHz (20us tick)
PWM_RANGE = 1000 # range at 1000 ticks (20ms)
# setup pin as an output
wiringpi.pinMode(SERVO_PIN, 2)
wiringpi.pwmSetMode(0)
wiringpi.pwmSetClock(PWM_DIVISOR)
wiringpi.pwmSetRange(PWM_RANGE)
wiringpi.pwmWrite(SERVO_PIN, 0) #theretically 50 (1ms) to 100 (2ms) on my servo 40-200 works ok
def set_servo_position(pos):
"""
Turn servo on specified angle in degrees
Params:
pos: angle to turn servo, in degrees
"""
move = int(40 + math.floor(pos * (200 - 40) / 180))
print("pos: {}".format(pos))
print("move: {}".format(move))
if move:
print("move: ", move)
try:
wiringpi.pwmWrite(SERVO_PIN, move)
# time.sleep(0.5)
# print("wiringpi.pwmWrite(18,{})".format(move))
except:
# clean up
wiringpi.pwmWrite(18, 0)
print("Exception")
print("Module out_servo.py have imported")
###Test display_4digits()
def test_servo(pos):
while True:
try:
### Open Candy Jar
set_servo_position(pos)
print("Left")
time.sleep(0.25)
### Close Candy Jar
set_servo_position(0)
print("Right")
time.sleep(1)
except KeyboardInterrupt:
# clean up
set_servo_position(0)
print("KeyboardInterrupt Exception")
break
if __name__ == "__main__":
test_servo(20)