-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo_control.py
executable file
·77 lines (53 loc) · 1.18 KB
/
servo_control.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
import RPi.GPIO as GPIO
import time
import requests
import json
pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
p = GPIO.PWM(pin,50)
p.start(0)
left_angle = 10
left_center_angle = 8.75
center_angle = 7.5
right_center_angle = 6.25
right_angle = 5
def doAngle(angle):
p.ChangeDutyCycle(angle)
time.sleep(0.5)
f = open("/home/pi/rsl.txt","r")
data = f.read()
if(float(data) > 125):
doAngle(right_angle)
elif(float(data) > 95 and float(data) <= 125):
doAngle(right_center_angle)
if(float(data) >= 65 and float(data) <= 95):
doAngle(center_angle)
elif(float(data) >= 30 and float(data) < 65):
doAngle(left_center_angle)
elif(float(data) < 30):
doAngle(left_angle)
print(data)
f.close()
time.sleep(2)
while True:
url_servo = "http://192.168.0.3:5000/post"
response_u = requests.get(url=url_servo)
j_servo = response_u.json()
rsl = j_servo["rsl"]
print(rsl)
if(rsl == "left"):
doAngle(left_angle)
elif(rsl == "left_center"):
doAngle(left_center_angle)
elif(rsl == "center"):
doAngle(center_angle)
elif(rsl == "right_center"):
doAngle(right_center_angle)
elif(rsl == "right"):
doAngle(right_angle)
else:
doAngle(0)
time.sleep(1)
p.stop()
GPIO.cleanup()