10
0
Fork 0
sumorobot-firmware/main.py

157 lines
5.8 KiB
Python
Raw Normal View History

# The code processing thread
2018-01-10 21:14:15 +00:00
def step():
while True:
2018-07-11 19:28:05 +00:00
# Execute to see LED feedback for sensors
2019-02-10 20:07:38 +00:00
sumorobot.update_sensor_feedback()
# Update sensor scope
sumorobot.update_sensor_scope()
# Try to execute the Python code
2018-11-13 20:36:20 +00:00
try:
2019-02-10 20:07:38 +00:00
exec(sumorobot.compiled_python_code)
2018-11-13 20:36:20 +00:00
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)
# The WebSocket processing thread
2018-01-10 21:14:15 +00:00
def ws_handler():
global conn
2018-01-10 21:14:15 +00:00
while True:
2018-07-11 19:28:05 +00:00
# When WiFi has just been reconnected
2019-02-10 20:07:38 +00:00
if wlan.isconnected() and not sumorobot.is_wifi_connected:
print("main.py reconnected to Wi-Fi")
timer.deinit()
2018-07-11 19:28:05 +00:00
sumorobot.set_led(STATUS, True)
2019-02-10 20:07:38 +00:00
sumorobot.is_wifi_connected = True
2018-07-11 19:28:05 +00:00
# When WiFi has just been disconnected
2019-02-10 20:07:38 +00:00
elif not wlan.isconnected() and sumorobot.is_wifi_connected:
print("main.py lost Wi-Fi connection, reconnecting...")
2018-07-11 19:28:05 +00:00
sumorobot.set_led(STATUS, False)
2019-02-10 20:07:38 +00:00
sumorobot.is_wifi_connected = False
timer.init(period=2000, mode=Timer.PERIODIC, callback=sumorobot.toggle_led)
2018-07-11 19:28:05 +00:00
elif not wlan.isconnected():
# Continue to wait for a WiFi connection
2018-05-17 15:46:54 +00:00
continue
data = None
2018-07-11 19:28:05 +00:00
try: # Try to read from the WebSocket
data = conn.recv()
except: # Socket timeout, no data received
# TODO: implement watchdog
# Try reconnecting to the websocket if Wi-Fi is connected
if wlan.isconnected():
print("main.py WebSocket error, reconnecting")
conn = uwebsockets.connect(uri)
2018-07-11 19:28:05 +00:00
# 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:
2019-02-10 20:07:38 +00:00
sumorobot.compiled_python_code = ""
2018-01-11 18:35:09 +00:00
sumorobot.move(FORWARD)
2018-07-11 19:28:05 +00:00
elif b'backward' in data:
2019-02-10 20:07:38 +00:00
sumorobot.compiled_python_code = ""
2018-01-11 18:35:09 +00:00
sumorobot.move(BACKWARD)
2018-07-11 19:28:05 +00:00
elif b'right' in data:
2019-02-10 20:07:38 +00:00
sumorobot.compiled_python_code = ""
2018-01-11 18:35:09 +00:00
sumorobot.move(RIGHT)
2018-07-11 19:28:05 +00:00
elif b'left' in data:
2019-02-10 20:07:38 +00:00
sumorobot.compiled_python_code = ""
2018-01-11 18:35:09 +00:00
sumorobot.move(LEFT)
2018-07-11 19:28:05 +00:00
elif b'stop' in data:
2019-02-10 20:07:38 +00:00
sumorobot.compiled_python_code = ""
2018-01-11 18:35:09 +00:00
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
2019-02-10 20:07:38 +00:00
elif b'get_line_scope' in data:
conn.send(ujson.dumps(sumorobot.get_line_scope()))
elif b'get_sensor_scope' in data:
conn.send(ujson.dumps(sumorobot.get_sensor_scope()))
elif b'get_python_code' in data:
2019-02-17 12:24:46 +00:00
#print("main.py sending python code=", sumorobot.get_python_code())
2019-02-10 20:07:38 +00:00
conn.send(ujson.dumps(sumorobot.get_python_code()))
elif b'get_blockly_code' in data:
2019-02-17 12:24:46 +00:00
#print("main.py sending blockly code=", sumorobot.get_blockly_code())
2019-02-10 20:07:38 +00:00
conn.send(ujson.dumps(sumorobot.get_blockly_code()))
2019-02-17 12:24:46 +00:00
elif b'toggle_sensor_feedback' in data:
data = ujson.loads(data)
sumorobot.sensor_feedback = not sumorobot.sensor_feedback
2019-02-10 20:07:38 +00:00
elif b'set_blockly_code' in data:
data = ujson.loads(data)
2019-02-17 12:24:46 +00:00
#print("main.py Blockly code=", data['val'])
2019-02-10 20:07:38 +00:00
sumorobot.blockly_code = data['val']
elif b'set_python_code' in data:
data = ujson.loads(data)
sumorobot.python_code = data['val']
data['val'] = data['val'].replace(";;", "\n")
2019-02-17 12:24:46 +00:00
#print("main.py python code=", data['val'])
2019-02-10 20:07:38 +00:00
sumorobot.compiled_python_code = compile(data['val'], "snippet", "exec")
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')
2019-02-10 20:07:38 +00:00
elif b'set_line_threshold' in data:
2018-12-27 07:58:49 +00:00
data = ujson.loads(data)
2019-02-10 20:07:38 +00:00
sumorobot.set_line_threshold(int(data['val']))
#print('main.py: set_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)
# When user code (copy.py) exists
if 'code.py' in os.listdir():
print("main.py: loading user code")
# Try to load the user code
try:
with open("code.py", "r") as code:
temp = code.read()
sumorobot.python_code = temp
sumorobot.compiled_python_code = compile(temp, "snippet", "exec")
except:
print("main.py: error loading code.py file")
2018-12-29 21:24:37 +00:00
# Start the code processing thread
_thread.start_new_thread(step, ())
2018-01-10 21:14:15 +00:00
# Wifi connection counter
wifi_connection_counter = 0
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)
wifi_connection_counter += 1
# When Wi-Fi didn't connect in X seconds
if wifi_connection_counter == 30:
print("main.py reinitiating Wi-Fi connection")
# Re initiate the Wi-Fi connection
wlan.connect(ssid, config["wifis"][ssid])
wifi_connection_counter = 0
2018-01-10 21:14:15 +00:00
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
2019-02-10 20:07:38 +00:00
sumorobot.is_wifi_connected = True
2018-07-11 19:28:05 +00:00
# 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, ())