added wireless control via sockets

This commit is contained in:
Simon Pirkelmann 2019-04-13 14:25:41 +02:00
parent 157c61ad87
commit 254a4e25ae

View File

@ -54,16 +54,16 @@ class Robot:
def spin_right(self, seconds):
print("You spin my head right round, right round..")
self.m1.control(0.8)
self.m2.control(-0.8)
self.m1.control(1.0)
self.m2.control(-1.0)
time.sleep(seconds)
self.m1.control(0.0)
self.m2.control(0.0)
def spin_left(self, seconds):
print("spinning left")
self.m1.control(-0.8)
self.m2.control(0.8)
self.m1.control(-1.0)
self.m2.control(1.0)
time.sleep(seconds)
self.m1.control(0.0)
self.m2.control(0.0)
@ -75,3 +75,30 @@ class Robot:
self.backward(0.5)
wall_e = Robot()
import usocket
# wait for connections on port 1234
mysocket = usocket.socket()
myaddr = usocket.getaddrinfo('192.168.4.1', 1234)[0][-1]
mysocket.bind(myaddr)
mysocket.listen(1)
res = mysocket.accept() # this blocks until someone connects to the socket
comm_socket = res[0]
# read data until 'q\n' is sent
listening = True
while listening:
data = comm_socket.readline()
data_str = data.decode()
print(data_str)
if data_str == 'f\n':
wall_e.forward(0.5)
elif data_str == 'b\n':
wall_e.backward(0.5)
elif data_str == 'l\n':
wall_e.spin_left(0.5)
elif data_str == 'r\n':
wall_e.spin_right(0.5)
elif data_str == 'q\n':
listening = False