import sys
try:
sys.path.append('D:\work_software\Carla\Carla_0.9.10\CARLA_0.9.10\WindowsNoEditor\PythonAPI\carla\dist\carla-0.9.10-py3.7-win-amd64.egg')
except IndexError:
pass
import carla
import random
import time
# 创建 client
client = carla.Client("localhost", 2000)
client.set_timeout(5.0)
# 创建 world
world = client.get_world()
# 创建 vehicle actor
blueprint_library = world.get_blueprint_library()
vehicle_blueprint = random.choice(blueprint_library.filter("vehicle.*.*"))
vehicle_transform = carla.Transform(carla.Location(x = 100, y = 200 , z = 10), carla.Rotation(yaw = -180))
vehicle_actor = world.spawn_actor(vehicle_blueprint, vehicle_transform)
#(3)Types of actorss
# ------------------------------------------------------------------------------------------------------------------------
# 4.汽车 -- Vehicles
# (1)定义车辆驾驶行为 -- 油门/刹车/转向
vehicle_actor.apply_control(carla.VehicleControl(throttle = 1.0, reverse = True))
# 函数【carla.Vehicle.apply_control(参数类型 -- carla.VehicleControl)】:在下一时刻将参数规定的操作应用于控制对象上,其中包含油门/转向/换档...驾驶参数
# 类【carla.VehicleControl】:使用典型的驾驶控制来管理车辆的基本运动,其中包含参数【油门 throttle】【转向 steer】【制动 Brake】【手刹 hand_brake】【倒车 reverse】【手动挡】【挡位】
# (2)定义车辆物理属性 -- 发动机扭矩/发动机转速/油门阻尼比/离合器强度/车的质量/质心........很多车辆的基本物理量
# 【类 carla.VehiclePhysicsControl】中的参数的类型还会用到【类 carla.GearPhysicsControl -- 用于齿轮控制的类】 + 【类 carla.WheelPhysicsControl -- 提供对不同轮子的特定控制的类】
vehicle_actor.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]]))
# 函数【carla.Vehicle.apply_physics_control(参数类型 -- carla.VehiclePhysicsControl)】:在下一个tick中将指定的物理参数应用于actor上,其中包含转动惯量/质量/阻力系数.....
# 【类 carla.VehiclePhysicsControl】:如上
# (3)应用物理原理并检测碰撞 -- 则需要车辆有一个carla.BoundingBox封装这些物理参数
box = vehicle_actor.bounding_box
# 属性【carla.Vehicle.bounding_box】:返回车辆几何形状的边界框,它的位置和旋转是相对于它所附着的车辆而言的
# 返回值类型为【类 carla.BoundingBox】
print("\n", box) # 输出:BoundingBox(Location(x=0.029219, y=0.000000, z=0.735531), Extent(x=2.395890, y=1.081725, z=0.744160), Rotation(pitch=0.000000, yaw=0.000000, roll=0.000000))
print(type(box)) # 输出:<class 'carla.libcarla.BoundingBox'>
print(box.location) # 属性【carla.BoundingBox.location】 输出:Location(x=0.029219, y=0.000000, z=0.735531)
print(box.extent) # 属性【carla.BoundingBox.extent】 输出:Vector3D(x=2.395890, y=1.081725, z=0.744160)
# (4)打开自动驾驶模式
vehicle_actor.set_autopilot(True)
# (5)车灯 -- 使用【类 carla.VehicleLightState】
current_lights = carla.VehicleLightState.NONE # 属性【carla.VehicleLightState.NONE】:关闭所有灯光
print("\n", current_lights) # 输出:NONE
print(type(current_lights)) # 输出:<class 'carla.libcarla.VehicleLightState'>
temp_light = carla.VehicleLightState.Position # 属性【carla.VehicleLightState.Position】:返回位置灯
print("\n", temp_light) # 输出:Position
print(type(temp_light)) # 输出:<class 'carla.libcarla.VehicleLightState'>
current_lights |= carla.VehicleLightState.Position
vehicle_actor.set_light_state(current_lights) # 函数【carla.Vehicle.set_light_state(参数类型 carla.VehicleLightState)】
# 这一行报错,我也不知道为什么【存疑】
# 总结:这段代码的作用是将车辆的位置灯打开,通过将位置灯的状态添加到current_lights变量中,将其应用到车辆上
注:my_test_script_7.py