0

我有一个自定义LeafSystem的输入端口和输出端口如下

template <typename T>
MyLeafSystem<T>::MyLeafSystem() :
    systems::LeafSystem<T>(systems::SystemTypeTag<MyLeafSystem>{}),
    input1_idx(this->DeclareVectorInputPort("input1", systems::BasicVector<T>(1)).get_index()),
    input2_idx(this->DeclareVectorInputPort("input2", systems::BasicVector<T>(1)).get_index()),
    output_idx(this->DeclareVectorOutputPort("output", systems::BasicVector<T>(2), &TorqueCombiner::convert).get_index())
{}

我通过将它添加到我的系统中

auto my_leaf_system = builder.AddSystem(std::make_unique<MyLeafSystem<double>>());
// Connect one of the input ports of my_leaf_system
// Connect one of the output port of my_leaf_system
auto diagram = builder.Build();

对于剩余的端口,我希望现在连接修复输入端口。我这样做是通过

auto diagram = builder.Build();

std::unique_ptr<systems::Context<double>> diagram_context = diagram->CreateDefaultContext();
Context<double>& context = diagram->GetMutableSubsystemContext(plant, diagram_context.get());

context.FixInputPort(my_leaf_system->get_second_input_port().get_index(), Vector1d::Zero());

但是,std::logic_error当我跑步时它会抛出一个FixInputPort

terminate called after throwing an instance of 'std::logic_error'
  what():  System::FixInputPortTypeCheck(): expected value of type drake::geometry::QueryObject<double> for input port 'geometry_query' (index 0) but the actual type was drake::systems::BasicVector<double>. (System ::_::drake/multibody/MultibodyPlant@000055e087146ec0)

奇怪的是

int(my_leaf_system->get_second_input_port().get_index())) == 0

int(plant.get_geometry_query_input_port().get_index())) == 0

我的工厂添加如下

auto pair = drake::multibody::AddMultibodyPlantSceneGraph(&builder, std::make_unique<drake::multibody::MultibodyPlant<double>>(0));

drake::multibody::MultibodyPlant<double>& plant = pair.plant;
drake::geometry::SceneGraph<double>& scene_graph = pair.scene_graph;

// Make and add the model.
drake::multibody::Parser(&plant, &scene_graph).AddModelFromFile(model_filename);

// Connect scene graph to visualizer
drake::geometry::ConnectDrakeVisualizer(&builder, scene_graph);

plant.Finalize();



auto my_leaf_system = builder.AddSystem(std::make_unique<MyLeafSystem<double>>());
...
4

2 回答 2

2

你的帖子有几个不一致的地方。对于初学者来说,错误消息意味着在尝试修复 multibodyplant 的输入端口(而不是您的叶子系统)时会生成错误。并且该输入端口的类型不是向量,因此错误很明显。

怀疑你在某处越过了你的电线(或指针)。修复派生叶系统的向量输入端口应该没问题——这是非常常见的。

于 2019-05-12T09:54:49.513 回答
0

我犯的错误在

Context<double>& context = diagram->GetMutableSubsystemContext(plant, diagram_context.get());

context.FixInputPort(my_leaf_system->get_second_input_port().get_index(), Vector1d::Zero());

在这里,我使用contextfor the plantsubsystem to FixInputPorton my_leaf_system

正确的代码应该是

Context<double>& my_leaf_system_context = diagram->GetMutableSubsystemContext(*my_leaf_system, diagram_context.get());

my_leaf_system_context.FixInputPort(my_leaf_system->get_mip_input_port().get_index(), Vector1d::Zero());

谢谢拉斯,谢尔姆!

于 2019-05-13T03:01:49.590 回答