carla/Docs/core_world.md

211 lines
11 KiB
Markdown
Raw Normal View History

# 1st. World and client
2020-02-19 17:10:48 +08:00
The client and the world are two of the fundamentals of CARLA, a necessary abstraction to operate the simulation and its actors.
2020-02-19 17:10:48 +08:00
2020-03-29 18:51:16 +08:00
This tutorial goes from defining the basics and creation of these elements, to describing their possibilities. If any doubt or issue arises during the reading, the [CARLA forum](https://forum.carla.org/) is there to solve them.
* [__The client__](#the-client)
* [Client creation](#client-creation)
* [World connection](#world-connection)
* [Other client utilities](#other-client-utilities)
* [__The world__](#the-world)
* [Actors](#actors)
* [Weather](#weather)
* [Debugging](#debugging)
* [World snapshots](#world-snapshots)
* [World settings](#world-settings)
2020-03-02 21:35:50 +08:00
---
## The client
2020-02-19 17:10:48 +08:00
Clients are one of the main elements in the CARLA architecture. They connect to the server, retrieve information, and command changes. That is done via scripts. The client identifies itself, and connects to the world to then operate with the simulation.
Besides that, clients are able to access advanced CARLA modules, features, and apply command batches. Only command batches will be covered in this section. These are useful for basic things such as spawning lots of actors. The rest of features are more complex, and they will be addressed in their respective pages in __Advanced steps__.
Take a look at [__carla.Client__](python_api.md#carla.Client) in the Python API reference to learn on specific methods and variables of the class.
2020-02-19 17:10:48 +08:00
### Client creation
2020-02-19 17:10:48 +08:00
Two things are needed. The __IP__ address identifying it, and __two TCP ports__ to communicate with the server. An optional third parameter sets the amount of working threads. By default this is set to all (`0`). [This code recipe](ref_code_recipes.md#parse-client-creation-arguments) shows how to parse these as arguments when running the script.
2020-02-19 17:10:48 +08:00
```py
client = carla.Client('localhost', 2000)
```
By default, CARLA uses local host IP, and port 2000 to connect but these can be changed at will. The second port will always be `n+1`, 2001 in this case.
2020-02-19 17:10:48 +08:00
Once the client is created, set its __time-out__. This limits all networking operations so that these don't block the client forever. An error will be returned if connection fails.
2020-02-19 17:10:48 +08:00
```py
client.set_timeout(10.0) # seconds
```
It is possible to have many clients connected, as it is common to have more than one script running at a time. Working in a multiclient scheme with advanced CARLA features, such as the traffic manager, is bound to make communication more complex.
2020-02-19 17:10:48 +08:00
!!! Note
Client and server have different `libcarla` modules. If the versions differ, issues may arise. This can be checked using the `get_client_version()` and `get_server_version()` methods.
2020-02-19 17:10:48 +08:00
### World connection
2020-02-19 17:10:48 +08:00
A client can connect and retrieve the current world fairly easily.
2020-02-19 17:10:48 +08:00
```py
world = client.get_world()
2020-02-19 17:10:48 +08:00
The client can also get a list of available maps to change the current one. This will destroy the current world and create a new one.
```py
print(client.get_available_maps())
...
world = client.load_world('Town01')
# client.reload_world() creates a new instance of the world with the same map.
2020-02-19 17:10:48 +08:00
```
Every world object has an `id` or episode. Everytime the client calls for `load_world()` or `reload_world()` the previous one is destroyed. A new one is created from scratch with a new episode. Unreal Engine is not rebooted in the process.
2020-02-19 17:10:48 +08:00
### Using commands
2020-02-19 17:10:48 +08:00
__Commands__ are adaptations of some of the most-common CARLA methods, that can be applied in batches. For instance, the [command.SetAutopilot](python_api.md#command.SetAutopilot) is equivalent to [Vehicle.set_autopilot()](python_api.md#carla.Vehicle.set_autopilot), enables the autopilot for a vehicle. However, using the methods [Client.apply_batch](python_api.md#carla.Client.apply_batch) or [Client.apply_batch_sync()](python_api.md#carla.Client.apply_batch_sync), a list of commands can be applied in one single simulation step. This becomes extremely useful for methods that are usually applied to even hundreds of elements.
2020-02-19 17:10:48 +08:00
The following example uses a batch to destroy a list of vehicles all at once.
2020-02-19 17:10:48 +08:00
```py
client.apply_batch([carla.command.DestroyActor(x) for x in vehicles_list])
```
All the commands available are listed in the [latest section](python_api.md#command.ApplyAngularVelocity) of the Python API reference.
### Other client utilities
The main purpose of the client object is to get or change the world, and apply commands. However, it also provides access to some additional features.
* __Traffic manager.__ This module is in charge of every vehicle set to autopilot to recreate urban traffic.
* __[Recorder](adv_recorder.md).__ Allows to reenact a previous simulation. Uses [snapshots](core_world.md#world-snapshots) summarizing the simulation state per frame.
2020-02-19 17:10:48 +08:00
2020-03-02 21:35:50 +08:00
---
## The world
2020-02-19 17:10:48 +08:00
The major ruler of the simulation. Its instance should be retrieved by the client. It does not contain the model of the world itself, that is part of the [Map](core_map.md) class. Instead, most of the information, and general settings can be accessed from this class.
2020-02-19 17:10:48 +08:00
* Actors in the simulation and the spectator.
* Blueprint library.
* Map.
* Simulation settings.
* Snapshots.
* Weather and light manager.
2020-02-19 17:10:48 +08:00
Some of its most important methods are _getters_, precisely to retrieve information or instances of these elements. Take a look at [carla.World](python_api.md#carla.World) to learn more about it.
2020-02-19 17:10:48 +08:00
### Actors
2020-02-19 17:10:48 +08:00
The world has different methods related with actors that allow for different functionalities.
2020-02-19 17:10:48 +08:00
* Spawn actors (but not destroy them).
* Get every actor on scene, or find one in particular.
* Access the blueprint library.
* Access the spectator actor, the simulation's point of view.
* Retrieve a random location that is fitting to spawn an actor.
2020-02-19 17:10:48 +08:00
Spawning will be explained in [2nd. Actors and blueprints](core_actors.md). It requires some understanding on the blueprint library, attributes, etc.
2020-02-19 17:10:48 +08:00
### Weather
2020-02-19 17:10:48 +08:00
The weather is not a class on its own, but a set of parameters accessible from the world. The parametrization includes sun orientation, cloudiness, wind, fog, and much more. The helper class [carla.WeatherParameters](python_api.md#carla.WeatherParameters) is used to define a custom weather.
2020-02-19 17:10:48 +08:00
```py
weather = carla.WeatherParameters(
cloudiness=80.0,
precipitation=30.0,
sun_altitude_angle=70.0)
world.set_weather(weather)
print(world.get_weather())
```
There are some weather presets that can be directly applied to the world. These are listed in [carla.WeatherParameters](python_api.md#carla.WeatherParameters) and accessible as an enum.
2020-02-19 17:10:48 +08:00
```py
world.set_weather(carla.WeatherParameters.WetCloudySunset)
```
!!! Note
Changes in the weather do not affect physics. They are only visuals that can be captured by the camera sensors.
__Night mode starts when sun_altitude_angle < 0__, which is considered sunset. This is when street and vehicle lights become especially relevant.
* __Street lights__ automatically turn on when night mode starts. The lights are placed by the developers of the map, and accessible as [__carla.Light__](python_api.md#carla.Light) objects. Their properties can be changed at will. An instance of [__carla.LightManager__](python_api.md#carla.LightManager) can be retrieved to handle groups of lights.
* __Vehicle lights__ have to be turned on/off by the user. Each vehicle has a set of lights listed in [__carla.VehicleLightState__](python_api.md#carla.VehicleLightState). So far, not all vehicles have lights integrated. Here is a list of those that are available by the time of writing.
* __Bikes.__ All of them have a front and back position light.
* __Motorcycles.__ Yamaha and Harley Davidson models.
* __Cars.__ Audi TT, Chevrolet, Dodge (the police car), Etron, Lincoln, Mustang, Tesla 3S, Wolkswagen T2 and the new guests coming to CARLA.
The lights of a vehicle can be retrieved and updated anytime using the methods [carla.Vehicle.get_light_state](python_api.md#carla.Vehicle.get_light_state) and [carla.Vehicle.set_light_state](#python_api.md#carla.Vehicle.set_light_state). These use binary operations to customize the light setting.
```py
# Turn on position lights
current_lights = carla.VehicleLightState.NONE
current_lights |= carla.VehicleLightState.Position
vehicle.set_light_state(current_lights)
```
### Debugging
2020-02-19 17:10:48 +08:00
World objects have a [carla.DebugHelper](python_api.md#carla.DebugHelper) object as a public attribute. It allows for different shapes to be drawn during the simulation. These are used to trace the events happening. The following example would draw a red box at an actor's location and rotation.
2020-02-19 17:10:48 +08:00
```py
debug = world.debug
debug.draw_box(carla.BoundingBox(actor_snapshot.get_transform().location,carla.Vector3D(0.5,0.5,2)),actor_snapshot.get_transform().rotation, 0.05, carla.Color(255,0,0,0),0)
```
This example is extended in a [code recipe](ref_code_recipes.md#debug-bounding-box-recipe) to draw boxes for every actor in a world snapshot.
2020-02-19 17:10:48 +08:00
### World snapshots
2020-02-19 17:10:48 +08:00
Contains the state of every actor in the simulation at a single frame. A sort of still image of the world with a time reference. The information comes from the same simulation step, even in asynchronous mode.
2020-02-19 17:10:48 +08:00
```py
# Retrieve a snapshot of the world at current frame.
world_snapshot = world.get_snapshot()
```
A [carla.WorldSnapshot](python_api.md#carla.WorldSnapshot) contains a [carla.Timestamp](python_api.md#carla.Timestamp) and a list of [carla.ActorSnapshot](python_api.md#carla.ActorSnapshot). Actor snapshots can be searched using the `id` of an actor. A snapshot lists the `id` of the actors appearing in it.
2020-02-19 17:10:48 +08:00
```py
timestamp = world_snapshot.timestamp # Get the time reference
2020-02-19 17:10:48 +08:00
for actor_snapshot in world_snapshot: # Get the actor and the snapshot information
2020-02-19 17:10:48 +08:00
actual_actor = world.get_actor(actor_snapshot.id)
actor_snapshot.get_transform()
actor_snapshot.get_velocity()
actor_snapshot.get_angular_velocity()
actor_snapshot.get_acceleration()
actor_snapshot = world_snapshot.find(actual_actor.id) # Get an actor's snapshot
2020-02-19 17:10:48 +08:00
```
### World settings
The world has access to some advanced configurations for the simulation. These determine rendering conditions, simulation time-steps, and synchrony between clients and server. They are accessible from the helper class [carla.WorldSettings](python_api.md#carla.WorldSettings).
2020-02-19 17:10:48 +08:00
For the time being, default CARLA runs with the best graphics quality, a variable time-step, and asynchronously. To dive further in this matters take a look at the __Advanced steps__ section. The pages on [synchrony and time-step](adv_synchrony_timestep.md), and [rendering options](adv_rendering_options.md) could be a great starting point.
2020-02-19 17:10:48 +08:00
2020-03-02 21:35:50 +08:00
---
That is a wrap on the world and client objects. The next step takes a closer look into actors and blueprints to give life to the simulation.
Keep reading to learn more. Visit the forum to post any doubts or suggestions that have come to mind during this reading.
2020-02-19 17:10:48 +08:00
<div text-align: center>
<div class="build-buttons">
<p>
<a href="https://forum.carla.org/" target="_blank" class="btn btn-neutral" title="CARLA forum">
2020-02-19 17:10:48 +08:00
CARLA forum</a>
</p>
</div>
<div class="build-buttons">
<p>
<a href="../core_actors" target="_blank" class="btn btn-neutral" title="2nd. Actors and blueprints">
2nd. Actors and blueprints</a>
</p>
</div>
</div>