2019-04-12 17:17:12 +00:00
|
|
|
import machine
|
2019-06-10 21:16:14 +00:00
|
|
|
import sys
|
2019-05-05 08:26:30 +00:00
|
|
|
from machine import I2C, Pin
|
2019-04-12 17:17:12 +00:00
|
|
|
|
2019-05-05 08:26:30 +00:00
|
|
|
import d1motor
|
2019-04-12 19:43:13 +00:00
|
|
|
|
2019-05-05 08:26:30 +00:00
|
|
|
import time
|
|
|
|
import usocket
|
2019-05-06 20:09:52 +00:00
|
|
|
import esp
|
2019-04-12 19:43:13 +00:00
|
|
|
|
|
|
|
class Robot:
|
|
|
|
def __init__(self):
|
2019-06-05 06:04:26 +00:00
|
|
|
print("setting up I2C ...")
|
2019-05-05 08:26:30 +00:00
|
|
|
d1 = Pin(5)
|
|
|
|
d2 = Pin(4)
|
|
|
|
i2c = I2C(scl=d1, sda=d2)
|
2019-06-10 21:16:14 +00:00
|
|
|
i2c_scan = i2c.scan()
|
|
|
|
if len(i2c_scan) > 0:
|
|
|
|
i2c_addr = i2c_scan[0]
|
|
|
|
print("i2c scan = {}".format(i2c_addr))
|
|
|
|
print("setting up motors ...")
|
|
|
|
self.m1 = d1motor.Motor(0, i2c, address=i2c_addr)
|
|
|
|
self.m2 = d1motor.Motor(1, i2c, address=i2c_addr)
|
|
|
|
self.m1.speed(0)
|
|
|
|
self.m2.speed(0)
|
|
|
|
print("motor setup complete!")
|
|
|
|
else:
|
|
|
|
print("error: no i2c interfaces found!")
|
|
|
|
sys.exit(1)
|
2019-06-05 06:04:26 +00:00
|
|
|
|
|
|
|
ip = my_ip[0]
|
2019-05-05 08:26:30 +00:00
|
|
|
# setup socket for remote control
|
2019-06-05 06:04:26 +00:00
|
|
|
self.addr = usocket.getaddrinfo(ip, 1234)[0][-1]
|
2019-04-12 17:17:12 +00:00
|
|
|
|
2019-04-14 20:07:47 +00:00
|
|
|
def remote_control(self):
|
2019-05-06 20:09:52 +00:00
|
|
|
while True:
|
2019-06-05 06:04:26 +00:00
|
|
|
print("setting up socket communication ...")
|
|
|
|
socket_setup_complete = False
|
|
|
|
while not socket_setup_complete:
|
|
|
|
try:
|
|
|
|
print("trying to create socket on address {}".format(self.addr))
|
|
|
|
socket = usocket.socket()
|
|
|
|
socket.bind(self.addr)
|
|
|
|
print("socket setup complete!")
|
|
|
|
socket_setup_complete = True
|
|
|
|
except Exception as e:
|
|
|
|
print("could not create socket. error msg: {}\nwaiting 1 sec and retrying...".format(e))
|
|
|
|
time.sleep(1.0)
|
2019-05-06 20:09:52 +00:00
|
|
|
|
2019-06-05 06:04:26 +00:00
|
|
|
print("waiting for connections on {} ...".format(self.addr))
|
2019-05-06 20:09:52 +00:00
|
|
|
socket.listen(1)
|
|
|
|
res = socket.accept() # this blocks until someone connects to the socket
|
|
|
|
comm_socket = res[0]
|
|
|
|
print("connected!")
|
|
|
|
listening = True
|
|
|
|
while listening:
|
2019-06-13 11:18:46 +00:00
|
|
|
# expected data: '(t, u1, u2)'\n"
|
2019-05-06 20:09:52 +00:00
|
|
|
# where ui = control for motor i
|
|
|
|
# ui \in [-1.0, 1.0]
|
|
|
|
try:
|
2019-06-06 13:19:14 +00:00
|
|
|
data = comm_socket.readline()
|
|
|
|
data_str = data.decode()
|
2019-06-13 11:18:46 +00:00
|
|
|
print("Data received: {}".format(data_str))
|
|
|
|
print("processing data = {}".format(data_str))
|
2019-05-06 20:09:52 +00:00
|
|
|
l = data_str.strip('()\n').split(',')
|
2019-06-13 11:18:46 +00:00
|
|
|
print("l = {}".format(l))
|
|
|
|
t = float(l[0])
|
|
|
|
print("t = {}".format(t))
|
|
|
|
u1 = int(float(l[1])*100)
|
|
|
|
print("u1 = {}".format(u1))
|
|
|
|
u2 = int(float(l[2])*100)
|
|
|
|
print("u2 = {}".format(u2))
|
2019-05-06 20:09:52 +00:00
|
|
|
except ValueError:
|
|
|
|
print("ValueError: Data has wrong format.")
|
|
|
|
print("Data received: {}".format(data_str))
|
2019-06-05 06:04:26 +00:00
|
|
|
print("Shutting down ...")
|
2019-05-06 20:09:52 +00:00
|
|
|
u1 = u2 = 0
|
2019-06-13 11:18:46 +00:00
|
|
|
t = 0.0
|
2019-05-06 20:09:52 +00:00
|
|
|
listening = False
|
2019-06-13 11:18:46 +00:00
|
|
|
comm_socket.close()
|
|
|
|
socket.close()
|
|
|
|
del comm_socket
|
|
|
|
del socket
|
|
|
|
print("disconnected!")
|
2019-05-06 20:09:52 +00:00
|
|
|
except IndexError:
|
|
|
|
print("IndexError: Data has wrong format.")
|
|
|
|
print("Data received: {}".format(data_str))
|
2019-06-05 06:04:26 +00:00
|
|
|
print("Shutting down ...")
|
2019-05-06 20:09:52 +00:00
|
|
|
u1 = u2 = 0
|
2019-06-13 11:18:46 +00:00
|
|
|
t = 0.0
|
2019-05-06 20:09:52 +00:00
|
|
|
listening = False
|
2019-06-13 11:18:46 +00:00
|
|
|
comm_socket.close()
|
|
|
|
socket.close()
|
|
|
|
del comm_socket
|
|
|
|
del socket
|
|
|
|
print("disconnected!")
|
2019-06-06 13:19:14 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Some other error occured")
|
|
|
|
print("Exception: {}".format(e))
|
|
|
|
print("Shutting down ...")
|
|
|
|
u1 = u2 = 0
|
2019-06-13 11:18:46 +00:00
|
|
|
t = 0.0
|
2019-06-06 13:19:14 +00:00
|
|
|
listening = False
|
2019-06-13 11:18:46 +00:00
|
|
|
comm_socket.close()
|
|
|
|
socket.close()
|
|
|
|
del comm_socket
|
|
|
|
del socket
|
|
|
|
print("disconnected!")
|
2019-05-06 20:09:52 +00:00
|
|
|
finally:
|
|
|
|
self.m1.speed(u1)
|
|
|
|
self.m2.speed(u2)
|
2019-06-13 11:18:46 +00:00
|
|
|
time.sleep(t)
|
|
|
|
self.m1.speed(0)
|
|
|
|
self.m2.speed(0)
|
2019-04-13 12:25:41 +00:00
|
|
|
|
2019-04-14 20:07:47 +00:00
|
|
|
wall_e = Robot()
|
|
|
|
wall_e.remote_control()
|