40 using namespace std::placeholders;
44 bool FollowJointTrajectoryControllerHandle::sendTrajectory(
const moveit_msgs::msg::RobotTrajectory& trajectory)
46 RCLCPP_DEBUG_STREAM(LOGGER,
"new trajectory to " << name_);
48 if (!controller_action_client_)
53 RCLCPP_ERROR_STREAM(LOGGER,
"Action client not connected to action server: " << getActionName());
58 RCLCPP_INFO_STREAM(LOGGER,
"sending trajectory to " << name_);
60 RCLCPP_INFO_STREAM(LOGGER,
"sending continuation for the currently executed trajectory to " << name_);
62 control_msgs::action::FollowJointTrajectory::Goal goal = goal_template_;
63 goal.trajectory = trajectory.joint_trajectory;
64 goal.multi_dof_trajectory = trajectory.multi_dof_joint_trajectory;
66 rclcpp_action::Client<control_msgs::action::FollowJointTrajectory>::SendGoalOptions send_goal_options;
68 send_goal_options.goal_response_callback =
70 const rclcpp_action::Client<control_msgs::action::FollowJointTrajectory>::GoalHandle::SharedPtr& goal_handle) {
71 RCLCPP_INFO_STREAM(LOGGER, name_ <<
" started execution");
73 RCLCPP_WARN(LOGGER,
"Goal request rejected");
75 RCLCPP_INFO(LOGGER,
"Goal request accepted!");
82 auto current_goal_future = controller_action_client_->async_send_goal(goal, send_goal_options);
83 current_goal_ = current_goal_future.get();
86 RCLCPP_ERROR(LOGGER,
"Goal was rejected by server");
105 enum ToleranceVariables
111 template <ToleranceVariables>
112 double& variable(control_msgs::msg::JointTolerance& msg);
115 inline double& variable<POSITION>(control_msgs::msg::JointTolerance& msg)
120 inline double& variable<VELOCITY>(control_msgs::msg::JointTolerance& msg)
125 inline double& variable<ACCELERATION>(control_msgs::msg::JointTolerance& msg)
127 return msg.acceleration;
130 static std::map<ToleranceVariables, std::string> VAR_NAME = { {
POSITION,
"position" },
131 { VELOCITY,
"velocity" },
132 { ACCELERATION,
"acceleration" } };
133 static std::map<ToleranceVariables, decltype(&variable<POSITION>)> VAR_ACCESS = { {
POSITION, &variable<POSITION> },
134 { VELOCITY, &variable<VELOCITY> },
136 &variable<ACCELERATION> } };
138 const char* errorCodeToMessage(
int error_code)
142 case control_msgs::action::FollowJointTrajectory::Result::SUCCESSFUL:
144 case control_msgs::action::FollowJointTrajectory::Result::INVALID_GOAL:
145 return "INVALID_GOAL";
146 case control_msgs::action::FollowJointTrajectory::Result::INVALID_JOINTS:
147 return "INVALID_JOINTS";
148 case control_msgs::action::FollowJointTrajectory::Result::OLD_HEADER_TIMESTAMP:
149 return "OLD_HEADER_TIMESTAMP";
150 case control_msgs::action::FollowJointTrajectory::Result::PATH_TOLERANCE_VIOLATED:
151 return "PATH_TOLERANCE_VIOLATED";
152 case control_msgs::action::FollowJointTrajectory::Result::GOAL_TOLERANCE_VIOLATED:
153 return "GOAL_TOLERANCE_VIOLATED";
155 return "unknown error";
202 control_msgs::msg::JointTolerance&
203 FollowJointTrajectoryControllerHandle::getTolerance(std::vector<control_msgs::msg::JointTolerance>& tolerances,
204 const std::string&
name)
206 auto it = std::lower_bound(tolerances.begin(), tolerances.end(),
name,
207 [](
const control_msgs::msg::JointTolerance& lhs,
const std::string& rhs) {
208 return lhs.name < rhs;
210 if (it == tolerances.cend() || it->name !=
name)
212 it = tolerances.insert(it, control_msgs::msg::JointTolerance());
218 void FollowJointTrajectoryControllerHandle::controllerDoneCallback(
219 const rclcpp_action::ClientGoalHandle<control_msgs::action::FollowJointTrajectory>::WrappedResult& wrapped_result)
222 if (!wrapped_result.result)
223 RCLCPP_WARN_STREAM(LOGGER,
"Controller '" << name_ <<
"' done, no result returned");
224 else if (wrapped_result.result->error_code == control_msgs::action::FollowJointTrajectory::Result::SUCCESSFUL)
225 RCLCPP_INFO_STREAM(LOGGER,
"Controller '" << name_ <<
"' successfully finished");
227 RCLCPP_WARN_STREAM(LOGGER,
"Controller '" << name_ <<
"' failed with error "
228 << errorCodeToMessage(wrapped_result.result->error_code) <<
": "
229 << wrapped_result.result->error_string);
230 finishControllerExecution(wrapped_result.code);