-
Notifications
You must be signed in to change notification settings - Fork 206
/
testserver.py
executable file
·77 lines (59 loc) · 1.8 KB
/
testserver.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
70
71
72
73
74
75
76
77
#!/usr/bin/env python2
import zmq
import numpy
import time
SERVER_PORT=7777
# Camera image size
CAMERA_WIDTH = 100
CAMERA_HEIGHT = 100
# Camera image shape
IMG_SHAPE = (CAMERA_WIDTH, CAMERA_HEIGHT, 3)
def sendArray(socket, array):
"""Send a numpy array with metadata over zmq"""
md = dict(
dtype = str(array.dtype),
shape = array.shape,
)
# SNDMORE flag specifies this is a multi-part message
socket.send_json(md, flags=zmq.SNDMORE)
return socket.send(array, flags=0, copy=True, track=False)
print('Starting up')
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.bind("tcp://*:%s" % SERVER_PORT)
while True:
print('Waiting for a command')
msg = socket.recv_json()
print msg
if msg['command'] == 'reset':
print('resetting the simulation')
elif msg['command'] == 'action':
print('received motor velocities')
print(msg['values'])
else:
assert False, "unknown command"
# TODO: fill in this data
# Send world position data, etc
# Note: the Gym client needs this to craft a reward function
socket.send_json(
{
# XYZ position
"position": [0, 0, 0],
# Are we properly sitting inside our lane?
"inside_lane": True,
# Are we colliding with a building or other car?
"colliding": False,
},
flags=zmq.SNDMORE
)
# Send a camera frame
img = numpy.ndarray(shape=IMG_SHAPE, dtype='uint8')
# Note: image is encoded in RGB format
# Coordinates (0,0) are at the top-left corner
for j in range(0, CAMERA_HEIGHT):
for i in range(0, CAMERA_WIDTH):
img[j, i, 0] = j # R
img[j, i, 1] = i # G
img[j, i, 2] = 0 # B
sendArray(socket, img)
time.sleep(0.05)