-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
69 lines (49 loc) · 1.7 KB
/
client.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
import socket, struct
##import time
import pygame
from modules.utils import *
import json
from numpy import interp
import queue
import time
q = queue.LifoQueue(maxsize = 1)
def run_client():
# Main configuration
UDP_IP = '192.168.43.157' # Vehicle IP address
UDP_PORT = 3000 # This port match the ones using on other scripts
update_rate = 0.01 # 100 hz loop cycle
# Create UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# this triggers the training mode on the server
sock.sendto(bytes('train', 'utf-8'), (UDP_IP,UDP_PORT ))
try:
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
except:
print( "No joystick connected on the computer")
while True:
current = time.time()
elapsed = 0
# Joystick reading
pygame.event.pump()
## interp(256,[1,512],[5,10])
roll = int(interp(round(joystick.get_axis(1),3), [-1,1],[0,1020]))
yaw = int(interp(round(joystick.get_axis(3),3), [-1,1], [0,1020]))
mode = joystick.get_button(8)
if q.full():
q.queue.clear()
## q.put([yaw, roll, mode])
q.put([round(joystick.get_axis(1),3),round(joystick.get_axis(3),3),mode])
buf = str([roll,yaw])
sock.sendto(bytes(buf, 'utf-8'), (UDP_IP,UDP_PORT ))
if mode == 1:
break
# Make this loop work at update_rate
while elapsed < update_rate:
elapsed = time.time() - current
sock.sendto(bytes('disconnect', 'utf-8'), (UDP_IP, UDP_PORT))
sock.close()
sys.exit("pressed home button. Shutting down!!")