2019-02-21 19:13:48 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2019-03-29 17:58:05 +08:00
|
|
|
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
|
2019-02-21 19:13:48 +08:00
|
|
|
# Barcelona (UAB).
|
|
|
|
#
|
|
|
|
# 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:
|
2019-03-29 02:15:13 +08:00
|
|
|
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
|
2019-02-21 19:13:48 +08:00
|
|
|
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 random
|
|
|
|
import time
|
2019-04-11 21:00:12 +08:00
|
|
|
import logging
|
2019-02-21 19:13:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
'-n', '--number-of-vehicles',
|
|
|
|
metavar='N',
|
|
|
|
default=10,
|
|
|
|
type=int,
|
|
|
|
help='number of vehicles (default: 10)')
|
|
|
|
argparser.add_argument(
|
|
|
|
'-d', '--delay',
|
|
|
|
metavar='D',
|
|
|
|
default=2.0,
|
|
|
|
type=float,
|
|
|
|
help='delay in seconds between spawns (default: 2.0)')
|
|
|
|
argparser.add_argument(
|
|
|
|
'--safe',
|
|
|
|
action='store_true',
|
|
|
|
help='avoid spawning vehicles prone to accidents')
|
|
|
|
argparser.add_argument(
|
|
|
|
'-f', '--recorder_filename',
|
|
|
|
metavar='F',
|
2019-02-28 19:37:02 +08:00
|
|
|
default="test1.log",
|
|
|
|
help='recorder filename (test1.log)')
|
2019-02-21 19:13:48 +08:00
|
|
|
argparser.add_argument(
|
|
|
|
'-t', '--recorder_time',
|
|
|
|
metavar='T',
|
|
|
|
default=0,
|
|
|
|
type=int,
|
|
|
|
help='recorder duration (auto-stop)')
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
actor_list = []
|
2019-04-11 21:00:12 +08:00
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
|
2019-02-21 19:13:48 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
client = carla.Client(args.host, args.port)
|
|
|
|
client.set_timeout(2.0)
|
|
|
|
world = client.get_world()
|
|
|
|
blueprints = world.get_blueprint_library().filter('vehicle.*')
|
|
|
|
|
2019-06-17 17:25:07 +08:00
|
|
|
spawn_points = world.get_map().get_spawn_points()
|
2019-02-21 19:13:48 +08:00
|
|
|
random.shuffle(spawn_points)
|
|
|
|
|
|
|
|
print('found %d spawn points.' % len(spawn_points))
|
|
|
|
|
|
|
|
count = args.number_of_vehicles
|
|
|
|
|
2019-04-11 21:00:12 +08:00
|
|
|
print("Recording on file: %s" % client.start_recorder(args.recorder_filename))
|
2019-02-21 19:13:48 +08:00
|
|
|
|
2019-04-11 21:00:12 +08:00
|
|
|
if args.safe:
|
|
|
|
blueprints = [x for x in blueprints if int(x.get_attribute('number_of_wheels')) == 4]
|
2021-08-02 19:40:54 +08:00
|
|
|
blueprints = [x for x in blueprints if not x.id.endswith('microlino')]
|
2019-04-11 21:00:12 +08:00
|
|
|
blueprints = [x for x in blueprints if not x.id.endswith('carlacola')]
|
2021-08-02 19:40:54 +08:00
|
|
|
blueprints = [x for x in blueprints if not x.id.endswith('cybertruck')]
|
|
|
|
blueprints = [x for x in blueprints if not x.id.endswith('t2')]
|
|
|
|
blueprints = [x for x in blueprints if not x.id.endswith('sprinter')]
|
|
|
|
blueprints = [x for x in blueprints if not x.id.endswith('firetruck')]
|
|
|
|
blueprints = [x for x in blueprints if not x.id.endswith('ambulance')]
|
2019-04-11 21:00:12 +08:00
|
|
|
|
|
|
|
spawn_points = world.get_map().get_spawn_points()
|
|
|
|
number_of_spawn_points = len(spawn_points)
|
|
|
|
|
2019-04-11 23:47:59 +08:00
|
|
|
if count < number_of_spawn_points:
|
2019-04-11 21:00:12 +08:00
|
|
|
random.shuffle(spawn_points)
|
2019-04-11 23:47:59 +08:00
|
|
|
elif count > number_of_spawn_points:
|
2019-04-11 21:00:12 +08:00
|
|
|
msg = 'requested %d vehicles, but could only find %d spawn points'
|
2019-04-11 23:47:59 +08:00
|
|
|
logging.warning(msg, count, number_of_spawn_points)
|
|
|
|
count = number_of_spawn_points
|
2019-04-11 21:00:12 +08:00
|
|
|
|
|
|
|
# @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):
|
2019-04-11 23:47:59 +08:00
|
|
|
if n >= count:
|
2019-02-21 19:13:48 +08:00
|
|
|
break
|
2019-04-11 21:00:12 +08:00
|
|
|
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)))
|
2019-02-21 19:13:48 +08:00
|
|
|
|
2019-04-11 21:00:12 +08:00
|
|
|
for response in client.apply_batch_sync(batch):
|
|
|
|
if response.error:
|
|
|
|
logging.error(response.error)
|
|
|
|
else:
|
|
|
|
actor_list.append(response.actor_id)
|
2019-02-21 19:13:48 +08:00
|
|
|
|
2019-04-11 21:00:12 +08:00
|
|
|
print('spawned %d vehicles, press Ctrl+C to exit.' % len(actor_list))
|
2019-02-21 19:13:48 +08:00
|
|
|
|
|
|
|
if (args.recorder_time > 0):
|
|
|
|
time.sleep(args.recorder_time)
|
|
|
|
else:
|
|
|
|
while True:
|
2019-04-11 21:00:12 +08:00
|
|
|
world.wait_for_tick()
|
|
|
|
# time.sleep(0.1)
|
2019-02-21 19:13:48 +08:00
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
print('\ndestroying %d actors' % len(actor_list))
|
2019-04-24 18:40:43 +08:00
|
|
|
client.apply_batch_sync([carla.command.DestroyActor(x) for x in actor_list])
|
2019-02-21 19:13:48 +08:00
|
|
|
|
2019-02-28 19:37:02 +08:00
|
|
|
print("Stop recording")
|
2019-02-21 19:13:48 +08:00
|
|
|
client.stop_recorder()
|
|
|
|
|
2019-03-19 19:20:59 +08:00
|
|
|
|
2019-02-21 19:13:48 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
print('\ndone.')
|