I'm a bit unfamiliar with Drake and I've been trying to write a function to return the body indices involved in collisions in my MultibodyPlant. I'm having some trouble getting the correct collision results. I'm currently trying to get the contact results from the plant using a LeafSystem:
class ContactResultsReader : public drake::systems::LeafSystem<double> {
public:
ContactResultsReader() : drake::systems::LeafSystem<double>() {
contact_results_input_port = &DeclareAbstractInputPort("contact_results_input_port", *drake::AbstractValue::Make<drake::multibody::ContactResults<double>>());
};
drake::systems::InputPort<double> & get_contact_results_input_port() {
return *contact_results_input_port;
}
private:
drake::systems::InputPort<double> *contact_results_input_port = nullptr;
};
connecting the contact results output port into this
builder->Connect(plant->get_contact_results_output_port(), contact_results_reader->get_contact_results_input_port());
and reading the contact results from the input port in the LeafSystem:
const drake::systems::InputPortIndex contact_results_input_port_index = contact_results_reader->get_contact_results_input_port().get_index();
const drake::AbstractValue* abstract_value =
contact_results_reader->EvalAbstractInput(*contact_results_reader_context, contact_results_input_port_index);
const drake::multibody::ContactResults<double> &contact_results = abstract_value->get_value<drake::multibody::ContactResults<double>>();
For some reason, I'm not reading a collision between two bodies that I load into the plant directly on top of each other. Is there something wrong with this method?
I've also tried getting the QueryObject from the plant and using query_object.ComputePointPairPenetration();
, but that returns the bodies themselves and I would like to get the body indices. Thank you!