Carla 开源自动驾驶仿真软件使用指南 [AD simulator]


introdution:
- 开源软件是广泛使用的开源软件平台。
- Unreal Engine 4是经过精心设计的游戏引擎。
- 开发者可利用Python编程接口调用CARLA模拟器中的客户端与服务器之间的交互模式- REF: https://carla.readthedocs.io/en/latest/python_api/
let's start:
The API has undergone substantial modifications since the release of version 0.9.0 and is now referred to as the 0.9.X interface, distinguishing it from its predecessor, which was referred to as the 0.8.X version of this system
Section 1 API 学习
访问官方下载页面获取软件包,并解压缩文件。使用快捷键 shirt + 右键 设置 shell 执行路径为 .\CarlaUE4.exe 所在的目录,并启动该程序
通过鼠标+ AWSD 控制运行,

打开路径:D:\study\codes\CARLA_0.9.5\PythonAPI\examples
打开另一个shell, 运行官网
创建(spawn) 场景元素, demo命令
python spawn_npc.py -n 80
With this script, we introduce 80 autonomous vehicles into our simulation environment. Upon returning to the simulator, these cars will be observed as they navigate through the city. The simulation script will cease operating once it detects abnormal movement, but it is recommended to leave them in such a configuration temporarily.
该命令负责生成80辆车辆,并使它们呈现随机分布的自动运行状态。 按下Ctrl+C键后结束演示过程。 在学习过程中发现脚本较为复杂,在完成 Lesson 2 后需查阅相关API文档。
该命令负责生成80辆车辆,并使它们呈现随机分布的自动运行状态。 按下Ctrl+C键后结束演示过程。 在学习过程中发现脚本较为复杂,在完成 Lesson 2 后需查阅相关API文档。
修改天气
- 天气自动变化
python dynamic_weather.py
手动控制车辆
python manual_control.py

运行窗口如下:第三视角

第一课时结束。下课!:)
Lesson2 :
几个概念:
创建Client 对象:
client = carla.Client('localhost', 2000)
client.set_timeout(10.0) # seconds
模拟世界:
world = client.get_world()
actor (模拟器中的演员对象-比如汽车)
actor对应的各种属性(如颜色、车型等)在构建blueprints时被设定。
所有的这些属性都被整合到同一个库内。
blueprint_library = world.get_blueprint_library()
Through the library, we can retrieve specific blueprints using their IDs. Wildcard filters can be applied to narrow down the search results. Additionally, users have the flexibility to either search for a specific blueprint or randomly select one.
# Find specific blueprint.
collision_sensor_bp = blueprint_library.find('sensor.other.collision')
# Chose a vehicle blueprint at random.
vehicle_bp = random.choice(blueprint_library.filter('vehicle.bmw.*'))
Some attributes of blueprints have the ability to be modified, whereas others cannot be modified. For example, it is not possible to alter the number of wheels on a vehicle, although it is possible to change its color.
vehicles = blueprint_library.filter('vehicle.*')
bikes = [x for x in vehicles if int(x.get_attribute('number_of_wheels')) == 2]
for bike in bikes:
bike.set_attribute('color', '255,0,0')
Modifiable attributes also come with a list of recommended values
for attr in blueprint:
if attr.is_modifiable:
blueprint.set_attribute(attr.id, random.choice(attr.recommended_values))
了解了属性设置后,开始创建actor,
Once we have the blueprint properly configured, spawning an actor turns out to be a straightforward process.
transform = Transform(Location(x=230, y=195, z=40), Rotation(yaw=180))# 生成点坐标创建
actor = world.spawn_actor(blueprint, transform) #这个时候会检查生成点坐标是否会发生碰撞。如果有碰撞,会报错
shen
The spawn actor functionality is available in two forms, namely spawn_actor and try_spawn_actor. When using the first function, an exception will be thrown if the actor cannot be properly spawned, whereas the second function will simply return None. The most common reason for failure occurs when there's a collision at the spawn point, indicating that the actor does not fit well in that specific location. This could be due to another vehicle occupying that spot or an attempt to spawn onto a static object.
Simplifying the process of locating a spawn location, each map offers a collection of suggested transformations.
推荐
spawn_points = world.get_map().get_spawn_points() #返回所有的可用collision-free points.
We'll add more on the map object later in this tutorial.
Specifically, these spawn functions feature a controllable parameter that manages whether an actor will be connected to another entity. This characteristic is particularly advantageous for sensor applications. As demonstrated in a subsequent example, the camera maintains a rigid attachment to our vehicle throughout the remainder of the simulation.
camera = world.spawn_actor(camera_bp, relative_transform, attach_to=my_vehicle)
# (附加参数,可以控制物体是否和其他的actor绑定)--适用于传感器
Please note that in such a scenario, the transform provided is considered relative to its parent entity.
内容较多, 现在开始动手实践. 直接在 calar 解压目录下, 建立测试代码文件夹.(如果需要其他地方运行, 则需添加相应的环境变量设置.) 否则系统会在后续步骤中提示缺少必要的文件.
import carla
首先打开模拟器(server), 否则会报错

创建一个client. 创建actor, blueprints. 调用生成函数,在世界中生成。
代码在spyder中编译会报错。报“calra 没有 client这个属性!”---TB fix
偶尔发现可以正常运行。
暂时通过shell 命令行来执行。运行OK
参考官方example: spawn_npc.py ,尝试练习
import glob
import os
import sys
#import future
#import pygame
#添加系统变量
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
import argparse
import logging
import random
client=carla.Client('127.0.0.1',2000)
print ('create client')
client.set_timeout(2.0)
world=client.get_world()
print ('crated VR world')

都放到一起! 如下code
#!/usr/bin/env python
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
"""Spawn NPCs into the simulation"""
import glob
import os
import sys
#import future
#import pygame
#添加系统变量
'''
try
finally 函数体格式
21. def 函数名():
定义函数
'''
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
import argparse
import logging
import random
def main():
try:
client=carla.Client('127.0.0.1',2000) #默认地址
print ('create client')
client.set_timeout(2.0)
world=client.get_world()
print ('crated VR world')
bp_library = world.get_blueprint_library()
'''
#获得blueprints 库-API 如下
#carla.BlueprintLibrary
#find(id)
#filter(wildcard_pattern)
#__getitem__(pos)
#__len__()
#__iter__()
'''
bps=bp_library.filter('vehicle.*')
'''
#获得了所有车的属性集合--matrix
#进一步筛选
'''
bps = [x for x in bps if int(x.get_attribute('number_of_wheels')) == 4]
'''获得所有的2轮车'''
bps = [x for x in bps if not x.id.endswith('isetta')]
bps = [x for x in bps if not x.id.endswith('carlacola')]
'''获得可用生成点'''
spoints=world.get_map().get_spawn_points()
number_of_spawn_points = len(spoints)
print (number_of_spawn_points)
batch=[]
actor_list=[]
i=0
while i <10 :
spoint=random.choice(spoints)
bp=random.choice(bps)
actorlist=world.spawn_actor(bp,spoint).set_autopilot(enabled=True)
batch.append(actorlist)
# set_autopilot-- 设置这些车辆自动行驶
i =i +1
'''
记录生成的actor 对象,
提供摧毁他们的途径,下面的代码有问题,报错。TypeError: No registered converter was able to produce a C++ r
'''
####
for response in client.apply_batch_sync(batch):
if response.error:
logging.error(response.error)
else:
actor_list.append(response.actor_id)
print('spawned %d vehicles, press Ctrl+C to exit.' % len(actor_list))
#
while True:
world.wait_for_tick()
#
finally:
#
print('\ndestroying %d actors' % len(batch))
client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
####
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('\ndone.')
确认无误,在初次尝试中,我们完成了汽车对象的建立,并对它们进行了有序配置。这一步骤形成了一个虚拟化的交通流系统。
总结一下API 调用过程:

Lession 3:
操作者--程序正常运行后仍无法验证,在模拟器中进行观察分析。1. 是坐标值吗?不清楚如何设定坐标系的原点位置。2. 在中断处理时将目标对象从内存中删除。
车辆目标
#显示位置,速度,加速度信息
location = actor.get_location()
location.z += 10.0
actor.set_location(location)
print(actor.get_acceleration())
print(actor.get_velocity())
#销毁目标--主页代码有问题
carla.command.DestroyActor(actor)
#操控车辆
vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=-1.0))
#全部的属性API:
carla.VehicleControl(
throttle = 0.0
steer = 0.0
brake = 0.0
hand_brake = False
reverse = False
manual_gear_shift = False
gear = 0)
# 车辆的动力性特性--调整质量,力矩图,最大RPM
vehicle.apply_physics_control(carla.VehiclePhysicsControl(max_rpm = 5000.0,
center_of_mass = carla.Vector3D(0.0, 0.0, 0.0), torque_curve=[[0,400],[5000,400]]))
#API 结构体定义
carla.VehiclePhysicsControl(
torque_curve,
max_rpm,
moi,
damping_rate_full_throttle,
damping_rate_zero_throttle_clutch_engaged,
damping_rate_zero_throttle_clutch_disengaged,
use_gear_autobox,
gear_switch_time,
clutch_strength,
mass,
drag_coefficient,
center_of_mass,
steering_curve, # 转向maximum steering for a specific forward speed
wheels)
#轮胎的特性API 定义
carla.WheelPhysicsControl(
tire_friction,
damping_rate,
steer_angle,
disable_steering)
#actor 自动驾驶
vehicle.set_autopilot(True)
#获得车辆的bounding box
box = vehicle.bounding_box
print(box.location) # Location relative to the vehicle.
print(box.extent) # XYZ half-box extents in meters.
传感器对象
camera_bp = blueprint_library.find('sensor.camera.rgb')
#relative_transform 如何定义--应该是个R|t matrix.
camera = world.spawn_actor(camera_bp, relative_transform, attach_to=my_vehicle)
#把传感器的数据存到本地
camera.listen(lambda image: image.save_to_disk('output/%06d.png' % image.frame_number))
TBC待专题里面 展开!!!????
其他对象-道路标志,交通灯 ,车道线 ,etc
A part of this system comprises several key components, including both hardware and software elements. These components interact seamlessly to ensure optimal performance across diverse operational scenarios.
actor_list = world.get_actors()
The returned instance of the actor list object possesses functionalities to identify, filter, and iterate through the actors.
# Find an actor by id.
actor = actor_list.find(id)
# Print the location of all the speed limit signs in the world.
for speed_sign in actor_list.filter('traffic.speed_limit.*'):
print(speed_sign.get_location())
In this list, you can identify the participating actors. Where can you obtain a detailed API definition along with all properties etc.?
- 交通标志灯带有
state字段以检测当前状态。- 速度限制标志通过字段编码指定速度值。
- 观众角色用于调整模拟器视图窗口的位置。
修改天气:
1. carla.WeatherParameters
2. cloudyness
3. precipitation
4. precipitation_deposits
5. wind_intensity
6. sun_azimuth_angle
7. sun_altitude_angle
8. __eq__(other)
9. __ne__(other)
get_weather() 获取
set_weather(weather_parameters) 设置
Static presets 定义好的天气。
carla.WeatherParameters.ClearNoon
carla.WeatherParameters.CloudyNoon
carla.WeatherParameters.WetNoon
carla.WeatherParameters.WetCloudyNoon
carla.WeatherParameters.MidRainyNoon
carla.WeatherParameters.HardRainNoon
carla.WeatherParameters.SoftRainNoon
carla.WeatherParameters.ClearSunset
carla.WeatherParameters.CloudySunset
carla.WeatherParameters.WetSunset
carla.WeatherParameters.WetCloudySunset
carla.WeatherParameters.MidRainSunset
carla.WeatherParameters.HardRainSunset
carla.WeatherParameters.SoftRainSunset
地图对象
- 获得生成点
- 获得路标!-to be test!
import carla
#import random
client=carla.Client('127.0.0.1',2000) #默认地址
print ('create client')
client.set_timeout(2.0)
world=client.get_world()
print ('crated VR world')
map=world.get_map()
print ('map name--',map.name)
sps=map.get_spawn_points()
print (' map first avaliable spawn points--',sps[0])
获得路标!
the real power of this map API becomes apparent when we introduce waypoints. It becomes possible for us to obtain a waypoint on the road nearest to our vehicle when introducing waypoints.
waypoint = map.get_waypoint(vehicle.get_location())
The waypoint's transformation is situated within a drivable lane, and its orientation aligns with the road direction at that location.
路标节点还具备查询"下一个"路标的功能。
# Retrieve the closest waypoint.
waypoint = map.get_waypoint(vehicle.get_location())
# Disable physics, in this example we're just teleporting the vehicle.
vehicle.set_simulate_physics(False)
while True:
# Find next waypoint 2 meters ahead.
waypoint = random.choice(waypoint.next(2.0))
# Teleport the vehicle.
vehicle.set_transform(waypoint.transform)
The map object itself offers a suite of techniques designed to generate waypoints across its entire area at intervals of approximately [X] units.
waypoint_list = map.generate_waypoints(2.0)
With the aim of establishing connectivity, there exists a method to access the road network topology.
waypoint_tuple_list = map.get_topology()
this method outputs a collection of waypoint pairs (tuples), where each pair consists of two waypoints. For every pair, the first waypoint is connected to the second. Only a minimal required set of waypoints is generated to define the topology, specifically one waypoint per lane per road segment in the map.
Finally, in order to enable access to the entire road information, the map object can be transformed into the OpenDrive format and saved to disk in the specified format.
记录和回放--传感器数据
1.actor 生成销毁的事件。 actor的多维度状态
2.world里面actor 的属性,比如交通灯。etc
数据存储位置位于根目录中的CarlaUE4/Saved文件夹内,并附有--占用的数据空间为:一个模拟场景包含约159个参与者(其中包含67个交通信号灯和92辆车辆),在持续一小时的运行中占据了约387MB的空间。
client.start_recorder("recording01.log")
#to stop- 其它的API ,参数定义,使用时再深入
client.stop_recorder()
client.replay_file("recording01.log")
client.replay_file("recording01.log", start, duration, camera)
client.set_replayer_time_factor(2.0) #快进,快退
client.show_recorder_file_info("recording01.log")
回放功能可以分析碰撞,等等

Sample PY scripts to use with the recording / replaying system
There are some scripts you could use:
start_recording.py: this will start recording, and optionally you can spawn several actors and define how much time you want to record.
-f: filename of write
-n: vehicles to spawn (optional, 10 by default)
-t: duration of the recording (optional)
start_replaying.py: this will start a replay of a file. We can define the starting time, duration and also an actor to follow.
-f: filename of write
-s: starting time (optional, by default from start)
-d: duration (optional, by default all)
-c: actor to follow (id) (optional)
show_recorder_collisions.py: this will show all the collisions hapenned while recording (currently only involved by hero actors).
-f: filename of write
-t: two letters definning the types of the actors involved, for example: -t aa
h = Hero
v = Vehicle
w = Walker
t = Traffic light
o = Other
a = Any
show_recorder_actors_blocked.py: this will show all the actors that are blocked (stopped) in the recorder. We can define the time and distance to be considered as blocked.
-f: filename of write
-t: minimum seconds stopped to be considered as blocked (optional)
-d: minimum distance to be considered stopped (optional)
API 的基本学习暂告一段路。
Section 2--仿真配置
- CARLA 支持固定fps和可变fps. 推荐使用固定,更快。 fps >=10fps!
$ ./CarlaUE4.sh -benchmark -fps=10
window:
.\CarlaUE4.exe -benchmark -fps=10
- 更换地图:+其它参数 test OK
Linux
./CarlaUE4.sh /Game/Carla/Maps/Town01
rem Windows
.\CarlaUE4.exe /Game/Carla/Maps/Town05
- 同步工作模式:
Whenever utilizing the synchronous mode, execute the simulator with a fixed timestep.
# Example: Synchronizing a camera with synchronous mode.
settings = world.get_settings()
settings.synchronous_mode = True
world.apply_settings(settings)
camera = world.spawn_actor(blueprint, transform)
image_queue = queue.Queue()
camera.listen(image_queue.put)
while True:
world.tick()
timestamp = world.wait_for_tick()
image = image_queue.get()
Other command-line options
-carla-port=N配置客户端连接监听在端口N,并将流媒体端口设置为N+1。-quality-level={Low,Epic}调整图形质量级别时,默认选择'Low'模式会显著提升运行效率。-no-rendering关闭渲染功能。- 全UE4命令行参数列表。
camera & sensors
传感器设置了一个监听机制,在触发后会启动相应的处理流程。观察以下示例:TBT
# Find the blueprint of the sensor.
blueprint = world.get_blueprint_library().find('sensor.camera.rgb')
# Modify the attributes of the blueprint to set image resolution and field of view.
blueprint.set_attribute('image_size_x', '1920')
blueprint.set_attribute('image_size_y', '1080')
blueprint.set_attribute('fov', '110')
# Set the time in seconds between sensor captures
blueprint.set_attribute('sensor_tick', '1.0')
# Provide the position of the sensor relative to the vehicle.
transform = carla.Transform(carla.Location(x=0.8, z=1.7))
# Tell the world to spawn the sensor, don't forget to attach it to your vehicle actor.
sensor = world.spawn_actor(blueprint, transform, attach_to=my_vehicle)
# Subscribe to the sensor stream by providing a callback function, this function is
# called each time a new image is generated by the sensor.
sensor.listen(lambda data: do_something(data))
数据的基本属性:
| Sensor data attribute | Type | Description |
|---|---|---|
frame_number |
int | Frame number when the measurement took place |
timestamp |
double | Timestamp of the measurement in simulation seconds since the beginning of the epispode |
transform |
carla.Transform | Transform in world coordinates of the sensor at the time of the measurement |
传感器类型:
- sensor.camera.RGB
- sensor.camera.distance
- sensor.camera.语义分割
- sensor.lidar.ray tracing
- sensor.other.碰撞检测
- sensor.other.车道侵入检测
- sensor.other.障碍物检测
具体说明参考官网。
API:
carla.Sensor(carla.Actor)
is_listeninglisten(callback_function)stop()
carla.SensorData
frame_numbertimestamptransform
carla.Image(carla.SensorData)
宽度
高度
视野角度
原始数据
将颜色转换为指定格式
保存到指定路径(可选颜色格式)
获取长度
遍历操作
根据位置获取元素
设置指定位置的颜色值(可选颜色)
carla.LidarMeasurement(carla.SensorData)
水平角度
信道
原始数据
count_points_in_channel()
write_data_to_file(path)
data_count()
data_iterator()
get_item_at(pos)
set_item_at(pos, location)
index
carla.CollisionEvent(carla.SensorData)
actorother_actornormal_impulse
carla.LaneInvasionEvent(carla.SensorData)
actorcrossed_lane_markings
carla.GnssEvent(carla.SensorData)
latitudelongitudealtitude
carla.ObstacleDetectionSensorEvent(carla.SensorData)
actorother_actordistance
carla.LaneType
NONEDrivingStopShoulderBikingSidewalkBorderRestrictedParkingBidirectionalMedianSpecial1Special2Special3RoadWorksTramRailEntryExitOffRampOnRampAny
carla.LaneChange
NONERightLeftBoth
carla.LaneMarkingColor
Standard = WhiteBlueGreenRedWhiteYellowOther
carla.LaneMarkingType
- 空状态
- 其他类别
- 故障状态
- 稳定状态
- SS状态
- SB状态
- BS状态
- BB状态
- BT.Dots状态
- 草地元素
- 路沿线元素
carla.LaneMarking
type→ carla.LaneMarking(车道标记)color→ carla.RoadMarkColor(路面标记颜色)lane_change→ carla.LaneChange(车道变更)width→ carla.Width(宽度)
QA
Is it possible to dump images from the CARLA simulator view?
Yes, it represents a key feature of the Unreal Engine. By using CARLA, you can render images captured by a spectator camera (in simulator view) when executing $ ./CarlaUE4.sh -benchmark -fps=30 -dumpmovie. These images are saved to "CarlaUE4/Saved/Screenshots/LinuxNoEditor".
How to make a new map with RoadRunner? (特别繁琐。不方便!!)
Automatically generate a map from RoadRunner
--需要在code项目下面尝试,编译发行版本不行!
Importing and exporting maps for distribution builds
--linux环境下,
Where can I learn more about Unreal Engine? TBC!!
A fundamental overview of C++ programming within Unreal Engine can be located on Unreal’s C++ Programming Tutorials. If you’re eager to explore more in terms of UE4 C++ development, several paid options are available online. The Udemy course titled Unreal C++ Course is quite comprehensive, and regular discounts often make it highly accessible.
Are there any examples in CARLA to see how Unreal programming works?
Find an example of how C++ classes function within UE4 in the ASceneCaptureToDiskCamera class (and its parent ASceneCaptureCamera). This class facilitates the creation of an actor that can be integrated into the scene. In the editor, locate and input "Scene Capture To Disk" in the Modes tab, then drag and drop the actor into your scene. Upon examining its properties through a detail tab, you'll observe all UPROPERTY members. This demonstrates how to utilize C++ classes within Unreal Editor.
For a more advanced example on how to extend C++ classes with blueprints, you can refer to the VehicleSpawner blueprint. This blueprint inherits from the C++ class AVehicleSpawnerBase. The underlying C++ class determines where and when it spawns a vehicle, subsequently invoking the SpawnVehicle() function, which is defined within the blueprint. The blueprint itself then determines both the model and color of the spawned vehicle. It's worth noting that this blueprint can dynamically invoke C++ functions as needed, such as fetching a random engine. Through this mechanism, there exists an ongoing exchange between C++ code and blueprints.
