97 auto const & constraints = params.constraints;
98 auto const n_joints = params.joints.size();
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);
110 for (
size_t i = 0; i < n_joints; ++i)
112 auto const joint = params.joints[i];
113 tolerances.
state_tolerance[i].position = constraints.joints_map.at(joint).trajectory;
118 logger,
"%s %f", (joint +
".trajectory.position").c_str(),
121 logger,
"%s %f", (joint +
".goal.position").c_str(),
124 logger,
"%s %f", (joint +
".goal.velocity").c_str(),
169 const control_msgs::action::FollowJointTrajectory::Goal & goal,
170 const std::vector<std::string> & joints)
173 static auto logger = jtc_logger.get_child(
"tolerance");
178 default_tolerances.
goal_time_tolerance, rclcpp::Duration(goal.goal_time_tolerance).seconds());
180 catch (
const std::runtime_error & e)
183 logger,
"Specified illegal goal_time_tolerance: %f. Using default tolerances",
184 rclcpp::Duration(goal.goal_time_tolerance).seconds());
185 return default_tolerances;
190 for (
auto joint_tol : goal.path_tolerance)
192 auto const joint = joint_tol.name;
194 auto it = std::find(joints.begin(), joints.end(), joint);
195 if (it == joints.end())
200 "' specified in goal.path_tolerance does not exist. "
201 "Using default tolerances.")
203 return default_tolerances;
205 auto i =
static_cast<size_t>(std::distance(joints.cbegin(), it));
206 std::string
interface = "";
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);
219 catch (const std::runtime_error & e)
223 "joint '%s
' specified in goal.path_tolerance has a invalid %s tolerance. Using default "
225 joint.c_str(), interface.c_str());
226 return default_tolerances;
230 logger, "%s %f", (joint + ".state_tolerance.position").c_str(),
231 active_tolerances.state_tolerance[i].position);
233 logger, "%s %f", (joint + ".state_tolerance.velocity").c_str(),
234 active_tolerances.state_tolerance[i].velocity);
236 logger, "%s %f", (joint + ".state_tolerance.acceleration").c_str(),
237 active_tolerances.state_tolerance[i].acceleration);
239 for (auto goal_tol : goal.goal_tolerance)
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())
249 "' specified in goal.goal_tolerance does not exist. "
250 "Using default tolerances.")
252 return default_tolerances;
254 auto i = static_cast<size_t>(std::distance(joints.cbegin(), it));
255 std::string interface = "";
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);
268 catch (const std::runtime_error & e)
272 "joint '%s
' specified in goal.goal_tolerance has a invalid %s tolerance. Using default "
274 joint.c_str(), interface.c_str());
275 return default_tolerances;
279 logger, "%s %f", (joint + ".goal_state_tolerance.position").c_str(),
280 active_tolerances.goal_state_tolerance[i].position);
282 logger, "%s %f", (joint + ".goal_state_tolerance.velocity").c_str(),
283 active_tolerances.goal_state_tolerance[i].velocity);
285 logger, "%s %f", (joint + ".goal_state_tolerance.acceleration").c_str(),
286 active_tolerances.goal_state_tolerance[i].acceleration);
289 return active_tolerances;
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)
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];
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);
322 const auto logger = rclcpp::get_logger("tolerances");
323 RCLCPP_ERROR(logger, "State tolerances failed for joint %zu:", joint_idx);
325 if (state_tolerance.position > 0.0 && abs(error_position) > state_tolerance.position)
328 logger, "Position Error: %f, Position Tolerance: %f", error_position,
329 state_tolerance.position);
331 if (state_tolerance.velocity > 0.0 && abs(error_velocity) > state_tolerance.velocity)
334 logger, "Velocity Error: %f, Velocity Tolerance: %f", error_velocity,
335 state_tolerance.velocity);
338 state_tolerance.acceleration > 0.0 && abs(error_acceleration) > state_tolerance.acceleration)
341 logger, "Acceleration Error: %f, Acceleration Tolerance: %f", error_acceleration,
342 state_tolerance.acceleration);
SegmentTolerances get_segment_tolerances(rclcpp::Logger &jtc_logger, const Params ¶ms)
Populate trajectory segment tolerances using data from the ROS node.
Definition tolerances.hpp:95