RoboRally/roboflut/keyboard_controller.py

44 lines
1.2 KiB
Python

import socket
import pygame
pygame.init()
pygame.display.set_mode((640, 480))
rc_socket = socket.socket()
try:
rc_socket.connect(('127.0.0.1', 1337)) # connect to robot
except socket.error:
print("could not connect to socket")
while True:
u1 = 0
u2 = 0
vmax = 10.0
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
u1 = -vmax / 10.0
u2 = vmax/ 10.0
print("turn left: ({},{})".format(u1, u2))
elif event.key == pygame.K_RIGHT:
u1 = vmax/ 10.0
u2 = -vmax/ 10.0
print("turn right: ({},{})".format(u1, u2))
elif event.key == pygame.K_UP:
u1 = vmax
u2 = vmax
print("forward: ({},{})".format(u1, u2))
elif event.key == pygame.K_DOWN:
u1 = -vmax
u2 = -vmax
print("backward: ({},{})".format(u1, u2))
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))