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

Bugfix: Fixed Centre of Mass and Inertia Matrix Calculation Bug MeshInertiaCalculator::CalculateMassProperties() function #2182

Merged

Conversation

jasmeet0915
Copy link
Contributor

@jasmeet0915 jasmeet0915 commented Sep 29, 2023

🦟 Bug fix

Previous Understanding:
The MeshInertiaCalculator.cc computes the inertia w.r.t the mesh origin. This can lead to incorrect inertia values in case the Mesh Origin was not at the Center of Mass as mentioned here.
Since many times users may tend to use meshes with origins, not at the center, this PR adds and uses a function to transform the Inertia Matrix to the COM in such cases.

Actual Scenario:
There was a bug in the center of mass and inertia matrix calculation. This PR fixes bugs in a moment of inertia matrix calculation and adds a test for the inertia calculation of a cylinder model with the origin set not at the center of mass.

Some extra functions were also added according to the previous understanding but are not needed after the bugfix:

  • CalculateMeshCentroid()
  • TransformInertiaMatrixToCOM()

Note: I have still kept the functions as a part of this PR as they might come in handy later but they can be removed if required.

Below you can see multiple cylinder meshes with origins set at different places inside or outside the mesh getting similar inertia values:

inertia_different_origins

Test it

Checklist

  • Signed all commits for DCO
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

@github-actions github-actions bot added the 🎵 harmonic Gazebo Harmonic label Sep 29, 2023
@jasmeet0915 jasmeet0915 force-pushed the jasmeet/inertia_tensor_transformation branch from 9c0eed4 to fcda45f Compare September 29, 2023 21:02
@codecov
Copy link

codecov bot commented Sep 29, 2023

Codecov Report

Merging #2182 (2a3ffdf) into gz-sim8 (18aa48f) will decrease coverage by 0.19%.
Report is 26 commits behind head on gz-sim8.
The diff coverage is 36.58%.

❗ Current head 2a3ffdf differs from pull request most recent head 7424bab. Consider uploading reports for the commit 7424bab to get more accurate results

@@             Coverage Diff             @@
##           gz-sim8    #2182      +/-   ##
===========================================
- Coverage    66.00%   65.81%   -0.19%     
===========================================
  Files          323      323              
  Lines        30719    30740      +21     
===========================================
- Hits         20275    20233      -42     
- Misses       10444    10507      +63     
Files Coverage Δ
...rc/systems/ackermann_steering/AckermannSteering.hh 100.00% <ø> (ø)
src/systems/breadcrumbs/Breadcrumbs.hh 100.00% <ø> (ø)
src/systems/buoyancy/Buoyancy.hh 100.00% <ø> (ø)
src/systems/detachable_joint/DetachableJoint.hh 100.00% <ø> (ø)
src/systems/diff_drive/DiffDrive.hh 100.00% <ø> (ø)
src/systems/elevator/Elevator.hh 100.00% <ø> (ø)
src/systems/joint_controller/JointController.hh 100.00% <ø> (ø)
...int_position_controller/JointPositionController.hh 100.00% <ø> (ø)
...stems/joint_state_publisher/JointStatePublisher.hh 100.00% <ø> (ø)
...ms/joint_traj_control/JointTrajectoryController.hh 100.00% <ø> (ø)
... and 4 more

... and 2 files with indirect coverage changes

@azeey
Copy link
Contributor

azeey commented Oct 4, 2023

@jasmeet0915 would it be possible to add a test for this?

@jasmeet0915 jasmeet0915 changed the title Added function for Transformation of Inertia Matrix to COM in MeshInertiaCalculator Bugfix: Fixed Centre of Mass and Inertia Matrix Calculation Bug MeshInertiaCalculator::CalculateMassProperties() function Oct 20, 2023
@jasmeet0915 jasmeet0915 marked this pull request as ready for review October 20, 2023 09:30
@jasmeet0915
Copy link
Contributor Author

@azeey I have added a test in a161374

Comment on lines 119 to 120
gz::math::Pose3d &_centreOfMass,
gz::math::Pose3d &_inertiaOrigin);
Copy link
Contributor

Choose a reason for hiding this comment

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

can these be const ref?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 01aa539

@mjcarroll mjcarroll requested a review from iche033 October 30, 2023 18:42
@iche033
Copy link
Contributor

iche033 commented Oct 30, 2023

code looks good to me. Maybe @azeey can take a one final look before merging.

@scpeters
Copy link
Member

is there documentation of the algorithm / integrals used in MeshInertiaCalculator? In particular I'm not sure where the terms in CalculateMassProperties are coming from

@jasmeet0915
Copy link
Contributor Author

is there documentation of the algorithm / integrals used in MeshInertiaCalculator? In particular I'm not sure where the terms in CalculateMassProperties are coming from

@scpeters The method used was referred from this doc: https://www.geometrictools.com/Documentation/PolyhedralMassProperties.pdf
You can find the documentation of all the integrals in the same.

@@ -101,12 +107,61 @@ void MeshInertiaCalculator::CalculateMeshCentroid(
_centreOfMass.SetZ(centroid.Z());
}

//////////////////////////////////////////////////
void MeshInertiaCalculator::TransformInertiaMatrixToCOM(
Copy link
Contributor

@azeey azeey Nov 8, 2023

Choose a reason for hiding this comment

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

Since CalculateMeshCentroid and TransformInertiaMatrixToCOM are unused and untested, I think we should remove them. I'd be in favor of adding TransformInertiaMatrixToCOM to gz::math::Inertial with tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the unused functions in 7424bab. Would create a PR over at gz-math as soon as possible for the TransformInertiaMatrixToCOM() function.

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for the fix. My only suggestion is we remove unused code.

@jasmeet0915
Copy link
Contributor Author

Looks good! Thanks for the fix. My only suggestion is we remove unused code.

@azeey I have removed the unused code in this commit: 7424bab

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

Thanks @jasmeet0915!!

@azeey azeey merged commit 9db8061 into gazebosim:gz-sim8 Nov 13, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎵 harmonic Gazebo Harmonic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants