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

Refactor Servo velocity bounds enforcement #2471

Merged
merged 9 commits into from
Feb 4, 2021

Conversation

jliukkonen
Copy link
Contributor

@jliukkonen jliukkonen commented Jan 5, 2021

The enforceVelLimits function struck me as bit odd so I figured I make this PR to start a conversation about what I think it is doing and I why I think it looks like it might not be correct.

Prior to my changes the function would calculate joint angle velocity for each joint based on the delta theta (joint angle change) and would determined the smallest scaling factor for the velocities based on the biggest out-of-bounds velocity value. So if one would have joint velocities of, say, [1, 1, 1, 2, 3, 100], and each joint was bounded to [-1, 1] range, then the scaling factor would be 0.01 . That's at least how I interpreted it. This scaling factor was then applied to the delta thetas meaning if one joint received a command that would translate into very large velocity, the other joints would slow to a crawl.

I felt it would make more sense and be more correct to just apply the velocity limits as they are used elsewhere in MoveIt, that is, make them upper and lower bounds for the joint velocities by clamping the velocities to [min_vel, max_vel] range.

One thing I don't currently understand is how the delta thetas and joint bounds are matched together. Is it guaranteed that the bounds are in correct order with respect to the delta_theta vector and just applying them in order is enough?

@codecov
Copy link

codecov bot commented Jan 5, 2021

Codecov Report

Merging #2471 (fb5b233) into master (59a3724) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2471      +/-   ##
==========================================
- Coverage   60.26%   60.24%   -0.02%     
==========================================
  Files         351      351              
  Lines       26477    26472       -5     
==========================================
- Hits        15955    15946       -9     
- Misses      10522    10526       +4     
Impacted Files Coverage Δ
moveit_ros/moveit_servo/src/servo_calcs.cpp 62.67% <100.00%> (-0.41%) ⬇️
...dl_kinematics_plugin/src/kdl_kinematics_plugin.cpp 74.78% <0.00%> (-1.32%) ⬇️
...nning_scene_monitor/src/planning_scene_monitor.cpp 67.68% <0.00%> (-0.47%) ⬇️
moveit_ros/moveit_servo/src/pose_tracking.cpp 46.11% <0.00%> (ø)
.../moveit_servo/include/moveit_servo/pose_tracking.h 100.00% <0.00%> (ø)
.../collision_detection_fcl/src/collision_env_fcl.cpp 88.42% <0.00%> (ø)
...sion_detection_bullet/src/collision_env_bullet.cpp 78.38% <0.00%> (ø)
...industrial_motion_planner/joint_limits_container.h 100.00% <0.00%> (ø)
...industrial_motion_planner/joint_limits_extension.h 100.00% <0.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 59a3724...fb5b233. Read the comment docs.

@AndyZe
Copy link
Member

AndyZe commented Jan 5, 2021

The reason it scales velocities down uniformly is so the robot follows a straight line. Thus, I'm not a huge fan of this change.

If you can show it's really important from a performance standpoint, we could make your new method optional.

@jliukkonen jliukkonen changed the title Refactor/fix Servo velocity bounds enforcement [WIP] Refactor Servo velocity bounds enforcement Jan 6, 2021
@AndyZe
Copy link
Member

AndyZe commented Jan 22, 2021

@jliukkonen I hope you can finish this PR! I know you're pretty busy but it's a nice simplification.

@jschleicher
Copy link
Contributor

The reason it scales velocities down uniformly is so the robot follows a straight line. Thus, I'm not a huge fan of this change.

Definitely! According to our previous tests it is essential, that you keep the direction, when scaling the velocity vector. Nevertheless I suspect, there is some problem hidden in enforceVelLimits() leading to artifacts as described in #2096.

Copy link
Member

@AndyZe AndyZe left a comment

Choose a reason for hiding this comment

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

I added some commits for unit testing. Looks good to me, now. Thanks for a nice simplification.

@jliukkonen
Copy link
Contributor Author

Thanks for fixing some of the bugs and adding unit tests @AndyZe! I think I managed to nail all the remaining issues. I added some comments to the code, please check them out when re-reviewing.

}
const double unbounded_velocity = velocity(joint_delta_index);
// Clamp each joint velocity to a joint specific [min_velocity, max_velocity] range.
const auto bounded_velocity = std::min(std::max(unbounded_velocity, bounds.min_velocity_), bounds.max_velocity_);
Copy link
Contributor Author

@jliukkonen jliukkonen Feb 2, 2021

Choose a reason for hiding this comment

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

This was wrong in my previous version. The velocities were bounded before applying velocity scaling factor so velocity was possibly scaled down twice depending on the velocity values and joint limits. First by applying velocity bounds and then by applying scaling factor to the already bounded velocities. Now the velocity bounds are only used to determine the scaling factor.

@jliukkonen jliukkonen changed the title [WIP] Refactor Servo velocity bounds enforcement Refactor Servo velocity bounds enforcement Feb 2, 2021
enforceVelLimits(delta_theta);

// From Panda arm MoveIt joint_limits.yaml. The largest velocity limits for a joint.
const double panda_max_joint_vel = 2.610; // rad/s
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since joint_limits.yaml take precedence over the URDF the value is actually 2.61 and not 2.871. Took me a while to understand that that was the source of errors. :)

Copy link
Member

Choose a reason for hiding this comment

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

good catch there

enforceVelLimits(delta_theta);

// From Panda arm MoveIt joint_limits.yaml. The largest velocity limits for a joint.
const double panda_max_joint_vel = 2.610; // rad/s
Copy link
Member

Choose a reason for hiding this comment

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

good catch there

const double publish_period = parameters.publish_period;

// Request joint angle changes that are too fast, given the control period in servo settings YAML file.
Eigen::ArrayXd delta_theta(7);
Copy link
Member

Choose a reason for hiding this comment

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

i see you made this a 7-vector because the joint group has 7 joints. Good catch.

@AndyZe
Copy link
Member

AndyZe commented Feb 3, 2021

Small numerical errors are throwing off the unit test, for example:

Expected equality of these values:&#x0A; orig_delta_theta(i)&#x0A; Which is: -0.012999999999999999&#x0A; delta_theta(i)&#x0A; Which is: -0.012293478260869564"

Prob need to use EXPECT_NEAR (expected, actual, absolute_range)

@AndyZe
Copy link
Member

AndyZe commented Feb 3, 2021

I'll push a commit to do that with, say, 1e-3 tolderance

@AndyZe AndyZe force-pushed the pr-refactor-servo-velocity-bounds branch from 8e58e45 to fb5b233 Compare February 3, 2021 20:46
Copy link
Member

@henningkayser henningkayser left a comment

Choose a reason for hiding this comment

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

This looks much cleaner than the previous implementation.

@AndyZe AndyZe merged commit 8878682 into moveit:master Feb 4, 2021
@tylerjw tylerjw mentioned this pull request Apr 9, 2021
tylerjw pushed a commit to tylerjw/moveit that referenced this pull request Apr 29, 2021
* Refactor/fix Servo enforceVelLimits function to utilize joint velocity limits correctly.

* Use uniform scaling factor instead of joint specific velocity bounds.

* Move joint increment outside of if()

* Add friend test for velocity limits

* Clean up debug code, clang format

* Test for negative velocities, too

* Apply velocity scaling correctly and skip clamping zero velocities.

* Improve enforceVelLimits unit tests.

* Use EXPECT_NEAR in the new unit test

Co-authored-by: AndyZe <andyz@utexas.edu>
@tylerjw tylerjw mentioned this pull request Apr 29, 2021
tylerjw pushed a commit to tylerjw/moveit that referenced this pull request May 3, 2021
* Refactor/fix Servo enforceVelLimits function to utilize joint velocity limits correctly.

* Use uniform scaling factor instead of joint specific velocity bounds.

* Move joint increment outside of if()

* Add friend test for velocity limits

* Clean up debug code, clang format

* Test for negative velocities, too

* Apply velocity scaling correctly and skip clamping zero velocities.

* Improve enforceVelLimits unit tests.

* Use EXPECT_NEAR in the new unit test

Co-authored-by: AndyZe <andyz@utexas.edu>
tylerjw pushed a commit that referenced this pull request May 3, 2021
* Refactor/fix Servo enforceVelLimits function to utilize joint velocity limits correctly.

* Use uniform scaling factor instead of joint specific velocity bounds.

* Move joint increment outside of if()

* Add friend test for velocity limits

* Clean up debug code, clang format

* Test for negative velocities, too

* Apply velocity scaling correctly and skip clamping zero velocities.

* Improve enforceVelLimits unit tests.

* Use EXPECT_NEAR in the new unit test

Co-authored-by: AndyZe <andyz@utexas.edu>
v4hn added a commit to v4hn/moveit that referenced this pull request Jun 30, 2021
See moveit/moveit2#526 for MoveIt2 patch.

moveit#2471 was not sufficiently reviewed.
sjahr pushed a commit to sjahr/moveit that referenced this pull request Jun 21, 2024
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

4 participants