Skip to content

Commit d5e55c3

Browse files
committed
Don't recompute parameters
1 parent 77262c1 commit d5e55c3

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

multibody/tree/multibody_tree.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,9 +1404,25 @@ void MultibodyTree<T>::CalcPositionKinematicsCache(
14041404
const systems::Context<T>& context, PositionKinematicsCache<T>* pc) const {
14051405
DRAKE_DEMAND(pc != nullptr);
14061406

1407+
// Ensure parameter-dependent quantities are up to date.
14071408
const FrameBodyPoseCache<T>& frame_body_pose_cache =
14081409
EvalFrameBodyPoses(context);
14091410

1411+
// Check whether parameter-dependent quantities in the position cache are
1412+
// up to date with respect to the FrameBodyPoseCache in the context. If not,
1413+
// then we need to update the position cache before we can use it.
1414+
1415+
const systems::CacheEntryValue& fbpc_value =
1416+
tree_system_->frame_body_poses_cache_entry().get_cache_entry_value(
1417+
context);
1418+
const int64_t fbpc_serial_number = fbpc_value.serial_number();
1419+
1420+
if (pc->world_composite_serial_number() != fbpc_serial_number) {
1421+
// The position cache is not up to date with respect to the frame body pose
1422+
// cache, so we need to update it before we can use it.
1423+
pc->PrecomputeWorldComposite(forest(), frame_body_pose_cache, fbpc_serial_number);
1424+
}
1425+
14101426
const Eigen::VectorBlock<const VectorX<T>> q_block = get_positions(context);
14111427
const T* q = q_block.data();
14121428

multibody/tree/position_kinematics_cache.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ PositionKinematicsCache<T>::PositionKinematicsCache(
2727
}
2828

2929
template <typename T>
30-
void PositionKinematicsCache<T>::PrecomputeWorldComposite(
30+
void PositionKinematicsCache<T>::ComputeWorldComposite(
3131
const SpanningForest& forest,
3232
const FrameBodyPoseCache<T>& frame_body_pose_cache) {
3333
const SpanningForest::Mobod& world_mobod = forest.mobods(MobodIndex(0));

multibody/tree/position_kinematics_cache.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,28 @@ class PositionKinematicsCache {
174174
// in X_WL for those links once and for all. X_WL₀ (≜ X_WL[link₀]) and
175175
// X_WB₀ (≜ X_WB[mobod₀]) are identity transforms (set during allocation).
176176
// Consequently, X_WLᵢ = X_WB₀ * X_B₀Lᵢ = X_B₀Lᵢ for each of the links Lᵢ that
177-
// are fixed to World.
177+
// are fixed to World. Do nothing if the serial number hasn't changed.
178178
void PrecomputeWorldComposite(
179179
const SpanningForest& forest,
180-
const FrameBodyPoseCache<T>& frame_body_pose_cache);
180+
const FrameBodyPoseCache<T>& frame_body_pose_cache,
181+
int64_t frame_body_pose_cache_serial_number) {
182+
if (world_composite_serial_number_ == frame_body_pose_cache_serial_number) {
183+
return;
184+
}
185+
ComputeWorldComposite(forest, frame_body_pose_cache);
186+
world_composite_serial_number_ = frame_body_pose_cache_serial_number;
187+
}
181188

182189
private:
183190
// Allocates resources for this position kinematics cache.
184191
void Allocate();
185192

193+
// Called when we know we have to recompute the parameter-dependent
194+
// world composite kinematics.
195+
void ComputeWorldComposite(
196+
const SpanningForest& forest,
197+
const FrameBodyPoseCache<T>& frame_body_pose_cache);
198+
186199
// Helper method to initialize poses to garbage values including NaNs.
187200
// This allow us to quickly verify some of the values stored in the pools are
188201
// never used (however we store them anyway to simplify the indexing).
@@ -201,6 +214,12 @@ class PositionKinematicsCache {
201214
// (including the World link) and possibly some ephemeral links.
202215
int num_links_{0};
203216

217+
// The serial number of the last FrameBodyPoseCache that was used to update
218+
// the state-independent quantities in this cache (such as the X_WL for links
219+
// fixed to World). This is used to determine whether we need to recompute
220+
// those quantities when PrecomputeWorldComposite() is called.
221+
int64_t world_composite_serial_number_{-1};
222+
204223
// These are indexed by MobodIndex so are in depth-first order.
205224
std::vector<RigidTransform<T>> X_WB_pool_;
206225
std::vector<RigidTransform<T>> X_PB_pool_;

0 commit comments

Comments
 (0)