Skip to content

Conversation

@Ishan1923
Copy link

@Ishan1923 Ishan1923 commented Feb 7, 2026

This PR resolves real-time safety violations in the admittance_controller.

Previously, the update_and_write_commands() loop was creating new geometry_msgs objects and calling get_node()->get_logger() every cycle. These operations involve dynamic memory allocation and potential mutex locking, which are not real-time safe.

Changes:

  • Member Variables: Promoted offsetted_ft_values to a private class member (offsetted_ft_values_) to ensure zero-copy/zero-allocation during runtime.
  • Cached Logger: Added logger_ as a class member initialized in on_init() to avoid retrieving the logger node-side in the update loop.
  • Helper Optimization: Refactored the add_wrenches() helper function to use output references (void return) instead of returning objects by value.
  • Cleanup: Removed a duplicate check/assignment of wrench_command_msg_ inside the update loop.

Testing & Verification

I have verified that these changes maintain strict backward compatibility and pass all existing tests.

  • Build: Passed (colcon build --packages-up-to admittance_controller)
  • Tests: Passed 100% (colcon test --packages-select admittance_controller) - 38/38 tests passed.

Fixes #2130

Copy link
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.

The dynamic allocation part is already being handled in this PR: #2150

private:
geometry_msgs::msg::Wrench offsetted_ft_values_;
rclcpp::Logger logger_ = rclcpp::get_logger("AdmittanceController");
};
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is needed. Wrench has no dynamically allocated variables

Copy link
Author

Choose a reason for hiding this comment

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

Currently geometry_msgs::msg::Wrench objects are being created on the stack on every cycle in the update while they don't allocate heap memory, promoting them to members, avoid unnecessary stack construction/destruction overhead in the RT loop.

Comment on lines +400 to +409
auto & dst = state_message_.ft_sensor_frame.data;
const auto & src = admittance_state_.ft_sensor_frame;
if (src.size() <= dst.capacity())
{
dst.assign(src.c_str(), src.size());
}
else
{
dst.assign(src.c_str(), dst.capacity()); // truncate to reserved size
}
Copy link
Member

Choose a reason for hiding this comment

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

Why this approach¿?

if (!success)
{
RCLCPP_WARN(logger, "Error while setting command for joint %zu.", joint_ind);
RCLCPP_WARN(logger_, "Error while setting command for joint %zu.", joint_ind);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
RCLCPP_WARN(logger_, "Error while setting command for joint %zu.", joint_ind);
RCLCPP_WARN(get_node()->get_logger(), "Error while setting command for joint %zu.", joint_ind);

Copy link
Author

Choose a reason for hiding this comment

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

I thought caching the logger would be a better choice; as normally, calling get_node()->get_logger() will involve pointer derefrencing with potentially, internel mutexes/allocations in rclcpp

@Ishan1923
Copy link
Author

The dynamic allocation part is already being handled in this PR: #2150

@saikishor Thanks for the review.

I agree that #2150 is the cleaner solution for the string allocation issue since it handles it in the configuration phase.

Regarding the other optimizations (cached logger and stack allocation): I see your points that the overhead is likely negligible. Since the critical real-time violation is being fixed in #2150, I will close this PR.

@Ishan1923 Ishan1923 closed this Feb 9, 2026
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.

[admittance_controller] Real-time safety violation: Dynamic allocation in update loop

2 participants