首页/文章/ 详情

记录Carla使用过程中的几个常见代码案例

1年前浏览1303

我是李慢慢。

关于Carla的使用,研究了好长一段时间的代码。在这里简单做个记录。

注:本篇分享的代码都可以直接运行,但需要将仿真世界打开,即打开Carla程序,如下。


我这里使用的是Windows版的预编译版。Carla的软件下载安装初步使用见上一篇文章,如下。


介绍一款开源的自动驾驶仿真模拟器-Carla


废话不多说了,以下直接步入正文。



示例1-创建车辆并控制其行驶

此乃基础软件架构案例。

创建一个套接字客户端,往仿真世界中添加一辆车辆,并控制车辆的驾驶。







































# -*- coding:utf-8 -*-#import carlaimport randomimport time##try:    print("================ Starting ================")# 创建一个客户端    client = carla.Client('localhost',2000)    client.set_timeout(5)# 连接到仿真世界    world = client.get_world()    print("world:", world)    blueprint_library = world.get_blueprint_library()    print("length of blueprint_library:", len(blueprint_library))#for bp in blueprint_library:#   print("bp:", bp)# 在仿真世界中创建车辆    v_bp = blueprint_library.filter("model3")[0]    print("v_bp:",v_bp)    spawn_points = world.get_map().get_spawn_points()#print("spawn_points:",spawn_points)#spawn_point = random.choice(spawn_points)    spawn_point = spawn_points[0]    my_car = world.spawn_actor(v_bp, spawn_point)    print("my_car:", my_car)    # 控制当前车辆的驾驶行为:加速直行    # my_car.apply_control(carla.VehicleControl(throttle=1.0, steer=0))# 控制当前车辆的驾驶行为:自动驾驶    my_car.set_autopilot(True)    time.sleep(10)except Exception as e:    print("Exception detected:", e)finally:    my_car.destroy()    print("================ ending ================")


这个程序运行起来,你有10s的时间可以去仿真世界里找到这个车在哪里(键盘上的WASD键配合鼠标可以转动仿真世界的视角)。


示例2-切换观察者视角

上面这个程序,运行时想要找到车辆在哪很麻烦,因此,以下利用spector即观察者对象,来实时跟踪车辆的位置。



































































# -*- coding:utf-8 -*-#import carlaimport randomimport time##try:    print("================ Starting ================")# 创建一个客户端    client = carla.Client('localhost',2000)    client.set_timeout(5)# 连接到仿真世界    world = client.get_world()    print("world:", world)    blueprint_library = world.get_blueprint_library()    print("length of blueprint_library:",len(blueprint_library))for bp in blueprint_library:       print("bp:", bp)# 在仿真世界中创建车辆    v_bp = blueprint_library.filter("model3")[0]    print("v_bp:",v_bp)    spawn_points = world.get_map().get_spawn_points()#print("spawn_points:",spawn_points)#spawn_point = random.choice(spawn_points)    spawn_point = spawn_points[0]    my_car = world.spawn_actor(v_bp, spawn_point)    print("my_car:", my_car)# 控制当前车辆的驾驶行为:加速直行# my_car.apply_control(carla.VehicleControl(throttle=1.0, steer=0))# 控制当前车辆的驾驶行为:自动驾驶    my_car.set_autopilot(True)
# 设置观察者视角# 获取世界的观察者对象,即spectator对象    spectator = world.get_spectator()# 获取spectator的坐标# transform = spectator.get_transform()# location = transform.location  # 定位# rotation = transform.rotation  # 角度
# Set the spectator with an empty transform# spectator.set_transform(carla.Transform())
#    simTime = 0while simTime <= 2000:        print("simTime:", simTime)# spectator = world.get_spectator()        transform = my_car.get_transform()        print("location:", transform.location)# 上帝视角        my_transform = carla.Transform((transform.location + carla.Location(z=50)), carla.Rotation(pitch=-90))# 车后视角# my_transform = carla.Transform((transform.location + carla.Location(x=-6, y=0, z=3)), carla.Rotation(pitch=-10))# 驾驶员视角# my_transform = carla.Transform((transform.location + carla.Location(x=2, y=0, z=1.5)), carla.Rotation(pitch=-10))        spectator.set_transform(my_transform)# 以旁观者视角跟随观察车辆的运行        simTime += 1#time.sleep(100)except Exception as e:    print("Exception detected:", e)finally:    my_car.destroy()    print("================ ending ================")


以下是以上帝视角观察主车的效果,该程序将循环更新仿真世界的视角,实时跟进主车的位置,以上帝视角来跟进。


示例3-切换仿真世界

如果想要切换仿真世界,给它换一个地图,可以参考下面的代码:


























# -*- coding:utf-8 -*-#import carlaimport randomimport time##try:    print("================ Starting ================")# 创建一个客户端    client = carla.Client('localhost', 2000)    client.set_timeout(5)# 客户端连接到仿真世界    world = client.get_world()    print("the current world is:", world)# 获得所有内部地图列表    map_list = client.get_available_maps()for map_index in map_list:        print("map available:", map_index)# 设置世界的地图(从上述列表中选择)    client.load_world('Town11_Opt')except Exception as e:    print("Exception detected:", e)finally:    print("================ ending ================")

运行后的输出如下:


可以看到有很多预制地图可以使用的,当我们加载了Town11_Opt后,仿真世界也随之发生了变化。


上述地图都是Carla官方提供的,我们如何自己制作地图,并加载到Carla的仿真世界里呢?道友请继续往下看。


示例4:加载OpenDrive地图

OpenDrive是自动驾驶仿真领域公用的道路地图格式,我在之前的文章中有介绍到如何使用开源软件truevision或者开源Python库scenariogeneration来创建OpenDrive格式的地图。诸君可以戳开看看:

1、介绍一款开源的OpenDrive地图制作软件-truevision

2、如何用python库scenariogeneration来创建静态地图+动态场景进行自动驾驶仿真

Carla软件是和MathWorks推出的RoadRunner软件合作来制作OpenDrive地图的,但是RoadRunner是收费的,所以上面推荐的两个开源途径,完全免费,没有灵石的道友可以试试看,亲测可用。

当我们有了自制的OpenDrive地图时,就可以使用Carla来进行加载并使用了,以下示例:







































# -*- coding:utf-8 -*-#import carlaimport randomimport time##try:    print("================ Starting ================")# 创建一个客户端    client = carla.Client('localhost', 2000)    client.set_timeout(5)# 加载OpenDrive地图    xodr_path = str("F:/truevision/xodr/crossing8_carla.xodr")# xodr_path = str("F:/truevision/xodr/straight_road_2lanes_200m.xodr")# xodr_path = str("F:/truevision/xodr/sample_database.xodr")with open(xodr_path, encoding='utf-8') as od_file:            data = od_file.read()            vertex_distance = 1.0  # in meters            max_road_length = 500.0  # in meters            wall_height = 0.5      # in meters            extra_width = 1.0      # in meters            world = client.generate_opendrive_world(                data, carla.OpendriveGenerationParameters(                    vertex_distance=vertex_distance,                    max_road_length=max_road_length,                    wall_height=wall_height,                    additional_width=extra_width,                    smooth_junctions=True,                    enable_mesh_visibility=True))    print("the current world is:", world)#except Exception as e:    print("Exception detected:", e)finally:pass    print("================ ending ================")


以上代码运行后效果如下:


接下来可以试着往道路中添加一辆车了,并控制其自动驾驶。再运行下示例2的代码,就能看到如下的效果了。


本期先记录这么多吧。

诸位道友如有所得,欢迎点赞-关注-转发-收藏。

祝大家中秋愉快。

瑞斯拜。

来源:车路慢慢
python自动驾驶控制
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2023-06-22
最近编辑:1年前
李慢慢
硕士 自动驾驶仿真工程师一枚
获赞 11粉丝 63文章 122课程 0
点赞
收藏
未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈