10
0
Fork 0
sumorobot-firmware/main.py

139 lines
4.3 KiB
Python
Raw Normal View History

2018-01-10 21:14:15 +00:00
import _thread
import ubinascii
2018-01-12 09:24:28 +00:00
import uwebsockets
2018-01-10 21:14:15 +00:00
2018-07-11 19:28:05 +00:00
# Code to execute
ast = ""
2018-07-11 19:28:05 +00:00
# Scope, info to be sent to the client
2018-01-10 21:14:15 +00:00
scope = dict()
def step():
global scope
while True:
2018-07-11 19:28:05 +00:00
# Execute to see LED feedback for sensors
2018-01-16 17:04:38 +00:00
sumorobot.is_opponent()
sumorobot.is_line(LEFT)
sumorobot.is_line(RIGHT)
2018-07-11 19:28:05 +00:00
# Update scope
2018-01-10 21:14:15 +00:00
scope = dict(
2019-02-04 19:46:12 +00:00
left_line = sumorobot.get_line(LEFT),
right_line = sumorobot.get_line(RIGHT),
2018-01-16 17:04:38 +00:00
opponent = sumorobot.get_opponent_distance(),
2018-01-11 18:35:09 +00:00
battery_voltage = sumorobot.get_battery_voltage(),
2019-02-04 19:46:12 +00:00
left_line_value = sumorobot.config["left_line_value"],
right_line_value = sumorobot.config["right_line_value"],
left_line_threshold = sumorobot.config["left_line_threshold"],
right_line_threshold = sumorobot.config["right_line_threshold"]
2018-01-10 21:14:15 +00:00
)
2018-07-11 19:28:05 +00:00
# Execute code
2018-11-13 20:36:20 +00:00
try:
exec(ast)
except:
pass
2018-07-11 19:28:05 +00:00
# When robot was stopped
2018-01-11 18:35:09 +00:00
if sumorobot.terminate:
2018-07-11 19:28:05 +00:00
# Disable forceful termination of delays in code
2018-01-11 18:35:09 +00:00
sumorobot.terminate = False
2018-07-11 19:28:05 +00:00
# Stop the robot
2018-01-11 18:35:09 +00:00
sumorobot.move(STOP)
2018-07-11 19:28:05 +00:00
# Leave time to process WebSocket commands
2018-01-10 21:14:15 +00:00
sleep_ms(50)
def ws_handler():
global ast
2018-05-17 15:46:54 +00:00
global has_wifi_connection
2018-01-10 21:14:15 +00:00
while True:
2018-07-11 19:28:05 +00:00
# When WiFi has just been reconnected
if wlan.isconnected() and not has_wifi_connection:
#conn = uwebsockets.connect(url)
sumorobot.set_led(STATUS, True)
has_wifi_connection = True
# When WiFi has just been disconnected
elif not wlan.isconnected() and has_wifi_connection:
sumorobot.set_led(STATUS, False)
has_wifi_connection = False
elif not wlan.isconnected():
# Continue to wait for a WiFi connection
2018-05-17 15:46:54 +00:00
continue
2018-07-11 19:28:05 +00:00
try: # Try to read from the WebSocket
data = conn.recv()
except: # Socket timeout, no data received
# Continue to try to read data
2018-01-10 21:14:15 +00:00
continue
2018-07-11 19:28:05 +00:00
# When an empty frame was received
if not data:
# Continue to receive data
continue
elif b'forward' in data:
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(FORWARD)
2018-07-11 19:28:05 +00:00
elif b'backward' in data:
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(BACKWARD)
2018-07-11 19:28:05 +00:00
elif b'right' in data:
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(RIGHT)
2018-07-11 19:28:05 +00:00
elif b'left' in data:
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(LEFT)
2018-07-11 19:28:05 +00:00
elif b'ping' in data:
conn.send(repr(scope).replace("'", '"'))
elif b'code' in data:
data = ujson.loads(data)
data['val'] = data['val'].replace(";;", "\n")
2018-12-29 21:24:37 +00:00
#print("main.py code=", data['val'])
2018-07-11 19:28:05 +00:00
ast = compile(data['val'], "snippet", "exec")
elif b'stop' in data:
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(STOP)
2018-01-12 08:40:10 +00:00
# for terminating delays in code
2018-01-11 18:35:09 +00:00
sumorobot.terminate = True
2018-12-27 07:56:16 +00:00
elif b'calibrate_line_value' in data:
sumorobot.calibrate_line_value()
2018-12-29 21:27:26 +00:00
#print('main.py: calibrate_line_value')
2018-12-27 07:56:16 +00:00
elif b'calibrate_line_threshold' in data:
2018-12-27 07:58:49 +00:00
data = ujson.loads(data)
sumorobot.calibrate_line_threshold(int(data['val']))
2018-12-29 21:27:26 +00:00
#print('main.py: calibrate_line_threshold')
2018-07-11 19:28:05 +00:00
elif b'Gone' in data:
2018-12-29 21:24:37 +00:00
print("main.py: server said 410 Gone, attempting to reconnect...")
2018-07-11 19:28:05 +00:00
#conn = uwebsockets.connect(url)
2018-01-10 21:14:15 +00:00
else:
2018-12-29 21:24:37 +00:00
print("main.py: unknown cmd=", data)
# Try to load the user code
try:
with open("code.py", "r") as code:
ast = compile(code.read(), "snippet", "exec")
except:
print("main.py: error loading code.py file")
# Start the code processing thread
_thread.start_new_thread(step, ())
2018-01-10 21:14:15 +00:00
2018-07-11 19:28:05 +00:00
# Wait for WiFi to get connected
2018-01-10 21:14:15 +00:00
while not wlan.isconnected():
sleep_ms(100)
2018-07-11 19:28:05 +00:00
# Connect to the websocket
2018-11-13 20:36:20 +00:00
uri = "ws://%s/p2p/sumo-%s/browser/" % (config['sumo_server'], config['sumo_id'])
conn = uwebsockets.connect(uri)
2018-01-10 21:14:15 +00:00
2018-07-11 19:28:05 +00:00
# Set X seconds timeout for socket reads
2018-05-17 15:46:54 +00:00
conn.settimeout(1)
2018-07-11 19:28:05 +00:00
# Stop bootup blinking
timer.deinit()
2018-01-11 18:35:09 +00:00
2018-07-11 19:28:05 +00:00
# WiFi is connected
has_wifi_connection = True
# Indicate that the WebSocket is connected
2018-01-11 18:35:09 +00:00
sumorobot.set_led(STATUS, True)
2018-01-10 21:14:15 +00:00
2018-07-11 19:28:05 +00:00
# Start the Websocket processing thread
2018-01-10 21:14:15 +00:00
_thread.start_new_thread(ws_handler, ())