CARLA 笔记(04)— Client 和 World (创建 Client、连接 World 、批处理对象、设置 Weather、设置 Lights、World snapshots)

WorldClientCARLA的两个基本要素,是操作仿真及其参与者的必要抽象概念。

Client 是用户向仿真服务器请求信息或者改变状态的模块, Client 运行时需要一个与服务器连接的 IPport,同时可以有多个 Client 端运行,更高级的多客户端管理需要深入了解 Carla 和同步。

World 是一个代表仿真的对象。它作为一个抽象层,包含生成角色、改变天气、获取世界当前状态等主要方法。每个模拟只有一个世界。当地图改变时,它将被销毁并被一个新的 World 所取代。

1. Client

clientCARLA体系结构中的主要元素之一。它们连接到 server、检索信息以及发送命令,这些都是通过脚本完成的。客户端与 world连接,然后与操作 simulation。除此之外,client还可以访问高级CARLA模块、特性,还可以使用批处理命令。

1.1 创建 Client

需要两个参数,第一个是 IP地址,第二个是用于连接 serverTCP端口,第三个是可选参数用于设置工作线程的数量,默认设置为 all(0)

client = carla.Client('localhost', 2000)

默认情况下,CARLA使用本地主机 IP和端口 2000 进行连接,但这些可以随意更改。第二个端口经常为 N+1,类似于 2001 。

一旦客户端 client 被建立,就需要设置它的超时时间 timeout 。这限制了所有的网络操作,以便这些操作不会永远阻塞客户端。如果连接失败,将返回一个错误。

client.set_timeout(10.0) # seconds

可以连接多个客户端,因为通常有多个脚本同时运行。在具有高级 CARLA特性的多客户端方案中工作,如 traffic manager,必然会使通信变得更加复杂。

客户端和服务端是不同的 libcarla 模块。如果版本不同,可能会出现问题。这可以分别通过 get_client_version()get_server_version() 方法来检查各自的版本号。

1.2 连接 World

client 对象是跟 Carla 环境进行交互的唯一的口,有了 client 对象以后,我们就可以获取到 Carla 环境中的世界(World)了,World 对象代表 Carla 环境中的世界,你想要在世界中创建任何东西,都是往这个 World 对象中添加。

我们如果想让仿真世界有任何变化,都要对这个获取的 world 进行操作。Client 通过下面命令可以很容易的连接的到 world

world = client.get_world()

从客户端可以得到很多 maps以改变当前所选,那么当前的 world 将会被摧毁并新建一个 world

print(client.get_available_maps())

输出

['/Game/Carla/Maps/Town01', '/Game/Carla/Maps/Town01_Opt',
'/Game/Carla/Maps/Town02', '/Game/Carla/Maps/Town02_Opt',
'/Game/Carla/Maps/Town03', '/Game/Carla/Maps/Town03_Opt', 
'/Game/Carla/Maps/Town04', '/Game/Carla/Maps/Town04_Opt',    
'/Game/Carla/Maps/Town05', '/Game/Carla/Maps/Town05_Opt',
'/Game/Carla/Maps/Town10HD_Opt', '/Game/Carla/Maps/Town10HD', 
]

加载地图

world = client.load_world('Town01')
#client.reload_world() creates a new instance of the world with the same map.

每个 world中的对象都有一个 id。每次客户端调用 load_world()reload_world() 时,前一 world 个就会被销毁。一个新的 world 从头开始,虚幻引擎 UE在这个过程中没有重新启动。

1.3 使用批处理

命令行有一些最常见的 CARLA 方法的适配,也可以批量应用。例如,command.SetAutopilot 等同于 vehicle. set_autopilot(),启用车辆的自动驾驶。也可以使用批量的 Client 方法。如 apply_batchClient.apply_batch_sync(),就可以在一个仿真步长内处理多个命令。

下面的例子使用一个批处理来一次性销毁一个车辆列表。

client.apply_batch([carla.command.DestroyActor(x) for x in vehicles_list])

1.4 其它客户端组件

client 对象的主要目的是获取或更改世界,并应用命令。不过,它还提供了对一些附加特性。

  • Traffic manager:这个模块负责每一辆自动驾驶的车辆,以重现城市交通。
  • Recorder:允许重现以前的模拟,使用快照汇总每帧的模拟状态。

2. World

simulation 的主要控制者。它的实例由 client 检索。它不包含 world 本身的模型,这些模型是 Map 类的一部分。相反,大多数信息和一般设置都可以从这个类中访问,例如:

  • Actors in the simulation and the spectator
  • Blueprint library
  • Map
  • Simulation settings
  • Snapshots
  • Weather and light manager

2.1 Actors

world 中有与参与者相关的不同方法,这些方法允许不同的功能。

  • Spawn actors (but not destroy them)
  • Get every actor on scene, or find one in particular
  • Access the blueprint library
  • Access the spectator actor, the simulation’s point of view
  • Retrieve a random location that is fitting to spawn an actor

2.2 Weather

weather 本身不是一个类,而是一组可从世界获取的参数。参数包括太阳方向、云、风、雾等等。

weather = carla.WeatherParameters(
    cloudiness=80.0,
    precipitation=30.0,
    sun_altitude_angle=70.0)

world.set_weather(weather)

print(world.get_weather())

有些天气参数可以预设在 world

world.set_weather(carla.WeatherParameters.WetCloudySunset)

Carla 中也可以自己定制 weather,这就涉及到两个脚本:

  • environment.py (在 PythonAPI/util 中) 提供对天气和光线参数的访问,以便可以实时更改这些参数。该脚本可选的参数有:
  -h, --help            show this help message and exit
  --host H              IP of the host server (default: 127.0.0.1)
  -p P, --port P        TCP port to listen to (default: 2000)
  --sun SUN             Sun position presets [sunset | day | night]
  --weather WEATHER     Weather condition presets [clear | overcast | rain]
  --altitude A, -alt A  Sun altitude [-90.0, 90.0]
  --azimuth A, -azm A   Sun azimuth [0.0, 360.0]
  --clouds C, -c C      Clouds amount [0.0, 100.0]
  --rain R, -r R        Rain amount [0.0, 100.0]
  --puddles Pd, -pd Pd  Puddles amount [0.0, 100.0]
  --wind W, -w W        Wind intensity [0.0, 100.0]
  --fog F, -f F         Fog intensity [0.0, 100.0]
  --fogdist Fd, -fd Fd  Fog Distance [0.0, inf)
  --wetness Wet, -wet Wet
                        Wetness intensity [0.0, 100.0]
  • dynamic_weather.py (在 PythonAPI/examples 中) 启用由开发人员为每个 CARLA 地图准备的特定天气循环。该脚本可选参数有:
  -h, --help            show this help message and exit
  --host H              IP of the host server (default: 127.0.0.1)
  -p P, --port P        TCP port to listen to (default: 2000)
  -s FACTOR, --speed FACTOR
                        rate at which the weather changes (default: 1.0)

注意:天气的变化不会影响物理世界。它们只是摄像头传感器能够捕捉到的视觉效果。

sun_altitude_angle < 0 时,夜间模式开始,这被认为是日落。这就是灯光变得特别重要的时候。

2.3 Lights

当仿真进入夜间模式时,路灯会自动打开。 灯光由地图的开发人员放置,并可作为 carla.Light 对象访问。 颜色和强度等属性可以随意更改。 carla.LightState 类型的变量 light_state 允许在一次调用中设置所有这些。
路灯使用 carla.LightGroup 类型的属性 light_group 进行分类。 这允许将灯分类为路灯、建筑灯……可以检索 carla.LightManager 的一个实例以在一次调用中处理一组灯。

# Get the light manager and lights
lmanager = world.get_lightmanager()
mylights = lmanager.get_all_lights()

# Custom a specific light
light01 = mylights[0]
light01.turn_on()
light01.set_intensity(100.0)
state01 = carla.LightState(200.0,red,carla.LightGroup.Building,True)
light01.set_light_state(state01)

# Custom a group of lights
my_lights = lmanager.get_light_group(carla.LightGroup.Building)
lmanager.turn_on(my_lights)
lmanager.set_color(my_lights,carla.Color(255,0,0))
lmanager.set_intensities(my_lights,list_of_intensities)

Vehicle lights 必须由用户打开/关闭。 每辆车都有一组在 carla.VehicleLightState 中列出的灯。 到目前为止,并非所有车辆都集成了灯光。 以下是撰写本文时可用的列表。

  • Bikes:它们都有一个前后位置灯
  • Motorcycles: Yamaha and Harley Davidson models
  • Cars: 奥迪TT、雪佛兰、道奇(警车)、Etron、林肯、野马、特斯拉3S、大众T2

可以使用方法 carla.Vehicle.get_light_statecarla.Vehicle.set_light_state 随时获取和更新车辆的灯光, 这些使用二进制操作来自定义灯光设置。

# Turn on position lights
current_lights = carla.VehicleLightState.NONE
current_lights |= carla.VehicleLightState.Position
vehicle.set_light_state(current_lights)

也可以使用 environment.py 设置实时灯光。

2.4 Debugging

World 对象有一个 carla.DebugHelper 对象作为公共属性。 它允许在仿真过程中绘制不同的形状,用于跟踪正在发生的事件。 以下示例将在演员 Actor 的位置和旋转处绘制一个红色框。

debug = world.debug
debug.draw_box(carla.BoundingBox(actor_snapshot.get_transform().location,carla.Vector3D(0.5,0.5,2)),
               actor_snapshot.get_transform().rotation,
               0.05,
               carla.Color(255,0,0,0),0)

这个示例在 carla 的一个片段中得到了扩展。它显示了如何为世界快照中的每个参与者绘制框。

2.5 World snapshots

仿真中每个角色在单个帧中的状态即为一张静止图像。 信息来自相同的仿真步骤,即使在异步模式下也是如此。

# Retrieve a snapshot of the world at current frame.
world_snapshot = world.get_snapshot()

carla.WorldSnapshot 包含一个 carla.Timestamp 和一个 carla.ActorSnapshot 列表。 可以使用 ActorID 搜索 Actor 的快照。 快照列出了出现在其中的 ActorID

timestamp = world_snapshot.timestamp # Get the time reference 

for actor_snapshot in world_snapshot: # Get the actor and the snapshot information
    actual_actor = world.get_actor(actor_snapshot.id)
    actor_snapshot.get_transform()
    actor_snapshot.get_velocity()
    actor_snapshot.get_angular_velocity()
    actor_snapshot.get_acceleration()  

actor_snapshot = world_snapshot.find(actual_actor.id) # Get an actor's snapshot

2.6 World settings

World 可以访问一些用于模拟的高级配置。 这些决定了渲染条件、模拟时间步长以及客户端和服务器之间的同步。 它们可以从助手类 carla.WorldSettings 中访问。

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值