blueprint.set_attribute('vehicle_physics_control', physics_control) Boost.Python.ArgumentError: Python argument types in ActorBlueprint.set_attribute(ActorBlueprint, str, VehiclePhysicsControl) did not match C++ signature: set_attribute(class carla::client::ActorBlueprint {lvalue}, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
时间: 2024-01-20 11:03:10 浏览: 207
这个错误出现的原因是`set_attribute`方法的参数类型与C++签名不匹配。你需要将`physics_control`对象转换为Python字符串类型的JSON格式,然后再传递给`set_attribute`方法。可以使用`json.dumps`函数将对象转换为JSON字符串,如下所示:
```python
import json
npc_speed = npc_speeds[i]
npc_acceleration = npc_accelerations[i]
physics_control = carla.VehiclePhysicsControl()
physics_control.max_speed = npc_speed / 3.6 # convert km/h to m/s
physics_control.max_acceleration = npc_acceleration
physics_control_json = json.dumps(physics_control.as_dict())
blueprint.set_attribute('vehicle_physics_control', physics_control_json)
```
这里我们使用`as_dict`方法将`VehiclePhysicsControl`对象转换为字典,然后使用`json.dumps`将其转换为JSON格式的字符串。最后,我们将其传递给`set_attribute`方法,以应用到蓝图上。
相关问题
解释这段代码 for n, transform in enumerate(spawn_points): if n >= args.number_of_vehicles: break blueprint = random.choice(blueprints) if blueprint.has_attribute('color'): color = random.choice(blueprint.get_attribute('color').recommended_values) blueprint.set_attribute('color', color) if blueprint.has_attribute('driver_id'): driver_id = random.choice(blueprint.get_attribute('driver_id').recommended_values) blueprint.set_attribute('driver_id', driver_id) blueprint.set_attribute('role_name', 'autopilot')
这段代码是在使用CARLA自动驾驶仿真环境时,用于生成多个车辆的语句。具体来说,代码中的`spawn_points`是一个包含多个生成点的列表,每个生成点都是一个位置和方向的元组。代码通过遍历`spawn_points`列表,为每个生成点生成一个车辆。
具体来说,代码通过`enumerate()`函数获取生成点的索引`n`和对应的位置和方向`transform`。如果生成的车辆数量已经达到了指定的数量`args.number_of_vehicles`,则跳出循环。否则,代码随机选择一个蓝图`blueprint`,并根据蓝图属性设置车辆的颜色和驾驶员ID。如果蓝图支持设置车辆颜色,则从推荐的值中随机选择一个颜色,并将其设置为车辆的颜色。如果蓝图支持设置驾驶员ID,则从推荐的值中随机选择一个ID,并将其设置为车辆的驾驶员ID。
最后,代码将车辆的角色名称设置为“autopilot”,表示车辆将使用自动驾驶模式。这样生成的车辆就可以在仿真环境中进行自动驾驶行驶了。
请帮我将这段代码改为设置8个固定的NPC代码,并加入设置每个NPC的速度和加速度以及行驶路线的代码# Spawn vehicles # -------------- batch = [] for n, transform in enumerate(spawn_points): print('n',n) print('transform',transform) if n >= args.number_of_vehicles: break blueprint = random.choice(blueprints) print('blueprints的长度', len(blueprints)) print('blueprints',blueprints) print('blueprint', blueprint) if blueprint.has_attribute('color'): color = random.choice(blueprint.get_attribute('color').recommended_values) blueprint.set_attribute('color', color) if blueprint.has_attribute('driver_id'): driver_id = random.choice(blueprint.get_attribute('driver_id').recommended_values) blueprint.set_attribute('driver_id', driver_id) blueprint.set_attribute('role_name', 'autopilot') # prepare the light state of the cars to spawn light_state = vls.NONE if args.car_lights_on: light_state = vls.Position | vls.LowBeam | vls.LowBeam # spawn the cars and set their autopilot and light state all together batch.append(SpawnActor(blueprint, transform) .then(SetAutopilot(FutureActor, True, traffic_manager.get_port())) .then(SetVehicleLightState(FutureActor, light_state))) for response in client.apply_batch_sync(batch, synchronous_master): if response.error: logging.error(response.error) else: vehicles_list.append(response.actor_id)
以下是修改后的代码,设置8个固定的NPC,每个NPC的速度、加速度和行驶路线:
```python
# Spawn vehicles
# --------------
batch = []
npc_blueprints = ["vehicle.nissan.micra", "vehicle.audi.a2", "vehicle.tesla.model3", "vehicle.bmw.grandtourer",
"vehicle.toyota.prius", "vehicle.nissan.patrol", "vehicle.audi.etron", "vehicle.mercedes-benz.coupe"]
npc_speeds = [20, 25, 30, 35, 40, 45, 50, 55] # in km/h
npc_accelerations = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5] # in m/s^2
npc_waypoints = [[-100, -100, 0], [0, -100, 0], [100, -100, 0], [100, 0, 0], [100, 100, 0], [0, 100, 0], [-100, 100, 0], [-100, 0, 0]]
for i in range(8):
blueprint = world.get_blueprint_library().find(npc_blueprints[i])
color = random.choice(blueprint.get_attribute('color').recommended_values)
blueprint.set_attribute('color', color)
driver_id = random.choice(blueprint.get_attribute('driver_id').recommended_values)
blueprint.set_attribute('driver_id', driver_id)
blueprint.set_attribute('role_name', 'autopilot')
transform = carla.Transform(carla.Location(x=npc_waypoints[i][0], y=npc_waypoints[i][1], z=npc_waypoints[i][2]),
carla.Rotation(yaw=0))
# set the speed and acceleration of the NPC
npc_speed = npc_speeds[i]
npc_acceleration = npc_accelerations[i]
physics_control = blueprint.get_physics_control()
physics_control.max_speed = npc_speed / 3.6 # convert km/h to m/s
physics_control.max_acceleration = npc_acceleration
blueprint.set_physics_control(physics_control)
# prepare the light state of the cars to spawn
light_state = vls.NONE
if args.car_lights_on:
light_state = vls.Position | vls.LowBeam | vls.LowBeam
# spawn the cars and set their autopilot and light state all together
batch.append(SpawnActor(blueprint, transform)
.then(SetAutopilot(FutureActor, True, traffic_manager.get_port()))
.then(SetVehicleLightState(FutureActor, light_state)))
for response in client.apply_batch_sync(batch, synchronous_master):
if response.error:
logging.error(response.error)
else:
vehicles_list.append(response.actor_id)
```
其中,`npc_blueprints` 为8种固定的NPC车辆蓝图,`npc_speeds` 和 `npc_accelerations` 分别为8个NPC的速度和加速度,`npc_waypoints` 为8个NPC的行驶路线,每个路线由一组三维坐标表示。在循环中,通过 `world.get_blueprint_library().find()` 获取对应的蓝图,然后设置车辆的颜色、驾驶员ID、角色名等属性,并设置车辆的初始位置和朝向。接着,通过 `blueprint.get_physics_control()` 获取车辆的物理属性控制器,并设置车辆的最大速度和最大加速度,最后通过 `blueprint.set_physics_control()` 设置车辆的物理属性。最后,再将所有车辆一起生成并设置自动驾驶和车灯状态。
阅读全文