在pyDrake中将两株植物添加到场景图时发生Python绑定错误



我希望在同一生成器和场景图中有两个工厂。(我不希望它们在同一个工厂,因为我想分离它们的动力学,但我希望它们相互影响,从而使它们保持在同一构建器和场景图上。(

我的实现如下:

from pydrake.multibody.plant import AddMultibodyPlantSceneGraph
from pydrake.systems.framework import DiagramBuilder
builder = DiagramBuilder()
plant1, scene_graph  = AddMultibodyPlantSceneGraph(builder, 0.0)
plant2  = AddMultibodyPlantSceneGraph(builder, 0.0, scene_graph)

当我运行这个时,我得到错误:

Traceback (most recent call last):
File "/filepath/2plants1scene.py", line 6, in <module>
plant2  = AddMultibodyPlantSceneGraph(builder, 0.0, scene_graph)
RuntimeError: C++ object must be owned by pybind11 when attempting to release to C++

这是绑定问题吗?AddMultibodyPlantSceneGraph的文档使其看起来好像可以将植物添加到现有的场景中。

错误消息与2018年的此问题类似:https://github.com/RobotLocomotion/drake/issues/8160

提前感谢您的任何想法。

这是绑定问题吗?

关于您的特定错误消息,您正在尝试获取一个对象(其所有权由unique_ptr<>控制(,并尝试将其拥有的数据传递两次(或更多(。

来自C++API:
https://drake.mit.edu/doxygen_cxx/classdrake_1_1multibody_1_1_multibody_plant.html#aac66563a5f3eb9e2041bd4fa8d438827请注意,scene_graph参数是unique_ptr<>

因此,这是一个绑定错误,因为错误消息有点糟糕;然而,它更多的是一个语义问题,使用C/C++API。

AddMultibodyPlantSceneGraph的文档使其看起来好像可以将植物添加到现有的场景中。

为了参考,这里是该方法的核心实现:
https://github.com/RobotLocomotion/drake/blob/v0.32.0/multibody/plant/multibody_plant.cc#L3346-L3370

对于您的用例,您应该只将SceneGraph添加到DiagramBuilder一次。由于您想将一个SceneGraph连接到多个MultibodyPlant实例,我建议您不要使用AddMultibodyPlantSceneGraph,因为这是1:1配对的糖。

相反,您应该手动注册并连接SceneGraph;我想它看起来像这样:

def register_plant_with_scene_graph(scene_graph, plant):
plant.RegsterAsSourceForSceneGraph(scene_graph)
builder.Connect(
plant.get_geometry_poses_output_port(),
scene_graph.get_source_pose_port(plant.get_source_id()),
)
builder.Connect(
scene_graph.get_query_output_port(),
plant.get_geometry_query_input_port(),
)
builder = DiagramBuilder()
scene_graph = builder.AddSystem(SceneGraph())
plant_1 = builder.AddSystem(MultibodyPlant(time_step=0.0))
register_plant_with_scene_graph(scene_graph, plant_1)
plant_2 = builder.AddSystem(MultibodyPlant(time_step=0.0))
register_plant_with_scene_graph(scene_graph, plant_2)

正如Sean在上面警告的那样,你需要小心这对搭档。

相关内容

最新更新