-
Notifications
You must be signed in to change notification settings - Fork 1
/
radar_plotter.py
executable file
·319 lines (263 loc) · 10 KB
/
radar_plotter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
# Copyright (c) 2019 Marc G Puig. All rights reserved.
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
import glob
import os
import sys
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
import argparse
import math
import time
import weakref
def get_correct_type(attr):
if attr.type == carla.ActorAttributeType.Bool:
return attr.as_bool()
if attr.type == carla.ActorAttributeType.Int:
return attr.as_int()
if attr.type == carla.ActorAttributeType.Float:
return attr.as_float()
if attr.type == carla.ActorAttributeType.String:
return attr.as_str()
if attr.type == carla.ActorAttributeType.RGBColor:
return attr.as_color()
def main():
argparser = argparse.ArgumentParser(
description=__doc__)
argparser.add_argument(
'--host',
metavar='H',
default='127.0.0.1',
help='IP of the host server (default: 127.0.0.1)')
argparser.add_argument(
'-p', '--port',
metavar='P',
default=2000,
type=int,
help='TCP port to listen to (default: 2000)')
argparser.add_argument(
'--fps',
metavar='N',
default=30,
type=int,
help='set fixed FPS, zero for variable FPS')
argparser.add_argument(
'--vertical-fov',
metavar='N',
default=30.0,
type=float,
help='radar\'s vertical FOV (default: 30.0)')
argparser.add_argument(
'--horizontal-fov',
metavar='N',
default=30.0,
type=float,
help='radar\'s horizontal FOV (default: 30.0)')
argparser.add_argument(
'--points-per-second',
metavar='N',
default=1500,
type=int,
help='radar\'s total points per second (default: 1500)')
argparser.add_argument(
'--range',
metavar='D',
default=100.0,
type=float,
help='radar\'s maximum range in meters (default: 100.0)')
argparser.add_argument(
'--time',
metavar='S',
default=10,
type=float,
help='time to run in seconds (default: 10)')
args = argparser.parse_args()
try:
client = carla.Client(args.host, args.port)
client.set_timeout(2.0)
world = client.get_world()
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = (1.0 / args.fps) if args.fps > 0.0 else 0.0
world.apply_settings(settings)
bp_lb = world.get_blueprint_library()
radar_bp = bp_lb.filter('sensor.other.radar')[0]
# Set Radar attributes, by default are:
radar_bp.set_attribute('horizontal_fov', str(args.horizontal_fov)) # degrees
radar_bp.set_attribute('vertical_fov', str(args.vertical_fov)) # degrees
radar_bp.set_attribute('points_per_second', str(args.points_per_second))
radar_bp.set_attribute('range', str(args.range)) # meters
# Display Radar attributes
print(f"{'- Radar Information ':-<45}")
print(f" - {'ID':<23}{radar_bp.id}")
for attr in radar_bp:
print(f" - {attr.id+':':<23}{get_correct_type(attr)}")
print(f"{'':-<45}")
mustang_bp = bp_lb.filter('vehicle.ford.mustang')[0]
start_location = world.get_map().get_spawn_points()[0]
mustang = world.spawn_actor(
mustang_bp,
start_location)
mustang.set_autopilot(True)
radar = world.spawn_actor(
radar_bp,
carla.Transform(location=carla.Location(y=1.5, z=0.5)),
mustang)
camera_bp = bp_lb.filter('sensor.camera.rgb')[0]
image_size = (600, 400)
camera_bp.set_attribute('image_size_x', str(image_size[0]))
camera_bp.set_attribute('image_size_y', str(image_size[1]))
camera = world.spawn_actor(
camera_bp,
carla.Transform(location=carla.Location(y=1.5, z=0.5)),
mustang)
# Ploting variables
p_depth = []
p_azimuth = []
p_altitude = []
p_velocity = []
p_images = []
def radar_callback(weak_radar, sensor):
self = weak_radar()
if not self:
return
current_depth = []
current_azimuth = []
current_altitude = []
current_velocity = []
for i in sensor:
current_depth.append(i.depth)
current_azimuth.append(i.azimuth)
current_altitude.append(i.altitude)
current_velocity.append(i.velocity)
p_depth.append(current_depth)
p_azimuth.append(current_azimuth)
p_altitude.append(current_altitude)
p_velocity.append(current_velocity)
weak_radar = weakref.ref(radar)
radar.listen(lambda sensor: radar_callback(weak_radar, sensor))
def camera_callback(weak_camera, image):
self = weak_camera()
if not self:
return
# p_images.append(image)
image.save_to_disk('_out/%08d' % image.frame)
weak_camera = weakref.ref(camera)
camera.listen(lambda image: camera_callback(weak_camera, image))
time_to_run = args.time # in seconds
initial_time = time.time()
close_time = initial_time + time_to_run
while time.time() < close_time:
sys.stdout.write(f"\rElapsed time: {time.time() - initial_time:4.1f}/{time_to_run} s")
sys.stdout.flush()
world.tick()
finally:
print('')
try:
settings.synchronous_mode = False
settings.fixed_delta_seconds = None
world.apply_settings(settings)
except NameError:
pass
try:
radar.destroy()
except NameError:
pass
try:
camera.destroy()
except NameError:
pass
try:
mustang.destroy()
except NameError:
pass
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
p_depth = np.array([np.array(i) for i in p_depth])
p_azimuth = np.array([np.array(i) for i in p_azimuth])
p_altitude = np.array([np.array(i) for i in p_altitude])
p_velocity = np.array([np.array(i) for i in p_velocity])
p_images = np.array([np.array(i.raw_data) for i in p_images])
# im = np.frombuffer(p_images[5], dtype=np.dtype("uint8"))
# im = im.reshape((image_size[1], image_size[0], 4))
# im = im[:, :, :3]
# im = im[:, :, ::-1]
# plt.imshow(im)
# plt.show()
# sys.exit(0)
# Animation ##################################################
fig, (ax_depth, ax_vel) = plt.subplots(nrows=1, ncols=2, figsize=(14, 6))
half_hor_fov_to_deg = math.radians(args.horizontal_fov) / 2.0
half_ver_fov_to_deg = math.radians(args.vertical_fov) / 2.0
horizontal_axes_limits = (-half_hor_fov_to_deg, half_hor_fov_to_deg)
vertical_axes_limits = (-half_ver_fov_to_deg, half_ver_fov_to_deg)
# - Depth Axes ----------------------------------
ax_depth.set(xlim=horizontal_axes_limits, ylim=vertical_axes_limits)
ax_depth.set_title('Depth (m)')
ax_depth.set_xlabel('Azimuth (rad)')
ax_depth.set_ylabel('Altitude (rad)')
# Initialize the depth scater plot without data
scat_depth = ax_depth.scatter(
np.array([]),
np.array([]),
c=np.array([]), # dot intensity/color
s=20, # dot size
vmin=0.0, # colorize from 0 m
vmax=args.range, # to Radar's maximum disance (far)
cmap='viridis') # viridis, plasma, gray...
fig.colorbar(scat_depth, ax=ax_depth, extend='max', shrink=1.0, pad=0.01)
# - Velocity Axes -------------------------------
ax_vel.set(xlim=horizontal_axes_limits, ylim=vertical_axes_limits)
ax_vel.set_title('Velocity (m/s)')
ax_vel.set_xlabel('Azimuth (rad)')
ax_vel.set_ylabel('Altitude (rad)')
# Initialize the velocity scater plot without data
scat_vel = ax_vel.scatter(
np.array([]),
np.array([]),
c=np.array([]), # dot intensity/color
s=20, # dot size
vmin=-10.0, # colorize from -10 m/s
vmax=10.0, # to +10 m/s
cmap='RdBu') # RdBu, Spectral, coolwarm...
fig.colorbar(scat_vel, ax=ax_vel, extend='both', shrink=1.0, pad=0.01)
# Animation initialization callback
def acc_init():
scat_depth.set_array(p_depth[0])
scat_vel.set_array(p_velocity[0])
scat_depth.set_offsets(np.c_[p_azimuth[0], p_altitude[0]])
scat_vel.set_offsets(np.c_[p_azimuth[0], p_altitude[0]])
return scat_depth, scat_vel
# Animation update callback
def radar_anim_update(i):
scat_depth.set_array(p_depth[i])
scat_vel.set_array(p_velocity[i])
scat_depth.set_offsets(np.c_[p_azimuth[i], p_altitude[i]])
scat_vel.set_offsets(np.c_[p_azimuth[i], p_altitude[i]])
return scat_depth, scat_vel
# Do the animation
radar_animation = animation.FuncAnimation(
fig,
radar_anim_update,
init_func=acc_init,
frames=len(p_azimuth),
interval=(1.0 / args.fps) if args.fps > 0.0 else 0.0,
blit=True,
repeat=True)
# Store the animation as mp4
radar_animation.save(
f"./radar_plot{time.time()}.mp4",
fps=args.fps,
extra_args=["-vcodec", "libx264"])
##############################################################
if __name__ == '__main__':
main()