Pydrake中set_actuation_vector()参数不兼容的TypeError问题求助
解决Drake中
JointActuator.set_actuation_vector类型错误问题 问题根源
你误用了JointActuator.set_actuation_vector方法——这个方法的作用不是直接设置关节的驱动值,而是从全局系统的驱动向量(u_instance)中提取当前关节驱动对应的分量,并写入到可选的输出数组(u)中。你的调用方式不符合该方法的设计用途,因此触发类型错误。
正确设置关节驱动的方法
要设置关节的驱动值,需要通过MultibodyPlant的输入端口来操作,具体步骤如下:
- 创建Plant的上下文(Context),所有状态和输入的修改都需要基于上下文进行;
- 获取目标关节驱动对应的输入端口;
- 通过端口的
FixValue方法设置驱动值。
修正后的代码示例
from pydrake.all import ( AddMultibodyPlantSceneGraph, Simulator, StartMeshcat, DiagramBuilder, ) from pydrake.multibody.tree import JointActuator import numpy as np import os # 假设project_dir已定义 builder = DiagramBuilder() plant, scene_graph = AddMultibodyPlantSceneGraph(builder, time_step=0.0) parser = plant.GetParser() parser.package_map().Add("URDF_LargeWings", os.path.join(project_dir)) parser.AddModelFromFile(os.path.join(project_dir, "urdf", "URDF_LargeWings.sdf")) plant.Finalize() # 获取关节驱动 rw_passive = plant.GetJointActuatorByName("joint_RW_J_Pitch") lw_passive = plant.GetJointActuatorByName("joint_LW_J_Pitch") assert isinstance(rw_passive, JointActuator) assert rw_passive.joint().num_velocities() == 1 # 创建默认上下文 context = plant.CreateDefaultContext() # 方法1:针对单个关节驱动的输入端口设置值 plant.get_joint_actuator_input_port(rw_passive.index()).FixValue(context, np.array([0.0])) # 方法2:针对全局关节驱动输入端口设置值(适用于多关节场景) # u_global = np.zeros(plant.num_actuated_dofs()) # u_global[rw_passive.index()] = 0.0 # plant.get_mutable_joint_actuation_input_port().FixValue(context, u_global) # 后续可将context传入Simulator等组件使用 simulator = Simulator(plant, context)
额外说明
如果确实需要从全局驱动向量提取当前关节的分量,set_actuation_vector的正确用法示例:
# 假设u_global是全局驱动向量 u_global = np.array([0.0, 1.0]) # 示例:包含两个关节驱动的全局向量 u_local = np.zeros(rw_passive.num_actuation_dofs()) rw_passive.set_actuation_vector(u_global, u_local) # 此时u_local会被填充为rw_passive对应的驱动分量
内容的提问来源于stack exchange,提问作者twSoulz
相关产品推荐
相关产品推荐

