Skip to content

Fix PyBullet stale inverse dynamics cache after dynamics change or body removal (#4809) - #4817

Open
paneendrakumar0 wants to merge 1 commit into
bulletphysics:masterfrom
paneendrakumar0:fix-pybullet-inverse-dynamics-cache-4809
Open

paneendrakumar0 wants to merge 1 commit into
bulletphysics:masterfrom
paneendrakumar0:fix-pybullet-inverse-dynamics-cache-4809

Conversation

@paneendrakumar0

Copy link
Copy Markdown

Fixes #4809.

Description

In examples/SharedMemory/PhysicsServerCommandProcessor.cpp, calculateInverseDynamics and calculateMassMatrix cache btInverseDynamics::MultiBodyTree (and cRBDModel) instances indexed by raw btMultiBody* pointer in m_inverseDynamicsBodies (and m_rbdModels).

Previously, two cache synchronization issues occurred:

  1. processChangeDynamicsInfoCommand() changed base or link mass/inertia without invalidating the cached inverse dynamics tree or RBD model. Subsequent calls to calculateInverseDynamics and calculateMassMatrix continued using the stale pre-mutation tree.
  2. processRemoveBodyCommand() deleted btMultiBody instances without purging them from m_inverseDynamicsBodies or m_inverseKinematicsHelpers. When a new body was allocated at the same memory address or reused body ID 0, calculations for the new body silently used the old body's cached tree.

Solution

  • Added deleteCachedInverseDynamicsBody and deleteCachedBodyData helpers to PhysicsServerCommandProcessorInternalData.
  • In PhysicsServerCommandProcessor::processChangeDynamicsInfoCommand(), purge the cached inverse dynamics tree and RBD model whenever CHANGE_DYNAMICS_INFO_SET_MASS or CHANGE_DYNAMICS_INFO_SET_LOCAL_INERTIA_DIAGONAL is present in clientCmd.m_updateFlags.
  • In PhysicsServerCommandProcessor::processRemoveBodyCommand(), clean up both m_inverseDynamicsBodies and m_inverseKinematicsHelpers prior to deleting bodyHandle->m_multiBody.
  • Added regression test test_inverse_dynamics_cache_invalidation in examples/pybullet/unittests/unittests.py covering both dynamics mutation and body reuse after removal.

…dy removal (bulletphysics#4809)

In PhysicsServerCommandProcessor, calculateInverseDynamics and
calculateMassMatrix cache btInverseDynamics::MultiBodyTree (and
cRBDModel) instances indexed by btMultiBody* pointer.

Previously, two cache synchronization issues occurred:
1. processChangeDynamicsInfoCommand changed base or link mass/inertia
   without invalidating the cached inverse dynamics tree or RBD model,
   causing subsequent inverse dynamics and mass matrix calculations to
   return stale results computed with pre-mutation mass values.
2. processRemoveBodyCommand deleted btMultiBody instances without
   removing them from m_inverseDynamicsBodies or
   m_inverseKinematicsHelpers. When a new body reused the freed memory
   address or body ID 0, calculations for the new body silently used the
   old body's cached tree.

This patch:
- Invalidate and free cached inverse dynamics trees (and RBD models)
  when mass or local inertia is modified in
  processChangeDynamicsInfoCommand.
- Invalidate and free cached inverse dynamics trees and inverse
  kinematics helpers when a multibody is removed in
  processRemoveBodyCommand.
- Adds regression tests in unittests.py covering both dynamic mass
  mutation and body reuse scenarios.

Signed-off-by: paneendrakumar0 <paneendra100@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PyBullet inverse-dynamics cache is stale after mass changes and body reuse

1 participant