xarm_simple_planner.cpp
Go to the documentation of this file.
1 /* Copyright 2018 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: Jason Peng <jason@ufactory.cc>
6  ============================================================================*/
9 
10 #include <moveit_msgs/DisplayRobotState.h>
11 #include <moveit_msgs/DisplayTrajectory.h>
12 
13 #include <moveit_msgs/AttachedCollisionObject.h>
14 #include <moveit_msgs/CollisionObject.h>
15 
17 #include <std_msgs/Bool.h>
18 #include <xarm_planner/pose_plan.h>
19 #include <xarm_planner/joint_plan.h>
20 #include <xarm_planner/exec_plan.h>
21 #include <xarm_planner/single_straight_plan.h>
22 
23 #define SPINNER_THREAD_NUM 2
24 
25 /* Used for Cartesian path computation, please modify as needed: */
26 const double jump_threshold = 0.0;
27 const double eef_step = 0.005;
28 const double maxV_scale_factor = 0.3; // check!!
29 
30 
31 namespace rvt = rviz_visual_tools;
32 
34 {
35  public:
36  XArmSimplePlanner(const std::string plan_group_name):spinner(SPINNER_THREAD_NUM), group(plan_group_name){init();};
39  void start();
40  void stop();
41 
42  static std::string PLANNING_GROUP; // declaration of static class member
43 
44  private:
48  std::vector<std::string> joint_names;
52 
57  ros::Subscriber exec_plan_sub; /* non-blocking*/
58  ros::ServiceServer exec_plan_srv; /* blocking with result feedback */
59 
60  void init();
61  bool do_pose_plan(xarm_planner::pose_plan::Request &req, xarm_planner::pose_plan::Response &res);
62  bool do_joint_plan(xarm_planner::joint_plan::Request &req, xarm_planner::joint_plan::Response &res);
63  bool do_single_cartesian_plan(xarm_planner::single_straight_plan::Request &req, xarm_planner::single_straight_plan::Response &res);
64  bool exec_plan_cb(xarm_planner::exec_plan::Request &req, xarm_planner::exec_plan::Response &res);
65  void execute_plan_topic(const std_msgs::Bool::ConstPtr& exec);
66  void show_trail(bool plan_result);
67 };
68 
70 {
72 
73  display_path = node_handle.advertise<moveit_msgs::DisplayTrajectory>("move_group/display_planned_path", 1, true); /*necessary?*/
74 
75  ROS_INFO_NAMED("move_group_planner", "Reference frame: %s", group.getPlanningFrame().c_str());
76 
77  ROS_INFO_NAMED("move_group_planner", "End effector link: %s", group.getEndEffectorLink().c_str());
78 
79  /* Notice: the correct way to specify member function as callbacks */
83 
86 
88  Eigen::Isometry3d text_pose = Eigen::Isometry3d::Identity();
89  text_pose.translation().z() = 0.8;
90  visual_tools->publishText(text_pose, "xArm Planner Demo", rvt::WHITE, rvt::XLARGE);
92 
93 }
94 
96 {
97  ROS_INFO("Spinning");
98  spinner.start();
99 }
100 
102 {
103  spinner.stop();
104 }
105 
106 void XArmSimplePlanner::show_trail(bool plan_result)
107 {
108  if(plan_result)
109  {
110  ROS_INFO_NAMED("xarm_planner", "Visualizing plan as trajectory line");
111 
113  const robot_state::JointModelGroup* joint_model_group = group.getCurrentState()->getJointModelGroup(PLANNING_GROUP);
116  }
117 }
118 
119 bool XArmSimplePlanner::do_pose_plan(xarm_planner::pose_plan::Request &req, xarm_planner::pose_plan::Response &res)
120 {
121  group.setPoseTarget(req.target);
122 
123  ROS_INFO("xarm_planner received new target: [ position: (%f, %f, %f), orientation: (%f, %f, %f, %f)", \
124  req.target.position.x, req.target.position.y, req.target.position.z, req.target.orientation.x, \
125  req.target.orientation.y, req.target.orientation.z, req.target.orientation.w);
126 
127  bool success = (group.plan(my_xarm_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
128  res.success = success;
129  ROS_INFO_NAMED("move_group_planner", "This plan (pose goal) %s", success ? "SUCCEEDED" : "FAILED");
130 
131  show_trail(success);
132 
133  return success;
134 }
135 
136 bool XArmSimplePlanner::do_single_cartesian_plan(xarm_planner::single_straight_plan::Request &req, xarm_planner::single_straight_plan::Response &res)
137 {
138  std::vector<geometry_msgs::Pose> waypoints;
139  waypoints.push_back(req.target);
141  moveit_msgs::RobotTrajectory trajectory;
142 
143  double fraction = group.computeCartesianPath(waypoints, eef_step, jump_threshold, trajectory);
144  bool success = true;
145  if(fraction<0.9)
146  success = false;
147  else
148  {
149  my_xarm_plan.trajectory_ = trajectory;
150  }
151  fprintf(stderr, "[XArmSimplePlanner::do_single_cartesian_plan(): ] Coverage: %lf\n", fraction);
152 
153  res.success = success;
154  show_trail(success);
155 
156  return success;
157 
158 }
159 
160 bool XArmSimplePlanner::do_joint_plan(xarm_planner::joint_plan::Request &req, xarm_planner::joint_plan::Response &res)
161 {
162  ROS_INFO("move_group_planner received new plan Request");
163  group.setJointValueTarget(req.target);
164 
165  bool success = (group.plan(my_xarm_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
166  res.success = success;
167  ROS_INFO_NAMED("move_group_planner", "This plan (joint goal) %s", success ? "SUCCEEDED" : "FAILED");
168  show_trail(success);
169  return success;
170 }
171 
172 bool XArmSimplePlanner::exec_plan_cb(xarm_planner::exec_plan::Request &req, xarm_planner::exec_plan::Response &res)
173 {
174  if(req.exec)
175  {
176  ROS_INFO("Received Execution Service Request");
177  bool finish_ok = (group.execute(my_xarm_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS); /* return after execution finish */
178  res.success = finish_ok;
179  return finish_ok;
180  }
181 
182  res.success = false;
183  return false;
184 }
185 
186 /* execution subscriber call-back function */
187 void XArmSimplePlanner::execute_plan_topic(const std_msgs::Bool::ConstPtr& exec)
188 {
189  if(exec->data)
190  {
191  ROS_INFO("Received Execution Command !!!!!");
192  group.asyncExecute(my_xarm_plan); /* return without waiting for finish */
193  }
194 }
195 
196 
197 std::string XArmSimplePlanner::PLANNING_GROUP; // Definition of static class member
198 
199 int main(int argc, char** argv)
200 {
201  ros::init(argc, argv, "xarm_move_group_planner");
202  ros::NodeHandle nh;
203  int jnt_num;
204  nh.getParam("DOF", jnt_num);
205  switch(jnt_num)
206  {
207  case 7:
208  {
210  break;
211  }
212  case 6:
213  {
215  break;
216  }
217  case 5:
218  {
220  break;
221  }
222  default:
223  {
224  ROS_ERROR("ROS parameter DOF not correct, please CHECK!!");
225  }
226  }
227 
228  XArmSimplePlanner planner;
229 
230  planner.start();
231 
232  ROS_INFO("Waiting for \'pose_plan\' or \'joint_plan\' service Request ...");
233 
234  /* necessary: because AsyncSpinner is not operating in the same thread */
236  return 0;
237 }
238 
const double jump_threshold
#define ROS_INFO_NAMED(name,...)
moveit_visual_tools::MoveItVisualTools * visual_tools
ros::AsyncSpinner spinner
bool publishTrajectoryLine(const moveit_msgs::RobotTrajectory &trajectory_msg, const moveit::core::LinkModel *ee_parent_link, const robot_model::JointModelGroup *arm_jmg, const rviz_visual_tools::colors &color=rviz_visual_tools::LIME_GREEN)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool setJointValueTarget(const std::vector< double > &group_variable_values)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Subscriber exec_plan_sub
double computeCartesianPath(const std::vector< geometry_msgs::Pose > &waypoints, double eef_step, double jump_threshold, moveit_msgs::RobotTrajectory &trajectory, bool avoid_collisions=true, moveit_msgs::MoveItErrorCodes *error_code=NULL)
bool setPoseTarget(const Eigen::Affine3d &end_effector_pose, const std::string &end_effector_link="")
const double maxV_scale_factor
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
stderr
bool do_single_cartesian_plan(xarm_planner::single_straight_plan::Request &req, xarm_planner::single_straight_plan::Response &res)
bool do_pose_plan(xarm_planner::pose_plan::Request &req, xarm_planner::pose_plan::Response &res)
const std::string & getPlanningFrame() const
const std::string & getEndEffectorLink() const
ros::ServiceServer sing_cart_srv
ros::ServiceServer exec_plan_srv
void show_trail(bool plan_result)
ros::NodeHandle node_handle
bool exec_plan_cb(xarm_planner::exec_plan::Request &req, xarm_planner::exec_plan::Response &res)
MoveItErrorCode asyncExecute(const Plan &plan)
static std::string PLANNING_GROUP
#define ROS_INFO(...)
ros::ServiceServer plan_pose_srv
ros::ServiceServer plan_joint_srv
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")
robot_state::RobotStatePtr getCurrentState(double wait=1)
void setMaxVelocityScalingFactor(double max_velocity_scaling_factor)
moveit::planning_interface::PlanningSceneInterface planning_scene_interface
XArmSimplePlanner(const std::string plan_group_name)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
MoveItErrorCode execute(const Plan &plan)
void execute_plan_topic(const std_msgs::Bool::ConstPtr &exec)
moveit::planning_interface::MoveGroupInterface::Plan my_xarm_plan
const double eef_step
bool getParam(const std::string &key, std::string &s) const
bool publishText(const Eigen::Affine3d &pose, const std::string &text, colors color=WHITE, scales scale=MEDIUM, bool static_id=true)
const std::vector< std::string > & getJointNames()
std::vector< std::string > joint_names
ros::Publisher display_path
int main(int argc, char **argv)
moveit::planning_interface::MoveGroupInterface group
#define ROS_ERROR(...)
#define SPINNER_THREAD_NUM
ROSCPP_DECL void waitForShutdown()
bool do_joint_plan(xarm_planner::joint_plan::Request &req, xarm_planner::joint_plan::Response &res)


xarm_planner
Author(s):
autogenerated on Sat May 8 2021 02:51:51