-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunch_multi_ego_with_ros_pub.py
383 lines (301 loc) · 17.6 KB
/
launch_multi_ego_with_ros_pub.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
""" Launces the ego vehicle.
This script launches the ego-vechile (currently a Tesla Model 3) in Town-02. It returns an ego
vehicle ID to be used while controlling, using control.py.
USAGE:
python3 launch_ego_vehicle.py [--host] [--port] [--save_lidar_data]
"""
import rospy
import carla
import argparse
import random
import numpy as np
# from custom_locations import spawn_points_custom
from pathlib import Path
import sys
sys.path.append('../')
from util.loam_pub_sub import LoamPubSub
rospy.init_node('Carla')
loam_pub_sub = LoamPubSub()
# Town2 - Near a cross road
# ego_transform = carla.Transform(carla.Location(x=-78.116066, y=-81.958496, z=-0.696164),
# carla.Rotation(pitch=1.174273, yaw=-90.156158, roll=0.000019))
# ego_transform = carla.Transform(carla.Location(x=166.122238, y=106.114136, z=0.821694), carla.Rotation(pitch=0.000000, yaw=-177.648560, roll=0.000014))
# ego_transform = carla.Transform(carla.Location(x=166.122238, y=106.114136, z=0.821694), carla.Rotation(pitch=0.000000, yaw=-177.648560, roll=0.000014))
# Town03 - ego + cars in front, left and right
# define as global variable to automatically obtain # of cars in the control script
# ego_transforms = [
# carla.Transform(carla.Location(x=93.220924, y=198.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=93.220924, y=195.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=93.220924, y=201.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=85.220924, y=194.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=85.220924, y=198.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=85.220924, y=202.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=100.220924, y=194.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=100.220924, y=198.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# # carla.Transform(carla.Location(x=100.220924, y=202.343231, z=1.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# ]
# ego_transforms = [
# carla.Transform(carla.Location(x=187.220924, y=198.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=187.220924, y=195.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=187.220924, y=201.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=182.220924, y=198.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=182.220924, y=195.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=182.220924, y=201.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=192.220924, y=198.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=192.220924, y=195.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=192.220924, y=201.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578))
# ]
# ego_transforms = [
# carla.Transform(carla.Location(x=187.220924, y=198.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=167.220924, y=195.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=167.220924, y=201.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=162.220924, y=195.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=162.220924, y=201.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=192.220924, y=195.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578)),
# carla.Transform(carla.Location(x=192.220924, y=201.343231, z=3.553675), carla.Rotation(pitch=-1.277402, yaw=-179.359268, roll=-0.017578))
# ]
# ego_transforms =[
# # Narrow Road Scene
# # EGO
# carla.Transform(carla.Location(x=-145.406204, y=105.677322, z=1.501337), carla.Rotation(pitch=0.000000, yaw=-89.725250, roll=0.000011)),
# # # Traffic
# # carla.Transform(carla.Location(x=-149.446106, y=50.746162, z=1.501142), carla.Rotation(pitch=-0.000219, yaw=-89.357307, roll=0.000015)),
# # carla.Transform(carla.Location(x=-149.511078, y=45.975445, z=1.501333), carla.Rotation(pitch=0.001079, yaw=-89.357307, roll=0.000014)),
# # carla.Transform(carla.Location(x=-149.511078, y=40.975445, z=1.501333), carla.Rotation(pitch=0.001079, yaw=-89.357307, roll=0.000014)),
# # carla.Transform(carla.Location(x=-142.446106, y=50.746162, z=1.501142), carla.Rotation(pitch=-0.000219, yaw=-89.357307, roll=0.000015)),
# # carla.Transform(carla.Location(x=-142.511078, y=45.975445, z=1.501333), carla.Rotation(pitch=0.001079, yaw=-89.357307, roll=0.000014)),
# # carla.Transform(carla.Location(x=-142.511078, y=40.975445, z=1.501333), carla.Rotation(pitch=0.001079, yaw=-89.357307, roll=0.000014)),
# ]
# ego_transforms =[
# # Inclination
# # EGO
# carla.Transform(carla.Location(x=-36.135696, y=-143.153244, z=1.001348), carla.Rotation(pitch=-0.001434, yaw=1.013101, roll=0.000015))
# ]
# ego_transforms =[
# # Plane Wall Lane, Feature Lane Scene
# # Ego
# # carla.Transform(carla.Location(x=60.692669, y=-10.527537, z=1.501337), carla.Rotation(pitch=0.000382, yaw=-178.177094, roll=0.000015)),
# carla.Transform(carla.Location(x=-22.200941, y=-11.201141, z=1.501347), carla.Rotation(pitch=-0.001311, yaw=-41.370190, roll=0.000016)),
# ]
# ego_transforms = [
# # Wide intersection scene scene
# # ego
# carla.Transform(carla.Location(x=-78.722710, y=168.484650, z=2.501263), carla.Rotation(pitch=-0.001468, yaw=-89.411835, roll=0.004054)),
# # Close to building
# # carla.Transform(carla.Location(x=-71.104912, y=116.512695, z=1.513339), carla.Rotation(pitch=0.662153, yaw=-88.713875, roll=0.070444)),
# # traffic
# # carla.Transform(carla.Location(x=-78.722710, y=168.484650, z=1.501263), carla.Rotation(pitch=-0.001468, yaw=-89.411835, roll=0.004054)),
# # carla.Transform(carla.Location(x=-78.722710, y=168.484650, z=1.501263), carla.Rotation(pitch=-0.001468, yaw=-89.411835, roll=0.004054)),
# # carla.Transform(carla.Location(x=-78.722710, y=168.484650, z=1.501263), carla.Rotation(pitch=-0.001468, yaw=-89.411835, roll=0.004054)),
# # carla.Transform(carla.Location(x=-78.722710, y=168.484650, z=1.501263), carla.Rotation(pitch=-0.001468, yaw=-89.411835, roll=0.004054)),
# # carla.Transform(carla.Location(x=-78.722710, y=168.484650, z=1.501263), carla.Rotation(pitch=-0.001468, yaw=-89.411835, roll=0.004054)),
# # carla.Transform(carla.Location(x=-78.722710, y=168.484650, z=1.501263), carla.Rotation(pitch=-0.001468, yaw=-89.411835, roll=0.004054))
# ]
# ego_transforms = [
# # Wide plane road, vehicles ahead
# # Ego
# carla.Transform(carla.Location(x=150.072968, y=200.271362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
# carla.Transform(carla.Location(x=140.072968, y=206.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
# carla.Transform(carla.Location(x=135.072968, y=206.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
# carla.Transform(carla.Location(x=130.072968, y=206.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
# carla.Transform(carla.Location(x=140.072968, y=193.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
# carla.Transform(carla.Location(x=135.072968, y=193.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
# carla.Transform(carla.Location(x=130.072968, y=193.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
# ]
ego_transforms = [
# Wide plane road, vehicles with
# Ego
carla.Transform(carla.Location(x=155.072968, y=200.271362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
carla.Transform(carla.Location(x=160.072968, y=206.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
carla.Transform(carla.Location(x=155.072968, y=206.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
carla.Transform(carla.Location(x=150.072968, y=206.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
carla.Transform(carla.Location(x=160.072968, y=193.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
carla.Transform(carla.Location(x=155.072968, y=193.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
carla.Transform(carla.Location(x=150.072968, y=193.871362, z=3.684593), carla.Rotation(pitch=-1.183624, yaw=179.648993, roll=-0.084534)),
]
def process_point_cloud(point_cloud_carla):
# Creating a numpy array as well. To be used later
pcd = np.copy(np.frombuffer(point_cloud_carla.raw_data, dtype=np.dtype('float32')))
pcd = np.reshape(pcd, (int(pcd.shape[0] / 4), 4))
loam_pub_sub.publish_points(pcd)
def dummy_function(image):
pass
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(
'--town',
default='Town03',
help='Spawn in Town01, Town02 or Town03'
)
args = argparser.parse_args()
# keyboard = Keyboard(0.05)
client = carla.Client(args.host, args.port)
client.set_timeout(10.0)
client.load_world(args.town)
# Setting synchronous mode
# This is essential for proper workiong of sensors
world = client.get_world()
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05 # FPS = 1/0.05 = 20
world.apply_settings(settings)
try:
world = client.get_world()
ego_vehicles = []
ego_cam = None
ego_col = None
ego_lane = None
ego_obs = None
ego_gnss = None
ego_imu = None
dummy = None
# --------------
# Start recording
# --------------
"""
client.start_recorder('~/tutorial/recorder/recording01.log')
"""
# --------------
# Spawn ego vehicle
# --------------
# vehicles_list = custom_spawn(world)
num_vehicles = len(ego_transforms)
ego_bps = []
for i in range(num_vehicles):
ego_bps.append(world.get_blueprint_library().find('vehicle.tesla.model3'))
for i in range(num_vehicles):
ego_bps[i].set_attribute('role_name', 'ego')
print('\nEgo role_names are set')
for i in range(num_vehicles):
ego_color = random.choice(ego_bps[i].get_attribute('color').recommended_values)
ego_bps[i].set_attribute('color', ego_color)
print('\nEgo colors are set')
# spawn_points = world.get_map().get_spawn_points()
# number_of_spawn_points = len(spawn_points)
# spawn num_vehicles vehicles
for i in range(num_vehicles):
ego_vehicles.append(world.spawn_actor(ego_bps[i], ego_transforms[i]))
# --------------
# Add a new LIDAR sensor to my ego
# --------------
# VLP 16
lidar_cam = None
lidar_bp = world.get_blueprint_library().find('sensor.lidar.ray_cast')
lidar_bp.set_attribute('channels',str(16))
lidar_bp.set_attribute('rotation_frequency',str(20)) # Set the fps of simulator same as this
lidar_bp.set_attribute('range',str(50))
lidar_bp.set_attribute('lower_fov', str(-15))
lidar_bp.set_attribute('upper_fov', str(15))
lidar_bp.set_attribute('points_per_second',str(300000))
lidar_bp.set_attribute('dropoff_general_rate',str(0.0))
# lidar_bp.set_attribute('noise_stddev',str(0.173))
# lidar_bp.set_attribute('noise_stddev',str(0.141)) Works in this case
lidar_location = carla.Location(0,0,2)
lidar_rotation = carla.Rotation(0,0,0)
lidar_transform = carla.Transform(lidar_location,lidar_rotation)
lidar_sen = world.spawn_actor(lidar_bp,lidar_transform,attach_to=ego_vehicles[0])
lidar_sen.listen(lambda point_cloud: process_point_cloud(point_cloud))
# --------------
# Enable autopilot for ego vehicle
# --------------
for i in range(num_vehicles):
ego_vehicles[i].set_autopilot(False)
# --------------
# Dummy Actor for spectator
# --------------
dummy_bp = world.get_blueprint_library().find('sensor.camera.rgb')
dummy_transform = carla.Transform(carla.Location(x=-6, z=4), carla.Rotation(pitch=10.0))
dummy = world.spawn_actor(dummy_bp, dummy_transform, attach_to=ego_vehicles[0], attachment_type=carla.AttachmentType.SpringArm)
dummy.listen(lambda image: dummy_function(image))
spectator = world.get_spectator()
spectator.set_transform(dummy.get_transform())
gt_array = []
# --------------
# Game loop. Prevents the script from finishing.
# --------------
count = -1
while True:
# This is for async mode
# world_snapshot = world.wait_for_tick()
# In synchronous mode, the client ticks the world
world.tick()
count+= 1
if count == 0:
for i in range(50):
world.tick() # Sometimes the vehicle is spawned at a height
start_tf = ego_vehicles[0].get_transform()
start_tf = ego_vehicles[0].get_transform()
tf_matrix = np.array(start_tf.get_matrix())
print('tf_matrix', tf_matrix)
start_vec = np.array([start_tf.location.x, start_tf.location.y, start_tf.location.z, 1]).reshape(-1,1)
print('Start Vec:', start_vec)
print('After TF:\n', np.linalg.pinv(tf_matrix) @ start_vec)
# T_start = np.array(start_tf.get_inverse_matrix()) Can be used?
# print(start_tf.transform(start_tf.location))
print('Ego Vehicle IDs:')
[print(ego_vehicles[i].id, end=' ') for i in range(num_vehicles)]
print()
input('Continue?: ')
# print(start_tf.transform(ego_vehicle.get_transform().location))
spectator.set_transform(dummy.get_transform())
vehicle_tf = ego_vehicles[0].get_transform()
vehicle_tf_loc = np.array([vehicle_tf.location.x, vehicle_tf.location.y, vehicle_tf.location.z, 1]).reshape(-1,1)
vehicle_tf_odom = np.linalg.pinv(tf_matrix) @ vehicle_tf_loc
vehicle_tf_odom = vehicle_tf_odom.flatten()[:-1]
vehicle_tf_odom[1] = - vehicle_tf_odom[1] # Y axis flip is required
loam_pub_sub.publish_gt(*vehicle_tf_odom.flatten()[:-1])
except Exception as e:
print(e)
finally:
# --------------
# Stop recording and destroy actors
# --------------
client.stop_recorder()
if ego_vehicles is not None:
print('Here')
if ego_cam is not None:
ego_cam.stop()
ego_cam.destroy()
if ego_col is not None:
ego_col.stop()
ego_col.destroy()
if ego_lane is not None:
ego_lane.stop()
ego_lane.destroy()
if ego_obs is not None:
ego_obs.stop()
ego_obs.destroy()
if ego_gnss is not None:
ego_gnss.stop()
ego_gnss.destroy()
if ego_imu is not None:
ego_imu.stop()
ego_imu.destroy()
if lidar_sen is not None:
lidar_sen.stop()
lidar_sen.destroy()
if dummy is not None:
dummy.stop()
dummy.destroy()
print('Here Here')
[ego_vehicles[i].destroy() for i in range(num_vehicles)]
world.tick()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('\nDone with launch_multi_ego_vehicles.')