Update app.py
Browse files
app.py
CHANGED
@@ -1 +1,75 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request
|
2 |
+
from flask_socketio import SocketIO, emit
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
# Allow all origins for Socket.IO connections
|
7 |
+
socketio = SocketIO(app, cors_allowed_origins="*")
|
8 |
+
|
9 |
+
# Global variables to store the rover commands
|
10 |
+
forward = False
|
11 |
+
backward = False
|
12 |
+
left = False
|
13 |
+
right = False
|
14 |
+
stop = False
|
15 |
+
|
16 |
+
# Event handler for sending commands to the rover
|
17 |
+
def command_for_moment_rover():
|
18 |
+
global forward, backward, left, right, stop
|
19 |
+
|
20 |
+
# Check the flag and emit the corresponding command
|
21 |
+
if forward:
|
22 |
+
print("Moving Forward")
|
23 |
+
socketio.emit('rover_control', {'data': 'forward'}, namespace='/')
|
24 |
+
elif backward:
|
25 |
+
print("Moving Backward")
|
26 |
+
socketio.emit('rover_control', {'data': 'backward'}, namespace='/')
|
27 |
+
elif left:
|
28 |
+
print("Turning Left")
|
29 |
+
socketio.emit('rover_control', {'data': 'left'}, namespace='/')
|
30 |
+
elif right:
|
31 |
+
print("Turning Right")
|
32 |
+
socketio.emit('rover_control', {'data': 'right'}, namespace='/')
|
33 |
+
elif stop:
|
34 |
+
print("Stopping the rover")
|
35 |
+
socketio.emit('rover_control', {'data': 'stop'}, namespace='/')
|
36 |
+
|
37 |
+
# Route for the UI and buttons
|
38 |
+
@app.route('/')
|
39 |
+
def index():
|
40 |
+
return render_template('index.html')
|
41 |
+
|
42 |
+
# Route to handle commands from the UI buttons (POST method)
|
43 |
+
@app.route('/command_for_the_rover', methods=['POST'])
|
44 |
+
def command():
|
45 |
+
global forward, backward, left, right, stop
|
46 |
+
|
47 |
+
# Reset all flags
|
48 |
+
forward = backward = left = right = stop = False
|
49 |
+
|
50 |
+
# Check which button was pressed and set corresponding flags
|
51 |
+
if 'forward' in request.form:
|
52 |
+
forward = True
|
53 |
+
if 'backward' in request.form:
|
54 |
+
backward = True
|
55 |
+
if 'left' in request.form:
|
56 |
+
left = True
|
57 |
+
if 'right' in request.form:
|
58 |
+
right = True
|
59 |
+
if 'stop' in request.form:
|
60 |
+
stop = True
|
61 |
+
|
62 |
+
# Call the rover command function to send the socket message
|
63 |
+
command_for_moment_rover()
|
64 |
+
|
65 |
+
# Return to the main page with a confirmation message
|
66 |
+
return render_template('index.html', message="Command Sent!")
|
67 |
+
|
68 |
+
# Socket.IO event listener for connection (for communication purposes)
|
69 |
+
@socketio.on('connect')
|
70 |
+
def handle_connect():
|
71 |
+
print("Client connected")
|
72 |
+
|
73 |
+
# Start the Flask-SocketIO app
|
74 |
+
if __name__ == '__main__':
|
75 |
+
socketio.run(app, host='0.0.0.0', port=5000)
|