53 #include <moveit_msgs/PickupAction.h>
54 #include <moveit_msgs/ExecuteTrajectoryAction.h>
55 #include <moveit_msgs/PlaceAction.h>
56 #include <moveit_msgs/ExecuteKnownTrajectory.h>
57 #include <moveit_msgs/QueryPlannerInterfaces.h>
58 #include <moveit_msgs/GetCartesianPath.h>
59 #include <moveit_msgs/GraspPlanning.h>
60 #include <moveit_msgs/GetPlannerParams.h>
61 #include <moveit_msgs/SetPlannerParams.h>
63 #include <std_msgs/String.h>
64 #include <geometry_msgs/TransformStamped.h>
80 const std::string
LOGNAME =
"move_group_interface";
93 class MoveGroupInterface::MoveGroupInterfaceImpl
105 std::string error =
"Unable to construct robot model. Please make sure all needed information is on the "
108 throw std::runtime_error(error);
113 std::string error =
"Group '" + opt.group_name_ +
"' was not found.";
115 throw std::runtime_error(error);
133 std::string desc = opt.robot_description_.length() ? opt.robot_description_ :
ROBOT_DESCRIPTION;
135 std::string kinematics_desc = desc +
"_kinematics/";
140 node_handle_.
param<
double>(kinematics_desc + opt.group_name_ +
"/goal_orientation_tolerance",
143 std::string planning_desc = desc +
"_planning/";
163 double allotted_time = wait_for_servers.
toSec();
165 move_action_client_ = std::make_unique<actionlib::SimpleActionClient<moveit_msgs::MoveGroupAction>>(
169 pick_action_client_ = std::make_unique<actionlib::SimpleActionClient<moveit_msgs::PickupAction>>(
173 place_action_client_ = std::make_unique<actionlib::SimpleActionClient<moveit_msgs::PlaceAction>>(
177 execute_action_client_ = std::make_unique<actionlib::SimpleActionClient<moveit_msgs::ExecuteTrajectoryAction>>(
196 template <
typename T>
239 if (!action->isServerConnected())
241 std::stringstream error;
242 error <<
"Unable to connect to move_group action server '" <<
name <<
"' within allotted time (" << allotted_time
244 throw std::runtime_error(error.str());
258 const std::shared_ptr<tf2_ros::Buffer>&
getTF()
const
268 const moveit::core::RobotModelConstPtr&
getRobotModel()
const
285 moveit_msgs::QueryPlannerInterfaces::Request req;
286 moveit_msgs::QueryPlannerInterfaces::Response
res;
288 if (!
res.planner_interfaces.empty())
290 desc =
res.planner_interfaces.front();
298 moveit_msgs::QueryPlannerInterfaces::Request req;
299 moveit_msgs::QueryPlannerInterfaces::Response
res;
301 if (!
res.planner_interfaces.empty())
303 desc =
res.planner_interfaces;
309 std::map<std::string, std::string>
getPlannerParams(
const std::string& planner_id,
const std::string&
group =
"")
311 moveit_msgs::GetPlannerParams::Request req;
312 moveit_msgs::GetPlannerParams::Response
res;
313 req.planner_config = planner_id;
315 std::map<std::string, std::string> result;
318 for (
unsigned int i = 0, end =
res.params.keys.size(); i < end; ++i)
319 result[
res.params.keys[i]] =
res.params.values[i];
325 const std::map<std::string, std::string>& params,
bool replace =
false)
327 moveit_msgs::SetPlannerParams::Request req;
328 moveit_msgs::SetPlannerParams::Response
res;
329 req.planner_config = planner_id;
331 req.replace = replace;
332 for (
const std::pair<const std::string, std::string>& param : params)
334 req.params.keys.push_back(
param.first);
335 req.params.values.push_back(
param.second);
342 std::string default_planning_pipeline;
344 return default_planning_pipeline;
370 std::stringstream param_name;
371 param_name <<
"move_group";
372 if (!pipeline_id.empty())
373 param_name <<
"/planning_pipelines/" << pipeline_id;
375 param_name <<
"/" <<
group;
376 param_name <<
"/default_planner_config";
378 std::string default_planner_config;
380 return default_planner_config;
408 void setMaxScalingFactor(
double& variable,
const double target_value,
const char* factor_name,
double fallback_value)
410 if (target_value > 1.0)
415 else if (target_value <= 0.0)
417 node_handle_.
param<
double>(std::string(
"robot_description_planning/default_") + factor_name, variable,
419 if (target_value < 0.0)
426 variable = target_value;
452 void setStartState(
const moveit_msgs::RobotState& start_state)
472 moveit::core::RobotStatePtr
s;
478 bool setJointValueTarget(
const geometry_msgs::Pose& eef_pose,
const std::string& end_effector_link,
479 const std::string& frame,
bool approx)
481 const std::string& eef = end_effector_link.empty() ?
getEndEffectorLink() : end_effector_link;
554 const std::vector<std::string>& possible_eefs =
556 for (
const std::string& possible_eef : possible_eefs)
560 static std::string empty;
564 bool setPoseTargets(
const std::vector<geometry_msgs::PoseStamped>& poses,
const std::string& end_effector_link)
566 const std::string& eef = end_effector_link.empty() ?
end_effector_link_ : end_effector_link;
576 std::vector<geometry_msgs::PoseStamped>& stored_poses =
pose_targets_[eef];
577 for (geometry_msgs::PoseStamped& stored_pose : stored_poses)
583 bool hasPoseTarget(
const std::string& end_effector_link)
const
585 const std::string& eef = end_effector_link.empty() ?
end_effector_link_ : end_effector_link;
589 const geometry_msgs::PoseStamped&
getPoseTarget(
const std::string& end_effector_link)
const
591 const std::string& eef = end_effector_link.empty() ?
end_effector_link_ : end_effector_link;
594 std::map<std::string, std::vector<geometry_msgs::PoseStamped>>::const_iterator jt =
pose_targets_.find(eef);
596 if (!jt->second.empty())
597 return jt->second.at(0);
600 static const geometry_msgs::PoseStamped UNKNOWN;
605 const std::vector<geometry_msgs::PoseStamped>&
getPoseTargets(
const std::string& end_effector_link)
const
607 const std::string& eef = end_effector_link.empty() ?
end_effector_link_ : end_effector_link;
609 std::map<std::string, std::vector<geometry_msgs::PoseStamped>>::const_iterator jt =
pose_targets_.find(eef);
611 if (!jt->second.empty())
615 static const std::vector<geometry_msgs::PoseStamped> EMPTY;
661 bool getCurrentState(moveit::core::RobotStatePtr& current_state,
double wait_seconds = 1.0)
684 std::vector<moveit_msgs::PlaceLocation>
687 std::vector<moveit_msgs::PlaceLocation> locations;
688 for (
const geometry_msgs::PoseStamped& pose : poses)
690 moveit_msgs::PlaceLocation location;
691 location.pre_place_approach.direction.vector.z = -1.0;
692 location.post_place_retreat.direction.vector.x = -1.0;
693 location.pre_place_approach.direction.header.frame_id =
getRobotModel()->getModelFrame();
696 location.pre_place_approach.min_distance = 0.1;
697 location.pre_place_approach.desired_distance = 0.2;
698 location.post_place_retreat.min_distance = 0.0;
699 location.post_place_retreat.desired_distance = 0.2;
702 location.place_pose = pose;
703 locations.push_back(location);
714 return moveit::core::MoveItErrorCode::FAILURE;
719 return moveit::core::MoveItErrorCode::COMMUNICATION_FAILURE;
745 return moveit::core::MoveItErrorCode::FAILURE;
750 return moveit::core::MoveItErrorCode::COMMUNICATION_FAILURE;
778 std::map<std::string, moveit_msgs::CollisionObject> objects = psi.
getObjects(std::vector<std::string>(1,
object));
783 "Asked for grasps for the object '" <<
object <<
"', but the object could not be found");
784 return moveit::core::MoveItErrorCode::INVALID_OBJECT_NAME;
796 <<
"' is not available."
797 " This has to be implemented and started separately.");
798 return moveit::core::MoveItErrorCode::COMMUNICATION_FAILURE;
801 moveit_msgs::GraspPlanning::Request request;
802 moveit_msgs::GraspPlanning::Response
response;
805 request.target = object;
810 response.error_code.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
813 return moveit::core::MoveItErrorCode::FAILURE;
824 return moveit::core::MoveItErrorCode::FAILURE;
829 return moveit::core::MoveItErrorCode::COMMUNICATION_FAILURE;
832 moveit_msgs::MoveGroupGoal goal;
834 goal.planning_options.plan_only =
true;
835 goal.planning_options.look_around =
false;
836 goal.planning_options.replan =
false;
837 goal.planning_options.planning_scene_diff.is_diff =
true;
838 goal.planning_options.planning_scene_diff.robot_state.is_diff =
true;
865 return moveit::core::MoveItErrorCode::FAILURE;
870 return moveit::core::MoveItErrorCode::COMMUNICATION_FAILURE;
873 moveit_msgs::MoveGroupGoal goal;
875 goal.planning_options.plan_only =
false;
876 goal.planning_options.look_around =
can_look_;
879 goal.planning_options.planning_scene_diff.is_diff =
true;
880 goal.planning_options.planning_scene_diff.robot_state.is_diff =
true;
885 return moveit::core::MoveItErrorCode::SUCCESS;
910 return moveit::core::MoveItErrorCode::FAILURE;
915 return moveit::core::MoveItErrorCode::COMMUNICATION_FAILURE;
918 moveit_msgs::ExecuteTrajectoryGoal goal;
919 goal.trajectory = trajectory;
924 return moveit::core::MoveItErrorCode::SUCCESS;
945 moveit_msgs::RobotTrajectory& msg,
const moveit_msgs::Constraints& path_constraints,
946 bool avoid_collisions, moveit_msgs::MoveItErrorCodes& error_code)
948 moveit_msgs::GetCartesianPath::Request req;
949 moveit_msgs::GetCartesianPath::Response
res;
955 req.waypoints = waypoints;
957 req.path_constraints = path_constraints;
958 req.avoid_collisions = avoid_collisions;
965 error_code =
res.error_code;
966 if (
res.error_code.val == moveit_msgs::MoveItErrorCodes::SUCCESS)
976 error_code.val = error_code.FAILURE;
985 std_msgs::String event;
991 bool attachObject(
const std::string&
object,
const std::string& link,
const std::vector<std::string>& touch_links)
1005 moveit_msgs::AttachedCollisionObject aco;
1006 aco.object.id = object;
1007 aco.link_name.swap(l);
1008 if (touch_links.empty())
1009 aco.touch_links.push_back(aco.link_name);
1011 aco.touch_links = touch_links;
1012 aco.object.operation = moveit_msgs::CollisionObject::ADD;
1019 moveit_msgs::AttachedCollisionObject aco;
1022 aco.link_name =
name;
1024 aco.object.id =
name;
1025 aco.object.operation = moveit_msgs::CollisionObject::REMOVE;
1026 if (aco.link_name.empty() && aco.object.id.empty())
1030 for (
const std::string& lname : lnames)
1032 aco.link_name = lname;
1098 request.goal_constraints.resize(1);
1105 std::size_t goal_count = 0;
1107 goal_count = std::max(goal_count, pose_target.second.size());
1113 request.goal_constraints.resize(goal_count);
1117 for (std::size_t i = 0; i < pose_target.second.size(); ++i)
1122 c.position_constraints.clear();
1124 c.orientation_constraints.clear();
1143 moveit_msgs::PickupGoal
constructPickupGoal(
const std::string&
object, std::vector<moveit_msgs::Grasp>&& grasps,
1144 bool plan_only =
false)
const
1146 moveit_msgs::PickupGoal goal;
1147 goal.target_name = object;
1151 goal.possible_grasps = std::move(grasps);
1153 goal.allow_gripper_support_collision =
true;
1161 goal.planning_options.plan_only = plan_only;
1162 goal.planning_options.look_around =
can_look_;
1165 goal.planning_options.planning_scene_diff.is_diff =
true;
1166 goal.planning_options.planning_scene_diff.robot_state.is_diff =
true;
1171 std::vector<moveit_msgs::PlaceLocation>&& locations,
1172 bool plan_only =
false)
const
1174 moveit_msgs::PlaceGoal goal;
1176 goal.attached_object_name = object;
1178 goal.place_locations = std::move(locations);
1180 goal.allow_gripper_support_collision =
true;
1188 goal.planning_options.plan_only = plan_only;
1189 goal.planning_options.look_around =
can_look_;
1192 goal.planning_options.planning_scene_diff.is_diff =
true;
1193 goal.planning_options.planning_scene_diff.robot_state.is_diff =
true;
1209 path_constraints_ = std::make_unique<moveit_msgs::Constraints>(
static_cast<moveit_msgs::Constraints
>(*msg_m));
1242 std::vector<std::string>
c;
1254 return moveit_msgs::Constraints();
1262 return moveit_msgs::TrajectoryConstraints();
1274 void setWorkspace(
double minx,
double miny,
double minz,
double maxx,
double maxy,
double maxz)
1293 conn->setParams(host, port);
1294 if (conn->connect())
1299 catch (std::exception& ex)
1311 std::unique_ptr<actionlib::SimpleActionClient<moveit_msgs::MoveGroupAction>>
move_action_client_;
1312 std::unique_ptr<actionlib::SimpleActionClient<moveit_msgs::ExecuteTrajectoryAction>>
execute_action_client_;
1313 std::unique_ptr<actionlib::SimpleActionClient<moveit_msgs::PickupAction>>
pick_action_client_;
1369 throw std::runtime_error(
"ROS does not seem to be running");
1386 const std::shared_ptr<tf2_ros::Buffer>& tf_buffer,
1398 : remembered_joint_values_(std::move(other.remembered_joint_values_)), impl_(other.impl_)
1400 other.impl_ =
nullptr;
1408 impl_ = other.
impl_;
1409 remembered_joint_values_ = std::move(other.remembered_joint_values_);
1410 other.impl_ =
nullptr;
1449 const std::string&
group)
const
1455 const std::map<std::string, std::string>& params,
bool replace)
1556 std::vector<moveit_msgs::Grasp> grasps,
1557 bool plan_only =
false)
const
1563 std::vector<moveit_msgs::PlaceLocation> locations,
1564 bool plan_only =
false)
const
1569 std::vector<moveit_msgs::PlaceLocation>
1597 moveit_msgs::RobotTrajectory& trajectory,
bool avoid_collisions,
1598 moveit_msgs::MoveItErrorCodes* error_code)
1600 return computeCartesianPath(waypoints, eef_step, trajectory, moveit_msgs::Constraints(), avoid_collisions, error_code);
1604 moveit_msgs::RobotTrajectory& trajectory,
1605 const moveit_msgs::Constraints& path_constraints,
bool avoid_collisions,
1606 moveit_msgs::MoveItErrorCodes* error_code)
1608 moveit_msgs::MoveItErrorCodes err_tmp;
1609 moveit_msgs::MoveItErrorCodes& err = error_code ? *error_code : err_tmp;
1652 std::map<std::string, double> positions;
1657 for (
size_t x = 0;
x < names.size(); ++
x)
1659 positions[names[
x]] = it->second[
x];
1696 if (joint_values.size() != n_group_joints)
1700 <<
" has " << n_group_joints <<
" joints");
1711 for (
const auto& pair : variable_values)
1713 if (std::find(allowed.begin(), allowed.end(), pair.first) == allowed.end())
1715 ROS_ERROR_STREAM(
"joint variable " << pair.first <<
" is not part of group "
1727 const std::vector<double>& variable_values)
1729 if (variable_names.size() != variable_values.size())
1735 for (
const auto& variable_name : variable_names)
1737 if (std::find(allowed.begin(), allowed.end(), variable_name) == allowed.end())
1739 ROS_ERROR_STREAM(
"joint variable " << variable_name <<
" is not part of group "
1788 const std::string& end_effector_link)
1795 geometry_msgs::Pose msg =
tf2::toMsg(eef_pose);
1800 const std::string& end_effector_link)
1806 const std::string& end_effector_link)
1812 const std::string& end_effector_link)
1814 geometry_msgs::Pose msg =
tf2::toMsg(eef_pose);
1867 std::vector<geometry_msgs::PoseStamped> pose_msg(1);
1876 std::vector<geometry_msgs::PoseStamped> pose_msg(1);
1877 pose_msg[0].pose = target;
1885 std::vector<geometry_msgs::PoseStamped> targets(1, target);
1891 std::vector<geometry_msgs::PoseStamped> pose_out(target.size());
1894 for (std::size_t i = 0; i < target.size(); ++i)
1897 pose_out[i].header.stamp = tm;
1898 pose_out[i].header.frame_id = frame_id;
1904 const std::string& end_effector_link)
1906 std::vector<geometry_msgs::PoseStamped> target_stamped(target.size());
1909 for (std::size_t i = 0; i < target.size(); ++i)
1911 target_stamped[i].pose = target[i];
1912 target_stamped[i].header.stamp = tm;
1913 target_stamped[i].header.frame_id = frame_id;
1919 const std::string& end_effector_link)
1938 const std::vector<geometry_msgs::PoseStamped>&
1947 geometry_msgs::PoseStamped& target)
1949 if (desired_frame != target.header.frame_id)
1951 geometry_msgs::PoseStamped target_in(target);
1961 geometry_msgs::PoseStamped target;
1969 target.pose.orientation.x = 0.0;
1970 target.pose.orientation.y = 0.0;
1971 target.pose.orientation.z = 0.0;
1972 target.pose.orientation.w = 1.0;
1976 target.pose.position.x =
x;
1977 target.pose.position.y =
y;
1978 target.pose.position.z =
z;
1986 geometry_msgs::PoseStamped target;
1994 target.pose.position.x = 0.0;
1995 target.pose.position.y = 0.0;
1996 target.pose.position.z = 0.0;
2008 const std::string& end_effector_link)
2010 geometry_msgs::PoseStamped target;
2018 target.pose.position.x = 0.0;
2019 target.pose.position.y = 0.0;
2020 target.pose.position.z = 0.0;
2024 target.pose.orientation.x =
x;
2025 target.pose.orientation.y =
y;
2026 target.pose.orientation.z =
z;
2027 target.pose.orientation.w = w;
2092 moveit::core::RobotStatePtr current_state;
2093 std::vector<double>
values;
2095 current_state->copyJointGroupPositions(
getName(), values);
2101 std::vector<double>
r;
2108 const std::string& eef = end_effector_link.empty() ?
getEndEffectorLink() : end_effector_link;
2109 Eigen::Isometry3d pose;
2115 moveit::core::RobotStatePtr current_state;
2121 pose = current_state->getGlobalLinkTransform(lm);
2124 geometry_msgs::PoseStamped pose_msg;
2134 Eigen::Isometry3d pose;
2140 moveit::core::RobotStatePtr current_state;
2145 pose = current_state->getGlobalLinkTransform(lm);
2148 geometry_msgs::PoseStamped pose_msg;
2157 std::vector<double> result;
2163 moveit::core::RobotStatePtr current_state;
2170 geometry_msgs::TransformStamped tfs =
tf2::eigenToTransform(current_state->getGlobalLinkTransform(lm));
2171 double pitch, roll, yaw;
2172 tf2::getEulerYPR<geometry_msgs::Quaternion>(tfs.transform.rotation, yaw, pitch, roll);
2199 moveit::core::RobotStatePtr current_state;
2201 return current_state;
2344 return attachObject(
object, link, std::vector<std::string>());
2348 const std::vector<std::string>& touch_links)