Commenting and renaming some variables to 'spawn_npc' script
This commit is contained in:
parent
c05ed8a0f2
commit
8ce3daadbe
|
@ -72,7 +72,7 @@ def main():
|
|||
|
||||
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
|
||||
|
||||
actor_list = []
|
||||
vehicles_list = []
|
||||
client = carla.Client(args.host, args.port)
|
||||
client.set_timeout(2.0)
|
||||
|
||||
|
@ -101,7 +101,9 @@ def main():
|
|||
SetAutopilot = carla.command.SetAutopilot
|
||||
FutureActor = carla.command.FutureActor
|
||||
|
||||
# --------------
|
||||
# Spawn vehicles
|
||||
# --------------
|
||||
batch = []
|
||||
for n, transform in enumerate(spawn_points):
|
||||
if n >= args.number_of_vehicles:
|
||||
|
@ -120,77 +122,79 @@ def main():
|
|||
if response.error:
|
||||
logging.error(response.error)
|
||||
else:
|
||||
actor_list.append(response.actor_id)
|
||||
|
||||
print('spawned %d vehicles, press Ctrl+C to exit.' % len(actor_list))
|
||||
vehicles_list.append(response.actor_id)
|
||||
|
||||
# -------------
|
||||
# Spawn Walkers
|
||||
# -------------
|
||||
# 1. take all the random locations to spawn
|
||||
spawn_points = []
|
||||
location_error = carla.Location(0, 0, 0)
|
||||
for i in range(args.number_of_walkers):
|
||||
spawn_point = carla.Transform()
|
||||
spawn_point.location = world.get_random_location_from_navigation()
|
||||
spawn_points.append(spawn_point)
|
||||
# if the returned location is at (0,0,0) we can consider a problem
|
||||
if (spawn_point.location != location_error):
|
||||
spawn_points.append(spawn_point)
|
||||
batch = []
|
||||
info = []
|
||||
walkers_list = []
|
||||
# 2. we spawn the walker object
|
||||
for spawn_point in spawn_points:
|
||||
walker_bp = random.choice(world.get_blueprint_library().filter('walker.pedestrian.*'))
|
||||
# set as not invencible
|
||||
if walker_bp.has_attribute('is_invincible'):
|
||||
walker_bp.set_attribute('is_invincible', 'false')
|
||||
batch.append(SpawnActor(walker_bp, spawn_point))
|
||||
|
||||
# apply
|
||||
results = client.apply_batch_sync(batch, True)
|
||||
for i in range(len(results)):
|
||||
if results[i].error:
|
||||
logging.error(results[i].error)
|
||||
else:
|
||||
info.append({"id": results[i].actor_id, "trans": spawn_points[i], "con": None})
|
||||
|
||||
# Spawn walker controller
|
||||
walkers_list.append({"id": results[i].actor_id, "trans": spawn_points[i], "con": None})
|
||||
# 3. we spawn the walker controller
|
||||
batch = []
|
||||
walker_controller_bp = world.get_blueprint_library().find('controller.ai.walker')
|
||||
for i in range(len(info)):
|
||||
batch.append(SpawnActor(walker_controller_bp, carla.Transform(), info[i]["id"]))
|
||||
# apply
|
||||
for i in range(len(walkers_list)):
|
||||
batch.append(SpawnActor(walker_controller_bp, carla.Transform(), walkers_list[i]["id"]))
|
||||
results = client.apply_batch_sync(batch, True)
|
||||
for i in range(len(results)):
|
||||
if results[i].error:
|
||||
logging.error(results[i].error)
|
||||
else:
|
||||
info[i]["con"] = results[i].actor_id
|
||||
|
||||
# get whole list of actors (child and parents in same list so world.get_actors() can find parents also)
|
||||
walkers_list[i]["con"] = results[i].actor_id
|
||||
# 4. we put altogether the walkers and controllers id to get the objects from their id
|
||||
all_id = []
|
||||
for i in range(len(info)):
|
||||
all_id.append(info[i]["con"])
|
||||
all_id.append(info[i]["id"])
|
||||
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)
|
||||
# initialize each controller and set target to walk to (list is [controler, actor, controller, actor ...])
|
||||
# 5. initialize each controller and set target to walk to (list is [controler, actor, controller, actor ...])
|
||||
for i in range(0, len(all_id), 2):
|
||||
# index in the info list
|
||||
# index in the walkers_list
|
||||
index = int(i / 2)
|
||||
all_actors[i].start(info[index]["trans"].location)
|
||||
# walk to random point
|
||||
# set start position of walker
|
||||
all_actors[i].start(walkers_list[index]["trans"].location)
|
||||
# set walk to random point
|
||||
target = world.get_random_location_from_navigation()
|
||||
all_actors[i].go_to_location(target)
|
||||
all_actors[i].set_max_speed(1 + random.random()) # max speed between 1 and 2
|
||||
# random max speed
|
||||
all_actors[i].set_max_speed(1 + random.random()) # max speed between 1 and 2 (default is 1.4 m/s)
|
||||
|
||||
print('spawned %d walkers, press Ctrl+C to exit.' % len(info))
|
||||
print('spawned %d vehicles and %d walkers, press Ctrl+C to exit.' % (len(vehicles_list), len(walkers_list)))
|
||||
|
||||
while True:
|
||||
world.wait_for_tick()
|
||||
|
||||
finally:
|
||||
|
||||
print('\ndestroying %d vehicles' % len(actor_list))
|
||||
client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
|
||||
print('\ndestroying %d vehicles' % len(vehicles_list))
|
||||
client.apply_batch([carla.command.DestroyActor(x) for x in vehicles_list])
|
||||
|
||||
# stop walker controllers (list is [controler, actor, controller, actor ...])
|
||||
for i in range(0, len(all_id), 2):
|
||||
all_actors[i].stop()
|
||||
|
||||
print('\ndestroying %d walkers' % len(info))
|
||||
print('\ndestroying %d walkers' % len(walkers_list))
|
||||
client.apply_batch([carla.command.DestroyActor(x) for x in all_id])
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue