ros2_control - rolling
Loading...
Searching...
No Matches
joint_limiter_interface.hpp
1// Copyright (c) 2024, Stogl Robotics Consulting UG (haftungsbeschränkt)
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#ifndef JOINT_LIMITS__JOINT_LIMITER_INTERFACE_HPP_
18#define JOINT_LIMITS__JOINT_LIMITER_INTERFACE_HPP_
19
20#include <cstddef>
21#include <string>
22#include <vector>
23
24#include "joint_limits/joint_limits.hpp"
25#include "joint_limits/joint_limits_rosparam.hpp"
26#include "rcl_interfaces/msg/set_parameters_result.hpp"
27#include "rclcpp/duration.hpp"
28#include "rclcpp/logging.hpp"
29#include "rclcpp/node.hpp"
30#include "rclcpp/parameter.hpp"
31#include "rclcpp_lifecycle/lifecycle_node.hpp"
32#include "realtime_tools/realtime_thread_safe_box.hpp"
33
34namespace joint_limits
35{
36
37template <typename JointLimitsStateDataType>
38class JointLimiterInterface
39{
40public:
41 JointLimiterInterface() = default;
42
43 virtual ~JointLimiterInterface() = default;
44
59 virtual bool init(
60 const std::vector<std::string> & joint_names,
61 const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & param_itf,
62 const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr & logging_itf,
63 const std::string & robot_description_topic = "/robot_description")
64 {
65 number_of_joints_ = joint_names.size();
66 joint_names_ = joint_names;
67 joint_limits_.resize(number_of_joints_);
68 node_param_itf_ = param_itf;
69 node_logging_itf_ = logging_itf;
70
71 bool result = true;
72
73 // TODO(destogl): get limits from URDF
74
76 {
77 for (size_t i = 0; i < number_of_joints_; ++i)
78 {
79 if (!declare_parameters(joint_names[i], node_param_itf_, node_logging_itf_))
80 {
81 RCLCPP_ERROR(
82 node_logging_itf_->get_logger(),
83 "JointLimiter: Joint '%s': parameter declaration has failed", joint_names[i].c_str());
84 result = false;
85 break;
86 }
87 if (!get_joint_limits(joint_names[i], node_param_itf_, node_logging_itf_, joint_limits_[i]))
88 {
89 RCLCPP_ERROR(
90 node_logging_itf_->get_logger(),
91 "JointLimiter: Joint '%s': getting parameters has failed", joint_names[i].c_str());
92 result = false;
93 break;
94 }
95 RCLCPP_INFO(
96 node_logging_itf_->get_logger(), "Limits for joint %zu (%s) are:\n%s", i,
97 joint_names[i].c_str(), joint_limits_[i].to_string().c_str());
98 }
99 updated_limits_.set(joint_limits_);
100
101 auto on_parameter_event_callback = [this](const std::vector<rclcpp::Parameter> & parameters)
102 {
103 rcl_interfaces::msg::SetParametersResult set_parameters_result;
104 set_parameters_result.successful = true;
105
106 std::vector<joint_limits::JointLimits> updated_joint_limits = joint_limits_;
107 bool changed = false;
108
109 for (size_t i = 0; i < number_of_joints_; ++i)
110 {
112 joint_names_[i], parameters, node_logging_itf_, updated_joint_limits[i]);
113 }
114
115 if (changed)
116 {
117 updated_limits_.set(updated_joint_limits);
118 RCLCPP_INFO(node_logging_itf_->get_logger(), "Limits are dynamically updated!");
119 }
120
121 return set_parameters_result;
122 };
123
124 parameter_callback_ =
125 node_param_itf_->add_on_set_parameters_callback(on_parameter_event_callback);
126 }
127
128 if (result)
129 {
130 result = on_init();
131 }
132
133 (void)robot_description_topic; // avoid linters output
134
135 return result;
136 }
137
141 virtual bool init(
142 const std::vector<std::string> & joint_names,
143 const std::vector<joint_limits::JointLimits> & joint_limits,
144 const std::vector<joint_limits::SoftJointLimits> & soft_joint_limits,
145 const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & param_itf,
146 const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr & logging_itf)
147 {
148 number_of_joints_ = joint_names.size();
149 joint_names_ = joint_names;
150 joint_limits_ = joint_limits;
151 soft_joint_limits_ = soft_joint_limits;
152 node_param_itf_ = param_itf;
153 node_logging_itf_ = logging_itf;
154 updated_limits_.set(joint_limits_);
155
156 if ((number_of_joints_ != joint_limits_.size()) && has_logging_interface())
157 {
158 RCLCPP_ERROR(
159 node_logging_itf_->get_logger(),
160 "JointLimiter: Number of joint names and limits do not match: %zu != %zu",
161 number_of_joints_, joint_limits_.size());
162 }
163 return (number_of_joints_ == joint_limits_.size()) && on_init();
164 }
165
171 virtual bool init(
172 const std::vector<std::string> & joint_names, const rclcpp::Node::SharedPtr & node,
173 const std::string & robot_description_topic = "/robot_description")
174 {
175 return init(
176 joint_names, node->get_node_parameters_interface(), node->get_node_logging_interface(),
177 robot_description_topic);
178 }
179
185 virtual bool init(
186 const std::vector<std::string> & joint_names,
187 const rclcpp_lifecycle::LifecycleNode::SharedPtr & lifecycle_node,
188 const std::string & robot_description_topic = "/robot_description")
189 {
190 return init(
191 joint_names, lifecycle_node->get_node_parameters_interface(),
192 lifecycle_node->get_node_logging_interface(), robot_description_topic);
193 }
194
195 virtual bool configure(const JointLimitsStateDataType & current_joint_states)
196 {
197 return on_configure(current_joint_states);
198 }
199
210 virtual bool enforce(
211 const JointLimitsStateDataType & current_joint_states,
212 JointLimitsStateDataType & desired_joint_states, const rclcpp::Duration & dt)
213 {
214 auto joint_limits_op = updated_limits_.try_get();
215 if (joint_limits_op.has_value())
216 {
217 joint_limits_ = joint_limits_op.value();
218 return on_enforce(current_joint_states, desired_joint_states, dt);
219 }
220 else
221 {
222 return false;
223 }
224 }
225
226 virtual void reset_internals() = 0;
227
228protected:
235 virtual bool on_init() = 0;
236
243 virtual bool on_configure(const JointLimitsStateDataType & current_joint_states) = 0;
244
256 virtual bool on_enforce(
257 const JointLimitsStateDataType & current_joint_states,
258 JointLimitsStateDataType & desired_joint_states, const rclcpp::Duration & dt) = 0;
259
268 bool has_logging_interface() const { return node_logging_itf_ != nullptr; }
269
278 bool has_parameter_interface() const { return node_param_itf_ != nullptr; }
279
280 size_t number_of_joints_;
281 std::vector<std::string> joint_names_;
282 std::vector<joint_limits::JointLimits> joint_limits_;
283 std::vector<joint_limits::SoftJointLimits> soft_joint_limits_;
284 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_param_itf_;
285 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_itf_;
286
287private:
288 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr parameter_callback_;
290};
291
292} // namespace joint_limits
293
294#endif // JOINT_LIMITS__JOINT_LIMITER_INTERFACE_HPP_
virtual bool on_init()=0
Initialize the limiter's internal states and libraries.
virtual bool init(const std::vector< std::string > &joint_names, const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &param_itf, const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr &logging_itf, const std::string &robot_description_topic="/robot_description")
Initialize every JointLimiter.
Definition joint_limiter_interface.hpp:59
bool has_logging_interface() const
Definition joint_limiter_interface.hpp:268
bool has_parameter_interface() const
Checks if the parameter interface is set.
Definition joint_limiter_interface.hpp:278
virtual bool on_enforce(const JointLimitsStateDataType &current_joint_states, JointLimitsStateDataType &desired_joint_states, const rclcpp::Duration &dt)=0
Enforce joint limits for multiple dependent physical quantities.
virtual bool enforce(const JointLimitsStateDataType &current_joint_states, JointLimitsStateDataType &desired_joint_states, const rclcpp::Duration &dt)
Enforce joint limits to desired joint state for multiple physical quantities.
Definition joint_limiter_interface.hpp:210
virtual bool on_configure(const JointLimitsStateDataType &current_joint_states)=0
Configure the limiter's internal states and libraries.
virtual bool init(const std::vector< std::string > &joint_names, const rclcpp_lifecycle::LifecycleNode::SharedPtr &lifecycle_node, const std::string &robot_description_topic="/robot_description")
Initialize joints using a LifecycleNode pointer.
Definition joint_limiter_interface.hpp:185
virtual bool init(const std::vector< std::string > &joint_names, const rclcpp::Node::SharedPtr &node, const std::string &robot_description_topic="/robot_description")
Initialize joints using a Node pointer.
Definition joint_limiter_interface.hpp:171
virtual bool init(const std::vector< std::string > &joint_names, const std::vector< joint_limits::JointLimits > &joint_limits, const std::vector< joint_limits::SoftJointLimits > &soft_joint_limits, const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &param_itf, const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr &logging_itf)
Initialize joints from directly provided names and limits.
Definition joint_limiter_interface.hpp:141
Definition realtime_thread_safe_box.hpp:68
Definition data_structures.hpp:38
bool check_for_limits_update(const std::string &joint_name, const std::vector< rclcpp::Parameter > &parameters, const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr &logging_itf, JointLimits &updated_limits)
Check if any of updated parameters are related to JointLimits.
Definition joint_limits_rosparam.hpp:445
bool declare_parameters(const std::string &joint_name, const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &param_itf, const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr &logging_itf)
Declare JointLimits and SoftJointLimits parameters for joint with joint_name using node parameters in...
Definition joint_limits_rosparam.hpp:94
bool get_joint_limits(const std::string &joint_name, const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &param_itf, const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr &logging_itf, JointLimits &limits)
Populate a JointLimits instance from the node parameters.
Definition joint_limits_rosparam.hpp:237