-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
87 lines (65 loc) · 2.59 KB
/
client.py
File metadata and controls
87 lines (65 loc) · 2.59 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import json
import pickle
import time
import argparse
from typing import Any, List, Tuple
import requests
from f110_gym.envs.rendering import EnvRenderer
parser = argparse.ArgumentParser(description='Live racing game visualizer')
parser.add_argument('token', type=int)
parser.add_argument('host', type=str)
parser.add_argument('--port', type=int, default=8000)
args = parser.parse_args()
SERVER_URL = f'http://{args.host}:{args.port}'
TEAM_TOKEN = args.token
WINDOW_W = 1000
WINDOW_H = 800
renderer = EnvRenderer(WINDOW_W, WINDOW_H)
renderer.update_map('example_map', '.png')
def get_observation() -> dict:
r = requests.get(f'{SERVER_URL}/')
if r.status_code == 403:
raise Exception('Invalid team token')
return pickle.loads(r.content)
def send_command(speed, steer):
data= {
'cmd': {'speed': speed, 'steer': steer},
'team_token': TEAM_TOKEN
}
requests.post(f'{SERVER_URL}/', data=json.dumps(data))
def render(o: Any, my_idx: int) -> None:
# So that our car has a unique color
o['ego_idx'] = my_idx
renderer.update_obs(o)
renderer.dispatch_events()
renderer.on_draw()
renderer.flip()
class MyController:
def __init__(self) -> None:
self.speed = 7
self.steer_amplitude = 0.2
def get_controls(self, x: float, y: float, theta: float, scan: List[float]) -> Tuple[float, float]:
"""
x, y and theta are basically what you expect them to be.
scan is a list of 100 distances to obstacles in the car's FOV,
such that the first element is the distance at the angle -2.35 rad and the last at 2.35 rad.
The remaining elements are the distances at evenly spaced angles between those two.
"""
# Implement your own logic here, this is just an example that works reasonably well
l_sum = sum(scan[30:40])
r_sum = sum(scan[60:70])
steer = (-1 if l_sum > r_sum else 1) * self.steer_amplitude
return self.speed, steer
def main():
my_idx = requests.get(f'{SERVER_URL}/team_id', params={'team_token': TEAM_TOKEN}).json()
controller = MyController()
while True:
o = get_observation()
# you can disable rendering if you wish to, but it won't speed things up
# because physics is simulated on the server
render(o, my_idx)
my_x, my_y, my_theta, my_scan = o['poses_x'][my_idx], o['poses_y'][my_idx], o['poses_theta'][my_idx], o['scans'][my_idx]
speed, steer = controller.get_controls(my_x, my_y, my_theta, my_scan)
send_command(speed, steer)
if __name__ == "__main__":
main()