moveit2
The MoveIt Motion Planning Framework for ROS 2.
execute_trajectory_action_capability.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016, Kentaro Wada.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Kentaro Wada */
36 
38 
44 #include <moveit/utils/logger.hpp>
45 
46 namespace move_group
47 {
48 namespace
49 {
50 rclcpp::Logger getLogger()
51 {
52  return moveit::getLogger("ClearOctomapService");
53 }
54 } // namespace
55 
57 {
58 }
59 
61 {
62  auto node = context_->moveit_cpp_->getNode();
63  // start the move action server
64  execute_action_server_ = rclcpp_action::create_server<ExecTrajectory>(
65  node->get_node_base_interface(), node->get_node_clock_interface(), node->get_node_logging_interface(),
66  node->get_node_waitables_interface(), EXECUTE_ACTION_NAME,
67  [](const rclcpp_action::GoalUUID& /*unused*/, const std::shared_ptr<const ExecTrajectory::Goal>& /*unused*/) {
68  return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
69  },
70  [](const std::shared_ptr<ExecTrajectoryGoal>& /* unused */) { return rclcpp_action::CancelResponse::ACCEPT; },
71  [this](const auto& goal) { executePathCallback(goal); });
72 }
73 
74 void MoveGroupExecuteTrajectoryAction::executePathCallback(const std::shared_ptr<ExecTrajectoryGoal>& goal)
75 {
76  auto action_res = std::make_shared<ExecTrajectory::Result>();
77  if (!context_->trajectory_execution_manager_)
78  {
79  const std::string response = "Cannot execute trajectory since ~allow_trajectory_execution was set to false";
80  action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::CONTROL_FAILED;
81  goal->abort(action_res);
82  return;
83  }
84 
85  executePath(goal, action_res);
86 
87  const std::string response = getActionResultString(action_res->error_code, false, false);
88  auto fb = std::make_shared<ExecTrajectory::Feedback>();
89  fb->state = response;
90  if (action_res->error_code.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS)
91  {
92  goal->publish_feedback(fb);
93  goal->succeed(action_res);
94  }
95  else
96  {
97  goal->publish_feedback(fb);
98  goal->abort(action_res);
99  }
100 
101  setExecuteTrajectoryState(IDLE, goal);
102 }
103 
104 void MoveGroupExecuteTrajectoryAction::executePath(const std::shared_ptr<ExecTrajectoryGoal>& goal,
105  std::shared_ptr<ExecTrajectory::Result>& action_res)
106 {
107  RCLCPP_INFO(getLogger(), "Execution request received");
108 
109  context_->trajectory_execution_manager_->clear();
110  if (context_->trajectory_execution_manager_->push(goal->get_goal()->trajectory, goal->get_goal()->controller_names))
111  {
112  setExecuteTrajectoryState(MONITOR, goal);
113  context_->trajectory_execution_manager_->execute();
114  moveit_controller_manager::ExecutionStatus status = context_->trajectory_execution_manager_->waitForExecution();
116  {
117  action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
118  }
120  {
121  action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::PREEMPTED;
122  }
124  {
125  action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::TIMED_OUT;
126  }
127  else
128  {
129  action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::CONTROL_FAILED;
130  }
131  RCLCPP_INFO_STREAM(getLogger(), "Execution completed: " << status.asString());
132  }
133  else
134  {
135  action_res->error_code.val = moveit_msgs::msg::MoveItErrorCodes::CONTROL_FAILED;
136  }
137 }
138 
139 void MoveGroupExecuteTrajectoryAction::preemptExecuteTrajectoryCallback()
140 {
141  context_->trajectory_execution_manager_->stopExecution(true);
142 }
143 
144 void MoveGroupExecuteTrajectoryAction::setExecuteTrajectoryState(MoveGroupState state,
145  const std::shared_ptr<ExecTrajectoryGoal>& goal)
146 {
147  auto execute_feedback = std::make_shared<ExecTrajectory::Feedback>();
148  execute_feedback->state = stateToStr(state);
149  goal->publish_feedback(execute_feedback);
150 }
151 
152 } // namespace move_group
153 
154 #include <pluginlib/class_list_macros.hpp>
155 
PLUGINLIB_EXPORT_CLASS(cached_ik_kinematics_plugin::CachedIKKinematicsPlugin< kdl_kinematics_plugin::KDLKinematicsPlugin >, kinematics::KinematicsBase)
std::string getActionResultString(const moveit_msgs::msg::MoveItErrorCodes &error_code, bool planned_trajectory_empty, bool plan_only)
std::string stateToStr(MoveGroupState state) const
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition: logger.cpp:79
std::string asString() const
Convert the execution status to a string.