3131#define JOINT_TRAJECTORY_CONTROLLER__TOLERANCES_HPP_
3232
3333#include < limits>
34+ #include < stdexcept>
3435#include < string>
3536#include < vector>
3637
@@ -126,6 +127,33 @@ SegmentTolerances get_segment_tolerances(rclcpp::Logger & jtc_logger, const Para
126127 return tolerances;
127128}
128129
130+ double resolve_tolerance_source (const double default_value, const double goal_value)
131+ {
132+ // from
133+ // https://github.com/ros-controls/control_msgs/blob/master/control_msgs/msg/JointTolerance.msg
134+ // There are two special values for tolerances:
135+ // * 0 - The tolerance is unspecified and will remain at whatever the default is
136+ // * -1 - The tolerance is "erased".
137+ // If there was a default, the joint will be allowed to move without restriction.
138+ constexpr double ERASE_VALUE = -1.0 ;
139+ auto is_erase_value = [](double value)
140+ { return fabs (value - ERASE_VALUE) < std::numeric_limits<float >::epsilon (); };
141+
142+ if (goal_value > 0.0 )
143+ {
144+ return goal_value;
145+ }
146+ else if (is_erase_value (goal_value))
147+ {
148+ return 0.0 ;
149+ }
150+ else if (goal_value < 0.0 )
151+ {
152+ throw std::runtime_error (" Illegal tolerance value." );
153+ }
154+ return default_value;
155+ }
156+
129157/* *
130158 * \brief Populate trajectory segment tolerances using data from an action goal.
131159 *
@@ -141,20 +169,22 @@ SegmentTolerances get_segment_tolerances(
141169 const std::vector<std::string> & joints)
142170{
143171 SegmentTolerances active_tolerances (default_tolerances);
144-
145- active_tolerances.goal_time_tolerance = rclcpp::Duration (goal.goal_time_tolerance ).seconds ();
146172 static auto logger = jtc_logger.get_child (" tolerance" );
147- RCLCPP_DEBUG (logger, " %s %f" , " goal_time" , active_tolerances.goal_time_tolerance );
148173
149- // from
150- // https://github.com/ros-controls/control_msgs/blob/master/control_msgs/msg/JointTolerance.msg
151- // There are two special values for tolerances:
152- // * 0 - The tolerance is unspecified and will remain at whatever the default is
153- // * -1 - The tolerance is "erased".
154- // If there was a default, the joint will be allowed to move without restriction.
155- constexpr double ERASE_VALUE = -1.0 ;
156- auto is_erase_value = [](double value)
157- { return fabs (value - ERASE_VALUE) < std::numeric_limits<float >::epsilon (); };
174+ try
175+ {
176+ active_tolerances.goal_time_tolerance = resolve_tolerance_source (
177+ default_tolerances.goal_time_tolerance , rclcpp::Duration (goal.goal_time_tolerance ).seconds ());
178+ }
179+ catch (const std::runtime_error & e)
180+ {
181+ RCLCPP_ERROR_STREAM (
182+ logger, " Specified illegal goal_time_tolerance: "
183+ << rclcpp::Duration (goal.goal_time_tolerance ).seconds ()
184+ << " . Using default tolerances" );
185+ return default_tolerances;
186+ }
187+ RCLCPP_DEBUG (logger, " %s %f" , " goal_time" , active_tolerances.goal_time_tolerance );
158188
159189 // State and goal state tolerances
160190 for (auto joint_tol : goal.path_tolerance )
@@ -173,60 +203,24 @@ SegmentTolerances get_segment_tolerances(
173203 return default_tolerances;
174204 }
175205 auto i = std::distance (joints.cbegin (), it);
176- if (joint_tol.position > 0.0 )
177- {
178- active_tolerances.state_tolerance [i].position = joint_tol.position ;
179- }
180- else if (is_erase_value (joint_tol.position ))
206+ std::string interface = " " ;
207+ try
181208 {
182- active_tolerances.state_tolerance [i].position = 0.0 ;
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 );
183218 }
184- else if (joint_tol. position < 0.0 )
219+ catch ( const std::runtime_error & e )
185220 {
186- RCLCPP_ERROR (
187- logger, " %s" ,
188- (" joint '" + joint +
189- " ' specified in goal.path_tolerance has a invalid position tolerance. "
190- " Using default tolerances." )
191- .c_str ());
192- return default_tolerances;
193- }
194-
195- if (joint_tol.velocity > 0.0 )
196- {
197- active_tolerances.state_tolerance [i].velocity = joint_tol.velocity ;
198- }
199- else if (is_erase_value (joint_tol.velocity ))
200- {
201- active_tolerances.state_tolerance [i].velocity = 0.0 ;
202- }
203- else if (joint_tol.velocity < 0.0 )
204- {
205- RCLCPP_ERROR (
206- logger, " %s" ,
207- (" joint '" + joint +
208- " ' specified in goal.path_tolerance has a invalid velocity tolerance. "
209- " Using default tolerances." )
210- .c_str ());
211- return default_tolerances;
212- }
213-
214- if (joint_tol.acceleration > 0.0 )
215- {
216- active_tolerances.state_tolerance [i].acceleration = joint_tol.acceleration ;
217- }
218- else if (is_erase_value (joint_tol.acceleration ))
219- {
220- active_tolerances.state_tolerance [i].acceleration = 0.0 ;
221- }
222- else if (joint_tol.acceleration < 0.0 )
223- {
224- RCLCPP_ERROR (
225- logger, " %s" ,
226- (" joint '" + joint +
227- " ' specified in goal.path_tolerance has a invalid acceleration tolerance. "
228- " Using default tolerances." )
229- .c_str ());
221+ RCLCPP_ERROR_STREAM (
222+ logger, " joint '" << joint << " ' specified in goal.path_tolerance has a invalid "
223+ << interface << " tolerance. Using default tolerances." );
230224 return default_tolerances;
231225 }
232226
@@ -256,60 +250,24 @@ SegmentTolerances get_segment_tolerances(
256250 return default_tolerances;
257251 }
258252 auto i = std::distance (joints.cbegin (), it);
259- if (goal_tol.position > 0.0 )
260- {
261- active_tolerances.goal_state_tolerance [i].position = goal_tol.position ;
262- }
263- else if (is_erase_value (goal_tol.position ))
264- {
265- active_tolerances.goal_state_tolerance [i].position = 0.0 ;
266- }
267- else if (goal_tol.position < 0.0 )
268- {
269- RCLCPP_ERROR (
270- logger, " %s" ,
271- (" joint '" + joint +
272- " ' specified in goal.goal_tolerance has a invalid position tolerance. "
273- " Using default tolerances." )
274- .c_str ());
275- return default_tolerances;
276- }
277-
278- if (goal_tol.velocity > 0.0 )
279- {
280- active_tolerances.goal_state_tolerance [i].velocity = goal_tol.velocity ;
281- }
282- else if (is_erase_value (goal_tol.velocity ))
253+ std::string interface = " " ;
254+ try
283255 {
284- active_tolerances.goal_state_tolerance [i].velocity = 0.0 ;
256+ interface = " position" ;
257+ active_tolerances.goal_state_tolerance [i].position = resolve_tolerance_source (
258+ default_tolerances.goal_state_tolerance [i].position , goal_tol.position );
259+ interface = " velocity" ;
260+ active_tolerances.goal_state_tolerance [i].velocity = resolve_tolerance_source (
261+ default_tolerances.goal_state_tolerance [i].velocity , goal_tol.velocity );
262+ interface = " acceleration" ;
263+ active_tolerances.goal_state_tolerance [i].acceleration = resolve_tolerance_source (
264+ default_tolerances.goal_state_tolerance [i].acceleration , goal_tol.acceleration );
285265 }
286- else if (goal_tol. velocity < 0.0 )
266+ catch ( const std::runtime_error & e )
287267 {
288- RCLCPP_ERROR (
289- logger, " %s" ,
290- (" joint '" + joint +
291- " ' specified in goal.goal_tolerance has a invalid velocity tolerance. "
292- " Using default tolerances." )
293- .c_str ());
294- return default_tolerances;
295- }
296-
297- if (goal_tol.acceleration > 0.0 )
298- {
299- active_tolerances.goal_state_tolerance [i].acceleration = goal_tol.acceleration ;
300- }
301- else if (is_erase_value (goal_tol.acceleration ))
302- {
303- active_tolerances.goal_state_tolerance [i].acceleration = 0.0 ;
304- }
305- else if (goal_tol.acceleration < 0.0 )
306- {
307- RCLCPP_ERROR (
308- logger, " %s" ,
309- (" joint '" + joint +
310- " ' specified in goal.goal_tolerance has a invalid acceleration tolerance. "
311- " Using default tolerances." )
312- .c_str ());
268+ RCLCPP_ERROR_STREAM (
269+ logger, " joint '" << joint << " ' specified in goal.goal_tolerance has a invalid "
270+ << interface << " tolerance. Using default tolerances." );
313271 return default_tolerances;
314272 }
315273
0 commit comments