Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ResolveConstraints logic to JointSliders (=> ModelVisualizer) #22496

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RussTedrake
Copy link
Contributor

@RussTedrake RussTedrake commented Jan 19, 2025

Updates JointSliders to have discrete state (previously it was
stateless). Additionally, if the new ResolveConstraints logic is
active, we publish the (rounded) resolved constraints back to Meshcat,
so that the other sliders are updated.

Since ModelVisualizer uses JointSliders, now ModelVisualizer also
satisfies the multibody constraints.

Resolves #18917.

Deprecates JointSliders::SetPositions(q), which has been replaced with
JointSliders::SetPositions(context, q).

Screen.Recording.2025-01-19.at.2.44.09.PM.mov

This change is Reviewable

They are computing this anyhow, so it is no burden to return them. And
it turns out to be useful for my follow-up PR to JointSliders which
has to reason explicitly about the rounding happening in the sliders.
Updates JointSliders to have discrete state (previously it was
stateless). Additionally, if the new ResolveConstraints logic is
active, we publish the (rounded) resolved constraints back to Meshcat,
so that the other sliders are updated.

Since ModelVisualizer uses JointSliders, now ModelVisualizer also
satisfies the multibody constraints.

Resolves RobotLocomotion#18917.

Deprecates `JointSliders::SetPositions(q)`, which has been replaced with
`JointSliders::SetPositions(context, q)`.
@RussTedrake RussTedrake added release notes: newly deprecated This pull request contains new deprecations release notes: feature This pull request contains a new feature labels Jan 19, 2025
Copy link
Contributor Author

@RussTedrake RussTedrake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+@jwnimmer-tri for feature review, please.

This PR contains #22495 as a first commit...; the request here is to review the second commit.

Reviewable status: LGTM missing from assignee jwnimmer-tri(platform), needs at least two assigned reviewers, commits need curation (https://drake.mit.edu/reviewable.html#curated-commits) (waiting on @RussTedrake)

Copy link
Collaborator

@jwnimmer-tri jwnimmer-tri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a high-level skim for now ...

Reviewed 13 of 13 files at r1, 2 of 10 files at r2, all commit messages.
Reviewable status: 6 unresolved discussions, LGTM missing from assignee jwnimmer-tri(platform), needs at least two assigned reviewers, commits need curation (https://drake.mit.edu/reviewable.html#curated-commits) (waiting on @RussTedrake)


multibody/meshcat/joint_sliders.h line 199 at r2 (raw file):

  systems::DiscreteStateIndex constrained_values_index_;
  systems::DiscreteStateIndex result_index_;
  mutable solvers::MathematicalProgram prog_;

This use of prog_ is not thread safe. We could either guard it with a mutex, place it into the context as a scratch value, or Clone the const program in ResolveConstraints (which would also then simplify the scope-cleanup logic).


multibody/meshcat/joint_sliders.h line 201 at r2 (raw file):

  mutable solvers::MathematicalProgram prog_;
  solvers::VectorXDecisionVariable q_;
  std::unique_ptr<solvers::SolverInterface> solver_{};

nit The missing const here was very confusing (when evaluating thread-safety of the callbacks).

Suggestion:

std::unique_ptr<const solvers::SolverInterface> solver_{};

multibody/meshcat/joint_sliders.h line 203 at r2 (raw file):

  std::unique_ptr<solvers::SolverInterface> solver_{};
  std::unique_ptr<systems::Context<double>> plant_context_{};
  std::unique_ptr<MultibodyPlant<double>> owned_plant_{};

BTW The owned_plant_ could be done away with by changing plant_ into a shared_ptr<const MbP> which can store both an owned or unowned plant depending on how the shared_ptr is constructed.


multibody/inverse_kinematics/add_multibody_plant_constraints.h line 35 at r2 (raw file):

*/
std::vector<solvers::Binding<solvers::Constraint>> AddMultibodyPlantConstraints(
    const MultibodyPlant<double>& plant,

The plant must be passed by const pointer, since the function captures it. Actually a shared_ptr<const MbP> would even more clearly communicate what's happening, without requiring a special warning in the docs. The python
bindings also seem like they are missing the lifetime annotation.

I'll prepare a separate pull request with those fixes.


multibody/meshcat/joint_sliders.cc line 81 at r2 (raw file):

      if (joint.num_positions() > 1) {
        description =
            fmt::format("{}_{}", joint.name(), joint.position_suffix(j));

BTW I'll open a new PR to enable clang-format in this directory in a moment. We'll either need to merge that change ahead of this PR, or else you'll need to revert the unrelated clang re-formatting here.


multibody/meshcat/joint_sliders.cc line 288 at r2 (raw file):

  solvers::Binding<solvers::QuadraticCost> cost =
      prog_.AddQuadraticErrorCost(1.0, target_values, q_);
  std::vector<solvers::Binding<solvers::Constraint>> constraints;

As soon as we've declared constraints, we should use https://github.com/RobotLocomotion/drake/blob/master/common/scope_exit.h to arm the "remove from program" cleanup logic, so that if there are any exceptions our invariants will still hold.

Or in the alternative, clone the program into a local variable, mutate it in place, and then just throw it away when we're done. MP cloning is shallow, so should be pretty fast.

Copy link
Collaborator

@jwnimmer-tri jwnimmer-tri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 6 unresolved discussions, LGTM missing from assignee jwnimmer-tri(platform), needs at least two assigned reviewers, commits need curation (https://drake.mit.edu/reviewable.html#curated-commits) (waiting on @RussTedrake)


multibody/inverse_kinematics/add_multibody_plant_constraints.h line 35 at r2 (raw file):

Previously, jwnimmer-tri (Jeremy Nimmer) wrote…

The plant must be passed by const pointer, since the function captures it. Actually a shared_ptr<const MbP> would even more clearly communicate what's happening, without requiring a special warning in the docs. The python
bindings also seem like they are missing the lifetime annotation.

I'll prepare a separate pull request with those fixes.

=> #22502


multibody/meshcat/joint_sliders.cc line 81 at r2 (raw file):

Previously, jwnimmer-tri (Jeremy Nimmer) wrote…

BTW I'll open a new PR to enable clang-format in this directory in a moment. We'll either need to merge that change ahead of this PR, or else you'll need to revert the unrelated clang re-formatting here.

=> #22501

Copy link
Collaborator

@jwnimmer-tri jwnimmer-tri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 5 unresolved discussions, LGTM missing from assignee jwnimmer-tri(platform), needs at least two assigned reviewers, commits need curation (https://drake.mit.edu/reviewable.html#curated-commits) (waiting on @RussTedrake)


multibody/meshcat/joint_sliders.cc line 81 at r2 (raw file):

Previously, jwnimmer-tri (Jeremy Nimmer) wrote…

=> #22501

Done.

Copy link
Collaborator

@jwnimmer-tri jwnimmer-tri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 5 unresolved discussions, LGTM missing from assignee jwnimmer-tri(platform), needs at least two assigned reviewers, commits need curation (https://drake.mit.edu/reviewable.html#curated-commits) (waiting on @RussTedrake)


multibody/inverse_kinematics/add_multibody_plant_constraints.h line 35 at r2 (raw file):
Actually if you want to just remove this newly-added sentence ...

The plant must stay alive for the lifetime of the added constraints.

... from this PR, then we can decouple the two changes.

Copy link
Contributor Author

@RussTedrake RussTedrake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll rebase and finish addressing your comments as soon as #22502 lands.

Reviewable status: 4 unresolved discussions, LGTM missing from assignee jwnimmer-tri(platform), needs at least two assigned reviewers, commits need curation (https://drake.mit.edu/reviewable.html#curated-commits)


multibody/inverse_kinematics/add_multibody_plant_constraints.h line 35 at r2 (raw file):

Previously, jwnimmer-tri (Jeremy Nimmer) wrote…

=> #22502

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: feature This pull request contains a new feature release notes: newly deprecated This pull request contains new deprecations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model visualizer should know about mimic tags (coupled joints)
2 participants