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 ContactModel base class and abstract contact handling in JaxSimModel and JaxSimModelData #178

Merged
merged 18 commits into from
Jun 18, 2024

Conversation

flferretti
Copy link
Collaborator

@flferretti flferretti commented Jun 13, 2024

This pull request refactors the contact models to enhance flexibility and future extensibility. Key changes include:

  • Introduced a ContactModel base class together with ContactsState and ContactParams
  • Refactored SoftContactModel, SoftContactParams and SoftContactState to inherit from ContactModel for consistency.
  • Added contact_model attribute to JaxSimModel.

These changes allow specifying different contact models during instantiation, facilitating the development of additional contact models in the future.

C.C. @xela-95


📚 Documentation preview 📚: https://jaxsim--178.org.readthedocs.build//178/

@flferretti flferretti self-assigned this Jun 13, 2024
@flferretti flferretti force-pushed the feature/base_contacts branch 3 times, most recently from 0755ca0 to c202825 Compare June 13, 2024 16:53
@flferretti
Copy link
Collaborator Author

For some reason, CI is failing only in Python 3.10 (https://github.com/ami-iit/jaxsim/actions/runs/9511311149)

@diegoferigo
Copy link
Member

For some reason, CI is failing only in Python 3.10 (https://github.com/ami-iit/jaxsim/actions/runs/9511311149)

Mmh, since it is related to hash and equality, maybe we can wait to merge #179.

Copy link
Member

@diegoferigo diegoferigo left a comment

Choose a reason for hiding this comment

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

Thanks @flferretti for this first attempt aimed at abstracting the contact model in view of the addition of alternative ones. I left few initial comments in the code.

  • What if we leave the actual soft-contact class defining the algorithm in jaxsim.rbda? If we plan to add multiple contact models, I'm ok in adding a new subpackage jaxsim.rbda.contacts.{soft|...}.
  • I'd keep the high-level detection of the contact model much more simple, right now there are many hacks I'd like to avoid at this initial stage.

src/jaxsim/api/contact.py Outdated Show resolved Hide resolved
src/jaxsim/api/contact.py Outdated Show resolved Hide resolved
src/jaxsim/api/contact.py Outdated Show resolved Hide resolved
src/jaxsim/api/contact.py Outdated Show resolved Hide resolved
src/jaxsim/api/ode_data.py Outdated Show resolved Hide resolved
src/jaxsim/api/ode_data.py Outdated Show resolved Hide resolved
src/jaxsim/api/ode_data.py Outdated Show resolved Hide resolved
src/jaxsim/api/contact.py Outdated Show resolved Hide resolved
@flferretti
Copy link
Collaborator Author

flferretti commented Jun 17, 2024

What if we leave the actual soft-contact class defining the algorithm in jaxsim.rbda? If we plan to add multiple contact models, I'm ok in adding a new subpackage jaxsim.rbda.contacts.{soft|...}.

Done in 2999198

@flferretti flferretti force-pushed the feature/base_contacts branch 4 times, most recently from 4556dbb to 5640547 Compare June 17, 2024 13:53
Copy link
Member

@diegoferigo diegoferigo left a comment

Choose a reason for hiding this comment

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

Great, thanks @flferretti! Last review pass with the final suggestions. Well done 🚀

src/jaxsim/api/contact.py Outdated Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Outdated Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Outdated Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Show resolved Hide resolved
position: jtp.Vector,
velocity: jtp.Vector,
**kwargs,
) -> tuple[Any, ...]:
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to provide as first output the computed contact forces, and as second output a tuple of auxiliary data. For the current soft contacts model, it will be (ṁ,)

Suggested change
) -> tuple[Any, ...]:
) -> tuple[jtp.Matrix, tuple[Any, ...]]:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done in f64764f

Copy link
Member

Choose a reason for hiding this comment

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

Note: this has to be propagated to the implementation before resolving this conversation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done in a9e3b2f

src/jaxsim/rbda/contacts/common.py Outdated Show resolved Hide resolved
Copy link
Member

@diegoferigo diegoferigo left a comment

Choose a reason for hiding this comment

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

Final suggestions that I overlooked in my previous review:

  • Can you double check in our codebase where JaxSimModel.build is called? For sure, the new JaxSimModel.contact_model attribute has to be copied when the model is reduced here.
  • Instead of pass, you can also use ... but I would not leave the body empty.

src/jaxsim/api/model.py Outdated Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Outdated Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Show resolved Hide resolved
src/jaxsim/rbda/contacts/common.py Show resolved Hide resolved
@flferretti
Copy link
Collaborator Author

Can you double check in our codebase where JaxSimModel.build is called? For sure, the new JaxSimModel.contact_model attribute has to be copied when the model is reduced here.

The only part in which the contact_model is not specified is in the test, yet this is not a problem since the soft contact model is the default value

@flferretti
Copy link
Collaborator Author

I added the pass statements and the rest of the suggestions in ab7a688

@diegoferigo
Copy link
Member

Can you double check in our codebase where JaxSimModel.build is called? For sure, the new JaxSimModel.contact_model attribute has to be copied when the model is reduced here.

The only part in which the contact_model is not specified is in the test, yet this is not a problem since the soft contact model is the default value

Ok. Instead, regarding the model reduction, I meant that something to prevent a problem similar to #168 is still missing, right?

@flferretti
Copy link
Collaborator Author

Can you double check in our codebase where JaxSimModel.build is called? For sure, the new JaxSimModel.contact_model attribute has to be copied when the model is reduced here.

The only part in which the contact_model is not specified is in the test, yet this is not a problem since the soft contact model is the default value

Ok. Instead, regarding the model reduction, I meant that something to prevent a problem similar to #168 is still missing, right?

It's indeed there

contact_model=model.contact_model,

@diegoferigo
Copy link
Member

Can you double check in our codebase where JaxSimModel.build is called? For sure, the new JaxSimModel.contact_model attribute has to be copied when the model is reduced here.

The only part in which the contact_model is not specified is in the test, yet this is not a problem since the soft contact model is the default value

Ok. Instead, regarding the model reduction, I meant that something to prevent a problem similar to #168 is still missing, right?

It's indeed there

contact_model=model.contact_model,

Woops, sorry my bad. All good then, thanks!

src/jaxsim/api/model.py Outdated Show resolved Hide resolved
@flferretti flferretti merged commit 5653178 into main Jun 18, 2024
29 checks passed
@flferretti flferretti deleted the feature/base_contacts branch June 18, 2024 16:15
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.

None yet

2 participants