10
0
sumorobot-firmware/main.py

123 lines
3.5 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-01-12 08:40:10 +00:00
# WebSocket connection
2018-01-10 21:14:15 +00:00
conn = None
2018-01-12 08:40:10 +00:00
# blockly highlihgt WebSocket connection
2018-01-10 21:14:15 +00:00
conn_highlight = None
2018-01-12 08:40:10 +00:00
# extract a unique name for the robot from the device MAC address
2018-01-10 21:14:15 +00:00
name = "sumo-%s" % ubinascii.hexlify(wlan.config("mac")[-3:]).decode("ascii")
# remote server
url = "ws://iot.koodur.com:80/p2p/" + name + "/browser/"
url_highlight = "ws://iot.koodur.com:80/p2p/" + name + "-highlight/browser/"
# local server
#url = "ws://10.42.0.1:80/p2p/" + name + "/browser/"
#url_highlight = "ws://10.42.0.1:80/p2p/" + name + "-highlight/browser/"
2018-01-12 08:40:10 +00:00
# code to execute
ast = ""
2018-01-12 08:40:10 +00:00
# scope, info to be sent to the client
2018-01-10 21:14:15 +00:00
scope = dict()
2018-01-12 08:40:10 +00:00
# SumoRobot object
2018-01-11 18:35:09 +00:00
sumorobot = None
2018-01-10 21:14:15 +00:00
def step():
global scope
while True:
2018-01-11 18:35:09 +00:00
# update scope
2018-01-10 21:14:15 +00:00
scope = dict(
2018-01-11 18:35:09 +00:00
enemy = sumorobot.is_enemy(),
line_left = sumorobot.is_line(LEFT),
line_right = sumorobot.is_line(RIGHT),
battery_voltage = sumorobot.get_battery_voltage(),
2018-01-10 21:14:15 +00:00
)
2018-01-11 18:35:09 +00:00
# execute code
exec(ast)
2018-01-12 08:40:10 +00:00
# when robot was stopped
2018-01-11 18:35:09 +00:00
if sumorobot.terminate:
2018-01-12 08:40:10 +00:00
# disable forceful termination of delays in code
2018-01-11 18:35:09 +00:00
sumorobot.terminate = False
2018-01-12 08:40:10 +00:00
# stop the robot
2018-01-11 18:35:09 +00:00
sumorobot.move(STOP)
2018-01-10 21:14:15 +00:00
# leave time to process WebSocket commands
sleep_ms(50)
def ws_handler():
global ast
global conn
while True:
try:
fin, opcode, data = conn.read_frame()
except: # urror
print("Exception while reading from socket, attempting reconnect")
conn = uwebsockets.connect(url)
continue
if data == b"forward":
#print("Going forward")
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(FORWARD)
2018-01-10 21:14:15 +00:00
elif data == b"backward":
#print("Going backward")
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(BACKWARD)
2018-01-10 21:14:15 +00:00
elif data == b"right":
#print("Going right")
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(RIGHT)
2018-01-10 21:14:15 +00:00
elif data == b"left":
#print("Going left")
2018-01-11 18:35:09 +00:00
ast = ""
sumorobot.move(LEFT)
2018-01-10 21:14:15 +00:00
elif data == b"kick":
conn.send(repr(scope))
elif data == b"ping":
conn.send(repr(scope))
elif data.startswith("start:"):
2018-01-12 08:40:10 +00:00
#print("Got code:", data[6:])
2018-01-10 21:14:15 +00:00
ast = compile(data[6:], "snippet", "exec")
elif data == b"stop":
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-01-12 08:40:10 +00:00
#print("Got stop")
2018-01-10 21:14:15 +00:00
elif b"Gone" in data:
print("Server said 410 Gone, attempting to reconnect...")
conn = uwebsockets.connect(url)
else:
print("unknown command:", data)
# wait for WiFi to get connected
while not wlan.isconnected():
sleep_ms(100)
# connect to the websocket
print("Connecting to:", url)
conn = uwebsockets.connect(url)
# send a ping to the robot
print("Sending ping")
conn.send("{'ping': true}")
conn.send("{'ip': '" + wlan.ifconfig()[0] + "'}")
2018-01-12 08:40:10 +00:00
# connect to the blockly highlight websocket
2018-01-10 21:14:15 +00:00
conn_highlight = uwebsockets.connect(url_highlight)
2018-01-11 18:35:09 +00:00
# initialize SumoRobot object
sumorobot = Sumorobot(conn_highlight.send)
2018-01-10 21:14:15 +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
print("Starting WebSocket and code loop")
# start the code processing thread
_thread.start_new_thread(step, ())
# start the Websocket processing thread
_thread.start_new_thread(ws_handler, ())