-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
172 lines (139 loc) · 5.14 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
import enum
import sys
import time
import getch
import socket
from flask_cors import CORS
from flask_socketio import SocketIO
from flask import Flask, request, Response, render_template, session
import AI_Driver.Controllers.CarController as CarController
import jyserver.Flask as jsf
import threading
car = CarController.CarController()
# Initialize web server for controlling the car
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app)
@jsf.use(app)
class App:
def __init__(self):
self.ids = ["ll","lm","mm","rm","rr"]
self.ir_update_thread = threading.Thread(target=self.update_ir,args=(),daemon=True)
def start_thread(self):
self.ir_update_thread.start()
def update_ir(self):
while True:
line_state = car.get_line_state()
for sensor,index in enumerate(line_state):
if sensor:
self.js.document.getElementById(self.ids[index]).style.color = "yellow"
else:
self.js.document.getElementById(self.ids[index]).style.color = "white"
# needs test
@app.route("/min_speed", methods=["POST"])
def min_speed():
if request.method == "POST":
todo = request.form.get("todo")
car.min_speed = int(todo)
# print("Minimum speed: " , todo)
return App.render(render_template('index.html')) # return render_template("index.html")
# needs test
@app.route("/max_speed", methods=["POST"])
def max_speed():
if request.method == "POST":
todo = request.form.get("todo")
car.max_speed = int(todo)
# print("Maximum speed: " , todo)
return App.render(render_template('index.html')) # return render_template("index.html")
# needs to be added
@app.route("/current_speed", methods=["GET"])
def current_speed():
if request.method == "GET":
return str(car.car_speed)
return App.render(render_template('index.html')) # return render_template("index.html")
# needs fix
@app.route("/controltype", methods=["POST"])
def control_type():
if request.method == "POST":
todo = request.form.get("todo")
# print(todo)
car.control_type = todo
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/forward")
def forward():
car.last_velo_time = time.time()
car.car_speed = car.max_speed
car.drive_forward()
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/backward")
def backward():
car.last_velo_time = time.time()
car.car_speed = -car.max_speed
car.drive_backward()
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/left")
def left():
car.last_steer_time = time.time()
car.straight = 0
car.turn_left()
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/right")
def right():
car.last_steer_time = time.time()
car.straight = 0
car.turn_right()
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/stop")
def stop():
car.stop()
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/center_steering")
def center_steering():
car.center_steering()
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/linetype", methods=["POST"])
def linetype():
if request.method == "POST":
todo = request.form.get("todo")
car.line_color = todo
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/p", methods=["POST"])
def set_p():
if request.method == "POST":
todo = request.form.get("todo")
# print(todo)
car.p = todo
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/i", methods=["POST"])
def set_i():
if request.method == "POST":
todo = request.form.get("todo")
# print("I " ,todo)
car.i = todo
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/d", methods=["POST"])
def set_d():
if request.method == "POST":
todo = request.form.get("todo")
# print("D ", todo)
car.d = todo
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/camera_feed")
def camera_feed():
return Response(car.get_frame(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route("/irsensors",methods=["POST"])
def irsensors():
print(session)
# return render_template('index.html',irlist=car.get_line_state())
return App.render(render_template('index.html')) # return render_template("index.html")
@app.route("/")
def execute():
#return App.render(render_template('index.html'))
return App.render(render_template('index.html')) # return render_template("index.html")
@socketio.on('disconnect')
def test_disconnect():
car.stop()
car.center_steering()
if __name__ == '__main__':
host_addr = socket.gethostbyname(socket.gethostname() + ".local")
app.run(host_addr, port=8080)