Carla实战 | 手把手教您在Carla中运行OpenSCENARIO 2.0示例

Step1:场景编写

基于 OpenSCENARIO 2.0 的场景描述简短易读,且表达能力丰富,如对于一个 NPC 变道场景,其描述如下:

import basic.oscscenario top:    path: Path    path.set_map("Town04")    path.path_min_driving_lanes(3)     ego_vehicle: Model3     npc: Rubicon    event start    event end    event left    event right    do serial:        get_ahead: parallel(duration: 15s):            ego_vehicle.drive(path) with:                speed(20kph)                lane(1, at: start)            npc.drive(path) with:                lane(right_of: ego_vehicle, at: start)                position(15m, behind: ego_vehicle, at: start)                position(20m, ahead_of: ego_vehicle, at: end)        change_lane: parallel(duration: 5s):            ego_vehicle.drive(path)            npc.drive(path) with:                change_lane(lane_changes:[1..2], side: left)        slow: parallel(duration: 20s):            ego_vehicle.drive(path)            npc.drive(path) with:                speed(20kph)
Step2:运行Carla

使用 Docker 启动 Carla 仿真器:

docker run -d --privileged --gpus all -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix:rw -p "2000-2003":"2000-2003" carlasim/carla:0.9.14 ./CarlaUE4.sh -nosound

Step3:运行场景

打开第一个终端,运行控制程序:

git clone git@github.com:carla-simulator/scenario_runner.gitcd scenario_runnerpython manual_control.py -a --rolename=ego_vehicle

打开另一个终端运行编写的场景文件:

python scenario_runner.py --openscenario2 srunner/examples/change_lane.osc

在 Carla 中的渲染效果如下图:

NPC变道场景效果图

示例场景代码简要说明

scenario top - 场景执行入口path: Path - 场景运行的路径path.set_map("Town04") - 内置函数,设置地图path.path_min_driving_lanes(3) - 内置函数,用于约束场景发生的路同一方向至少有三条车道。ego_vehicle: Model3 - 定义主车为Model3npc: Rubicon - 定义NPC车辆为Rubicon# start事件和end事件分别表示某个时间段的开始和结束。left事件和right事件可以用在变道函数中。event startevent endevent leftevent right# 场景事件可以为serial:顺序执行;parallel:并行执行;one_of:执行其中一个# 此场景中有三个顺序事件,get_ahead, change_lane, slow, 可以自定义名称do serial  # duration 指定此阶段时间持续15s  get_ahead: parallel(duration: 15s):    ...  # 主车保持正常行驶,npc执行change_lane的变道操作。  # npc会从2车道变道到1车道,并且在5秒内完成。  change_lane: parallel(duration: 5s):    ...  # 主车和npc正常行驶20秒,然后场景结束  slow: parallel(duration: 20s):    ...

本文通过OpenScenario2.0场景描述和Carla运行示例,帮助您应用更先进的标准概念和自动驾驶仿真工具链,促进开发符合业务需求和复杂场景的应用。

参考链接

开源地址:https://github.com/carla-simulator/scenario_runner

场景示例参考:https://github.com/carla-simulator/scenario_runner/blob/master/srunner/examples/change_lane.osc

OpenSCENARIO V2.0.0标准发布地址:https://www.asam.net/standards/detail/openscenario/v200

深信科创:致力于自动驾驶工业软件

深信科创是一家专注于提供自动驾驶仿真及智慧交通解决方案的国家高新技术企业。公司基于人工智能、软件测试、数字孪生与大数据等技术,一直致力于自动驾驶领域的研发和探索,拥有一支高素质的研发团队,自主研发了自动驾驶仿真及数据闭环工具链SYNKROTRON®  Oasis产品系列,能够提供高精度传感器模型、动力学模型及感知级交通环境仿真解决方案等,客户可以在仿真平台上对自动驾驶系统开展大规模的仿真测试和模型训练,提前识别自动驾驶系统缺陷、降低实车测试成本、消除场景端落地的安全隐患,加速自动驾驶技术在场景端的安全落地。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
CARLA使用PID控制需要遵循以下几个步骤: 1. 获取车辆当前状态,例如车辆的位置、速度和方向等。 2. 计算目标状态,例如目标位置、速度和方向等。 3. 计算误差,即当前状态与目标状态之间的差异。 4. 使用PID控制器计算控制输出,这个输出作为车辆的控制指令,例如油门、刹车和方向盘角度等。 5. 将控制指令应用于CARLA模拟器的车辆,使其按照预期方式运动。 下面是一个简单的示例代码,演示如何使用PID控制实现CARLA的跟随车道行驶: ```python import carla # 定义PID控制器参数 kp = 1.0 ki = 0.1 kd = 0.01 # 定义目标速度和目标车道心线 target_speed = 50 target_lane_center = carla.Location(x=100, y=200, z=0) # 创建CARLA客户端和模拟器世界 client = carla.Client('localhost', 2000) client.set_timeout(10.0) world = client.get_world() # 获取模拟器第一辆车 vehicle = world.get_actors().find('vehicle.*.*') # 创建PID控制器 pid_controller = carla.controller.PIDController(kp, ki, kd) while True: # 获取车辆当前位置、速度和方向 current_location = vehicle.get_location() current_velocity = vehicle.get_velocity() current_direction = vehicle.get_transform().rotation.yaw # 计算目标方向 target_direction = (target_lane_center - current_location).get_yaw() # 计算方向误差 direction_error = target_direction - current_direction # 使用PID控制器计算方向控制输出 steering = pid_controller.step(direction_error, world.get_snapshot().timestamp.delta_seconds) # 计算速度误差 speed_error = target_speed - current_velocity.length() # 使用PID控制器计算油门控制输出 throttle = pid_controller.step(speed_error, world.get_snapshot().timestamp.delta_seconds) # 应用控制指令到车辆 vehicle.apply_control(carla.VehicleControl(throttle=throttle, steer=steering)) # 等待一段时间,使模拟器模拟车辆运动 time.sleep(0.1) ``` 这段代码的具体实现可能需要根据你的具体应用场景进行调整和修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值