2019-05-06 20:23:31 +00:00
|
|
|
import socket
|
|
|
|
import pygame
|
2019-08-12 14:42:57 +00:00
|
|
|
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
|
2019-05-06 20:23:31 +00:00
|
|
|
|
|
|
|
pygame.init()
|
|
|
|
pygame.display.set_mode((640, 480))
|
|
|
|
|
|
|
|
rc_socket = socket.socket()
|
|
|
|
try:
|
2019-08-12 14:42:57 +00:00
|
|
|
rc_socket.connect((ip, 1234)) # connect to robot
|
2019-05-06 20:23:31 +00:00
|
|
|
except socket.error:
|
|
|
|
print("could not connect to socket")
|
|
|
|
|
2019-08-12 14:42:57 +00:00
|
|
|
running = True
|
|
|
|
while running:
|
2019-06-05 06:06:11 +00:00
|
|
|
u1 = 0
|
|
|
|
u2 = 0
|
2019-06-26 08:49:05 +00:00
|
|
|
vmax = 1.0
|
2019-05-06 20:23:31 +00:00
|
|
|
events = pygame.event.get()
|
|
|
|
for event in events:
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_LEFT:
|
2019-06-11 14:28:01 +00:00
|
|
|
u1 = -vmax
|
|
|
|
u2 = vmax
|
2020-10-24 18:05:00 +00:00
|
|
|
print(f"turn left: ({u1},{u2})")
|
2019-05-06 20:23:31 +00:00
|
|
|
elif event.key == pygame.K_RIGHT:
|
2019-06-11 14:28:01 +00:00
|
|
|
u1 = vmax
|
|
|
|
u2 = -vmax
|
2020-10-24 18:05:00 +00:00
|
|
|
print(f"turn right: ({u1},{u2})")
|
2019-05-06 20:23:31 +00:00
|
|
|
elif event.key == pygame.K_UP:
|
2019-06-11 14:28:01 +00:00
|
|
|
u1 = vmax
|
|
|
|
u2 = vmax
|
2020-10-24 18:05:00 +00:00
|
|
|
print(f"forward: ({u1},{u2})")
|
2019-05-06 20:23:31 +00:00
|
|
|
elif event.key == pygame.K_DOWN:
|
2019-06-11 14:28:01 +00:00
|
|
|
u1 = -vmax
|
|
|
|
u2 = -vmax
|
2020-10-24 18:05:00 +00:00
|
|
|
print(f"backward: ({u1},{u2})")
|
2019-08-12 14:42:57 +00:00
|
|
|
elif event.key == pygame.K_ESCAPE:
|
|
|
|
print("quit")
|
2020-10-24 18:05:00 +00:00
|
|
|
running = False
|
2019-08-12 14:42:57 +00:00
|
|
|
u1 = 0.0
|
|
|
|
u2 = 0.0
|
2020-10-24 18:05:00 +00:00
|
|
|
rc_socket.send(f'({u1},{u2})\n'.encode())
|
2019-05-06 20:23:31 +00:00
|
|
|
elif event.type == pygame.KEYUP:
|
|
|
|
print("key released, resetting: ({},{})".format(u1, u2))
|
2020-10-24 18:05:00 +00:00
|
|
|
rc_socket.send(f'({u1},{u2})\n'.encode())
|
2019-08-12 14:42:57 +00:00
|
|
|
|
|
|
|
|