CARLA Simulator: Difference between revisions

Created page with "CARLA is a simulator for self-driving cars. * [https://carla.org/ https://carla.org/] * [https://carla.readthedocs.io/en/latest/ Documentation] ==Getting Started== ===Instal..."
 
 
(17 intermediate revisions by the same user not shown)
Line 23: Line 23:
The client handles setting up the scene and all sensor data.
The client handles setting up the scene and all sensor data.
See [https://carla.readthedocs.io/en/latest/tuto_G_retrieve_data/ Tutorial: Retrieve Data] and scroll down to tutorial_ego.py.
See [https://carla.readthedocs.io/en/latest/tuto_G_retrieve_data/ Tutorial: Retrieve Data] and scroll down to tutorial_ego.py.
;Notes
* If you want a clean shutdown, you need to disable autopilot on the ego_vehicle before you destroy it.
==Build From Source==
To develop vehicles or make significant changes, you will want to build from source.
{{hidden | Build From Source |
See [https://carla.readthedocs.io/en/latest/build_linux/#linux-build-command-summary Linux build summary]
* Get Unreal Engine cloning the UnrealEngine repo and building it.
** You will need an Epic Games account linked to your GitHub to have access to the source code.
* Clone the carla repo and download assets.
<pre>
# Launch Unreal Engine
make launch
# Build Carla
make package
</pre>
}}
==Sensors==
[https://carla.readthedocs.io/en/latest/ref_sensors/ Sensors Reference]
===RGB Image===
;Disabling Effects for Stitching
See [https://github.com/carla-simulator/carla/issues/2185  Disable all lens simulating effects on cameras ] 
<pre>
# carla/CarlaUE4/Config/DefaultEngine.ini
# Add to section [/Script/Engine.RendererSettings]
r.BlackBorders=0
r.DepthOfFieldQuality=0
r.DisableDistortion=1
r.MotionBlurQuality=0
r.SceneColorFringeQuality=0 # might be chromatic aberrations
r.Tonemapper.Quality=0 # might be vignette
</pre>
===Depth Image===
Depths images encode as z-depth to camera as 8-bit images. 
To get the meters from the 8-bit RGB image, apply the following formula:
<pre>
normalized = (R + G * 256 + B * 256 * 256) / (256 * 256 * 256 - 1)
in_meters = 1000 * normalized
</pre>
I recommend resaving the depth images as 16-bit PNG images. 
This makes the depth values less sensitive to rounding errors, even though 8-bit PNG images are lossless.
To convert depth values to euclidean distance, you need to divide by \(\cos(\theta)\) where \(\theta\) is the angle to the center of the image.
{{hidden | Z-depth to Euclidean Depth |
See [https://github.com/carla-simulator/carla/issues/2287 https://github.com/carla-simulator/carla/issues/2287] for a formula to convert z-depth to euclidean depth. 
}}
==Fixed time step==
[https://carla.readthedocs.io/en/stable/configuring_the_simulation/ Reference]
Normal games run at realtime, i.e. frames are generated as fast as possble (or limited by vsync) and the time between frames set based on the elapsed time. For demos and games, this is fine.
However for simulations, you will want to fix the time between frames to a constant value. This allows faster-than-realtime simulations on more powerful hardware and slower-than-realtime simulations on slower hardware.
Below sets the timestep to <code>1/30</code> seconds per frame on the server.
<pre>
./CarlaUE4.sh -benchmark -fps=30
</pre>
Alternatively, you can do this on the client by modifying the time step setting of your world.
<pre>
settings = world.get_settings()
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)
</pre>
If your client needs to record data every frame, you may want to use [[#synchronous mode]].
==Synchronous Mode==
Synchronous mode will force the server to wait for <code>world.tick()</code> on the client rather than run as fast as possible. 
This is ideal if you need to do something per-frame which causes your client to be slower than your server.
<pre>
settings = world.get_settings()
settings.fixed_delta_seconds = 1 / 30
settings.synchronous_mode = True # Enables synchronous mode
world.apply_settings(settings)
</pre>
If you are using the built in traffic manager for autopilot, you will need to set that to synchronous mode as well:
<syntaxhighlight lang="python">
tm = client.get_trafficmanager()
tm.set_synchronous_mode(True)
</syntaxhighlight>
See [https://github.com/carla-simulator/carla/blob/master/PythonAPI/examples/spawn_npc.py spawn_npc.py] for an example of synchronous mode.
==Troubleshooting==
* <code>trying to create rpc server for traffic manager; but the system failed to create because of bind error</code> on <code>client.get_trafficmanager()</code>
** kill the program using port 8000 <code>lsof -i :8000</code>
==Coordinate System==
Carla is based on UE4 which uses a left-handed coordinate system. Specifically, up is +z, forward is +x, and right is +y.
To convert (yaw, pitch, roll) rotations to a right-handed coordinate system, just use (-yaw, pitch, -roll)