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

carla_timeline.png

modules

introdution:

  1. 开源软件
  2. unreal engine 4
  3. 用户通过Python API 调用, server --client 的形式- REF: https://carla.readthedocs.io/en/latest/python_api/

let's start:

The API has been significantly changed in the latest versions starting at 0.9.0. We commonly refer to the new API as 0.9.X API as opposed to the previous 0.8.X API


Section 1  API 学习

 

官网下载软件包,解压, shit+右键,解压路径下打开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 are adding 80 vehicles to the world driving in "autopilot" mode. Back to the simulator window we should see these vehicles driving around the city. They will keep driving randomly until we stop the script. Let's leave them there for now

这个命令实现了添加80个车辆,随机的自主运动。 CTR+C 结束演示。 脚本有难度,Lesson2 后,查看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 (模拟器中的演员对象-比如汽车)

blueprints--actor对应的属性(比如颜色,车型 etc),在构造蓝图blueprints中设置。--所有的属性都包含在 一个库中。

blueprint_library = world.get_blueprint_library()

The library allows us to find specific blueprints by ID, filter them with wildcards, or just choosing one at random

# 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 of the attributes of the blueprints can be modified while some other are just read-only. For instance, we cannot modify the number of wheels of a vehicle but we can 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 set up, spawning an actor is pretty straightforward

transform = Transform(Location(x=230, y=195, z=40), Rotation(yaw=180))# 生成点坐标创建
actor = world.spawn_actor(blueprint, transform) #这个时候会检查生成点坐标是否会发生碰撞。如果有碰撞,会报错

shen

The spawn actor function comes in two flavours, spawn_actor and try_spawn_actor. The former will raise an exception if the actor could not be spawned, the later will return None instead. The most typical cause of failure is collision at spawn point, meaning the actor does not fit at the spot we chose; probably another vehicle is in that spot or we tried to spawn into a static object.

To ease the task of finding a spawn location, each map provides a list of recommended transforms

推荐

spawn_points = world.get_map().get_spawn_points() #返回所有的可用collision-free points.

We'll add more on the map object later in this tutorial.

Finally, the spawn functions have an optional argument that controls whether the actor is going to be attached to another actor. This is specially useful for sensors. In the next example, the camera remains rigidly attached to our vehicle during the rest of the simulation

camera = world.spawn_actor(camera_bp, relative_transform, attach_to=my_vehicle)

# (附加参数,可以控制物体是否和其他的actor绑定)--适用于传感器

Note that in this case, the transform provided is treated relative to the parent actor.

讲了有点多,开始动手练习。直接在 calar解压路径下,创建练习code.(其他地方需要添加环境变量。要不然 下面会报缺少相关文件。

import carla

首先打开模拟器(server), 否则会报错

创建一个client. 创建actor, blueprints. 调用生成函数,在世界中生成。

代码在spyder中编译会报错。报“calra 没有 client这个属性!”---TB fix

偶尔发现可以正常运行。

暂时通过shell 命令行来执行。运行OK

 

参考官方example: spawn_npc.py ,尝试练习

import glob
import os
imp
### 自动驾驶仿真测试的技术与工具 #### 一、自动驾驶仿真测试的核心技术 自动驾驶仿真测试依赖于多种关键技术来构建逼真的虚拟环境并验证系统的性能。以下是几个核心领域: 1. **全栈仿真实现** 全栈仿真测试平台覆盖了自动驾驶系统的多个层面,包括感知、决策和控制模块。这种全方位的仿真可以精确地模拟传感器数据采集过程以及算法处理逻辑[^2]。 2. **数字孪生应用** 数字孪生技术通过创建物理实体的数字化副本,在虚拟环境中重现真实的车辆动态行为及其所处的道路状况。这种方法不仅提高了测试效率,还增强了对极端情况下的适应能力[^3]。 3. **高精度地图支持** 高精地图对于自动驾驶至关重要,它提供了详细的地理信息用于路径规划和定位服务。在仿真环境下,这些地图同样被用来定义道路结构和其他静态要素的位置关系。 4. **AI驱动的行为建模** 使用人工智能生成多样化的行人动作模式或者复杂的交通流态样貌,使得整个场景更加贴近实际生活中的不确定性因素影响下的人类驾驶员反应特性。 #### 二、主流仿真测试工具介绍 目前市场上存在多款成熟的自动驾驶仿真软件解决方案,下面列举几项较为知名的产品供参考: 1. **CARLA (Car Learning to Act)** CARLA 是一款开源的城市驾驶模拟器,专为训练和发展自主导航系统而设计。其特色在于开放源码性质允许开发者自由定制实验条件,并且内置丰富的天气变化选项以增加挑战度。 ```bash git clone https://github.com/carla-simulator/carla.git ``` 2. **VTD (Virtual Test Drive)** VTD 提供了一套综合性的ADAS 和 AD 开发流程管理框架,专注于提升大规模部署前的功能安全性评估质量。该产品强调硬件在环(HIL)集成能力和跨平台兼容性特点[^1]。 3. **Autoware.Auto Simulation Framework** 基于 ROS2 构建而成,旨在促进社区协作共同推进无人驾驶技术研发进程的同时保持高度灵活性以便适配不同类型的计算架构需求。 #### 三、具体实施方案建议 为了成功实施一套有效的自动驾驶仿真测试计划,需考虑以下几个方面: - 明确目标范围:确定要验证的具体功能点是什么? - 设计合理的指标体系:如何量化衡量各项表现优劣程度? - 数据收集准备阶段:确保有足够的样本量支撑后续分析工作开展; - 执行迭代优化循环直至达到预期效果为止; ```python import carla client = carla.Client('localhost', 2000) world = client.get_world() blueprint_library = world.get_blueprint_library() vehicle_bp = blueprint_library.find('vehicle.tesla.model3') spawn_point = random.choice(world.get_map().get_spawn_points()) ego_vehicle = world.spawn_actor(vehicle_bp, spawn_point) try: while True: ego_vehicle.apply_control(carla.VehicleControl(throttle=0.5)) finally: ego_vehicle.destroy() ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值