-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn_actors.py
90 lines (73 loc) · 3.09 KB
/
spawn_actors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import glob
import os
import sys
import time
from definitions import get_actor_blueprints
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
from carla import VehicleLightState as vls
import argparse
import logging
from numpy import random
def getVehicles(client, world, spawn_points, blueprintsVehicles, number):
print("Spawning vehicles...")
batch= []
for i in range(number):
spawn_point = random.choice(spawn_points)
vehicle_bp = random.choice(blueprintsVehicles)
batch.append(carla.command.SpawnActor(vehicle_bp, spawn_point).then(carla.command.SetAutopilot(carla.command.FutureActor, True)))
results = client.apply_batch_sync(batch, True)
all_id = [results[i].actor_id for i in range(len(results))]
all_actors = world.get_actors(all_id)
return all_actors, all_id
def getWalkers(client, world, blueprintsWalkers, number):
print("Spawning walkers...")
# 1. Take all the random locations to spawn
spawn_points = []
for i in range(number):
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)
walkers_list=[]
for i in range(len(results)):
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)):
walkers_list[i]["con"] = results[i].actor_id
# 4. Put altogether the walker and controller ids
all_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)
# wait for a tick to ensure client receives the last transform of the walkers we have just created
world.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)
return all_actors, all_id