Skip to content

Commit c431464

Browse files
author
Felix Exner
committed
Use ptr-safe RealTimeBoxBestEffort
the RealTimeBox used before is not really real-time safe and the way it was implemented there was unnecessary data allocation in both, the activate method and the service callback. Using the RealTimeBoxBestEffort makes allocating additional memory unnecessary and makes things really thread-safe.
1 parent 769f476 commit c431464

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

ur_controllers/include/ur_controllers/ur_configuration_controller.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
#ifndef UR_CONTROLLERS__UR_CONFIGURATION_CONTROLLER_HPP_
4242
#define UR_CONTROLLERS__UR_CONFIGURATION_CONTROLLER_HPP_
4343

44-
#include <realtime_tools/realtime_box.h>
44+
// TODO(fmauch): Currently, the realtime_box_best_effort doesn't include this
45+
#include <functional>
46+
#include <realtime_tools/realtime_box_best_effort.h> // NOLINT
4547

4648
#include <memory>
4749

@@ -86,7 +88,9 @@ class URConfigurationController : public controller_interface::ControllerInterfa
8688
CallbackReturn on_init() override;
8789

8890
private:
89-
realtime_tools::RealtimeBox<std::shared_ptr<VersionInformation>> robot_software_version_;
91+
realtime_tools::RealtimeBoxBestEffort<std::shared_ptr<VersionInformation>> robot_software_version_{
92+
std::make_shared<VersionInformation>()
93+
};
9094

9195
rclcpp::Service<ur_msgs::srv::GetRobotSoftwareVersion>::SharedPtr get_robot_software_version_srv_;
9296

ur_controllers/src/ur_configuration_controller.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ controller_interface::return_type URConfigurationController::update(const rclcpp
9494
controller_interface::CallbackReturn
9595
URConfigurationController::on_activate(const rclcpp_lifecycle::State& /* previous_state */)
9696
{
97-
VersionInformation temp;
98-
temp.major = static_cast<uint32_t>(state_interfaces_[StateInterfaces::ROBOT_VERSION_MAJOR].get_value());
99-
temp.minor = static_cast<uint32_t>(state_interfaces_[StateInterfaces::ROBOT_VERSION_MINOR].get_value());
100-
temp.bugfix = static_cast<uint32_t>(state_interfaces_[StateInterfaces::ROBOT_VERSION_BUGFIX].get_value());
101-
temp.build = static_cast<uint32_t>(state_interfaces_[StateInterfaces::ROBOT_VERSION_BUILD].get_value());
102-
103-
robot_software_version_.set(std::make_shared<VersionInformation>(temp));
97+
robot_software_version_.set([this](const std::shared_ptr<VersionInformation> ptr) {
98+
ptr->major = state_interfaces_[StateInterfaces::ROBOT_VERSION_MAJOR].get_value();
99+
ptr->minor = state_interfaces_[StateInterfaces::ROBOT_VERSION_MINOR].get_value();
100+
ptr->build = state_interfaces_[StateInterfaces::ROBOT_VERSION_BUILD].get_value();
101+
ptr->bugfix = state_interfaces_[StateInterfaces::ROBOT_VERSION_BUGFIX].get_value();
102+
});
104103
return controller_interface::CallbackReturn::SUCCESS;
105104
}
106105

@@ -115,13 +114,12 @@ bool URConfigurationController::getRobotSoftwareVersion(
115114
ur_msgs::srv::GetRobotSoftwareVersion::Response::SharedPtr resp)
116115
{
117116
std::shared_ptr<VersionInformation> temp;
118-
robot_software_version_.get(temp);
119-
resp->major = temp->major;
120-
resp->minor = temp->minor;
121-
resp->bugfix = temp->bugfix;
122-
resp->build = temp->build;
123-
124-
return true;
117+
return robot_software_version_.tryGet([resp](const std::shared_ptr<VersionInformation> ptr) {
118+
resp->major = ptr->major;
119+
resp->minor = ptr->minor;
120+
resp->build = ptr->build;
121+
resp->bugfix = ptr->bugfix;
122+
});
125123
}
126124
} // namespace ur_controllers
127125

0 commit comments

Comments
 (0)