ros2_control - galactic
Loading...
Searching...
No Matches
joint_limits_rosparam.hpp
1// Copyright 2020 PAL Robotics S.L.
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_INTERFACE__JOINT_LIMITS_ROSPARAM_HPP_
18#define JOINT_LIMITS_INTERFACE__JOINT_LIMITS_ROSPARAM_HPP_
19
20#include <joint_limits_interface/joint_limits.hpp>
21
22#include <rclcpp/rclcpp.hpp>
23
24#include <string>
25
27{
29
61inline bool getJointLimits(
62 const std::string & joint_name, const rclcpp::Node::SharedPtr & node, JointLimits & limits)
63{
64 const std::string param_base_name = "joint_limits." + joint_name;
65 try
66 {
67 if (
68 !node->has_parameter(param_base_name + ".has_position_limits") &&
69 !node->has_parameter(param_base_name + ".min_position") &&
70 !node->has_parameter(param_base_name + ".max_position") &&
71 !node->has_parameter(param_base_name + ".has_velocity_limits") &&
72 !node->has_parameter(param_base_name + ".min_velocity") &&
73 !node->has_parameter(param_base_name + ".max_velocity") &&
74 !node->has_parameter(param_base_name + ".has_acceleration_limits") &&
75 !node->has_parameter(param_base_name + ".max_acceleration") &&
76 !node->has_parameter(param_base_name + ".has_jerk_limits") &&
77 !node->has_parameter(param_base_name + ".max_jerk") &&
78 !node->has_parameter(param_base_name + ".has_effort_limits") &&
79 !node->has_parameter(param_base_name + ".max_effort") &&
80 !node->has_parameter(param_base_name + ".angle_wraparound") &&
81 !node->has_parameter(param_base_name + ".has_soft_limits") &&
82 !node->has_parameter(param_base_name + ".k_position") &&
83 !node->has_parameter(param_base_name + ".k_velocity") &&
84 !node->has_parameter(param_base_name + ".soft_lower_limit") &&
85 !node->has_parameter(param_base_name + ".soft_upper_limit"))
86 {
87 RCLCPP_ERROR(
88 node->get_logger(),
89 "No joint limits specification found for joint '%s' in the parameter server "
90 "(node: %s param name: %s).",
91 joint_name.c_str(), node->get_name(), param_base_name.c_str());
92 return false;
93 }
94 }
95 catch (const std::exception & ex)
96 {
97 RCLCPP_ERROR(node->get_logger(), "%s", ex.what());
98 return false;
99 }
100
101 // Position limits
102 bool has_position_limits = false;
103 if (node->get_parameter(param_base_name + ".has_position_limits", has_position_limits))
104 {
105 if (!has_position_limits)
106 {
107 limits.has_position_limits = false;
108 }
109 double min_pos, max_pos;
110 if (
111 has_position_limits && node->get_parameter(param_base_name + ".min_position", min_pos) &&
112 node->get_parameter(param_base_name + ".max_position", max_pos))
113 {
114 limits.has_position_limits = true;
115 limits.min_position = min_pos;
116 limits.max_position = max_pos;
117 }
118
119 bool angle_wraparound;
120 if (
121 !has_position_limits &&
122 node->get_parameter(param_base_name + ".angle_wraparound", angle_wraparound))
123 {
124 limits.angle_wraparound = angle_wraparound;
125 }
126 }
127
128 // Velocity limits
129 bool has_velocity_limits = false;
130 if (node->get_parameter(param_base_name + ".has_velocity_limits", has_velocity_limits))
131 {
132 if (!has_velocity_limits)
133 {
134 limits.has_velocity_limits = false;
135 }
136 double max_vel;
137 if (has_velocity_limits && node->get_parameter(param_base_name + ".max_velocity", max_vel))
138 {
139 limits.has_velocity_limits = true;
140 limits.max_velocity = max_vel;
141 }
142 }
143
144 // Acceleration limits
145 bool has_acceleration_limits = false;
146 if (node->get_parameter(param_base_name + ".has_acceleration_limits", has_acceleration_limits))
147 {
148 if (!has_acceleration_limits)
149 {
150 limits.has_acceleration_limits = false;
151 }
152 double max_acc;
153 if (
154 has_acceleration_limits &&
155 node->get_parameter(param_base_name + ".max_acceleration", max_acc))
156 {
157 limits.has_acceleration_limits = true;
158 limits.max_acceleration = max_acc;
159 }
160 }
161
162 // Jerk limits
163 bool has_jerk_limits = false;
164 if (node->get_parameter(param_base_name + ".has_jerk_limits", has_jerk_limits))
165 {
166 if (!has_jerk_limits)
167 {
168 limits.has_jerk_limits = false;
169 }
170 double max_jerk;
171 if (has_jerk_limits && node->get_parameter(param_base_name + ".max_jerk", max_jerk))
172 {
173 limits.has_jerk_limits = true;
174 limits.max_jerk = max_jerk;
175 }
176 }
177
178 // Effort limits
179 bool has_effort_limits = false;
180 if (node->get_parameter(param_base_name + ".has_effort_limits", has_effort_limits))
181 {
182 if (!has_effort_limits)
183 {
184 limits.has_effort_limits = false;
185 }
186 double max_effort;
187 if (has_effort_limits && node->get_parameter(param_base_name + ".max_effort", max_effort))
188 {
189 limits.has_effort_limits = true;
190 limits.max_effort = max_effort;
191 }
192 }
193
194 return true;
195}
196
198
220 const std::string & joint_name, const rclcpp::Node::SharedPtr & node,
221 SoftJointLimits & soft_limits)
222{
223 const std::string param_base_name = "joint_limits." + joint_name;
224 try
225 {
226 if (
227 !node->has_parameter(param_base_name + ".has_soft_limits") &&
228 !node->has_parameter(param_base_name + ".k_velocity") &&
229 !node->has_parameter(param_base_name + ".k_position") &&
230 !node->has_parameter(param_base_name + ".soft_lower_limit") &&
231 !node->has_parameter(param_base_name + ".soft_upper_limit"))
232 {
233 RCLCPP_DEBUG(
234 node->get_logger(),
235 "No soft joint limits specification found for joint '%s' in the parameter server "
236 "(node: %s param name: %s).",
237 joint_name.c_str(), node->get_name(), param_base_name.c_str());
238 return false;
239 }
240 }
241 catch (const std::exception & ex)
242 {
243 RCLCPP_ERROR(node->get_logger(), "%s", ex.what());
244 return false;
245 }
246
247 // Override soft limits if complete specification is found
248 bool has_soft_limits;
249 if (node->get_parameter(param_base_name + ".has_soft_limits", has_soft_limits))
250 {
251 if (
252 has_soft_limits && node->has_parameter(param_base_name + ".k_position") &&
253 node->has_parameter(param_base_name + ".k_velocity") &&
254 node->has_parameter(param_base_name + ".soft_lower_limit") &&
255 node->has_parameter(param_base_name + ".soft_upper_limit"))
256 {
257 node->get_parameter(param_base_name + ".k_position", soft_limits.k_position);
258 node->get_parameter(param_base_name + ".k_velocity", soft_limits.k_velocity);
259 node->get_parameter(param_base_name + ".soft_lower_limit", soft_limits.min_position);
260 node->get_parameter(param_base_name + ".soft_upper_limit", soft_limits.max_position);
261 return true;
262 }
263 }
264
265 return false;
266}
267
268} // namespace joint_limits_interface
269
270#endif // JOINT_LIMITS_INTERFACE__JOINT_LIMITS_ROSPARAM_HPP_
Definition joint_limits.hpp:21
bool getJointLimits(const std::string &joint_name, const rclcpp::Node::SharedPtr &node, JointLimits &limits)
Populate a JointLimits instance from the ROS parameter server.
Definition joint_limits_rosparam.hpp:61
bool getSoftJointLimits(const std::string &joint_name, const rclcpp::Node::SharedPtr &node, SoftJointLimits &soft_limits)
Populate a SoftJointLimits instance from the ROS parameter server.
Definition joint_limits_rosparam.hpp:219
Definition joint_limits.hpp:23
Definition joint_limits.hpp:56