Use batch commands to create and remove actors from recorder

This commit is contained in:
bernatx 2019-04-11 15:00:12 +02:00
parent c2b7e27ac5
commit 2d2f1654dc
1 changed files with 41 additions and 31 deletions

View File

@ -23,6 +23,7 @@ import carla
import argparse
import random
import time
import logging
def main():
@ -69,6 +70,7 @@ def main():
args = argparser.parse_args()
actor_list = []
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
try:
@ -77,24 +79,6 @@ def main():
world = client.get_world()
blueprints = world.get_blueprint_library().filter('vehicle.*')
if args.safe:
blueprints = [x for x in blueprints if int(x.get_attribute('number_of_wheels')) == 4]
blueprints = [x for x in blueprints if not x.id.endswith('isetta')]
def try_spawn_random_vehicle_at(transform):
blueprint = random.choice(blueprints)
if blueprint.has_attribute('color'):
color = random.choice(blueprint.get_attribute('color').recommended_values)
blueprint.set_attribute('color', color)
blueprint.set_attribute('role_name', 'autopilot')
vehicle = world.try_spawn_actor(blueprint, transform)
if vehicle is not None:
actor_list.append(vehicle)
vehicle.set_autopilot()
print('spawned %r at %s' % (vehicle.type_id, transform.location))
return True
return False
# @todo Needs to be converted to list to be shuffled.
spawn_points = list(world.get_map().get_spawn_points())
random.shuffle(spawn_points)
@ -103,32 +87,58 @@ def main():
count = args.number_of_vehicles
print("Recording on file:", client.start_recorder(args.recorder_filename))
print("Recording on file: %s" % client.start_recorder(args.recorder_filename))
for spawn_point in spawn_points:
if try_spawn_random_vehicle_at(spawn_point):
count -= 1
if count <= 0:
if args.safe:
blueprints = [x for x in blueprints if int(x.get_attribute('number_of_wheels')) == 4]
blueprints = [x for x in blueprints if not x.id.endswith('isetta')]
blueprints = [x for x in blueprints if not x.id.endswith('carlacola')]
spawn_points = world.get_map().get_spawn_points()
number_of_spawn_points = len(spawn_points)
if args.number_of_vehicles < number_of_spawn_points:
random.shuffle(spawn_points)
elif args.number_of_vehicles > number_of_spawn_points:
msg = 'requested %d vehicles, but could only find %d spawn points'
logging.warning(msg, args.number_of_vehicles, number_of_spawn_points)
args.number_of_vehicles = number_of_spawn_points
# @todo cannot import these directly.
SpawnActor = carla.command.SpawnActor
SetAutopilot = carla.command.SetAutopilot
FutureActor = carla.command.FutureActor
batch = []
for n, transform in enumerate(spawn_points):
if n >= args.number_of_vehicles:
break
blueprint = random.choice(blueprints)
if blueprint.has_attribute('color'):
color = random.choice(blueprint.get_attribute('color').recommended_values)
blueprint.set_attribute('color', color)
blueprint.set_attribute('role_name', 'autopilot')
batch.append(SpawnActor(blueprint, transform).then(SetAutopilot(FutureActor, True)))
while count > 0:
time.sleep(args.delay)
if try_spawn_random_vehicle_at(random.choice(spawn_points)):
count -= 1
for response in client.apply_batch_sync(batch):
if response.error:
logging.error(response.error)
else:
actor_list.append(response.actor_id)
print('spawned %d vehicles, press Ctrl+C to exit.' % args.number_of_vehicles)
print('spawned %d vehicles, press Ctrl+C to exit.' % len(actor_list))
if (args.recorder_time > 0):
time.sleep(args.recorder_time)
else:
while True:
time.sleep(0.1)
world.wait_for_tick()
# time.sleep(0.1)
finally:
print('\ndestroying %d actors' % len(actor_list))
for actor in actor_list:
actor.destroy()
client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
print("Stop recording")
client.stop_recorder()