1. 介绍
本文介绍的内容为Carla中的actor和blueprints,实在不知道怎么翻译actor,感觉翻译成演员有些奇奇怪怪,因此将actor代指成对象。
2. actor组成
车辆,行人,传感器,交通标志,交通信号灯,观察者都属于actor,从面向对象的角度,actor是上述对象的父类
It is crucial to have full understanding on how to operate on actors
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,随机或者正则表达式的方式实现
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))
actor = world.spawn_actor(blueprint, transform)
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()
actor = actor_list.find(id)
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世界中回收创建的对象。
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)
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())
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.
修改红绿灯状态。
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)
|