carla学习笔记(八)

        之前调试ros和carla的一起运行把carla给搞崩了,昨天才安装好.真的是麻烦,环境搭配是大问题.

        好久没有看carla的example,继续学习它的automatic_control.py,现在大部分可以明白了.

这个源码里面核心的部分就是关于agent的应用,它需要在while循环里面不断的更新agent的信息,并且要然后关于路径规划的部分只有set_destination()函数,我觉得按目前的要求,也只用掌握这个函数就可以了,剩下的部分就是解决传感器和数据采集的卡顿问题了.今天根据automatic_control.py实现了自己的一个小小的想法,让vehicle不断的运动,从一个点运动到另一个点,等到了点后自动更新下一个点,这样一直循环.下面是最后呈现的源码.bug很多,最重要的是中断程序carla就直接崩溃卡死了,不知道为什么,需要再研究学习下.

"""
本片文档主要练习在automatic里面学习的一些代码,
主要实现让vehicle设置一个路径点,
然后到达该路径点后自动寻找并前往下一个路径点,
主要运用的函数是
set_destination()函数
agent.done()
步骤:
1.先实现车辆从一个点到另一个点.(实现)
2.再实现到达另一个点后,自动随机选择下一个点(实现)
3.最后在加上lidar采集数据并保存在一个文件夹里面
反思:
对于同步频率的考虑不足,
对于agent的运行学习不足
对于最后退出的考虑不足
现在退出simulator后会直接卡死,不知道是什么原因
可能是没有使用try-except结构
"""

import random


import carla

from agents.navigation.behavior_agent import BehaviorAgent  
from agents.navigation.roaming_agent import RoamingAgent  
from agents.navigation.basic_agent import BasicAgent  

def show_point(world, point_location):
    world.debug.draw_string(point_location, 'X', draw_shadow=False,
                                        color=carla.Color(r=0, g=255, b=0), life_time=50,
                                        persistent_lines=True)

def main():
    try:
        client = carla.Client('localhost', 2000)
        client.set_timeout(2.0)

        world = client.get_world()

        settings = world.get_settings()
        settings.synchronous_mode = True
        settings.fixed_delta_seconds = 0.05
        world.apply_settings(settings)

        blueprint_library = world.get_blueprint_library()

        bp = blueprint_library.find('vehicle.tesla.cybertruck')

        spawn_points = world.get_map().get_spawn_points()
        
        show_point(world, spawn_points[0].location)

        show_point(world, spawn_points[1].location)

        vehicle_bp = world.try_spawn_actor(bp, spawn_points[0])
        world.tick()

        # 对于判断是否到达终点,一开始还不知道有agent.done()这个函数,
        # 最开始的设想是利用vehicle.get_location()获得的坐标与
        # 设定的destnation坐标进行比对然后发现它们居然z轴的坐标不一样,害我卡了好久
        # 然后就设定了x与y分别对比,发现也不能输出,然后在仿真界面才发现居然只是到达了点附近
        # 并没有准确的停在该点上...
        # 最后询问了师兄才知道agent里面有一个API,agent.done()专门解决这个问题.
        # 最后使用这个API实现了开头的任务.感觉还是得把agent相关的源码阅读一下.
        # test_location = vehicle_bp.get_location()
        # print(test_location.x, test_location.y)
        # print(spawn_points[0].location)
        # if ((test_location.x == spawn_points[0].location.x) and 
        #                 (test_location.y == spawn_points[0].location.y)):
        #     print(1111)
        # random.shuffle(spawn_points)

        agent = BasicAgent(vehicle_bp )

        agent.set_destination((spawn_points[1].location.x, 
                                            spawn_points[1].location.y,
                                            spawn_points[1].location.z))
        while True:
            agent.update_information(vehicle_bp)
            world.tick()
            if (agent.done()):
                print("到达目标点")
                random.shuffle(spawn_points)
                show_point(world, spawn_points[1].location)
                print("设立新目标点:%s", str(spawn_points[1].location))
                show_point(world, spawn_points[1].location)
                agent.set_destination((spawn_points[1].location.x, 
                                                                spawn_points[1].location.y,
                                                                spawn_points[1].location.z))
            
            control = agent.run_step(debug=True)
            vehicle_bp.apply_control(control)

    finally:
        world.destroy()

if __name__ == '__main__':

    try:
        main()
    except KeyboardInterrupt:
        pass
    finally:
        print('\ndone.')

### 关于CARLA学习资料与入门教程 #### CARLA简介 CARLA是一个开源的城市驾驶模拟器,专为自动驾驶研究而设计。该平台提供了丰富的传感器支持以及多样的城市环境场景设置功能[^3]。 #### 安装指南 对于希望快速上手CARLA的新手来说,可以参考预编译版本的安装方法。这其中包括针对独立显卡的具体配置建议、ROS集成步骤说明等内容。通过这些指导能够帮助用户顺利完成软件部署并启动虚拟测试环境。 #### 地图探索 为了更好地理解CARLA内部结构,在学习过程中还可以关注如何获取不同视角下的地图表示形式——比如鸟瞰图。这类可视化工具不仅有助于直观展示整个城市的布局情况,同时也便于开发者调试算法逻辑或者规划路径方案[^1]。 #### 数据收集脚本 除了理论知识外,实践操作同样重要。`collect_data.py`文件提供了一个很好的例子来演示怎样利用Python API接口实现数据采集工作。此脚本展示了从初始化客户端连接到最终保存图像序列等一系列完整的流程[^2]。 ```python import glob import os import sys 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 def collect_data(): client = carla.Client('localhost', 2000) world = client.get_world() settings = world.get_settings() settings.synchronous_mode = True settings.fixed_delta_seconds = 0.05 world.apply_settings(settings) blueprint_library = world.get_blueprint_library() vehicle_bp = blueprint_library.filter('vehicle.*')[0] spawn_point = random.choice(world.get_map().get_spawn_points()) vehicle = world.spawn_actor(vehicle_bp, spawn_point) try: while True: snapshot = world.tick() # Advance the simulation one step. # Collect sensor data here finally: vehicle.destroy() ``` #### 车辆模型定制化 当掌握了基本的操作之后,则可以根据项目需求进一步尝试自定义车辆外观或物理属性。这部分涉及到Unreal Engine编辑器的应用技巧,包括但不限于骨骼绑定、材质贴图调整等方面的知识点[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值