-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoy-stick-controll.py
48 lines (40 loc) · 1.21 KB
/
joy-stick-controll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import socket
import pygame
# init Socket
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
serverAddressPort = ("192.168.43.130", 20001)
bufferSize = 32
# init pygame
pygame.init()
done = False
clock = pygame.time.Clock()
pygame.joystick.init()
AI_MODE = False
while not done:
joystick = pygame.joystick.Joystick(0)
joystick.init()
'''
for b in range(joystick.get_numbuttons()):
if joystick.get_button(b) == 1:
print(f'Button {b}: {joystick.get_button(b)}')
'''
variables = None
if joystick.get_button(1) == 1:
AI_MODE = not AI_MODE
print("here")
if AI_MODE:
# AI MODE
variables = {'speed': 123, 'steer': 0}
else:
if joystick.get_button(0) == 1:
speed = -1*round(joystick.get_axis(3),2)
if speed < 0.15 and speed > -0.15:
speed = 0
variables = {'speed': speed, 'steer': round(joystick.get_axis(0),2)}
else:
variables = {'speed': 0, 'steer': round(joystick.get_axis(0),2)}
print(variables)
sendBytes = str.encode(str(variables))
UDPClientSocket.sendto(sendBytes, serverAddressPort)
clock.tick(50)
pygame.quit()