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-05-22 18:42:11 +00:00
|
|
|
# to remember WiFi disconnects
|
|
|
|
has_wifi_connection = False
|
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
|
2018-01-12 08:28:22 +00:00
|
|
|
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-16 17:04:38 +00:00
|
|
|
# execute to see LED feedback for sensors
|
|
|
|
sumorobot.is_opponent()
|
|
|
|
sumorobot.is_line(LEFT)
|
|
|
|
sumorobot.is_line(RIGHT)
|
2018-01-11 18:35:09 +00:00
|
|
|
# update scope
|
2018-01-10 21:14:15 +00:00
|
|
|
scope = dict(
|
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-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
|
2018-05-17 15:46:54 +00:00
|
|
|
global has_wifi_connection
|
2018-01-10 21:14:15 +00:00
|
|
|
|
|
|
|
while True:
|
2018-05-17 15:46:54 +00:00
|
|
|
# when WiFi is connected
|
|
|
|
if wlan.isconnected():
|
|
|
|
# when WiFi has just been reconnected
|
|
|
|
if not has_wifi_connection:
|
|
|
|
conn = uwebsockets.connect(url)
|
|
|
|
sumorobot.set_led(STATUS, True)
|
|
|
|
has_wifi_connection = True
|
|
|
|
else: # when WiFi is not connected
|
|
|
|
# when WiFi has just been disconnected
|
|
|
|
if has_wifi_connection:
|
|
|
|
sumorobot.set_led(STATUS, False)
|
|
|
|
has_wifi_connection = False
|
|
|
|
# continue to wait for a WiFi connection
|
|
|
|
continue
|
|
|
|
|
2018-05-22 18:42:11 +00:00
|
|
|
try: # try to read from the WebSocket
|
2018-01-10 21:14:15 +00:00
|
|
|
fin, opcode, data = conn.read_frame()
|
2018-05-22 18:42:11 +00:00
|
|
|
except: # socket timeout, no data received
|
|
|
|
# continue to reconnect to WiFi
|
2018-01-10 21:14:15 +00:00
|
|
|
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-13 15:19:27 +00:00
|
|
|
elif data == b"calibrate_line":
|
|
|
|
sumorobot.calibrate_line()
|
|
|
|
#print("Got calibrate")
|
2018-01-10 21:14:15 +00:00
|
|
|
elif b"Gone" in data:
|
2018-05-22 18:42:11 +00:00
|
|
|
#print("Server said 410 Gone, attempting to reconnect...")
|
2018-01-10 21:14:15 +00:00
|
|
|
conn = uwebsockets.connect(url)
|
|
|
|
else:
|
2018-05-22 18:42:11 +00:00
|
|
|
pass
|
|
|
|
#print("unknown command:", data)
|
2018-01-10 21:14:15 +00:00
|
|
|
|
|
|
|
# wait for WiFi to get connected
|
|
|
|
while not wlan.isconnected():
|
|
|
|
sleep_ms(100)
|
|
|
|
|
|
|
|
# connect to the websocket
|
2018-05-22 18:42:11 +00:00
|
|
|
#print("Connecting to:", url)
|
2018-01-10 21:14:15 +00:00
|
|
|
conn = uwebsockets.connect(url)
|
|
|
|
|
2018-05-17 15:46:54 +00:00
|
|
|
# set X seconds timeout for socket reads
|
|
|
|
conn.settimeout(1)
|
|
|
|
|
2018-01-10 21:14:15 +00:00
|
|
|
# send a ping to the robot
|
2018-05-22 18:42:11 +00:00
|
|
|
#print("Sending ping")
|
2018-01-10 21:14:15 +00:00
|
|
|
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
|
|
|
|
2018-05-22 18:42:11 +00:00
|
|
|
#print("Starting WebSocket and code loop")
|
2018-01-10 21:14:15 +00:00
|
|
|
# start the code processing thread
|
|
|
|
_thread.start_new_thread(step, ())
|
|
|
|
# start the Websocket processing thread
|
|
|
|
_thread.start_new_thread(ws_handler, ())
|