-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyCalib.py
More file actions
84 lines (66 loc) · 2.08 KB
/
EasyCalib.py
File metadata and controls
84 lines (66 loc) · 2.08 KB
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
import flask
from flask import render_template as render_template, request as request
from robot import Robot
import multiprocessing as mp
import json
app = flask.Flask(__name__)
nokill = {"kill": False}
config = {}
def load_config():
global config
with open("config.json") as file:
config = json.load(file)
load_config()
def start_action_thread(reqtype):
robot = Robot(nokill, config)
if reqtype == "forward":
robot.moveFD(10)
else:
robot.left(90)
robot.stop() #technically unecessary, but why not
@app.route('/')
def index():
return render_template("CalibrationPage.html")
@app.route('/calib_action')
def custom_calib_req():
if 'type' not in request.args:
return "Type arg missing."
reqtype = request.args['type']
if reqtype not in ["forward", "turn"]:
return "Type not valid. Try forward / turn"
else:
p = mp.Process(target=start_action_thread, args=(reqtype,))
p.start()
return "200 ok"
@app.route('/calib_response')
def handle_change_calib():
if ('type' not in request.args) or ('dir' not in request.args):
return "Missing type or dir"
reqtype = request.args['type']
if reqtype not in ["forward", "turn"]:
return "Type not valid. Try forward / turn"
reqdir = request.args['dir']
try:
reqdir = int(reqdir)
except ValueError:
return "Requestion direction is not int"
if reqtype == "forward":
if reqdir == 1:
config["lmotor_speed"] += 1
config["rmotor_speed"] -= 1
elif reqdir == -1:
config["lmotor_speed"] -= 1
config["rmotor_speed"] += 1
else:
if reqdir == 1:
config["turn_time"] += 0.02
elif reqdir == -1:
config["turn_time"] -= 0.02
config["rmotor_speed"] = round(config["rmotor_speed"])
config["lmotor_speed"] = round(config["lmotor_speed"])
config["turn_time"] = round(config["turn_time"], 2)
with open("config.json", "w+") as file:
json.dump(config, file)
return "200 ok"
def startFlask():
app.run()