17#ifndef PARALLEL_GRIPPER_CONTROLLER__PARALLEL_GRIPPER_ACTION_CONTROLLER_IMPL_HPP_
18#define PARALLEL_GRIPPER_CONTROLLER__PARALLEL_GRIPPER_ACTION_CONTROLLER_IMPL_HPP_
24#include <hardware_interface/types/hardware_interface_type_values.hpp>
25#include "parallel_gripper_controller/parallel_gripper_action_controller.hpp"
30void GripperActionController::preempt_active_goal()
37 active_goal->setCanceled(std::make_shared<GripperCommandAction::Result>());
46 param_listener_ = std::make_shared<ParamListener>(get_node());
48 catch (
const std::exception & e)
50 fprintf(stderr,
"Exception thrown during init stage with message: %s \n", e.what());
51 return controller_interface::CallbackReturn::ERROR;
54 return controller_interface::CallbackReturn::SUCCESS;
58 const rclcpp::Time & ,
const rclcpp::Duration & )
60 command_struct_rt_ = *(command_.readFromRT());
62 const double current_position = joint_position_state_interface_->get().get_value();
63 const double current_velocity = joint_velocity_state_interface_->get().get_value();
64 const double error_position = command_struct_rt_.position_cmd_ - current_position;
66 check_for_success(get_node()->now(), error_position, current_position, current_velocity);
68 joint_command_interface_->get().set_value(command_struct_rt_.position_cmd_);
69 if (speed_interface_.has_value())
71 speed_interface_->get().set_value(command_struct_rt_.max_velocity_);
73 if (effort_interface_.has_value())
75 effort_interface_->get().set_value(command_struct_rt_.max_effort_);
78 return controller_interface::return_type::OK;
81rclcpp_action::GoalResponse GripperActionController::goal_callback(
82 const rclcpp_action::GoalUUID &, std::shared_ptr<const GripperCommandAction::Goal> goal_handle)
84 if (goal_handle->command.position.size() != 1)
86 pre_alloc_result_ = std::make_shared<control_msgs::action::ParallelGripperCommand::Result>();
87 pre_alloc_result_->state.position.resize(1);
88 pre_alloc_result_->state.effort.resize(1);
90 get_node()->get_logger(),
91 "Received action goal with wrong number of position values, expects 1, got %zu",
92 goal_handle->command.position.size());
93 return rclcpp_action::GoalResponse::REJECT;
96 RCLCPP_INFO(get_node()->get_logger(),
"Received & accepted new action goal");
97 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
100void GripperActionController::accepted_callback(
101 std::shared_ptr<GoalHandle> goal_handle)
103 auto rt_goal = std::make_shared<RealtimeGoalHandle>(goal_handle);
106 preempt_active_goal();
110 command_struct_.position_cmd_ = goal_handle->get_goal()->command.position[0];
111 if (!params_.max_velocity_interface.empty() && !goal_handle->get_goal()->command.velocity.empty())
113 command_struct_.max_velocity_ = goal_handle->get_goal()->command.velocity[0];
117 command_struct_.max_velocity_ = params_.max_velocity;
119 if (!params_.max_effort_interface.empty() && !goal_handle->get_goal()->command.effort.empty())
121 command_struct_.max_effort_ = goal_handle->get_goal()->command.effort[0];
125 command_struct_.max_effort_ = params_.max_effort;
127 command_.writeFromNonRT(command_struct_);
129 pre_alloc_result_->reached_goal =
false;
130 pre_alloc_result_->stalled =
false;
137 goal_handle_timer_.reset();
140 goal_handle_timer_ = get_node()->create_wall_timer(
141 action_monitor_period_.to_chrono<std::chrono::nanoseconds>(),
142 std::bind(&RealtimeGoalHandle::runNonRealtime, rt_goal));
145rclcpp_action::CancelResponse GripperActionController::cancel_callback(
146 const std::shared_ptr<GoalHandle> goal_handle)
148 RCLCPP_INFO(get_node()->get_logger(),
"Got request to cancel goal");
152 if (active_goal && active_goal->gh_ == goal_handle)
158 get_node()->get_logger(),
"Canceling active action goal because cancel callback received.");
161 auto action_res = std::make_shared<GripperCommandAction::Result>();
162 active_goal->setCanceled(action_res);
166 return rclcpp_action::CancelResponse::ACCEPT;
169void GripperActionController::set_hold_position()
171 command_struct_.position_cmd_ = joint_position_state_interface_->get().get_value();
172 command_struct_.max_effort_ = params_.max_effort;
173 command_struct_.max_velocity_ = params_.max_velocity;
174 command_.writeFromNonRT(command_struct_);
178 const rclcpp::Time & time,
double error_position,
double current_position,
179 double current_velocity)
187 if (fabs(error_position) < params_.goal_tolerance)
190 pre_alloc_result_->state.position[0] = current_position;
191 pre_alloc_result_->reached_goal =
true;
192 pre_alloc_result_->stalled =
false;
193 RCLCPP_DEBUG(get_node()->get_logger(),
"Successfully moved to goal.");
194 active_goal->setSucceeded(pre_alloc_result_);
199 if (fabs(current_velocity) > params_.stall_velocity_threshold)
206 pre_alloc_result_->state.position[0] = current_position;
207 pre_alloc_result_->reached_goal =
false;
208 pre_alloc_result_->stalled =
true;
210 if (params_.allow_stalling)
212 RCLCPP_DEBUG(get_node()->get_logger(),
"Stall detected moving to goal. Returning success.");
213 active_goal->setSucceeded(pre_alloc_result_);
217 RCLCPP_DEBUG(get_node()->get_logger(),
"Stall detected moving to goal. Aborting action!");
218 active_goal->setAborted(pre_alloc_result_);
225controller_interface::CallbackReturn GripperActionController::on_configure(
226 const rclcpp_lifecycle::State &)
228 const auto logger = get_node()->get_logger();
229 params_ = param_listener_->get_params();
232 action_monitor_period_ = rclcpp::Duration::from_seconds(1.0 / params_.action_monitor_rate);
234 logger,
"Action status changes will be monitored at %f Hz.", params_.action_monitor_rate);
237 if (params_.joint.empty())
239 RCLCPP_ERROR(logger,
"Joint name cannot be empty");
240 return controller_interface::CallbackReturn::ERROR;
242 RCLCPP_INFO(logger,
"Joint name is : %s", params_.joint.c_str());
244 return controller_interface::CallbackReturn::SUCCESS;
247controller_interface::CallbackReturn GripperActionController::on_activate(
248 const rclcpp_lifecycle::State &)
250 auto command_interface_it = std::find_if(
253 { return command_interface.get_interface_name() == hardware_interface::HW_IF_POSITION; });
257 get_node()->get_logger(),
"Expected 1 %s command interface",
259 return controller_interface::CallbackReturn::ERROR;
261 if (command_interface_it->get_prefix_name() != params_.joint)
264 get_node()->get_logger(),
"Command interface is different than joint name `%s` != `%s`",
265 command_interface_it->get_prefix_name().c_str(), params_.joint.c_str());
266 return controller_interface::CallbackReturn::ERROR;
268 const auto position_state_interface_it = std::find_if(
271 { return state_interface.get_interface_name() == hardware_interface::HW_IF_POSITION; });
274 RCLCPP_ERROR(get_node()->get_logger(),
"Expected 1 position state interface");
275 return controller_interface::CallbackReturn::ERROR;
277 if (position_state_interface_it->get_prefix_name() != params_.joint)
280 get_node()->get_logger(),
281 "Position state interface is different than joint name `%s` != `%s`",
282 position_state_interface_it->get_prefix_name().c_str(), params_.joint.c_str());
283 return controller_interface::CallbackReturn::ERROR;
285 const auto velocity_state_interface_it = std::find_if(
288 { return state_interface.get_interface_name() == hardware_interface::HW_IF_VELOCITY; });
291 RCLCPP_ERROR(get_node()->get_logger(),
"Expected 1 velocity state interface");
292 return controller_interface::CallbackReturn::ERROR;
294 if (velocity_state_interface_it->get_prefix_name() != params_.joint)
297 get_node()->get_logger(),
298 "Velocity command interface is different than joint name `%s` != `%s`",
299 velocity_state_interface_it->get_prefix_name().c_str(), params_.joint.c_str());
300 return controller_interface::CallbackReturn::ERROR;
303 joint_command_interface_ = *command_interface_it;
304 joint_position_state_interface_ = *position_state_interface_it;
305 joint_velocity_state_interface_ = *velocity_state_interface_it;
309 if (interface.get_interface_name() ==
"set_gripper_max_effort")
311 effort_interface_ = interface;
313 else if (interface.get_interface_name() ==
"set_gripper_max_velocity")
315 speed_interface_ = interface;
320 command_struct_.position_cmd_ = joint_position_state_interface_->get().get_value();
321 command_struct_.max_effort_ = params_.max_effort;
322 command_struct_.max_velocity_ = params_.max_velocity;
323 command_.initRT(command_struct_);
326 pre_alloc_result_ = std::make_shared<control_msgs::action::ParallelGripperCommand::Result>();
327 pre_alloc_result_->state.position.resize(1);
328 pre_alloc_result_->state.effort.resize(1);
329 pre_alloc_result_->state.position[0] = command_struct_.position_cmd_;
330 pre_alloc_result_->reached_goal =
false;
331 pre_alloc_result_->stalled =
false;
334 action_server_ = rclcpp_action::create_server<control_msgs::action::ParallelGripperCommand>(
335 get_node(),
"~/gripper_cmd",
337 &GripperActionController::goal_callback,
this, std::placeholders::_1, std::placeholders::_2),
338 std::bind(&GripperActionController::cancel_callback,
this, std::placeholders::_1),
339 std::bind(&GripperActionController::accepted_callback,
this, std::placeholders::_1));
341 return controller_interface::CallbackReturn::SUCCESS;
344controller_interface::CallbackReturn GripperActionController::on_deactivate(
345 const rclcpp_lifecycle::State &)
347 joint_command_interface_ = std::nullopt;
348 joint_position_state_interface_ = std::nullopt;
349 joint_velocity_state_interface_ = std::nullopt;
351 return controller_interface::CallbackReturn::SUCCESS;
358 if (!params_.max_effort_interface.empty())
360 names.push_back({params_.max_effort_interface});
362 if (!params_.max_velocity_interface.empty())
364 names.push_back({params_.max_velocity_interface});
367 return {controller_interface::interface_configuration_type::INDIVIDUAL, names};
373 std::vector<std::string> interface_names;
374 for (
const auto & interface : params_.state_interfaces)
376 interface_names.push_back(params_.joint +
"/" + interface);
378 return {controller_interface::interface_configuration_type::INDIVIDUAL, interface_names};
381GripperActionController::GripperActionController()
382: controller_interface::ControllerInterface(),
383 action_monitor_period_(rclcpp::Duration::from_seconds(0))
std::vector< hardware_interface::LoanedCommandInterface > command_interfaces_
Definition controller_interface_base.hpp:340
virtual void release_interfaces()
Method that releases the Loaned interfaces from the controller.
Definition controller_interface_base.cpp:195
std::vector< hardware_interface::LoanedStateInterface > state_interfaces_
Definition controller_interface_base.hpp:350
Definition loaned_command_interface.hpp:30
Definition loaned_state_interface.hpp:29
controller_interface::return_type update(const rclcpp::Time &time, const rclcpp::Duration &period) override
Definition parallel_gripper_action_controller_impl.hpp:57
double computed_command_
Computed command.
Definition parallel_gripper_action_controller.hpp:151
controller_interface::InterfaceConfiguration command_interface_configuration() const override
command_interface_configuration This controller requires the position command interfaces for the cont...
Definition parallel_gripper_action_controller_impl.hpp:355
rclcpp::Time last_movement_time_
Store stall time.
Definition parallel_gripper_action_controller.hpp:150
controller_interface::InterfaceConfiguration state_interface_configuration() const override
command_interface_configuration This controller requires the position and velocity state interfaces f...
Definition parallel_gripper_action_controller_impl.hpp:371
RealtimeGoalHandleBuffer rt_active_goal_
Container for the currently active action goal, if any.
Definition parallel_gripper_action_controller.hpp:129
controller_interface::CallbackReturn on_init() override
Extending interface with initialization method which is individual for each controller.
Definition parallel_gripper_action_controller_impl.hpp:42
void check_for_success(const rclcpp::Time &time, double error_position, double current_position, double current_velocity)
Check for success and publish appropriate result and feedback.
Definition parallel_gripper_action_controller_impl.hpp:177
constexpr char HW_IF_POSITION[]
Constant defining position interface name.
Definition hardware_interface_type_values.hpp:21
Definition parallel_gripper_action_controller.hpp:46
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:58