Skip to content

Increase PID ROS wrapper test coverage #484

Merged
christophfroehlich merged 12 commits intoros-controls:ros2-masterfrom
Theonewhomadethings:409-PID-test-coverage
Sep 26, 2025
Merged

Increase PID ROS wrapper test coverage #484
christophfroehlich merged 12 commits intoros-controls:ros2-masterfrom
Theonewhomadethings:409-PID-test-coverage

Conversation

@Theonewhomadethings
Copy link
Copy Markdown
Contributor

Overview

This PR increases test coverage for control_toolbox::PidROS, focusing on methods that previously had little or no coverage. (as described in #409)

  • void PidROS::reset()
  • void PidROS::reset(bool save_i_term)
  • double PidROS::compute_command(double error, double error_dot, const rclcpp::Duration & dt)
  • void PidROS::set_current_cmd(double cmd) { pid_.set_current_cmd(cmd); }
  • double PidROS::get_current_cmd() { return pid_.get_current_cmd(); }
  • void PidROS::print_values()

What changed

files changed are:

  • control_toolbox/control_toolbox/test/pid_ros_publisher_tests.cpp
  • control_toolbox/control_toolbox/test/pid_ros_parameters_tests.cpp

Added 6 tests to cover these methods in the below files

In control_toolbox/test/pid_ros_parameters_tests.cpp, we added five tests: two I-only tests that check reset() and reset(bool) either clear or keep the integral term; one PI test that calls print_values() and captures the log output; and two P-only tests that cover get_current_cmd() and set_current_cmd(), using tight limits so the results are predictable.

In control_toolbox/test/pid_ros_publisher_tests.cpp, we added one test that runs the publisher end-to-end on a LifecycleNode and confirms a PidState message is published.

Before every commit I made sure to build the package locally, run colcon test and do pre-commit commands. All tests passed and no formatting was flagged.

Notes for Reviewer

This is my first contribution to ros2_control, so I am very open to feedback and am happy to make any changes that are requested.

I can remove comments after a initial review has been made?

Adds a new test in pid_ros_parameters_tests.cpp that verifies PidROS::reset()
honors the save_i_term ROS parameter. Using an integral-only controller makes the
effect of retaining vs. clearing the I-term directly observable without P/D noise.
The test confirms the integral is cleared when false and retained when true.
…term for PidROS::reset() and added new test for reset(bool save_i_term)

Reworked the test for the void PidROS::reset() that had a mistake to use an I-only controller initialized via initialize_from_args with large finite clamps and to snapshot the integral state using a zero-error compute_command(0.0, dt) so assertions match the controller’s step semantics. The test now explicitly toggles the save_i_term ROS parameter and verifies that reset() clears or retains the I-term accordingly.

Added a new test to cover the void PidROS::reset(bool save_i_term) method, asserting that reset(false) clears and reset(true) preserves the integral state using the same snapshot approach.
… that verifies the overload honors the supplied error_dot and publishes a consistent PidState. Using gains P=1, I=0, D=1 and wide output limits, the test expects cmd = e + e_dot, subscribes to /pid_state, and asserts the message’s error, timestep (converted to seconds), and output match the compute step. Test is placed in pid_ros_publisher_tests.cpp
… and verifies key fields are printed (gains, limits, anti-windup info, errors, command) without brittle formatting assumptions. The test initializes a deterministic PID state, avoids saturation effects, and ensures INFO output from the dedicated logger is correctly emitted.
Add a GTest that verifies PidROS::get_current_cmd() returns the current command across scenarios: default is 0 after init, it round-trips values set via set_current_cmd(), and it reflects commands produced by compute_command() (including saturation to ±5). Also sanity-checks a large finite value to ensure simple delegation to the underlying PID is robust.
It verifies round-trip behavior (set_current_cmd to get_current_cmd) for positive, negative, and large finite values, then confirms compute_command() remains stable (saturates as expected) and get_current_cmd() reflects the last computed output.
@Theonewhomadethings Theonewhomadethings changed the title Increase PID ROS wrapper test coverage (https://github.com/ros-controls/control_toolbox/issues/409) Increase PID ROS wrapper test coverage Sep 21, 2025
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 22, 2025

Codecov Report

❌ Patch coverage is 96.37681% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.77%. Comparing base (6a28f23) to head (83079f7).

Files with missing lines Patch % Lines
control_toolbox/test/pid_ros_parameters_tests.cpp 96.73% 2 Missing and 1 partial ⚠️
control_toolbox/test/pid_ros_publisher_tests.cpp 95.65% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           ros2-master     #484      +/-   ##
===============================================
+ Coverage        78.90%   80.77%   +1.86%     
===============================================
  Files               29       29              
  Lines             1896     2023     +127     
  Branches           119      124       +5     
===============================================
+ Hits              1496     1634     +138     
+ Misses             332      320      -12     
- Partials            68       69       +1     
Flag Coverage Δ
unittests 80.77% <96.37%> (+1.86%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
control_toolbox/test/pid_ros_publisher_tests.cpp 94.44% <95.65%> (ø)
control_toolbox/test/pid_ros_parameters_tests.cpp 99.00% <96.73%> (-1.00%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread control_toolbox/test/pid_ros_parameters_tests.cpp Outdated
Comment thread control_toolbox/test/pid_ros_parameters_tests.cpp Outdated
Comment thread control_toolbox/test/pid_ros_parameters_tests.cpp
Comment thread control_toolbox/test/pid_ros_parameters_tests.cpp Outdated
Comment thread control_toolbox/test/pid_ros_parameters_tests.cpp Outdated
@Theonewhomadethings
Copy link
Copy Markdown
Contributor Author

Ready for re-review @saikishor I believe I have addressed all your initial feedback

Copy link
Copy Markdown
Member

@christophfroehlich christophfroehlich left a comment

Choose a reason for hiding this comment

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

Overall LGTM, some minor comments

Comment thread control_toolbox/test/pid_ros_publisher_tests.cpp Outdated
Comment thread control_toolbox/test/pid_ros_publisher_tests.cpp
…r and executor.spin_some() in publisher test. This removes the deprecation warning and aligns with the guidance in ros-controls#488.
…ack fired, the received PidState is non-null, and its output matches the computed command.
Copy link
Copy Markdown
Member

@saikishor saikishor left a comment

Choose a reason for hiding this comment

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

Looks fine to me

Copy link
Copy Markdown
Member

@christophfroehlich christophfroehlich left a comment

Choose a reason for hiding this comment

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

Lgtm, thanks!

@christophfroehlich christophfroehlich added the backport-jazzy Triggers PR backport to ROS 2 jazzy. label Sep 26, 2025
@christophfroehlich christophfroehlich merged commit edeb343 into ros-controls:ros2-master Sep 26, 2025
25 checks passed
mergify Bot pushed a commit that referenced this pull request Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-jazzy Triggers PR backport to ROS 2 jazzy.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants