Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bindings/generated_docstrings/multibody_plant.h
Original file line number Diff line number Diff line change
Expand Up @@ -4528,6 +4528,9 @@ Parameter ``body_indexes``:
Array of selected bodies. This method does not distinguish between
welded bodies, joint-connected bodies, etc.

Raises:
RuntimeError if ``frame_F`` does not belong to ``this`` plant.

Raises:
RuntimeError if body_indexes contains an invalid BodyIndex or if
there is a repeated BodyIndex.
Expand Down
1 change: 1 addition & 0 deletions multibody/plant/multibody_plant.h
Original file line number Diff line number Diff line change
Expand Up @@ -4165,6 +4165,7 @@ class MultibodyPlant final : public internal::MultibodyTreeSystem<T> {
/// the expressed-in frame for the returned spatial inertia.
/// @param[in] body_indexes Array of selected bodies. This method does not
/// distinguish between welded bodies, joint-connected bodies, etc.
/// @throws std::exception if `frame_F` does not belong to `this` plant.
/// @throws std::exception if body_indexes contains an invalid BodyIndex or
/// if there is a repeated BodyIndex.
/// @note The mass and inertia of the world_body() does not contribute to the
Expand Down
9 changes: 9 additions & 0 deletions multibody/plant/test/multibody_plant_momentum_energy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/multibody/plant/multibody_plant.h"
#include "drake/multibody/tree/fixed_offset_frame.h"
#include "drake/multibody/tree/multibody_tree_indexes.h"
#include "drake/multibody/tree/revolute_joint.h"
#include "drake/multibody/tree/rigid_body.h"
Expand Down Expand Up @@ -335,6 +336,14 @@ TEST_F(TwoDofPlanarPendulumTest, CalcSpatialInertia) {
DRAKE_EXPECT_THROWS_MESSAGE(
plant_.CalcSpatialInertia(*context_, frame_A, body_indexes),
"CalcSpatialInertia\\(\\): contains a repeated BodyIndex.*");

// Verify an exception is thrown if frame_F was never added to the plant
// (regression test for #22636).
const FixedOffsetFrame<double> orphan_frame(
"orphan_frame", frame_A, math::RigidTransformd());
DRAKE_EXPECT_THROWS_MESSAGE(
plant_.CalcSpatialInertia(*context_, orphan_frame, {body_A.index()}),
".*does not belong to the supplied MultibodyTree.*");
}

} // namespace
Expand Down
3 changes: 3 additions & 0 deletions multibody/tree/multibody_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,9 @@ template <typename T>
SpatialInertia<T> MultibodyTree<T>::CalcSpatialInertia(
const systems::Context<T>& context, const Frame<T>& frame_F,
const std::vector<LinkIndex>& link_indexes) const {
// Ensure frame_F belongs to this tree; otherwise pose queries can segfault.
frame_F.HasThisParentTreeOrThrow(this);

// Check if there are repeated LinkIndex in link_indexes by converting the
// vector to a set (to eliminate duplicates) and see if their sizes differ.
const std::set<LinkIndex> without_duplicate_bodies(link_indexes.begin(),
Expand Down