Skip to content

Commit e45c8a0

Browse files
committed
Cache body spatial inertia
1 parent 3b90621 commit e45c8a0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

multibody/tree/frame_body_pose_cache.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class FrameBodyPoseCache {
4040
: X_BF_pool_(num_frame_body_pose_slots_needed),
4141
X_FB_pool_(num_frame_body_pose_slots_needed),
4242
is_X_BF_identity_(num_frame_body_pose_slots_needed),
43+
M_BBo_B_pool_(num_mobods, SpatialInertia<T>::NaN()),
4344
X_MbF_pool_(num_frames),
4445
X_FMb_pool_(num_frames),
4546
is_X_MbF_identity_(num_frames),
@@ -74,6 +75,12 @@ class FrameBodyPoseCache {
7475
return static_cast<bool>(is_X_BF_identity_[body_pose_index]);
7576
}
7677

78+
const SpatialInertia<T>& get_M_BBo_B(MobodIndex index) const {
79+
// This method must be very fast in Release.
80+
DRAKE_ASSERT(0 <= index && index < ssize(M_BBo_B_pool_));
81+
return M_BBo_B_pool_[index];
82+
}
83+
7784
void SetX_BF(int body_pose_index, const math::RigidTransform<T>& X_BF) {
7885
// This method is only called when parameters change.
7986
DRAKE_DEMAND(0 <= body_pose_index && body_pose_index < ssize(X_BF_pool_));
@@ -88,6 +95,14 @@ class FrameBodyPoseCache {
8895
}
8996
}
9097

98+
void SetM_BBo_B(MobodIndex index, const SpatialInertia<T>& M_BBo_B) {
99+
// This method is only called when parameters change.
100+
DRAKE_DEMAND(0 <= index && index < ssize(M_BBo_B_pool_));
101+
M_BBo_B_pool_[index] = M_BBo_B;
102+
}
103+
104+
// M-frame quantities below.
105+
91106
const math::RigidTransform<T>& get_X_MbF(FrameIndex index) const {
92107
// This method must be very fast in Release.
93108
DRAKE_ASSERT(0 <= index && index < ssize(X_MbF_pool_));
@@ -140,6 +155,10 @@ class FrameBodyPoseCache {
140155
std::vector<math::RigidTransform<T>> X_FB_pool_;
141156
std::vector<uint8_t> is_X_BF_identity_; // fast vector<bool> equivalent
142157

158+
// Spatial inertia of mobilized body B, about its body origin Bo, expressed
159+
// in B. These are indexed by MobodIndex.
160+
std::vector<SpatialInertia<T>> M_BBo_B_pool_;
161+
143162
// These are indexed by Frame::index().
144163
std::vector<math::RigidTransform<T>> X_MbF_pool_;
145164
std::vector<math::RigidTransform<T>> X_FMb_pool_;

multibody/tree/multibody_tree.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,8 @@ void MultibodyTree<T>::CalcSpatialInertiasInWorld(
14241424
DRAKE_THROW_UNLESS(M_B_W_all != nullptr);
14251425
DRAKE_THROW_UNLESS(ssize(*M_B_W_all) == topology_.num_mobods());
14261426

1427+
const FrameBodyPoseCache<T>& frame_body_pose_cache =
1428+
EvalFrameBodyPoses(context);
14271429
const PositionKinematicsCache<T>& pc = this->EvalPositionKinematics(context);
14281430

14291431
// Skip the world.
@@ -1437,8 +1439,8 @@ void MultibodyTree<T>::CalcSpatialInertiasInWorld(
14371439
const RotationMatrix<T>& R_WB = X_WB.rotation();
14381440

14391441
// Spatial inertia of body B about Bo and expressed in the body frame B.
1440-
// This call has zero cost for rigid bodies.
1441-
const SpatialInertia<T> M_B = body.CalcSpatialInertiaInBodyFrame(context);
1442+
const SpatialInertia<T>& M_B =
1443+
frame_body_pose_cache.get_M_BBo_B(body.mobod_index());
14421444
// Re-express body B's spatial inertia in the world frame W.
14431445
SpatialInertia<T>& M_B_W = (*M_B_W_all)[body.mobod_index()];
14441446
M_B_W = M_B.ReExpress(R_WB);
@@ -1547,6 +1549,7 @@ void MultibodyTree<T>::CalcFrameBodyPoses(
15471549
const RigidBody<T>& body = mobilizer.outboard_body();
15481550
const SpatialInertia<T> M_BBo_B =
15491551
body.CalcSpatialInertiaInBodyFrame(context);
1552+
frame_body_poses->SetM_BBo_B(mobod.index(), M_BBo_B);
15501553

15511554
if (!M_BBo_B.IsNaN()) {
15521555
// Shift and re-express.

0 commit comments

Comments
 (0)