enabled reconnect of socket
This commit is contained in:
parent
14ad366484
commit
efc34ed11e
|
@ -5,6 +5,7 @@ import d1motor
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import usocket
|
import usocket
|
||||||
|
import esp
|
||||||
|
|
||||||
class Robot:
|
class Robot:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -20,49 +21,63 @@ class Robot:
|
||||||
print("motor setup complete")
|
print("motor setup complete")
|
||||||
|
|
||||||
# setup socket for remote control
|
# setup socket for remote control
|
||||||
print("setting up socket communication")
|
|
||||||
self.socket = usocket.socket()
|
|
||||||
self.addr = usocket.getaddrinfo('192.168.4.1', 1234)[0][-1]
|
self.addr = usocket.getaddrinfo('192.168.4.1', 1234)[0][-1]
|
||||||
self.socket.bind(self.addr)
|
|
||||||
print("socket setup complete")
|
|
||||||
|
|
||||||
def remote_control(self):
|
def remote_control(self):
|
||||||
print("waiting for connections on {}".format(self.addr))
|
while True:
|
||||||
self.socket.listen(1)
|
# dirty hack: free memory such that reuse of socket is possible
|
||||||
res = self.socket.accept() # this blocks until someone connects to the socket
|
print(esp.freemem())
|
||||||
comm_socket = res[0]
|
if esp.freemem() < 9 * 1024:
|
||||||
print("connected!")
|
s = time.ticks_ms()
|
||||||
listening = True
|
while esp.freemem() < 9 * 1024:
|
||||||
while listening:
|
pass
|
||||||
# expected data: '(u1, u2)'\n"
|
print("waited {} ms".format((time.ticks_ms() - s)))
|
||||||
# where ui = control for motor i
|
print("setting up socket communication")
|
||||||
# ui \in [-1.0, 1.0]
|
socket = usocket.socket()
|
||||||
data = comm_socket.readline()
|
socket.bind(self.addr)
|
||||||
data_str = data.decode()
|
print("socket setup complete")
|
||||||
print("Data received: {}".format(data_str))
|
|
||||||
try:
|
print("waiting for connections on {}".format(self.addr))
|
||||||
print("processing data = {}".format(data_str))
|
socket.listen(1)
|
||||||
l = data_str.strip('()\n').split(',')
|
res = socket.accept() # this blocks until someone connects to the socket
|
||||||
print("l = {}".format(l))
|
comm_socket = res[0]
|
||||||
u1 = int(float(l[0])*10000)
|
print("connected!")
|
||||||
print("u1 = {}".format(u1))
|
listening = True
|
||||||
u2 = int(float(l[1])*10000)
|
while listening:
|
||||||
print("u2 = {}".format(u2))
|
# expected data: '(u1, u2)'\n"
|
||||||
except ValueError:
|
# where ui = control for motor i
|
||||||
print("ValueError: Data has wrong format.")
|
# ui \in [-1.0, 1.0]
|
||||||
|
data = comm_socket.readline()
|
||||||
|
data_str = data.decode()
|
||||||
print("Data received: {}".format(data_str))
|
print("Data received: {}".format(data_str))
|
||||||
print("Shutting down")
|
try:
|
||||||
u1 = u2 = 0
|
print("processing data = {}".format(data_str))
|
||||||
listening = False
|
l = data_str.strip('()\n').split(',')
|
||||||
except IndexError:
|
print("l = {}".format(l))
|
||||||
print("IndexError: Data has wrong format.")
|
u1 = int(float(l[0])*10000)
|
||||||
print("Data received: {}".format(data_str))
|
print("u1 = {}".format(u1))
|
||||||
print("Shutting down")
|
u2 = int(float(l[1])*10000)
|
||||||
u1 = u2 = 0
|
print("u2 = {}".format(u2))
|
||||||
listening = False
|
except ValueError:
|
||||||
finally:
|
print("ValueError: Data has wrong format.")
|
||||||
self.m1.speed(u1)
|
print("Data received: {}".format(data_str))
|
||||||
self.m2.speed(u2)
|
print("Shutting down")
|
||||||
|
u1 = u2 = 0
|
||||||
|
listening = False
|
||||||
|
except IndexError:
|
||||||
|
print("IndexError: Data has wrong format.")
|
||||||
|
print("Data received: {}".format(data_str))
|
||||||
|
print("Shutting down")
|
||||||
|
u1 = u2 = 0
|
||||||
|
listening = False
|
||||||
|
finally:
|
||||||
|
self.m1.speed(u1)
|
||||||
|
self.m2.speed(u2)
|
||||||
|
comm_socket.close()
|
||||||
|
socket.close()
|
||||||
|
del comm_socket
|
||||||
|
del socket
|
||||||
|
print("disconnected")
|
||||||
|
|
||||||
wall_e = Robot()
|
wall_e = Robot()
|
||||||
wall_e.remote_control()
|
wall_e.remote_control()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user