我有一个自定义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>>());
...