Skip to content

Commit be74a03

Browse files
committed
Don't recompute parameters
1 parent 425f45f commit be74a03

4 files changed

Lines changed: 36 additions & 24 deletions

File tree

multibody/tree/multibody_tree.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,9 +1404,21 @@ 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+
// Update parameter-dependent quantities in the position cache if they are
1412+
// not up to date with respect to the FrameBodyPoseCache in the context
1413+
// (which we just updated). PositionKinematicsCache needs the
1414+
// FrameBodyPoseCache serial number to make that determination.
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+
pc->PrecomputeWorldCompositeIfNeeded(forest(), frame_body_pose_cache,
1420+
fbpc_serial_number);
1421+
14101422
const Eigen::VectorBlock<const VectorX<T>> q_block = get_positions(context);
14111423
const T* q = q_block.data();
14121424

multibody/tree/multibody_tree_system.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,8 @@ class MultibodyTreeSystem : public systems::LeafSystem<T> {
9898
const FrameBodyPoseCache<T>& EvalFrameBodyPoses(
9999
const systems::Context<T>& context) const {
100100
this->ValidateContext(context);
101-
const FrameBodyPoseCache<T>& frame_body_pose_cache =
102-
frame_body_poses_cache_entry().template Eval<FrameBodyPoseCache<T>>(
103-
context);
104-
105-
// We don't know whether we actually had to update the FrameBodyPoseCache,
106-
// but if we did the PositionKinematicsCache will be out of date since
107-
// it depends on the parameterized data in the FrameBodyPoseCache.
108-
// In that case, we'll fill in now any position kinematics that are state
109-
// independent so that we don't have to recalculate when q's change.
110-
systems::CacheEntryValue& cev =
111-
position_kinematics_cache_entry().get_mutable_cache_entry_value(
112-
context);
113-
if (cev.is_out_of_date()) {
114-
PositionKinematicsCache<T>& pc =
115-
cev.GetMutableValueOrThrow<PositionKinematicsCache<T>>();
116-
pc.PrecomputeWorldComposite(tree_->forest(), frame_body_pose_cache);
117-
// This does not make the pc up to date!
118-
}
119-
120-
return frame_body_pose_cache;
101+
return frame_body_poses_cache_entry().template Eval<FrameBodyPoseCache<T>>(
102+
context);
121103
}
122104

123105
/* Returns a reference to the up-to-date PositionKinematicsCache in the

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,27 @@ 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.
178-
void PrecomputeWorldComposite(
177+
// are fixed to World. Do nothing if the serial number hasn't changed.
178+
void PrecomputeWorldCompositeIfNeeded(
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+
ComputeWorldComposite(forest, frame_body_pose_cache);
185+
world_composite_serial_number_ = frame_body_pose_cache_serial_number;
186+
}
181187

182188
private:
183189
// Allocates resources for this position kinematics cache.
184190
void Allocate();
185191

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

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

0 commit comments

Comments
 (0)