Carla学习(一) 小车简单直线行走

看官方的文档加晚上的一些博主的教程,动手写了一个小车直线跑动的例子。官方最新的文档比较全面,但是东西太多了,本垃圾不知道从哪里学起,慢慢来吧。。。。

导入Carla包

在编写自己的客户端时,我们首先需要导入Carla包,利用下面这段固定的代码:

try:
    sys.path.append(glob.glob('../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

sys.path.append将glob定位到的dist文件夹中的carla-0.9.4-py3.5-linux-x86_64.egg添加到impot的路径当中,使得可以导入carla包。

获取世界

 # 创建client连接到Carla当中
    client = carla.Client('localhost', 2000)

    # seconds,设置连接超时时间
    client.set_timeout(10.0)

    # 获取世界,之后不再使用client
    world = client.get_world()

创建actor的blueprint

    # 通过world获取world中的Blueprint Library
    blueprint_library = world.get_blueprint_library()

    # 通过carla.BlueprintLibrary的find()和filter()方法查找特定ID的bp
    collision_sensor_bp = blueprint_library.find('sensor.other.collision')
    vehicle_bp = random.choice(blueprint_library.filter('vehicle.bmw.*'))
    camera_bp = blueprint_library.find('sensor.camera.rgb')

设置传感器的相关参数

   # carla.ActorBlueprint的get_attribute(key)和set_attribute(key, value)方法获取/设置actor的属性
    # 设置RGB相机的分辨率和视野
    camera_bp.set_attribute('image_size_x', '480')
    camera_bp.set_attribute('image_size_y', '640')
    camera_bp.set_attribute('fov', '110')
    # 设置传感器捕捉数据时间间隔秒
    camera_bp.set_attribute('sensor_tick', '1.0')

创建车辆的transform,这里指的是车辆的坐标和旋转坐标,分为手动设置和自动获取两种方式

# 生成actors
    # 生成车辆,手动设置生成点
    '''
    location = carla.Location(230, 195, 40)
    rotation = carla.ratation(0, 0, 0)
    transform_vehicle = carla.Transform(location, rotation)
    '''
    # 自动选择生成点
    transform_vehicle = random.choice(world.get_map().get_spawn_points())

同样的设置相机的相对于车辆的坐标

   # 设置RGN相机相对依附对象位置
    transform_RGB = carla.Transform(carla.Location(x=0.8, z=1.7))

生成actor

    # 生成车辆和RGB摄像机
    my_vehicle = world.spawn_actor(vehicle_bp, transform_vehicle)
    my_camera = world.spawn_actor(camera_bp, transform_RGB, attach_to=my_vehicle)
    actor_list.append(my_camera)
    actor_list.append(my_vehicle)

简单的设置车辆一直向前行走,20s的时间,这里RGB相机数据还没有设置函数function处理:

    # 设置车辆运动参数
    my_vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=0))
    # 设置相机监听函数
    #my_camera.listen(lambda image: function(image))
   
     time.sleep(20)
        

运行结束后,销毁所有生成的actor:

    for actor in actor_list:
        actor.destroy()
        print(actor.id, "is destroyed.")

完整代码如下:

# 找到Carla包的路径
import glob
import os
import sys

try:
    sys.path.append(glob.glob('../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 time
import random
import cv2
import numpy as np

# 存储生成的actor,方便后续销毁获取
actor_list = []
try:
    # 创建client连接到Carla当中
    client = carla.Client('localhost', 2000)

    # seconds,设置连接超时时间
    client.set_timeout(10.0)

    # 获取世界,之后不再使用client
    world = client.get_world()

    # 通过world获取world中的Blueprint Library
    blueprint_library = world.get_blueprint_library()

    # 通过carla.BlueprintLibrary的find()和filter()方法查找特定ID的bp
    collision_sensor_bp = blueprint_library.find('sensor.other.collision')
    vehicle_bp = random.choice(blueprint_library.filter('vehicle.bmw.*'))
    camera_bp = blueprint_library.find('sensor.camera.rgb')

    # carla.ActorBlueprint的get_attribute(key)和set_attribute(key, value)方法获取/设置actor的属性
    # 设置RGB相机的分辨率和视野
    camera_bp.set_attribute('image_size_x', '480')
    camera_bp.set_attribute('image_size_y', '640')
    camera_bp.set_attribute('fov', '110')
    # 设置传感器捕捉数据时间间隔秒
    camera_bp.set_attribute('sensor_tick', '1.0')

    # 生成actors
    # 生成车辆,手动设置生成点
    '''
    location = carla.Location(230, 195, 40)
    rotation = carla.ratation(0, 0, 0)
    transform_vehicle = carla.Transform(location, rotation)
    '''
    # 自动选择生成点
    transform_vehicle = random.choice(world.get_map().get_spawn_points())
    # 设置RGN相机相对依附对象位置
    transform_RGB = carla.Transform(carla.Location(x=0.8, z=1.7))

    # 生成车辆和RGB摄像机
    my_vehicle = world.spawn_actor(vehicle_bp, transform_vehicle)
    my_camera = world.spawn_actor(camera_bp, transform_RGB, attach_to=my_vehicle)
    actor_list.append(my_camera)
    actor_list.append(my_vehicle)

    # 设置车辆运动参数
    my_vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=0))
    # 设置相机监听函数
    #my_camera.listen(lambda image: function(image))

    time.sleep(20)
finally:
    for actor in actor_list:
        actor.destroy()
        print(actor.id, "is destroyed.")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值