Fixing conflicts in documentation

This commit is contained in:
bernatx 2019-07-08 17:10:05 +02:00 committed by Néstor Subirón
parent 933d2b72ad
commit 629254be3f
1 changed files with 113 additions and 0 deletions

View File

@ -494,3 +494,116 @@ client.replay_file("recording01.log")
The replayer replicates the actor and traffic light information of the recording each frame.
For more details, [Recorder and Playback system](recorder_and_playback.md)
#### Pedestrians
![pedestrian types](img/pedestrian_types.png)
We can get a lit of all pedestrians from the blueprint library and choose one:
```py
world = client.get_world()
blueprintsWalkers = world.get_blueprint_library().filter("walker.pedestrian.*")
walker_bp = random.choice(blueprintsWalkers)
```
We can **get a list of random points** where to spawn the pedestrians. Those points are always from the areas where the pedestrian can walk:
```py
# 1. take all the random locations to spawn
spawn_points = []
for i in range(50):
spawn_point = carla.Transform()
spawn_point.location = world.get_random_location_from_navigation()
if (spawn_point.location != None):
spawn_points.append(spawn_point)
```
Now we can **spawn the pedestrians** at those positions using a batch of commands:
```py
# 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))
# apply the batch
results = client.apply_batch_sync(batch, True)
for i in range(len(results)):
if results[i].error:
logging.error(results[i].error)
else:
walkers_list.append({"id": results[i].actor_id})
```
We save the id of each walker from the results of the batch, in a dictionary because we will assign to them also a controller.
We need to **create the controller** that will manage the pedestrian automatically:
```py
# 3. we spawn the walker controller
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"]))
# apply the batch
results = client.apply_batch_sync(batch, True)
for i in range(len(results)):
if results[i].error:
logging.error(results[i].error)
else:
walkers_list[i]["con"] = results[i].actor_id
```
We create the controller as child of the walker, so the location we pass is (0,0,0).
At this point we have a list of pedestrians with a controller each one, but we need to get the actual actor from the id. Because the controller is a child of the pedestrian, we need to **put all id in the same list** so the parent can find the child in the same list.
```py
# 4. we put altogether the walkers and controllers id to get the objects from their 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)
```
The list all_actors has now all the actor objects we created.
At this point is a good idea to **wait for a tick** on client, because then the server has time to send all new data about the new actors we just created (we need the transform of each one updated). So we can do a call like:
```py
# wait for a tick to ensure client receives the last transform of the walkers we have just created
world.wait_for_tick()
```
After that, our client has the data about the actors updated.
**Using the controller** we can set the locations where we want each pedestrian walk to:
```py
# 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)
```
There we have set at each pedestrian (through its controller) a random point and random speed. When they reach the target point then automatically walk to another random point.
If the target point is not reachable, then they reach the closest point from the are where they are.
![pedestrian sample](img/pedestrians_shoot.png)
To **destroy the pedestrians**, we need to stop them from the navigation, and then destroy the objects (actor and controller):
```py
# stop pedestrians (list is [controller, actor, controller, actor ...])
for i in range(0, len(all_id), 2):
all_actors[i].stop()
# destroy pedestrian (actor and controller)
client.apply_batch([carla.command.DestroyActor(x) for x in all_id])
```