client = carla.Client('localhost', 2000)
创建客户端后,设置时间,超过这个时间,视为连接失败,返回错误
client.set_timeout(10.0) # seconds
翻译为客户端和服务器有不同的 libcarla 模块。如果版本不同,可能会出现问题。这可以使用 get_client_version() 和 get_server_version() 方法进行检查。
指定世界
world = client.load_world('Town04')#这是指定世界,Town04的世界的原因是它显示了类似于高速公路的工况,很适合用于建立危险场景。
连接当前世界
world = client.get_world()#获得当前世界
可以使用下述代码获取可用地图列表
print(client.get_available_maps())
此处有文档,天气官方API
weather = carla.WeatherParameters(
cloudiness=80.0,
precipitation=30.0,
sun_altitude_angle=70.0)
world.set_weather(weather)
print(world.get_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 地图准备的==特定天气周期。dynamic_weather.py的可选参数
-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)
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
blueprint_library = world.get_blueprint_library()
蓝图具有一个 ID 来标识它们以及随之生成的参与者。可以读取库以查找特定ID,随机选择蓝图或使用通配符模式过滤结果。(下面代码中的*.*)
# Find a specific blueprint.
collision_sensor_bp = blueprint_library.find('sensor.other.collision')
# Choose a vehicle blueprint at random.
vehicle_bp = random.choice(blueprint_library.filter('vehicle.*.*'))
可设置actor属性
is_bike = [vehicle.get_attribute('number_of_wheels') == 2]
if(is_bike)
vehicle.set_attribute('color', '255,0,0')
actor的建议生成点
spawn_points = world.get_map().get_spawn_points()
指定点
transform = Transform(Location(x=230, y=195, z=40), Rotation(yaw=180))
actor = world.spawn_actor(blueprint, transform)
actor的物理特性可以被禁用以将其冻结在原地
actor.set_simulate_physics(False)
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))
vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=-1.0))
vehicle.apply_physics_control(carla.VehiclePhysicsControl(max_rpm = 5000.0, center_of_mass = carla.Vector3D(0.0, 0.0, 0.0), torque_curve=[[0,400],[5000,400]]))
车辆自动驾驶
vehicle.set_autopilot(True)
分层地图的布局与非分层地图相同,但可以关闭地图图层并在地图上切换。有一个无法关闭的最小布局,由道路,人行道,交通信号灯和交通标志组成。
# Load layered map for Town 01 with minimum layout plus buildings and parked vehicles
world = client.load_world('Town01_Opt', carla.MapLayer.Buildings | carla.MapLayer.ParkedVehicles)
# Toggle all buildings off
world.unload_map_layer(carla.MapLayer.Buildings)
# Toggle all buildings on
world.load_map_layer(carla.MapLayer.Buildings)