improved keyboard controller script. added quit option and option to pass ip as command line argument

This commit is contained in:
bt304019 2019-08-12 16:42:57 +02:00
parent 8484264a65
commit 2987da4373

View File

@ -1,20 +1,29 @@
import socket
import pygame
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('ip', metavar='ip', type=str, help='ip address of the controlled robot')
args = parser.parse_args()
ip = args.ip
pygame.init()
pygame.display.set_mode((640, 480))
rc_socket = socket.socket()
try:
rc_socket.connect((ip, 1234)) # connect to robot
#rc_socket.connect(('192.168.4.1', 1234)) # connect to robot
#rc_socket.connect(('192.168.1.101', 1234)) # connect to robot
#rc_socket.connect(('192.168.1.102', 1234)) # connect to robot
rc_socket.connect(('192.168.1.103', 1234)) # connect to robot
#rc_socket.connect(('192.168.1.103', 1234)) # connect to robot
except socket.error:
print("could not connect to socket")
while True:
running = True
while running:
u1 = 0
u2 = 0
vmax = 1.0
@ -37,7 +46,14 @@ while True:
u1 = -vmax
u2 = -vmax
print("backward: ({},{})".format(u1, u2))
elif event.key == pygame.K_ESCAPE:
print("quit")
running = False
u1 = 0.0
u2 = 0.0
rc_socket.send('({},{})\n'.format(u1, u2))
elif event.type == pygame.KEYUP:
print("key released, resetting: ({},{})".format(u1, u2))
rc_socket.send('({},{})\n'.format(u1, u2))