Carla 对象和蓝图

1. 介绍

本文介绍的内容为Carla中的actorblueprints,实在不知道怎么翻译actor,感觉翻译成演员有些奇奇怪怪,因此将actor代指成对象

2. actor组成

车辆,行人,传感器,交通标志,交通信号灯,观察者都属于actor,从面向对象的角度,actor是上述对象的父类

It is crucial to have full understanding on how to operate on actors

actor的组成

3. 蓝图

获取Carla世界中的蓝图与蓝图对应的属性信息

blueprints = [bp for bp in world.get_blueprint_library().filter('*')]
for blueprint in blueprints:
   print(blueprint.id)
   for attr in blueprint:
       print('  - {}'.format(attr))

蓝图中包含的内容有
蓝图
各个对象(actor)的属性可以在官方文档中查询

4. 使用蓝图

获取蓝图库

blueprint_library = world.get_blueprint_library()

蓝图的获取可以通过id,随机或者正则表达式的方式实现

# 通过id
collision_sensor_bp = blueprint_library.find('sensor.other.collision')
# 随机获取
random_bp = random.choice(blueprint_library)
# 通过正则表达式获取
vehicle_bp = blueprint_library.filter('vehicle.*.*')

蓝图中包含了一系列的对象属性,可以通过修改蓝图中的对象属性创建个性化的对象。

each carla.ActorBlueprint has a series of carla.ActorAttribute that can be get and set.

# 设置行人属性
walker_bp = world.get_blueprint_library().filter('walker.pedestrian.0002')
walker_bp.set_attribute('is_invincible', True)

# 设置车辆属性
vehicle_bp = wolrd.get_blueprint_library().filter('vehicle.bmw.*')
color = random.choice(vehicle_bp.get_attribute('color').recommended_values)
vehicle_bp.set_attribute('color', color)

# 设置传感器属性
camera_bp = world.get_blueprint_library().filter('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', 600)
camera_bp.set_attribute('image_size_y', 600)

使用is_modifiable判断当前属性是否可以修改

for attr in blueprint:
    if attr.is_modifiable:
        blueprint.set_attribute(attr.id, random.choice(attr.recommended_values))

5. 对象(Actor)生命周期

对象的生命周期包括创建(Spawning),仿真(Handling)和回收(Destruction)三个过程。

1. 创建对象

transform = Transform(Location(x=230, y=195, z=40), Rotation(yaw=180))
# 创建对象spawn_actor,创建失败则会报错
actor = world.spawn_actor(blueprint, transform)
# 创建对象try_spawn_actor(),创建失败返回None
actor = world.try_spawn_actor(blueprint, transform)

获取地图上的生成点

# 获取车辆的生成点
spawn_points = world.get_map().get_spawn_points()
# 获取行人的生成点
spawn_point = carla.Transform()
spawn_point.location = world.get_random_location_from_navigation()

创建成功后,将对象添加到对象列表中,便于查找和管理

actor_list = []
actor_list.append(actor)

获取Carla世界中的对象列表

actor_list = world.get_actors()
# 使用id查找对象
actor = actor_list.find(id)
# 打印输出Carla世界中所有限速标志的位置
for speed_sign in actor_list.filter('traffic.speed_limit.*'):
    print(speed_sign.get_location())

2. 仿真对象

Carla对象中大多数类提供get()和set()方法用于设置读取和设置仿真对象的属性。例如获取车辆的加速度与速度,设置车辆的位置等。

print(actor.get_acceleration())
print(actor.get_velocity())

location = actor.get_location()
location.z += 10.0
actor.set_location(location)

3. 回收对象

当.py文件执行结束后,创建的对象并不会自动消失,必须显示调用destroy()函数,才能在Carla世界中回收创建的对象。

# Returns True if successful
destroyed_sucessfully = actor.destroy()

# 在一个仿真步长中批量回收创建的对象
client.apply_batch([carla.command.DestroyActor(x) for x in vehicles_list])

6. 示例代码

1. 创建车辆

创建车辆并开启自动驾驶

ego_vehicle_bp = blueprint_library.find('vehicle.mercedes-benz.coupe')
ego_vehicle_bp.set_attribute('color', '0, 0, 0')
transform = random.choice(world.get_map().get_spawn_points())
ego_vehicle = world.spawn_actor(ego_vehicle_bp, transform)
ego_vehicle.set_autopilot(True)

2. 创建行人

创建行人并使用Ai控制

import glob
import os
import sys
import random
import os

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 main():
    try:
        client = carla.Client('localhost', 2000)
        client.set_timeout(10.0)
        world = client.get_world()
        blueprintsWalkers = world.get_blueprint_library().filter("walker.pedestrian.*")
        # 创建行人
        walker_bp = random.choice(blueprintsWalkers)
        spawn_point = carla.Transform()
        spawn_point.location = world.get_random_location_from_navigation()
        walker = world.spawn_actor(walker_bp, spawn_point)
        # 创建用于控制行人的Ai
        walker_controller_bp = world.get_blueprint_library().find('controller.ai.walker')
        walker_controller = world.spawn_actor(walker_controller_bp, carla.Transform(), walker)
		# 开启控制
        walker_controller.start()
        walker_controller.go_to_location(world.get_random_location_from_navigation())
        walker_controller.set_max_speed(1 + random.random())  # Between 1 and 2 m/s (default is 1.4 m/s).
		# 放置观察者
        while(True):
            spectator = world.get_spectator()
            transform = walker.get_transform()
            spectator.set_transform(carla.Transform(transform.location + carla.Location(x=5),
            carla.Rotation(roll=0, yaw = 180, pitch = 0)))

    finally:
    	# 停止控制
        walker_controller.stop()
        # 回收行人
        walker.destroy()



if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print(' - Exited by user.')

3. 创建传感器

创建相机传感器,将其安装到车辆上,并将图片数据保存在电脑中

camera_bp = blueprint_library.find('sensor.camera.rgb')
camera = world.spawn_actor(camera_bp, relative_transform, attach_to=my_vehicle)
camera.listen(lambda image: image.save_to_disk('output/%06d.png' % image.frame))

4. 放置观察者

将观察者放置在车辆上方50m处,视角垂直向下。

spectator = world.get_spectator()
transform = vehicle.get_transform()
spectator.set_transform(carla.Transform(transform.location + carla.Location(z=50),
carla.Rotation(pitch=-90)))

5. 交通标志与信号灯

在Carla中,只有停车,让路(yield)和红绿灯被视为对象,其余的交通标志不被视为对象,在Carla中定义为carla.Landmark。在仿真开始时,停车,让路与红绿灯自动生成,无法使用蓝图生成这些交通标志,因为蓝图库中不存在这些交通标志的定义。

Only stops, yields and traffic lights are considered actors in CARLA so far.

When the simulation starts, stop, yields and traffic light are automatically generated using the information in the OpenDRIVE file. None of these can be found in the blueprint library and thus, cannot be spawned.

CARLA maps do not have traffic signs nor lights in the OpenDRIVE file. These are manually placed by developers.

yield标志
修改红绿灯状态。

# 获取路口处红绿灯的状态
if vehicle_actor.is_at_traffic_light():
    traffic_light = vehicle_actor.get_traffic_light()
	# 将红灯改为绿灯
	if traffic_light.get_state() == carla.TrafficLightState.Red:
	    traffic_light.set_state(carla.TrafficLightState.Green)
	    traffic_light.set_set_green_time(4.0)
  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值