ros2_control - rolling
Loading...
Searching...
No Matches
tolerances.hpp
1// Copyright 2013 PAL Robotics S.L.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9//
10// * Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13//
14// * Neither the name of the PAL Robotics S.L. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28// POSSIBILITY OF SUCH DAMAGE.
29
31
32#ifndef JOINT_TRAJECTORY_CONTROLLER__TOLERANCES_HPP_
33#define JOINT_TRAJECTORY_CONTROLLER__TOLERANCES_HPP_
34
35#include <limits>
36#include <stdexcept>
37#include <string>
38#include <vector>
39
40#include "control_msgs/action/follow_joint_trajectory.hpp"
41#include "joint_trajectory_controller/joint_trajectory_controller_parameters.hpp"
42
44{
51{
52 double position = 0.0;
53 double velocity = 0.0;
54 double acceleration = 0.0;
55};
56
61{
62 explicit SegmentTolerances(size_t size = 0) : state_tolerance(size), goal_state_tolerance(size) {}
63
65 std::vector<StateTolerances> state_tolerance;
66
68 std::vector<StateTolerances> goal_state_tolerance;
69
71 double goal_time_tolerance = 0.0;
72};
73
95SegmentTolerances get_segment_tolerances(rclcpp::Logger & jtc_logger, const Params & params)
96{
97 auto const & constraints = params.constraints;
98 auto const n_joints = params.joints.size();
99
100 SegmentTolerances tolerances;
101 // Negative goal_time means wait indefinitely for the goal to be reached
102 tolerances.goal_time_tolerance =
103 constraints.goal_time < 0.0 ? std::numeric_limits<double>::infinity() : constraints.goal_time;
104 static auto logger = jtc_logger.get_child("tolerance");
105 RCLCPP_DEBUG(logger, "goal_time %f", constraints.goal_time);
106
107 // State and goal state tolerances
108 tolerances.state_tolerance.resize(n_joints);
109 tolerances.goal_state_tolerance.resize(n_joints);
110 for (size_t i = 0; i < n_joints; ++i)
111 {
112 auto const joint = params.joints[i];
113 tolerances.state_tolerance[i].position = constraints.joints_map.at(joint).trajectory;
114 tolerances.goal_state_tolerance[i].position = constraints.joints_map.at(joint).goal;
115 tolerances.goal_state_tolerance[i].velocity = constraints.stopped_velocity_tolerance;
116
117 RCLCPP_DEBUG(
118 logger, "%s %f", (joint + ".trajectory.position").c_str(),
119 tolerances.state_tolerance[i].position);
120 RCLCPP_DEBUG(
121 logger, "%s %f", (joint + ".goal.position").c_str(),
122 tolerances.goal_state_tolerance[i].position);
123 RCLCPP_DEBUG(
124 logger, "%s %f", (joint + ".goal.velocity").c_str(),
125 tolerances.goal_state_tolerance[i].velocity);
126 }
127
128 return tolerances;
129}
130
131double resolve_tolerance_source(const double default_value, const double goal_value)
132{
133 // from
134 // https://github.com/ros-controls/control_msgs/blob/master/control_msgs/msg/JointTolerance.msg
135 // There are two special values for tolerances:
136 // * 0 - The tolerance is unspecified and will remain at whatever the default is
137 // * -1 - The tolerance is "erased".
138 // If there was a default, the joint will be allowed to move without restriction.
139 constexpr double ERASE_VALUE = -1.0;
140 auto is_erase_value = [=](double value)
141 { return fabs(value - ERASE_VALUE) < std::numeric_limits<float>::epsilon(); };
142
143 if (goal_value > 0.0)
144 {
145 return goal_value;
146 }
147 else if (is_erase_value(goal_value))
148 {
149 return 0.0;
150 }
151 else if (goal_value < 0.0)
152 {
153 throw std::runtime_error("Illegal tolerance value.");
154 }
155 return default_value;
156}
157
168 rclcpp::Logger & jtc_logger, const SegmentTolerances & default_tolerances,
169 const control_msgs::action::FollowJointTrajectory::Goal & goal,
170 const std::vector<std::string> & joints)
171{
172 SegmentTolerances active_tolerances(default_tolerances);
173 static auto logger = jtc_logger.get_child("tolerance");
174
175 try
176 {
177 active_tolerances.goal_time_tolerance = resolve_tolerance_source(
178 default_tolerances.goal_time_tolerance, rclcpp::Duration(goal.goal_time_tolerance).seconds());
179 }
180 catch (const std::runtime_error & e)
181 {
182 RCLCPP_ERROR(
183 logger, "Specified illegal goal_time_tolerance: %f. Using default tolerances",
184 rclcpp::Duration(goal.goal_time_tolerance).seconds());
185 return default_tolerances;
186 }
187 RCLCPP_DEBUG(logger, "%s %f", "goal_time", active_tolerances.goal_time_tolerance);
188
189 // State and goal state tolerances
190 for (auto joint_tol : goal.path_tolerance)
191 {
192 auto const joint = joint_tol.name;
193 // map joint names from goal to active_tolerances
194 auto it = std::find(joints.begin(), joints.end(), joint);
195 if (it == joints.end())
196 {
197 RCLCPP_ERROR(
198 logger, "%s",
199 ("joint '" + joint +
200 "' specified in goal.path_tolerance does not exist. "
201 "Using default tolerances.")
202 .c_str());
203 return default_tolerances;
204 }
205 auto i = static_cast<size_t>(std::distance(joints.cbegin(), it));
206 std::string interface = "";
207 try
208 {
209 interface = "position";
210 active_tolerances.state_tolerance[i].position = resolve_tolerance_source(
211 default_tolerances.state_tolerance[i].position, joint_tol.position);
212 interface = "velocity";
213 active_tolerances.state_tolerance[i].velocity = resolve_tolerance_source(
214 default_tolerances.state_tolerance[i].velocity, joint_tol.velocity);
215 interface = "acceleration";
216 active_tolerances.state_tolerance[i].acceleration = resolve_tolerance_source(
217 default_tolerances.state_tolerance[i].acceleration, joint_tol.acceleration);
218 }
219 catch (const std::runtime_error & e)
220 {
221 RCLCPP_ERROR(
222 logger,
223 "joint '%s' specified in goal.path_tolerance has a invalid %s tolerance. Using default "
224 "tolerances.",
225 joint.c_str(), interface.c_str());
226 return default_tolerances;
227 }
228
229 RCLCPP_DEBUG(
230 logger, "%s %f", (joint + ".state_tolerance.position").c_str(),
231 active_tolerances.state_tolerance[i].position);
232 RCLCPP_DEBUG(
233 logger, "%s %f", (joint + ".state_tolerance.velocity").c_str(),
234 active_tolerances.state_tolerance[i].velocity);
235 RCLCPP_DEBUG(
236 logger, "%s %f", (joint + ".state_tolerance.acceleration").c_str(),
237 active_tolerances.state_tolerance[i].acceleration);
238 }
239 for (auto goal_tol : goal.goal_tolerance)
240 {
241 auto const joint = goal_tol.name;
242 // map joint names from goal to active_tolerances
243 auto it = std::find(joints.begin(), joints.end(), joint);
244 if (it == joints.end())
245 {
246 RCLCPP_ERROR(
247 logger, "%s",
248 ("joint '" + joint +
249 "' specified in goal.goal_tolerance does not exist. "
250 "Using default tolerances.")
251 .c_str());
252 return default_tolerances;
253 }
254 auto i = static_cast<size_t>(std::distance(joints.cbegin(), it));
255 std::string interface = "";
256 try
257 {
258 interface = "position";
259 active_tolerances.goal_state_tolerance[i].position = resolve_tolerance_source(
260 default_tolerances.goal_state_tolerance[i].position, goal_tol.position);
261 interface = "velocity";
262 active_tolerances.goal_state_tolerance[i].velocity = resolve_tolerance_source(
263 default_tolerances.goal_state_tolerance[i].velocity, goal_tol.velocity);
264 interface = "acceleration";
265 active_tolerances.goal_state_tolerance[i].acceleration = resolve_tolerance_source(
266 default_tolerances.goal_state_tolerance[i].acceleration, goal_tol.acceleration);
267 }
268 catch (const std::runtime_error & e)
269 {
270 RCLCPP_ERROR(
271 logger,
272 "joint '%s' specified in goal.goal_tolerance has a invalid %s tolerance. Using default "
273 "tolerances.",
274 joint.c_str(), interface.c_str());
275 return default_tolerances;
276 }
277
278 RCLCPP_DEBUG(
279 logger, "%s %f", (joint + ".goal_state_tolerance.position").c_str(),
280 active_tolerances.goal_state_tolerance[i].position);
281 RCLCPP_DEBUG(
282 logger, "%s %f", (joint + ".goal_state_tolerance.velocity").c_str(),
283 active_tolerances.goal_state_tolerance[i].velocity);
284 RCLCPP_DEBUG(
285 logger, "%s %f", (joint + ".goal_state_tolerance.acceleration").c_str(),
286 active_tolerances.goal_state_tolerance[i].acceleration);
287 }
288
289 return active_tolerances;
290}
291
299inline bool check_state_tolerance_per_joint(
300 const trajectory_msgs::msg::JointTrajectoryPoint & state_error, size_t joint_idx,
301 const StateTolerances & state_tolerance, bool show_errors = false)
302{
303 using std::abs;
304 const double error_position = state_error.positions[joint_idx];
305 const double error_velocity =
306 state_error.velocities.empty() ? 0.0 : state_error.velocities[joint_idx];
307 const double error_acceleration =
308 state_error.accelerations.empty() ? 0.0 : state_error.accelerations[joint_idx];
309
310 const bool is_valid =
311 !(state_tolerance.position > 0.0 && abs(error_position) > state_tolerance.position) &&
312 !(state_tolerance.velocity > 0.0 && abs(error_velocity) > state_tolerance.velocity) &&
313 !(state_tolerance.acceleration > 0.0 && abs(error_acceleration) > state_tolerance.acceleration);
314
315 if (is_valid)
316 {
317 return true;
318 }
319
320 if (show_errors)
321 {
322 const auto logger = rclcpp::get_logger("tolerances");
323 RCLCPP_ERROR(logger, "State tolerances failed for joint %zu:", joint_idx);
324
325 if (state_tolerance.position > 0.0 && abs(error_position) > state_tolerance.position)
326 {
327 RCLCPP_ERROR(
328 logger, "Position Error: %f, Position Tolerance: %f", error_position,
329 state_tolerance.position);
330 }
331 if (state_tolerance.velocity > 0.0 && abs(error_velocity) > state_tolerance.velocity)
332 {
333 RCLCPP_ERROR(
334 logger, "Velocity Error: %f, Velocity Tolerance: %f", error_velocity,
335 state_tolerance.velocity);
336 }
337 if (
338 state_tolerance.acceleration > 0.0 && abs(error_acceleration) > state_tolerance.acceleration)
339 {
340 RCLCPP_ERROR(
341 logger, "Acceleration Error: %f, Acceleration Tolerance: %f", error_acceleration,
342 state_tolerance.acceleration);
343 }
344 }
345 return false;
346}
347
348} // namespace joint_trajectory_controller
349
350#endif // JOINT_TRAJECTORY_CONTROLLER__TOLERANCES_HPP_
Definition interpolation_methods.hpp:25
SegmentTolerances get_segment_tolerances(rclcpp::Logger &jtc_logger, const Params &params)
Populate trajectory segment tolerances using data from the ROS node.
Definition tolerances.hpp:95
Trajectory segment tolerances.
Definition tolerances.hpp:61
std::vector< StateTolerances > goal_state_tolerance
Definition tolerances.hpp:68
std::vector< StateTolerances > state_tolerance
Definition tolerances.hpp:65
double goal_time_tolerance
Definition tolerances.hpp:71
Trajectory state tolerances for position, velocity and acceleration variables.
Definition tolerances.hpp:51