fixed problems with simultaneous inputs not being handled and gamepads not detected
This commit is contained in:
parent
67405fbd3f
commit
9c222b1099
|
@ -13,15 +13,34 @@ class robot: # we have 4 arg for this class, because joysticks get the same (va
|
|||
self.robot0_stopped_3 = True
|
||||
self.robot0_stopped_4 = True
|
||||
self.rc_socket = socket.socket()
|
||||
self.u1 = 0
|
||||
self.u2 = 0
|
||||
try:
|
||||
self.rc_socket.connect((self.ip, self.port))
|
||||
except socket.error():
|
||||
print("couldn't connect to socket")
|
||||
|
||||
self.check_joystick = False
|
||||
def joystick_init(self): # Joystick's initialisation
|
||||
joystick_count = pygame.joystick.get_count()
|
||||
for count in range(joystick_count):
|
||||
if joystick_count == 2:
|
||||
self.joystick = pygame.joystick.Joystick(self.joy)
|
||||
print("{}-->joystick count".format(joystick_count))
|
||||
self.joystick.init()
|
||||
self.axes = self.joystick.get_numaxes()
|
||||
self.check_joystick = True
|
||||
elif joystick_count == 1:
|
||||
joystick = pygame.joystick.Joystick(0)
|
||||
joystick.init()
|
||||
print("connected only 1 joystick - {}".format(joystick))
|
||||
self.check_joystick = False
|
||||
elif not joystick_count:
|
||||
print("no joysticks connected")
|
||||
self.check_joystick = False
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def control(self, event): # the control of two robots with joysticks
|
||||
joy = event.joy
|
||||
|
@ -74,55 +93,56 @@ class robot: # we have 4 arg for this class, because joysticks get the same (va
|
|||
self.robot0_stopped_4 = True
|
||||
|
||||
def control_keyboard(self, event): # keyboard control for robot1
|
||||
command_received = False
|
||||
if event.key == pygame.K_LEFT:
|
||||
|
||||
if pressed[pygame.K_LEFT]:
|
||||
u1 = -1.0
|
||||
u2 = 1.0
|
||||
command_received = True
|
||||
elif event.key == pygame.K_RIGHT:
|
||||
u1 = 1.0
|
||||
u2 = -1.0
|
||||
command_received = True
|
||||
elif event.key == pygame.K_UP:
|
||||
u1 = -1.0
|
||||
u2 = -1.0
|
||||
command_received = True
|
||||
elif event.key == pygame.K_DOWN:
|
||||
u1 = 1.0
|
||||
u2 = 1.0
|
||||
command_received = True
|
||||
if command_received:
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
def control_keyboard_2(self, event): # keyboard control for robot2
|
||||
|
||||
command_received = False
|
||||
if event.key == pygame.K_a:
|
||||
u1 = -1.0
|
||||
u2 = 1.0
|
||||
command_received = True
|
||||
elif event.key == pygame.K_d:
|
||||
elif pressed[pygame.K_RIGHT]:
|
||||
u1 = 1.0
|
||||
u2 = -1.0
|
||||
command_received = True
|
||||
elif event.key == pygame.K_w:
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
elif pressed[pygame.K_UP]:
|
||||
u1 = -1.0
|
||||
u2 = -1.0
|
||||
command_received = True
|
||||
elif event.key == pygame.K_s:
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
elif pressed[pygame.K_DOWN]:
|
||||
u1 = 1.0
|
||||
u2 = 1.0
|
||||
command_received = True
|
||||
if command_received:
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
def control_keyboard_stop(self): # stop for both robot
|
||||
elif event.type == pygame.KEYUP:
|
||||
self.rc_socket.send('({},{})\n'.format(self.u1, self.u2).encode())
|
||||
|
||||
u1 = 0
|
||||
u2 = 0
|
||||
print("key released, resetting: ({},{})".format(u1, u2))
|
||||
def control_keyboard_2(self, event): # keyboard control for robot1
|
||||
|
||||
if pressed[pygame.K_a]:
|
||||
u1 = -1.0
|
||||
u2 = 1.0
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
elif pressed[pygame.K_d]:
|
||||
u1 = 1.0
|
||||
u2 = -1.0
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
elif pressed[pygame.K_w]:
|
||||
u1 = -1.0
|
||||
u2 = -1.0
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
elif pressed[pygame.K_s]:
|
||||
u1 = 1.0
|
||||
u2 = 1.0
|
||||
self.rc_socket.send('({},{})\n'.format(u1, u2).encode())
|
||||
|
||||
elif event.type == pygame.KEYUP:
|
||||
self.rc_socket.send('({},{})\n'.format(self.u1, self.u2).encode())
|
||||
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
pygame.display.set_mode((640, 480))
|
||||
|
@ -131,19 +151,18 @@ def main():
|
|||
robot_1.joystick_init()
|
||||
robot_2 = robot(1, '192.168.1.103', 1234)
|
||||
robot_2.joystick_init()
|
||||
|
||||
while True:
|
||||
events = pygame.event.get()
|
||||
global pressed
|
||||
pressed = pygame.key.get_pressed()
|
||||
for event in events:
|
||||
if event.type == pygame.JOYAXISMOTION:
|
||||
robot_1.control(event)
|
||||
robot_2.control(event)
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
else:
|
||||
robot_1.control_keyboard(event)
|
||||
robot_2.control_keyboard_2(event)
|
||||
elif event.type == pygame.KEYUP:
|
||||
robot_1.control_keyboard_stop()
|
||||
robot_2.control_keyboard_stop()
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue
Block a user