59 lines
2.1 KiB
Python
Executable File
59 lines
2.1 KiB
Python
Executable File
# 0. Choose a blueprint fo the walkers
|
|
world = client.get_world()
|
|
blueprintsWalkers = world.get_blueprint_library().filter("walker.pedestrian.*")
|
|
walker_bp = random.choice(blueprintsWalkers)
|
|
|
|
# 1. Take all the random locations to spawn
|
|
spawn_points = []
|
|
for i in range(50):
|
|
spawn_point = carla.Transform()
|
|
spawn_point.location = world.get_random_location_from_navigation()
|
|
if (spawn_point.location != None):
|
|
spawn_points.append(spawn_point)
|
|
|
|
# 2. Build the batch of commands to spawn the pedestrians
|
|
batch = []
|
|
for spawn_point in spawn_points:
|
|
walker_bp = random.choice(blueprintsWalkers)
|
|
batch.append(carla.command.SpawnActor(walker_bp, spawn_point))
|
|
|
|
# 2.1 apply the batch
|
|
results = client.apply_batch_sync(batch, True)
|
|
for i in range(len(results)):
|
|
if results[i].error:
|
|
logging.error(results[i].error)
|
|
else:
|
|
walkers_list.append({"id": results[i].actor_id})
|
|
|
|
# 3. Spawn walker AI controllers for each walker
|
|
batch = []
|
|
walker_controller_bp = world.get_blueprint_library().find('controller.ai.walker')
|
|
for i in range(len(walkers_list)):
|
|
batch.append(carla.command.SpawnActor(walker_controller_bp, carla.Transform(), walkers_list[i]["id"]))
|
|
|
|
# 3.1 apply the batch
|
|
results = client.apply_batch_sync(batch, True)
|
|
for i in range(len(results)):
|
|
if results[i].error:
|
|
logging.error(results[i].error)
|
|
else:
|
|
walkers_list[i]["con"] = results[i].actor_id
|
|
|
|
# 4. Put altogether the walker and controller ids
|
|
for i in range(len(walkers_list)):
|
|
all_id.append(walkers_list[i]["con"])
|
|
all_id.append(walkers_list[i]["id"])
|
|
all_actors = world.get_actors(all_id)
|
|
|
|
# wait for a tick to ensure client receives the last transform of the walkers we have just created
|
|
world.wait_for_tick()
|
|
|
|
# 5. initialize each controller and set target to walk to (list is [controller, actor, controller, actor ...])
|
|
for i in range(0, len(all_actors), 2):
|
|
# start walker
|
|
all_actors[i].start()
|
|
# set walk to random point
|
|
all_actors[i].go_to_location(world.get_random_location_from_navigation())
|
|
# random max speed
|
|
all_actors[i].set_max_speed(1 + random.random()) # max speed between 1 and 2 (default is 1.4 m/s)
|