这个问题与add-forces-to-body-post-finalize密切相关
我希望能够对 pydrake 中的简单几何图元施加外力。这是为了对物体之间的相互作用进行评估。
我目前的实现:
builder = DiagramBuilder()
plant = builder.AddSystem(MultibodyPlant(0.001))
parser = Parser(plant)
cube_instance = parser.AddModelFromFile('cube.urdf', model_name='cube')
plant.Finalize()
force = builder.AddSystem(ConstantVectorSource(np.zeros(6)))
builder.Connect(force.get_output_port(0), plant.get_applied_spatial_force_input_port())
diagram = builder.Build()
但是,当我运行它时,我收到以下错误:
builder.Connect(force.get_output_port(0), plant.get_applied_spatial_force_input_port())
RuntimeError: DiagramBuilder::Connect: Cannot mix vector-valued and abstract-valued ports while connecting output port y0 of System drake/systems/ConstantVectorSource@0000000002db5aa0 to input port applied_spatial_force of System drake/multibody/MultibodyPlant@0000000003118680
我有一个倾向,我必须实现一个在工厂上实现抽象值端口的 LeafSystem。
根据建议更新
使用:AbstractValue 和 ConstantValueSource
value = AbstractValue.Make([np.zeros(6)])
force = builder.AddSystem(ConstantValueSource(value))
ref_vector_externally_applied_spatial_force = plant.get_applied_spatial_force_input_port()
builder.Connect(force.get_output_port(0), ref_vector_externally_applied_spatial_force)
我收到以下错误:
RuntimeError: DiagramBuilder::Connect: Mismatched value types while connecting
output port y0 of System drake/systems/ConstantValueSource@0000000002533a30 (type pybind11::object) to
input port applied_spatial_force of System drake/multibody/MultibodyPlant@0000000002667760 (type std::vector<drake::multibody::ExternallyAppliedSpatialForce<double>,std::allocator<drake::multibody::ExternallyAppliedSpatialForce<double>>>)
输入输出端口的类型应该匹配是有道理的。预期的类型似乎是 ExternallyAppliedSpatialForce 的向量。
然后我将 Abstract 类型更改如下:
value = AbstractValue.Make(ExternallyAppliedSpatialForce())
RuntimeError: DiagramBuilder::Connect: Mismatched value types while connecting
output port y0 of System drake/systems/ConstantValueSource@0000000002623980 (type drake::multibody::ExternallyAppliedSpatialForce<double>)
to input port applied_spatial_force of System drake/multibody/MultibodyPlant@00000000027576b0 (type std::vector<drake::multibody::ExternallyAppliedSpatialForce<double>,std::allocator<drake::multibody::ExternallyAppliedSpatialForce<double>>>)
我越来越近了。但是,我无法发送 ExternallyAppliedSpatialForce 的向量。如果我尝试将其作为列表发送,我会收到抱怨说它无法腌制对象。我没有在 AbstractValue 示例中看到如何创建这样的对象向量。
任何额外的帮助将不胜感激。
解决方案是使用VectorExternallyAppliedSpatialForced类型
完整的解决方案稍后发布。