forked from AppStateRoboticsClub/rover2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (54 loc) · 1.43 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
import json
from flask import Flask, render_template
from flask_socketio import SocketIO
import roversim as move
controls: dict[str, float] = {
'w': 0
}
motion: dict[str, float] = {
'speed': 0,
'turn': 0
}
motion1: dict[str, float] = {
'speed': 0,
'turn': 0
}
app = Flask(__name__)
app.config['SECRET_KEY'] = '12345'
socketio = SocketIO(app)
move.init(socketio)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('connect')
def handle_connect():
print('Client connected')
# @socketio.on('message')
# def handle_message(data):
# print('Received message:', data)
# socketio.emit('response', 'Server received your message: ' + data)
@socketio.on('key-down')
def handle_key(data, m = 1):
print(motion)
if data == 'w':
motion['speed'] = -0.75 * m
if data == 's':
motion['speed'] = 0.75 * m
if data == 'a':
motion['turn'] = -1 * m
if data == 'd':
motion['turn'] = 1 * m
print(motion)
move.set_motion(movement=motion)
@socketio.on('key-up')
def handle_key_up(data):
handle_key(data, 0)
def update_motion():
if motion.values() == motion1.values():
print("smile")
return
for key in list(motion.keys()):
motion_old = motion[key]
socketio.emit('set-speed', {"speed": motion['speed'], "turn": motion['turn']})
if __name__ == '__main__':
socketio.run(app, debug=True, host="0.0.0.0") # type: ignore