17#ifndef GRIPPER_CONTROLLERS__GRIPPER_ACTION_CONTROLLER_IMPL_HPP_
18#define GRIPPER_CONTROLLERS__GRIPPER_ACTION_CONTROLLER_IMPL_HPP_
20#include "gripper_controllers/gripper_action_controller.hpp"
27using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
29template <const
char * HardwareInterface>
30void GripperActionController<HardwareInterface>::preempt_active_goal()
36 rt_active_goal_->setCanceled(std::make_shared<GripperCommandAction::Result>());
37 rt_active_goal_.reset();
41template <const
char * HardwareInterface>
42CallbackReturn GripperActionController<HardwareInterface>::on_init()
47 auto_declare<double>(
"action_monitor_rate", 20.0);
48 auto_declare<std::string>(
"joint", joint_name_);
49 auto_declare<double>(
"goal_tolerance", 0.01);
50 auto_declare<double>(
"max_effort", 0.0);
51 auto_declare<double>(
"stall_velocity_threshold", 0.001);
52 auto_declare<double>(
"stall_timeout", 1.0);
54 catch (
const std::exception & e)
56 fprintf(stderr,
"Exception thrown during init stage with message: %s \n", e.what());
57 return CallbackReturn::ERROR;
60 return CallbackReturn::SUCCESS;
63template <const
char * HardwareInterface>
64controller_interface::return_type GripperActionController<HardwareInterface>::update(
65 const rclcpp::Time & ,
const rclcpp::Duration & )
67 command_struct_rt_ = *(command_.readFromRT());
69 const double current_position = joint_position_state_interface_->get().get_value();
70 const double current_velocity = joint_velocity_state_interface_->get().get_value();
72 const double error_position = command_struct_rt_.position_ - current_position;
73 const double error_velocity = -current_velocity;
75 check_for_success(node_->now(), error_position, current_position, current_velocity);
78 computed_command_ = hw_iface_adapter_.updateCommand(
79 command_struct_rt_.position_, 0.0, error_position, error_velocity,
80 command_struct_rt_.max_effort_);
81 return controller_interface::return_type::OK;
84template <const
char * HardwareInterface>
85rclcpp_action::GoalResponse GripperActionController<HardwareInterface>::goal_callback(
86 const rclcpp_action::GoalUUID &, std::shared_ptr<const GripperCommandAction::Goal>)
88 RCLCPP_INFO(node_->get_logger(),
"Received & accepted new action goal");
89 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
92template <const
char * HardwareInterface>
93void GripperActionController<HardwareInterface>::accepted_callback(
94 std::shared_ptr<GoalHandle> goal_handle)
97 auto rt_goal = std::make_shared<RealtimeGoalHandle>(goal_handle);
100 preempt_active_goal();
104 command_struct_.position_ = goal_handle->get_goal()->command.position;
105 command_struct_.max_effort_ = goal_handle->get_goal()->command.max_effort;
106 command_.writeFromNonRT(command_struct_);
108 pre_alloc_result_->reached_goal =
false;
109 pre_alloc_result_->stalled =
false;
111 last_movement_time_ = node_->now();
112 rt_active_goal_ = rt_goal;
113 rt_active_goal_->execute();
116 goal_handle_timer_ = node_->create_wall_timer(
117 action_monitor_period_.to_chrono<std::chrono::seconds>(),
118 std::bind(&RealtimeGoalHandle::runNonRealtime, rt_active_goal_));
121template <const
char * HardwareInterface>
122rclcpp_action::CancelResponse GripperActionController<HardwareInterface>::cancel_callback(
123 const std::shared_ptr<GoalHandle> goal_handle)
125 RCLCPP_INFO(node_->get_logger(),
"Got request to cancel goal");
128 if (rt_active_goal_ && rt_active_goal_->gh_ == goal_handle)
134 node_->get_logger(),
"Canceling active action goal because cancel callback received.");
137 auto action_res = std::make_shared<GripperCommandAction::Result>();
138 rt_active_goal_->setCanceled(action_res);
140 rt_active_goal_.reset();
142 return rclcpp_action::CancelResponse::ACCEPT;
145template <const
char * HardwareInterface>
146void GripperActionController<HardwareInterface>::set_hold_position()
148 command_struct_.position_ = joint_position_state_interface_->get().get_value();
149 command_struct_.max_effort_ = default_max_effort_;
150 command_.writeFromNonRT(command_struct_);
153template <const
char * HardwareInterface>
154void GripperActionController<HardwareInterface>::check_for_success(
155 const rclcpp::Time & time,
double error_position,
double current_position,
156 double current_velocity)
158 if (!rt_active_goal_)
163 if (fabs(error_position) < goal_tolerance_)
165 pre_alloc_result_->effort = computed_command_;
166 pre_alloc_result_->position = current_position;
167 pre_alloc_result_->reached_goal =
true;
168 pre_alloc_result_->stalled =
false;
169 rt_active_goal_->setSucceeded(pre_alloc_result_);
170 rt_active_goal_.reset();
174 if (fabs(current_velocity) > stall_velocity_threshold_)
176 last_movement_time_ = time;
178 else if ((time - last_movement_time_).seconds() > stall_timeout_)
180 pre_alloc_result_->effort = computed_command_;
181 pre_alloc_result_->position = current_position;
182 pre_alloc_result_->reached_goal =
false;
183 pre_alloc_result_->stalled =
true;
184 rt_active_goal_->setAborted(pre_alloc_result_);
185 rt_active_goal_.reset();
190template <const
char * HardwareInterface>
191CallbackReturn GripperActionController<HardwareInterface>::on_configure(
192 const rclcpp_lifecycle::State &)
194 const auto logger = node_->get_logger();
197 const auto action_monitor_rate = node_->get_parameter(
"action_monitor_rate").as_double();
198 action_monitor_period_ =
199 rclcpp::Duration::from_seconds(1.0 / node_->get_parameter(
"action_monitor_rate").as_double());
201 logger,
"Action status changes will be monitored at " << action_monitor_rate <<
"Hz.");
204 joint_name_ = node_->get_parameter(
"joint").as_string();
205 if (joint_name_.empty())
207 RCLCPP_ERROR(logger,
"Could not find joint name on param server");
208 return CallbackReturn::ERROR;
212 goal_tolerance_ = node_->get_parameter(
"goal_tolerance").as_double();
213 goal_tolerance_ = fabs(goal_tolerance_);
215 default_max_effort_ = node_->get_parameter(
"max_effort").as_double();
216 default_max_effort_ = fabs(default_max_effort_);
218 stall_velocity_threshold_ = node_->get_parameter(
"stall_velocity_threshold").as_double();
219 stall_timeout_ = node_->get_parameter(
"stall_timeout").as_double();
221 return CallbackReturn::SUCCESS;
223template <const
char * HardwareInterface>
224CallbackReturn GripperActionController<HardwareInterface>::on_activate(
225 const rclcpp_lifecycle::State &)
227 auto position_command_interface_it = std::find_if(
228 command_interfaces_.begin(), command_interfaces_.end(),
230 return command_interface.get_interface_name() == hardware_interface::HW_IF_POSITION;
232 if (position_command_interface_it == command_interfaces_.end())
234 RCLCPP_ERROR(node_->get_logger(),
"Expected 1 position command interface");
235 return CallbackReturn::ERROR;
237 if (position_command_interface_it->get_name() != joint_name_)
240 node_->get_logger(),
"Position command interface is different than joint name `"
241 << position_command_interface_it->get_name() <<
"` != `" << joint_name_
243 return CallbackReturn::ERROR;
245 const auto position_state_interface_it = std::find_if(
246 state_interfaces_.begin(), state_interfaces_.end(),
248 return state_interface.get_interface_name() == hardware_interface::HW_IF_POSITION;
250 if (position_state_interface_it == state_interfaces_.end())
252 RCLCPP_ERROR(node_->get_logger(),
"Expected 1 position state interface");
253 return CallbackReturn::ERROR;
255 if (position_state_interface_it->get_name() != joint_name_)
258 node_->get_logger(),
"Position state interface is different than joint name `"
259 << position_state_interface_it->get_name() <<
"` != `" << joint_name_
261 return CallbackReturn::ERROR;
263 const auto velocity_state_interface_it = std::find_if(
264 state_interfaces_.begin(), state_interfaces_.end(),
266 return state_interface.get_interface_name() == hardware_interface::HW_IF_VELOCITY;
268 if (velocity_state_interface_it == state_interfaces_.end())
270 RCLCPP_ERROR(node_->get_logger(),
"Expected 1 velocity state interface");
271 return CallbackReturn::ERROR;
273 if (velocity_state_interface_it->get_name() != joint_name_)
276 node_->get_logger(),
"Velocity command interface is different than joint name `"
277 << velocity_state_interface_it->get_name() <<
"` != `" << joint_name_
279 return CallbackReturn::ERROR;
282 joint_position_command_interface_ = *position_command_interface_it;
283 joint_position_state_interface_ = *position_state_interface_it;
284 joint_velocity_state_interface_ = *velocity_state_interface_it;
287 hw_iface_adapter_.init(joint_position_command_interface_, node_);
290 command_struct_.position_ = joint_position_state_interface_->get().get_value();
291 command_struct_.max_effort_ = default_max_effort_;
294 pre_alloc_result_ = std::make_shared<control_msgs::action::GripperCommand::Result>();
295 pre_alloc_result_->position = command_struct_.position_;
296 pre_alloc_result_->reached_goal =
false;
297 pre_alloc_result_->stalled =
false;
300 action_server_ = rclcpp_action::create_server<control_msgs::action::GripperCommand>(
301 node_,
"~/gripper_cmd",
303 &GripperActionController::goal_callback,
this, std::placeholders::_1, std::placeholders::_2),
304 std::bind(&GripperActionController::cancel_callback,
this, std::placeholders::_1),
305 std::bind(&GripperActionController::accepted_callback,
this, std::placeholders::_1));
307 return CallbackReturn::SUCCESS;
310template <const
char * HardwareInterface>
311CallbackReturn GripperActionController<HardwareInterface>::on_deactivate(
312 const rclcpp_lifecycle::State &)
314 joint_position_command_interface_ = std::experimental::nullopt;
315 joint_position_state_interface_ = std::experimental::nullopt;
316 joint_velocity_state_interface_ = std::experimental::nullopt;
317 release_interfaces();
318 return CallbackReturn::SUCCESS;
321template <const
char * HardwareInterface>
326 controller_interface::interface_configuration_type::INDIVIDUAL,
330template <const
char * HardwareInterface>
335 controller_interface::interface_configuration_type::INDIVIDUAL,
340template <const
char * HardwareInterface>
342: controller_interface::ControllerInterface(),
343 action_monitor_period_(rclcpp::Duration::from_seconds(0))
Controller for executing a gripper command action for simple single-dof grippers.
Definition gripper_action_controller.hpp:62
GRIPPER_ACTION_CONTROLLER_PUBLIC controller_interface::InterfaceConfiguration state_interface_configuration() const override
command_interface_configuration This controller requires the position and velocity state interfaces f...
Definition gripper_action_controller_impl.hpp:332
GRIPPER_ACTION_CONTROLLER_PUBLIC controller_interface::InterfaceConfiguration command_interface_configuration() const override
command_interface_configuration This controller requires the position command interfaces for the cont...
Definition gripper_action_controller_impl.hpp:323
Definition loaned_command_interface.hpp:27
Definition loaned_state_interface.hpp:27
Definition gripper_action_controller.hpp:49
constexpr char HW_IF_VELOCITY[]
Constant defining velocity interface.
Definition hardware_interface_type_values.hpp:23
constexpr char HW_IF_POSITION[]
Constant defining position interface.
Definition hardware_interface_type_values.hpp:21
Configuring what command/state interfaces to claim.
Definition controller_interface.hpp:63