4545namespace joint_trajectory_controller
4646{
4747
48- // TODO(anyone): remove this 'using namespace''
49- using namespace std ::chrono_literals;
50- using lifecycle_msgs::msg::State;
51-
5248JointTrajectoryController::JointTrajectoryController ()
5349: controller_interface::ControllerInterface(),
5450 joint_names_ ({})
@@ -108,7 +104,7 @@ state_interface_configuration() const
108104controller_interface::return_type
109105JointTrajectoryController::update ()
110106{
111- if (get_current_state ().id () == State::PRIMARY_STATE_INACTIVE) {
107+ if (get_current_state ().id () == lifecycle_msgs::msg:: State::PRIMARY_STATE_INACTIVE) {
112108 if (!is_halted_) {
113109 halt ();
114110 is_halted_ = true ;
@@ -120,14 +116,10 @@ JointTrajectoryController::update()
120116 [&](trajectory_msgs::msg::JointTrajectoryPoint & point, size_t size)
121117 {
122118 point.positions .resize (size);
123- if (check_if_interface_type_exists (
124- state_interface_types_, hardware_interface::HW_IF_VELOCITY))
125- {
119+ if (has_velocity_state_interface_) {
126120 point.velocities .resize (size);
127121 }
128- if (check_if_interface_type_exists (
129- state_interface_types_, hardware_interface::HW_IF_ACCELERATION))
130- {
122+ if (has_acceleration_state_interface_) {
131123 point.accelerations .resize (size);
132124 }
133125 };
@@ -137,18 +129,10 @@ JointTrajectoryController::update()
137129 // error defined as the difference between current and desired
138130 error.positions [index] = angles::shortest_angular_distance (
139131 current.positions [index], desired.positions [index]);
140- if (check_if_interface_type_exists (
141- state_interface_types_, hardware_interface::HW_IF_VELOCITY) &&
142- check_if_interface_type_exists (
143- command_interface_types_, hardware_interface::HW_IF_VELOCITY))
144- {
132+ if (has_velocity_state_interface_ && has_velocity_command_interface_) {
145133 error.velocities [index] = desired.velocities [index] - current.velocities [index];
146134 }
147- if (check_if_interface_type_exists (
148- state_interface_types_, hardware_interface::HW_IF_ACCELERATION) &&
149- check_if_interface_type_exists (
150- command_interface_types_, hardware_interface::HW_IF_ACCELERATION))
151- {
135+ if (has_acceleration_state_interface_ && has_acceleration_command_interface_) {
152136 error.accelerations [index] = desired.accelerations [index] - current.accelerations [index];
153137 }
154138 };
@@ -190,13 +174,10 @@ JointTrajectoryController::update()
190174 // Position states always exist
191175 assign_point_from_interface (state_current.positions , joint_state_interface_[0 ]);
192176 // velocity and acceleration states are optional
193- if (check_if_interface_type_exists (state_interface_types_, hardware_interface::HW_IF_VELOCITY) ) {
177+ if (has_velocity_state_interface_ ) {
194178 assign_point_from_interface (state_current.velocities , joint_state_interface_[1 ]);
195179 // Acceleration is used only in combination with velocity
196- if (check_if_interface_type_exists (
197- state_interface_types_,
198- hardware_interface::HW_IF_ACCELERATION))
199- {
180+ if (has_acceleration_state_interface_) {
200181 assign_point_from_interface (state_current.accelerations , joint_state_interface_[2 ]);
201182 } else {
202183 // Make empty so the property is ignored during interpolation
@@ -230,22 +211,16 @@ JointTrajectoryController::update()
230211 const bool before_last_point = end_segment_itr != (*traj_point_active_ptr_)->end ();
231212
232213 // set values for next hardware write()
233- if (check_if_interface_type_exists (
234- command_interface_types_, hardware_interface::HW_IF_POSITION))
235- {
214+ if (has_position_command_interface_) {
236215 assign_interface_from_point (joint_command_interface_[0 ], state_desired.positions );
237216 }
238- if (check_if_interface_type_exists (
239- command_interface_types_, hardware_interface::HW_IF_VELOCITY))
240- {
217+ if (has_velocity_command_interface_) {
241218 assign_interface_from_point (joint_command_interface_[1 ], state_desired.velocities );
242219 }
243- if (check_if_interface_type_exists (
244- command_interface_types_, hardware_interface::HW_IF_ACCELERATION))
245- {
220+ if (has_acceleration_command_interface_) {
246221 assign_interface_from_point (joint_command_interface_[2 ], state_desired.accelerations );
247222 }
248- // TODO(anyone): Add here "if using_closed_loop_hw_interface_adapter" (see ROS1)
223+ // TODO(anyone): Add here "if using_closed_loop_hw_interface_adapter" (see ROS1) - #171
249224// if (check_if_interface_type_exist(
250225// command_interface_types_, hardware_interface::HW_IF_EFFORT)) {
251226// assign_interface_from_point(joint_command_interface_[3], state_desired.effort);
@@ -382,7 +357,7 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
382357 // 2. position [velocity, [acceleration]]
383358
384359 // effort can't be combined with other interfaces
385- if (check_if_interface_type_exists (command_interface_types_, hardware_interface::HW_IF_EFFORT)) {
360+ if (contains_interface_type (command_interface_types_, hardware_interface::HW_IF_EFFORT)) {
386361 if (command_interface_types_.size () == 1 ) {
387362 // TODO(anyone): This flag is not used for now
388363 // There should be PID-approach used as in ROS1:
@@ -397,28 +372,32 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
397372 }
398373 }
399374
400- if (check_if_interface_type_exists (
401- command_interface_types_, hardware_interface::HW_IF_VELOCITY))
402- {
375+ if (contains_interface_type (command_interface_types_, hardware_interface::HW_IF_POSITION)) {
376+ has_position_command_interface_ = true ;
377+ }
378+ if (contains_interface_type (command_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
379+ has_velocity_command_interface_ = true ;
380+ }
381+ if (contains_interface_type (command_interface_types_, hardware_interface::HW_IF_ACCELERATION)) {
382+ has_acceleration_command_interface_ = true ;
383+ }
384+
385+ if (has_velocity_command_interface_) {
403386 // if there is only velocity then use also PID adapter
404387 if (command_interface_types_.size () == 1 ) {
405388 use_closed_loop_pid_adapter = true ;
406389 // TODO(anyone): remove this when implemented
407390 RCLCPP_ERROR (logger, " using 'velocity' command interface alone is not yet implemented yet." );
408391 return CallbackReturn::ERROR;
409392 // if velocity interface can be used without position if multiple defined
410- } else if (!check_if_interface_type_exists (
411- command_interface_types_, hardware_interface::HW_IF_POSITION))
412- {
393+ } else if (!has_position_command_interface_) {
413394 RCLCPP_ERROR (
414395 logger, " 'velocity' command interface can be used either alone or 'position' "
415396 " interface has to be present." );
416397 return CallbackReturn::ERROR;
417398 }
418399 // invalid: acceleration is defined and no velocity
419- } else if (check_if_interface_type_exists (
420- command_interface_types_, hardware_interface::HW_IF_ACCELERATION))
421- {
400+ } else if (has_acceleration_command_interface_) {
422401 RCLCPP_ERROR (
423402 logger, " 'acceleration' command interface can only be used if 'velocity' and "
424403 " 'position' interfaces are present" );
@@ -435,7 +414,7 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
435414 return CallbackReturn::ERROR;
436415 }
437416
438- if (check_if_interface_type_exists (state_interface_types_, hardware_interface::HW_IF_EFFORT)) {
417+ if (contains_interface_type (state_interface_types_, hardware_interface::HW_IF_EFFORT)) {
439418 RCLCPP_ERROR (logger, " State interface type 'effort' not allowed!" );
440419 return CallbackReturn::ERROR;
441420 }
@@ -453,19 +432,21 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
453432 }
454433 }
455434
456- if (check_if_interface_type_exists (state_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
457- if (!check_if_interface_type_exists (
458- state_interface_types_,
459- hardware_interface::HW_IF_POSITION))
460- {
435+ if (contains_interface_type (state_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
436+ has_velocity_state_interface_ = true ;
437+ }
438+ if (contains_interface_type (state_interface_types_, hardware_interface::HW_IF_ACCELERATION)) {
439+ has_acceleration_state_interface_ = true ;
440+ }
441+
442+ if (has_velocity_state_interface_) {
443+ if (!contains_interface_type (state_interface_types_, hardware_interface::HW_IF_POSITION)) {
461444 RCLCPP_ERROR (
462445 logger, " 'velocity' state interface cannot be used if 'position' interface "
463446 " is missing." );
464447 return CallbackReturn::ERROR;
465448 }
466- } else if (check_if_interface_type_exists (
467- state_interface_types_, hardware_interface::HW_IF_ACCELERATION))
468- {
449+ } else if (has_acceleration_state_interface_) {
469450 RCLCPP_ERROR (
470451 logger, " 'acceleration' state interface cannot be used if 'position' and 'velocity' "
471452 " interfaces are not present." );
@@ -543,14 +524,11 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
543524 state_publisher_->msg_ .desired .accelerations .resize (n_joints);
544525 state_publisher_->msg_ .actual .positions .resize (n_joints);
545526 state_publisher_->msg_ .error .positions .resize (n_joints);
546- if (check_if_interface_type_exists (state_interface_types_, hardware_interface::HW_IF_VELOCITY) ) {
527+ if (has_velocity_state_interface_ ) {
547528 state_publisher_->msg_ .actual .velocities .resize (n_joints);
548529 state_publisher_->msg_ .error .velocities .resize (n_joints);
549530 }
550- if (check_if_interface_type_exists (
551- state_interface_types_,
552- hardware_interface::HW_IF_ACCELERATION))
553- {
531+ if (has_acceleration_state_interface_) {
554532 state_publisher_->msg_ .actual .accelerations .resize (n_joints);
555533 state_publisher_->msg_ .error .accelerations .resize (n_joints);
556534 }
@@ -754,15 +732,11 @@ void JointTrajectoryController::publish_state(
754732 state_publisher_->msg_ .desired .accelerations = desired_state.accelerations ;
755733 state_publisher_->msg_ .actual .positions = current_state.positions ;
756734 state_publisher_->msg_ .error .positions = state_error.positions ;
757- if (check_if_interface_type_exists (
758- state_interface_types_, hardware_interface::HW_IF_VELOCITY))
759- {
735+ if (has_velocity_state_interface_) {
760736 state_publisher_->msg_ .actual .velocities = current_state.velocities ;
761737 state_publisher_->msg_ .error .velocities = state_error.velocities ;
762738 }
763- if (check_if_interface_type_exists (
764- state_interface_types_, hardware_interface::HW_IF_ACCELERATION))
765- {
739+ if (has_acceleration_state_interface_) {
766740 state_publisher_->msg_ .actual .accelerations = current_state.accelerations ;
767741 state_publisher_->msg_ .error .accelerations = state_error.accelerations ;
768742 }
0 commit comments