introdution:
- 开源软件
- unreal engine 4
- 用户通过Python API 调用, server --client 的形式- REF: https://carla.readthedocs.io/en/latest/python_api/
let's start:
The API has been significantly changed in the latest versions starting at 0.9.0. We commonly refer to the new API as 0.9.X API as opposed to the previous 0.8.X API
Section 1 API 学习
官网下载软件包,解压, shit+右键,解压路径下打开shell窗口 .\CarlaUE4.exe 运行
通过鼠标+ AWSD 控制运行,
打开路径:D:\study\codes\CARLA_0.9.5\PythonAPI\examples
打开另一个shell, 运行官网
-
创建(spawn) 场景元素,demo命令
python spawn_npc.py -n 80
With this script we are adding 80 vehicles to the world driving in "autopilot" mode. Back to the simulator window we should see these vehicles driving around the city. They will keep driving randomly until we stop the script. Let's leave them there for now
这个命令实现了添加80个车辆,随机的自主运动。 CTR+C 结束演示。 脚本有难度,Lesson2 后,查看API
-
修改天气
- 天气自动变化
-
python dynamic_weather.py
-
手动控制车辆
-
python manual_control.py
运行窗口如下:第三视角
第一课时结束。下课!:)
Lesson2 :
几个概念:
创建Client 对象:
client = carla.Client('localhost', 2000)
client.set_timeout(10.0) # seconds
模拟世界:
world = client.get_world()
actor (模拟器中的演员对象-比如汽车)
blueprints--actor对应的属性(比如颜色,车型 etc),在构造蓝图blueprints中设置。--所有的属性都包含在 一个库中。
blueprint_library = world.get_blueprint_library()
The library allows us to find specific blueprints by ID, filter them with wildcards, or just choosing one at random
# Find specific blueprint.
collision_sensor_bp = blueprint_library.find('sensor.other.collision')
# Chose a vehicle blueprint at random.
vehicle_bp = random.choice(blueprint_library.filter('vehicle.bmw.*'))
Some of the attributes of the blueprints can be modified while some other are just read-only. For instance, we cannot modify the number of wheels of a vehicle but we can change its color
vehicles = blueprint_library.filter('vehicle.*')
bikes = [x for x in vehicles if int(x.get_attribute('number_of_wheels')) == 2]
for bike in bikes:
bike.set_attribute('color', '255,0,0')
Modifiable attributes also come with a list of recommended values
for attr in blueprint:
if attr.is_modifiable:
blueprint.set_attribute(attr.id, random.choice(attr.recommended_values))
了解了属性设置后,开始创建actor,
Once we have the blueprint set up, spawning an actor is pretty straightforward
transform = Transform(Location(x=230, y=195, z=40), Rotation(yaw=180))# 生成点坐标创建
actor = world.spawn_actor(blueprint, transform) #这个时候会检查生成点坐标是否会发生碰撞。如果有碰撞,会报错
shen
The spawn actor function comes in two flavours, spawn_actor
and try_spawn_actor
. The former will raise an exception if the actor could not be spawned, the later will return None
instead. The most typical cause of failure is collision at spawn point, meaning the actor does not fit at the spot we chose; probably another vehicle is in that spot or we tried to spawn into a static object.
To ease the task of finding a spawn location, each map provides a list of recommended transforms
推荐
spawn_points = world.get_map().get_spawn_points() #返回所有的可用collision-free points.
We'll add more on the map object later in this tutorial.
Finally, the spawn functions have an optional argument that controls whether the actor is going to be attached to another actor. This is specially useful for sensors. In the next example, the camera remains rigidly attached to our vehicle during the rest of the simulation
camera = world.spawn_actor(camera_bp, relative_transform, attach_to=my_vehicle)
# (附加参数,可以控制物体是否和其他的actor绑定)--适用于传感器
Note that in this case, the transform provided is treated relative to the parent actor.
讲了有点多,开始动手练习。直接在 calar解压路径下,创建练习code.(其他地方需要添加环境变量。要不然 下面会报缺少相关文件。
import carla
首先打开模拟器(server), 否则会报错
创建一个client. 创建actor, blueprints. 调用生成函数,在世界中生成。
代码在spyder中编译会报错。报“calra 没有 client这个属性!”---TB fix
偶尔发现可以正常运行。
暂时通过shell 命令行来执行。运行OK
参考官方example: spawn_npc.py ,尝试练习
import glob
import os
imp