Skip to content

Commit

Permalink
Refreshing tm_spawn_npc script
Browse files Browse the repository at this point in the history
  • Loading branch information
bernatx committed Dec 13, 2019
1 parent 6f3c4e2 commit c9358be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion LibCarla/source/carla/client/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace client {
void Vehicle::SetAutopilot(bool enabled) {
if (enabled) {
TM &tm = TM::GetInstance(TM::GetUniqueLocalClient());
tm.RegisterVehicles({carla::SharedPtr<carla::client::Actor>(this)});
tm.RegisterVehicles({shared_from_this()});
}
}

Expand Down
36 changes: 28 additions & 8 deletions PythonAPI/examples/tm_spawn_npc.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def main():
argparser.add_argument(
'-n', '--number-of-vehicles',
metavar='N',
default=50,
default=10,
type=int,
help='number of vehicles (default: 10)')
argparser.add_argument(
Expand All @@ -55,7 +55,7 @@ def main():
action='store_true',
help='avoid spawning vehicles prone to accidents')
argparser.add_argument(
'--filter',
'--filterv',
metavar='PATTERN',
default='vehicle.*',
help='vehicles filter (default: "vehicle.*")')
Expand All @@ -78,8 +78,8 @@ def main():
try:
traffic_manager = None
world = client.get_world()
blueprints = world.get_blueprint_library().filter(args.filter)
blueprints_walkers = world.get_blueprint_library().filter(args.filterw)
blueprints = world.get_blueprint_library().filter(args.filterv)
blueprintsWalkers = world.get_blueprint_library().filter(args.filterw)

if args.safe:
blueprints = [x for x in blueprints if int(x.get_attribute('number_of_wheels')) == 4]
Expand Down Expand Up @@ -120,6 +120,9 @@ def main():
# -------------
# Spawn Walkers
# -------------
# some settings
percentagePedestriansRunning = 0.0 # how many pedestrians will run
percentagePedestriansCrossing = 0.0 # how many pedestrians will walk through the road
# 1. take all the random locations to spawn
spawn_points = []
for i in range(args.number_of_walkers):
Expand All @@ -130,18 +133,33 @@ def main():
spawn_points.append(spawn_point)
# 2. we spawn the walker object
batch = []
walker_speed = []
for spawn_point in spawn_points:
walker_bp = random.choice(blueprints_walkers)
walker_bp = random.choice(blueprintsWalkers)
# set as not invencible
if walker_bp.has_attribute('is_invincible'):
walker_bp.set_attribute('is_invincible', 'false')
# set the max speed
if walker_bp.has_attribute('speed'):
if (random.random() > percentagePedestriansRunning):
# walking
walker_speed.append(walker_bp.get_attribute('speed').recommended_values[1])
else:
# running
walker_speed.append(walker_bp.get_attribute('speed').recommended_values[2])
else:
print("Walker has no speed")
walker_speed.append(0.0)
batch.append(SpawnActor(walker_bp, spawn_point))
results = client.apply_batch_sync(batch, True)
walker_speed2 = []
for i in range(len(results)):
if results[i].error:
logging.error(results[i].error)
else:
walkers_list.append({"id": results[i].actor_id})
walker_speed2.append(walker_speed[i])
walker_speed = walker_speed2
# 3. we spawn the walker controller
batch = []
walker_controller_bp = world.get_blueprint_library().find('controller.ai.walker')
Expand All @@ -163,13 +181,15 @@ def main():
world.wait_for_tick()

# 5. initialize each controller and set target to walk to (list is [controler, actor, controller, actor ...])
# set how many pedestrians can cross the road
world.set_pedestrians_cross_factor(percentagePedestriansCrossing)
for i in range(0, len(all_id), 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()/2) # max speed between 1 and 1.5 (default is 1.4 m/s)
# max speed
all_actors[i].set_max_speed(float(walker_speed[int(i/2)]))

print('Spawned %d vehicles and %d walkers, press Ctrl+C to exit.' % (len(vehicles_list), len(walkers_list)))

Expand All @@ -189,7 +209,7 @@ def main():
print('Destroying %d vehicles.\n' % len(vehicles_list))
client.apply_batch([carla.command.DestroyActor(x) for x in vehicles_list])

# stop walker controllers (list is [controler, actor, controller, actor ...])
# stop walker controllers (list is [controller, actor, controller, actor ...])
for i in range(0, len(all_id), 2):
all_actors[i].stop()

Expand Down

0 comments on commit c9358be

Please sign in to comment.