Skip to content

feat(hardware_interface): implement dynamic command toggling for MockHardware #3241

Open
shlok-mehndiratta wants to merge 1 commit intoros-controls:masterfrom
shlok-mehndiratta:issue-3214-MockHardware-service
Open

feat(hardware_interface): implement dynamic command toggling for MockHardware #3241
shlok-mehndiratta wants to merge 1 commit intoros-controls:masterfrom
shlok-mehndiratta:issue-3214-MockHardware-service

Conversation

@shlok-mehndiratta
Copy link
Copy Markdown
Contributor

Closes #3214

While working on some controller tests, I realized that the GenericSystem mock always mirrors commands to states perfectly. This makes it hard to test how high-level controllers or state machines react when the hardware actually stops responding or gets stuck (e.g., communication loss or a physical state-lock).

This PR adds a ~/set_command_propagation service (using std_srvs/SetBool) to the GenericSystem mock. It lets you manually "freeze" the command propagation at runtime:

  • data: false: The mock stops updating its state and stays at its last value, ignoring new commands.
  • data: true: Resumes normal mirroring.

Implementation Notes

  • Thread Safety: I used a std::atomic<bool> with memory_order_acquire/release for the toggle. This keeps the real-time read() loop lock-free and safe from the asynchronous service thread without adding mutex overhead.
  • Scope: This is strictly for the mock_components/GenericSystem plugin. I haven't tested this on physical hardware since it's a simulation-only feature, and it shouldn't have any side effects on real drivers.
  • Testing: I've updated the TestableResourceManager helper to support injecting an executor, which allowed me to verify the service logic in a standard unit test.

I've verified this both with a new test case (toggle_command_propagation_service) and manually with ros2 service call. All 29 existing tests pass.

Hope this helps anyone else trying to simulate hardware gremlins in their tests!

Verification Screenshot

(I'll attach a screenshot here showing the join states freezing after the service call)

Screenshot from 2026-04-22 02-12-43

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 75.86207% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.06%. Comparing base (c178e16) to head (d0d5b7d).

Files with missing lines Patch % Lines
...rface/test/mock_components/test_generic_system.cpp 71.73% 10 Missing and 3 partials ⚠️
...e_interface/src/mock_components/generic_system.cpp 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3241      +/-   ##
==========================================
- Coverage   89.10%   89.06%   -0.04%     
==========================================
  Files         160      160              
  Lines       19753    19821      +68     
  Branches     1597     1601       +4     
==========================================
+ Hits        17600    17653      +53     
- Misses       1486     1498      +12     
- Partials      667      670       +3     
Flag Coverage Δ
unittests 89.06% <75.86%> (-0.04%) ⬇️

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

Files with missing lines Coverage Δ
...terface/include/mock_components/generic_system.hpp 100.00% <ø> (ø)
...e_interface/src/mock_components/generic_system.cpp 81.18% <91.66%> (+0.26%) ⬆️
...rface/test/mock_components/test_generic_system.cpp 97.00% <71.73%> (-2.76%) ⬇️

... and 12 files with indirect coverage changes

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

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.

Add a ROS service to MockHardware to disable commands

1 participant