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

Physics: remove *VelocityCmd at each time step #2228

Merged
merged 10 commits into from
Apr 9, 2024

Conversation

scpeters
Copy link
Member

@scpeters scpeters commented Nov 4, 2023

🎉 New feature

Part of #1926.

Summary

This implements the suggestion from #1926 (comment) to delete all *VelocityCmd components after each time step.

Test it

I'm still figuring out how to 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.

@scpeters scpeters force-pushed the scpeters/physics_remove_velocity_cmds branch from 28bccb3 to 4070cac Compare November 5, 2023 06:04
Since JointVelocityCmd components are deleted after each
timestep, don't skip updating forces and moments if
the component does not exist, and use the
SetComponent API to more cleanly handle the component
creation logic.

Signed-off-by: Steve Peters <[email protected]>
@scpeters scpeters marked this pull request as ready for review November 5, 2023 17:11
Copy link

codecov bot commented Nov 5, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 65.84%. Comparing base (ae75c25) to head (714e948).
Report is 73 commits behind head on main.

❗ Current head 714e948 differs from pull request most recent head f4ffd4a. Consider uploading reports for the commit f4ffd4a to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2228      +/-   ##
==========================================
+ Coverage   65.66%   65.84%   +0.18%     
==========================================
  Files         324      323       -1     
  Lines       30938    30669     -269     
==========================================
- Hits        20315    20194     -121     
+ Misses      10623    10475     -148     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@scpeters
Copy link
Member Author

scpeters commented Nov 6, 2023

cleaned up some code and improved coverage with SetComponentData in b145c05

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 so far!

It would be good to characterize which systems are changing behavior due to this. For example, it looks like the DiffDrive system won't change behavior because it'll constantly be sending the last twist command it received.

{
_ecm.CreateComponent(this->dataPtr->jointEntity,
components::JointVelocityCmd({0}));
doUpdateForcesAndMoments = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to preserve the doUpdateForcesAndMoments = false;?

Copy link
Member Author

Choose a reason for hiding this comment

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

no, the other places that variable is set to false is when a physics state / sensor reading component is unavailable that is needed for the control calculation, but JointVelocityCmd is an actuator command written by this system, so it's ok if it's missing

@scpeters
Copy link
Member Author

It would be good to characterize which systems are changing behavior due to this. For example, it looks like the DiffDrive system won't change behavior because it'll constantly be sending the last twist command it received.

the following systems reference a *VelocityCmd component:

$ grep -rlI VelocityCmd src/systems | sort 
src/systems/ackermann_steering/AckermannSteering.cc
src/systems/battery_plugin/LinearBatteryPlugin.cc
src/systems/diff_drive/DiffDrive.cc
src/systems/joint_controller/JointController.cc
src/systems/joint_position_controller/JointPositionController.cc
src/systems/mecanum_drive/MecanumDrive.cc
src/systems/multicopter_motor_model/MulticopterMotorModel.cc
src/systems/physics/Physics.cc
src/systems/thruster/Thruster.cc
src/systems/trajectory_follower/TrajectoryFollower.cc
src/systems/velocity_control/VelocityControl.cc
  • The LinearBatteryPlugin only reads the content of a JointVelocityCmd; it does not modify them, so it's behavior will not change
  • The Physics system does not set the content of any *VelocityCmd components; it only removes them via the changes in this PR

The following systems are already modified by this pull request to use the SetComponentData API, and I have checked them to verify that their behavior will not change.

src/systems/ackermann_steering/AckermannSteering.cc
src/systems/diff_drive/DiffDrive.cc
src/systems/joint_controller/JointController.cc
src/systems/joint_position_controller/JointPositionController.cc
src/systems/multicopter_motor_model/MulticopterMotorModel.cc
src/systems/thruster/Thruster.cc
src/systems/velocity_control/VelocityControl.cc

I still need to review the following two systems:

  • src/systems/mecanum_drive/MecanumDrive.cc
  • src/systems/trajectory_follower/TrajectoryFollower.cc

Now that the physics system is removing AngularVelocityCmd
components at every timestep, that logic can be removed
from the trajectory follower system.

Signed-off-by: Steve Peters <[email protected]>
@scpeters
Copy link
Member Author

I still need to review the following two systems:

  • src/systems/mecanum_drive/MecanumDrive.cc
  • src/systems/trajectory_follower/TrajectoryFollower.cc

Upon further review, I found that the MecanumDrive system's behavior is unchanged, though I took the opportunity to use the SetComponentData API in 7dc33fe. The TrajectoryFollower system was already removing AngularVelocityCmd components at every timestep that it didn't want to set them, which is now not necessary, so I removed that logic in 120e5fe

@scpeters
Copy link
Member Author

I also noticed that the MecanumDrive and TrajectoryFollower systems don't have integration tests, so I filed #2238 and #2239.

@mjcarroll
Copy link
Contributor

So not something that needs to be addressed here, but I believe that adding and removing components is relatively expensive (compared to setting/unsetting them) as they can incur allocation/deallocation as well as additional bookkeeping inside the ECM.

I think in the future, we should consider having an optional<Component> type that remains attached to an entity, but isn't created/destroyed each iteration.

@scpeters
Copy link
Member Author

I think in the future, we should consider having an optional<Component> type that remains attached to an entity, but isn't created/destroyed each iteration.

That could make sense if it makes a significant performance improvement. We could define each component as Component<std::optional<T> ..., but I'm guessing your proposal sounds a little different than that

@mjcarroll
Copy link
Contributor

That could make sense if it makes a significant performance improvement.

I don't have a good gauge of the impact. I'm going to assume that not allocating/bookkeeping are always going to be better than allocation/bookkeeping. I think in this case, the trade-off would be a small amount of memory overhead, because you would be keeping the components in question (or a pointer to them).

We could define each component as Component<std::optional ..., but I'm guessing your proposal sounds a little different than that

That's basically what I'm suggesting. We may be able to do a little bit of syntactic sugar to make an OptionalComponent<T>.
That could enable letting Each calls only iterate over components that are set/unset.

@mjcarroll
Copy link
Contributor

as a followup on the performance side of things, the concept of "optional components" was mostly added already: #1334 (comment)

So that is one less thing to worry about here.

@azeey
Copy link
Contributor

azeey commented Mar 18, 2024

@scpeters what's the status of this PR?

@scpeters
Copy link
Member Author

scpeters commented Apr 4, 2024

@scpeters what's the status of this PR?

sorry I missed this notification until now. I thought it was good to go in November, but it may have gone out of date since then. I can open a separate pull request for the SetComponentData changes in the other systems to reduce the noise if that would help

@scpeters
Copy link
Member Author

scpeters commented Apr 5, 2024

I can open a separate pull request for the SetComponentData changes in the other systems to reduce the noise if that would help

#2360

@scpeters
Copy link
Member Author

scpeters commented Apr 5, 2024

I can open a separate pull request for the SetComponentData changes in the other systems to reduce the noise if that would help

#2360

I merged these changes to gz-sim8 and then forward to main and merged into this branch, so the diff is a bit smaller now. This is ready for review again

Copy link
Contributor

@iche033 iche033 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 to me

Migration.md Outdated
## Gazebo Sim 8.x to 9.0

* **Modified**:
+ In the Physics system, all `*VelocityCmd` components are deleted after
Copy link
Contributor

Choose a reason for hiding this comment

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

mention that previously it was setting them to zero?

Copy link
Member Author

Choose a reason for hiding this comment

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

Signed-off-by: Steve Peters <[email protected]>
@@ -5,6 +5,14 @@ Deprecated code produces compile-time warnings. These warning serve as
notification to users that their code should be upgraded. The next major
release will remove the deprecated code.

## Gazebo Sim 8.x to 9.0

* **Modified**:
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this!

@@ -91,7 +91,7 @@
</geometry>
</collision>
<visual name="rotor_0_visual">
<pose>0 0 0 1.57 0 0 0</pose>
<pose>0 0 0 1.57 0 0</pose>
Copy link
Contributor

Choose a reason for hiding this comment

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

I realized last week while trying to fix another test that the quadcopter test is passing even though this SDF was invalid. I'll be making the same changes in other branches.

@scpeters scpeters merged commit 77f4c00 into main Apr 9, 2024
3 of 6 checks passed
@scpeters scpeters deleted the scpeters/physics_remove_velocity_cmds branch April 9, 2024 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏛️ ionic Gazebo Ionic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants