sumorobot-firmware/main.py

131 lines
3.8 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
# Extract a unique name for the robot from the device MAC address
mac = ubinascii.hexlify(wlan.config("mac")[-3:]).decode("ascii")
# SumoRobot server
server_url = config["sumo_server"]
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(
2018-07-11 19:28:05 +00:00
to = "browser-%s@00000514" % mac,
2018-01-16 17:04:38 +00:00
line_left = sumorobot.get_line(LEFT),
line_right = sumorobot.get_line(RIGHT),
opponent = sumorobot.get_opponent_distance(),
2018-01-11 18:35:09 +00:00
battery_voltage = sumorobot.get_battery_voltage(),
2018-01-10 21:14:15 +00:00
)
2018-07-11 19:28:05 +00:00
# Execute code
2018-01-11 18:35:09 +00:00
exec(ast)
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")
print("code:", data['val'])
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-07-11 19:28:05 +00:00
elif b'calibrate_line' in data:
2018-01-13 15:19:27 +00:00
sumorobot.calibrate_line()
2018-07-11 19:28:05 +00:00
elif b'Gone' in data:
print("server said 410 Gone, attempting to reconnect...")
#conn = uwebsockets.connect(url)
2018-01-10 21:14:15 +00:00
else:
2018-07-11 19:28:05 +00:00
print("unknown cmd:", data)
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-01-10 21:14:15 +00:00
conn = uwebsockets.connect(url)
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
# Send a ping to the robot
conn.send('{"setID": "sumo-%s@00000514", "passwd": "salakala"}' % mac)
# Receive session and auth ok frames
conn.recv()
conn.recv()
2018-01-10 21:14:15 +00:00
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 code processing thread
2018-01-10 21:14:15 +00:00
_thread.start_new_thread(step, ())
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, ())