extended remote control to work with keyboard arrow keys

This commit is contained in:
Simon Pirkelmann 2019-04-14 22:07:47 +02:00
parent 80ee257a7d
commit f0f846dc04
2 changed files with 75 additions and 32 deletions

View File

@ -1,5 +1,6 @@
import machine import machine
import time import time
import usocket
class Motor: class Motor:
def __init__(self, enable, dir1, dir2): def __init__(self, enable, dir1, dir2):
@ -34,6 +35,11 @@ class Robot:
print("setting up motors") print("setting up motors")
self.m1 = Motor(4, 16, 5) self.m1 = Motor(4, 16, 5)
self.m2 = Motor(14, 0, 2) self.m2 = Motor(14, 0, 2)
# wait for connections on port 1234
self.socket = usocket.socket()
self.addr = usocket.getaddrinfo('192.168.4.1', 1234)[0][-1]
self.socket.bind(self.addr)
print("setup complete") print("setup complete")
def forward(self, seconds): def forward(self, seconds):
@ -74,31 +80,43 @@ class Robot:
self.forward(0.5) self.forward(0.5)
self.backward(0.5) self.backward(0.5)
wall_e = Robot() def remote_control(self):
print("waiting for connections on {}".format(self.addr))
import usocket self.socket.listen(1)
res = self.socket.accept() # this blocks until someone connects to the socket
# 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] comm_socket = res[0]
print("connected!")
# read data until 'q\n' is sent
listening = True listening = True
while listening: while listening:
# expected data: '(u1, u2)'\n"
# where ui = control for motor i
# ui \in [-1.0, 1.0]
data = comm_socket.readline() data = comm_socket.readline()
data_str = data.decode() data_str = data.decode()
print(data_str) print("Data received: {}".format(data_str))
if data_str == 'f\n': try:
wall_e.forward(0.5) print("processing data = {}".format(data_str))
elif data_str == 'b\n': l = data_str.strip('()\n').split(',')
wall_e.backward(0.5) print("l = {}".format(l))
elif data_str == 'l\n': u1 = float(l[0])
wall_e.spin_left(0.5) print("u1 = {}".format(u1))
elif data_str == 'r\n': u2 = float(l[1])
wall_e.spin_right(0.5) print("u2 = {}".format(u2))
elif data_str == 'q\n': except ValueError:
print("ValueError: Data has wrong format.")
print("Data received: {}".format(data_str))
print("Shutting down")
u1 = u2 = 0.0
listening = False listening = False
except IndexError:
print("IndexError: Data has wrong format.")
print("Data received: {}".format(data_str))
print("Shutting down")
u1 = u2 = 0.0
listening = False
finally:
self.m1.control(u1)
self.m2.control(u2)
wall_e = Robot()
wall_e.remote_control()

View File

@ -1,10 +1,35 @@
import socket import socket
import pygame
rc_socket = socket.socket() rc_socket = socket.socket()
rc_socket.connect(('192.168.4.1', 1234)) # connect to robot rc_socket.connect(('192.168.4.1', 1234)) # connect to robot
rc_socket.send('l\n') # left pygame.init()
rc_socket.send('r\n') # right pygame.display.set_mode()
rc_socket.send('f\n') # forward
rc_socket.send('b\n') # backward while True:
rc_socket.send('q\n') # quit events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
u1 = -1.0
u2 = 1.0
print("turn left: ({},{})".format(u1, u2))
elif event.key == pygame.K_RIGHT:
u1 = 1.0
u2 = -1.0
print("turn right: ({},{})".format(u1, u2))
elif event.key == pygame.K_UP:
u1 = 1.0
u2 = 1.0
print("forward: ({},{})".format(u1, u2))
elif event.key == pygame.K_DOWN:
u1 = -1.0
u2 = -1.0
print("forward: ({},{})".format(u1, u2))
rc_socket.send('({},{})\n'.format(u1, u2))
elif event.type == pygame.KEYUP:
u1 = 0.0
u2 = 0.0
print("key released, resetting: ({},{})".format(u1, u2))
rc_socket.send('({},{})\n'.format(u1, u2))