我有一个自定义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>>());
...
答案 0 :(得分:1)
您的帖子有几处不一致的地方。对于初学者,该错误消息表示尝试修复多体植物(不是您的叶子系统)的输入端口时会生成错误。而且该输入端口的类型不是向量,因此错误很明显。
怀疑您在某处越过了导线(或指针)。修复派生的叶子系统的向量输入端口应该没问题-这非常普遍。
答案 1 :(得分:0)
我犯的错误是
Context<double>& context = diagram->GetMutableSubsystemContext(plant, diagram_context.get());
context.FixInputPort(my_leaf_system->get_second_input_port().get_index(), Vector1d::Zero());
在这里,我将context
子系统的plant
用于FixInputPort
上的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());
Thx Russ,谢尔姆!