-
Notifications
You must be signed in to change notification settings - Fork 6
Friction compensation #40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1225cb4
Friction compensation
edantec 93e1a27
Compatible name with pinocchio 2
edantec 4f93372
Take PR comments into account
edantec 75f0d7f
Fix python example and some minor bugs
edantec 7e00661
Change nu initialization and restore throw exception
edantec 3da6485
Add proxy to friction binding
edantec d43c959
Trim space in example
edantec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /////////////////////////////////////////////////////////////////////////////// | ||
| // BSD 2-Clause License | ||
| // | ||
| // Copyright (C) 2025, INRIA | ||
| // Copyright note valid unless otherwise stated in individual files. | ||
| // All rights reserved. | ||
| /////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #include <eigenpy/eigenpy.hpp> | ||
| #include <pinocchio/multibody/fwd.hpp> | ||
|
|
||
| #include "simple-mpc/friction-compensation.hpp" | ||
|
|
||
| namespace simple_mpc | ||
| { | ||
| namespace python | ||
| { | ||
| namespace bp = boost::python; | ||
|
|
||
| Eigen::VectorXd | ||
| computeFrictionProxy(FrictionCompensation & self, Eigen::Ref<const VectorXd> velocity, Eigen::Ref<VectorXd> torque) | ||
| { | ||
| self.computeFriction(velocity, torque); | ||
|
|
||
| return torque; | ||
| } | ||
|
|
||
| void exposeFrictionCompensation() | ||
| { | ||
| bp::class_<FrictionCompensation>( | ||
| "FrictionCompensation", bp::init<const Model &, const bool>(bp::args("self", "model", "with_free_flyer"))) | ||
| .def("computeFriction", &computeFrictionProxy) | ||
| .add_property("dry_friction", &FrictionCompensation::dry_friction_) | ||
| .add_property("viscuous_friction", &FrictionCompensation::viscuous_friction_); | ||
| } | ||
|
|
||
| } // namespace python | ||
| } // namespace simple_mpc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /////////////////////////////////////////////////////////////////////////////// | ||
| // BSD 2-Clause License | ||
| // | ||
| // Copyright (C) 2025, INRIA | ||
| // Copyright note valid unless otherwise stated in individual files. | ||
| // All rights reserved. | ||
| /////////////////////////////////////////////////////////////////////////////// | ||
| #pragma once | ||
|
|
||
| #include <pinocchio/fwd.hpp> | ||
| // Include pinocchio first | ||
| #include <Eigen/Dense> | ||
|
|
||
| #include "simple-mpc/fwd.hpp" | ||
| #include <pinocchio/multibody/model.hpp> | ||
|
|
||
| namespace simple_mpc | ||
| { | ||
| using namespace pinocchio; | ||
|
|
||
| /** | ||
| * @brief Class managing the friction compensation for torque | ||
| * | ||
| * It applies a compensation term to a torque depending on dry and | ||
| * viscuous frictions. | ||
| */ | ||
| class FrictionCompensation | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Construct a new Friction Compensation object | ||
| * | ||
| * @param model Pinocchio model containing friction coefficients | ||
| @param with_free_flyer Bool indicating if model has free flyer joint | ||
| */ | ||
| FrictionCompensation(const Model & model, const bool with_free_flyer = true); | ||
|
|
||
| /** | ||
| * @brief Add friction correction to joint torque | ||
| * | ||
| * @param[in] velocity Joint velocity | ||
| * @param[in] torque Joint torque | ||
| */ | ||
| void computeFriction(Eigen::Ref<const VectorXd> velocity, Eigen::Ref<VectorXd> torque); | ||
|
|
||
| // Sign function for internal computation | ||
| static double signFunction(double x); | ||
|
|
||
| // Actuation size | ||
| int nu_; | ||
|
|
||
| // Friction coefficients | ||
| Eigen::VectorXd dry_friction_; | ||
| Eigen::VectorXd viscuous_friction_; | ||
| }; | ||
|
|
||
| } // namespace simple_mpc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #include "simple-mpc/friction-compensation.hpp" | ||
| #include <iostream> | ||
| namespace simple_mpc | ||
| { | ||
|
|
||
| FrictionCompensation::FrictionCompensation(const Model & model, const bool with_free_flyer) | ||
| { | ||
| if (with_free_flyer) | ||
| { | ||
| // Ignore free flyer joint | ||
| nu_ = model.nv - 6; | ||
| } | ||
| else | ||
| { | ||
| // If no free flyer | ||
| nu_ = model.nv; | ||
| } | ||
| dry_friction_ = model.friction.tail(nu_); | ||
| viscuous_friction_ = model.damping.tail(nu_); | ||
| } | ||
|
|
||
| void FrictionCompensation::computeFriction(Eigen::Ref<const VectorXd> velocity, Eigen::Ref<VectorXd> torque) | ||
| { | ||
| if (velocity.size() != nu_) | ||
| throw std::runtime_error("Velocity has wrong size"); | ||
| if (torque.size() != nu_) | ||
| throw std::runtime_error("Torque has wrong size"); | ||
|
|
||
| torque += viscuous_friction_.cwiseProduct(velocity) | ||
| + dry_friction_.cwiseProduct(velocity.unaryExpr(std::function(signFunction))); | ||
| } | ||
|
|
||
| double FrictionCompensation::signFunction(double x) | ||
| { | ||
| return (x > 0) - (x < 0); | ||
| } | ||
|
|
||
| } // namespace simple_mpc | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
|
|
||
| #include <boost/test/unit_test.hpp> | ||
|
|
||
| #include "simple-mpc/friction-compensation.hpp" | ||
| #include "test_utils.cpp" | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(friction) | ||
|
|
||
| using namespace simple_mpc; | ||
|
|
||
| BOOST_AUTO_TEST_CASE(dry_viscuous_friction) | ||
| { | ||
| Model model; | ||
|
|
||
| const std::string urdf_path = EXAMPLE_ROBOT_DATA_MODEL_DIR "/solo_description/robots/solo12.urdf"; | ||
|
|
||
| pinocchio::urdf::buildModel(urdf_path, JointModelFreeFlyer(), model); | ||
| long nu = model.nv - 6; | ||
| model.friction.tail(nu) = Eigen::VectorXd::Constant(nu, .5); | ||
| model.damping.tail(nu) = Eigen::VectorXd::Constant(nu, .05); | ||
|
|
||
| FrictionCompensation friction = FrictionCompensation(model); | ||
|
|
||
| BOOST_CHECK_EQUAL(model.friction.tail(nu), friction.dry_friction_); | ||
| BOOST_CHECK_EQUAL(model.damping.tail(nu), friction.viscuous_friction_); | ||
|
|
||
| Eigen::VectorXd velocity = Eigen::VectorXd::Random(nu); | ||
| Eigen::VectorXd torque = Eigen::VectorXd::Random(nu); | ||
|
|
||
| Eigen::VectorXd ctorque(nu); | ||
| for (long i = 0; i < nu; i++) | ||
| { | ||
| double sgn = (velocity[i] > 0) - (velocity[i] < 0); | ||
| ctorque[i] = torque[i] + model.friction[i + 6] * sgn + model.damping[i + 6] * velocity[i]; | ||
| } | ||
|
|
||
| friction.computeFriction(velocity, torque); | ||
|
|
||
| BOOST_CHECK(torque.isApprox(ctorque)); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.