-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcar.py
159 lines (137 loc) · 3.5 KB
/
webcar.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from flask import Flask, render_template, request
from flask import jsonify
import time
import RPi.GPIO as GPIO
IN1 = 11
IN2 = 12
IN3 = 13
IN4 = 15
IN12_PWM = 16
IN34_PWM = 22
SEC_NUM = 2
PWM_FREQ = 1000
PWM_DUTY = 40
CAR_TIME_OUT = 0.04
def init_gpio():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)
GPIO.setup(IN12_PWM, GPIO.OUT)
GPIO.setup(IN34_PWM, GPIO.OUT)
def cleanup():
stop()
GPIO.cleanup()
def stop():
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.LOW)
def fwd(sleep_time):
print "---- go foward ----"
GPIO.output(IN1,GPIO.HIGH)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.HIGH)
GPIO.output(IN4,GPIO.LOW)
time.sleep(sleep_time)
stop()
def back(sleep_time):
print "---- go back ----"
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.HIGH)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.HIGH)
time.sleep(sleep_time)
stop()
def left(sleep_time):
print "---- turn left ----"
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.HIGH)
GPIO.output(IN4,GPIO.LOW)
time.sleep(sleep_time)
stop()
def right(sleep_time):
print "---- turn right ----"
GPIO.output(IN1,GPIO.HIGH)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.LOW)
time.sleep(sleep_time)
stop()
def pivot_left(sleep_time):
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.HIGH)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.LOW)
time.sleep(sleep_time)
stop()
def pivot_right(sleep_time):
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.HIGH)
time.sleep(sleep_time)
stop()
def p_left(sleep_time):
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.HIGH)
GPIO.output(IN3,GPIO.HIGH)
GPIO.output(IN4,GPIO.LOW)
time.sleep(sleep_time)
stop()
def p_right(sleep_time):
GPIO.output(IN1,GPIO.HIGH)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.HIGH)
time.sleep(sleep_time)
stop()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/touchcontrol', methods=['GET', 'POST'])
def touchcontrol():
touch_direction = request.form.get("touch", "null")
print(touch_direction)
if touch_direction == "panup":
print("...forward...")
fwd(CAR_TIME_OUT)
elif touch_direction == "pandown":
print("...backward...")
back(CAR_TIME_OUT)
elif touch_direction == "panleft":
print("...turnleft...")
left(CAR_TIME_OUT)
elif touch_direction == "panright":
print("...turnright...")
right(CAR_TIME_OUT)
elif touch_direction == "tap":
print("...pause...")
stop()
elif touch_direction == "press":
print("...pause...")
stop()
else:
print("ERROR")
stop()
d = {'touchDirection': touch_direction}
return jsonify(d)
def main():
# initialize GPIO
init_gpio()
p12 = GPIO.PWM(IN12_PWM, PWM_FREQ)
p12.start(PWM_DUTY)
p34 = GPIO.PWM(IN34_PWM, PWM_FREQ)
p34.start(PWM_DUTY)
stop()
# start flask server
app.run(host='192.168.43.24', port=3000)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
cleanup()
print("**** exit program ****")