11 #include <descartes_planner/dense_planner.h> 12 #include <descartes_planner/sparse_planner.h> 13 #include <descartes_trajectory/axial_symmetric_pt.h> 16 #include <moveit_msgs/GetMotionPlan.h> 19 #include <moveit_msgs/ApplyPlanningScene.h> 20 #include <moveit_msgs/GetPlanningScene.h> 22 #include <moveit_msgs/GetMotionPlan.h> 56 const geometry_msgs::Point& pt)
58 Eigen::Affine3d eigen_pose;
59 Eigen::Vector3d eigen_pt;
65 Eigen::Affine3d to_point;
66 to_point = Eigen::Translation3d(eigen_pt);
69 Eigen::Affine3d flip_z;
70 flip_z = Eigen::AngleAxisd(
M_PI, Eigen::Vector3d::UnitY());
73 return eigen_pose * to_point * flip_z;
77 const double z_adjust)
79 Eigen::Affine3d eigen_pose;
87 const double z_adjust)
90 Eigen::Affine3d flip_z;
91 flip_z = Eigen::AngleAxisd(
M_PI, Eigen::Vector3d::UnitY());
93 return ref_pose * Eigen::Translation3d(0, 0, z_adjust) * flip_z;
96 static double calcDefaultTime(
const std::vector<double>& a,
const std::vector<double>& b,
101 std::vector<double> result (a.size());
102 std::transform(a.begin(), a.end(), b.begin(), result.begin(), [] (
double a,
double b)
104 return std::abs(a - b);
107 return *std::max_element(result.begin(), result.end()) / max_joint_vel;
111 const std::vector<std::vector<double>>& solution,
112 const descartes_core::RobotModel& model)
115 std::vector<double> joint_point;
116 std::vector<double> dummy;
117 trajectory_msgs::JointTrajectory ros_trajectory;
119 const int dof = model.getDOF();
121 for (std::size_t i = 0; i < solution.size(); ++i)
123 assert(solution[i].
size()==dof);
125 trajectory_msgs::JointTrajectoryPoint pt;
126 pt.positions = solution[i];
127 pt.velocities.resize(joint_point.size(), 0.0);
128 pt.accelerations.resize(joint_point.size(), 0.0);
129 pt.effort.resize(joint_point.size(), 0.0);
140 const auto& prev = ros_trajectory.points.back().positions;
141 const auto& next = pt.positions;
146 pt.time_from_start = from_start;
148 ros_trajectory.points.push_back(pt);
151 return ros_trajectory;
156 const descartes_core::RobotModel& model)
159 std::vector<double> joint_point;
160 std::vector<double> dummy;
161 trajectory_msgs::JointTrajectory ros_trajectory;
163 for (std::size_t i = 0; i < solution.size(); ++i)
165 solution[i]->getNominalJointPose(dummy, model, joint_point);
167 trajectory_msgs::JointTrajectoryPoint pt;
168 pt.positions = joint_point;
169 pt.velocities.resize(joint_point.size(), 0.0);
170 pt.accelerations.resize(joint_point.size(), 0.0);
171 pt.effort.resize(joint_point.size(), 0.0);
173 double time_step = solution[i]->getTiming().upper;
174 if (time_step == 0.0)
185 const auto& prev = ros_trajectory.points.back().positions;
186 const auto& next = pt.positions;
196 pt.time_from_start = from_start;
198 ros_trajectory.points.push_back(pt);
201 return ros_trajectory;
205 trajectory_msgs::JointTrajectory& traj,
206 const std::string world_frame)
208 traj.joint_names = joints;
209 traj.header.frame_id = world_frame;
214 trajectory_msgs::JointTrajectory& traj,
215 const double sim_time_scale)
217 traj.joint_names = orig_traj.joint_names;
218 traj.header.frame_id = orig_traj.header.frame_id;
219 traj.header.stamp = orig_traj.header.stamp + orig_traj.points.back().time_from_start;
224 for (
int i = 0; i < traj.points.size(); i++)
226 traj.points[i].time_from_start -= base_time;
229 traj.points[i].time_from_start *= sim_time_scale;
235 sensor_msgs::JointStateConstPtr state = ros::topic::waitForMessage<sensor_msgs::JointState>(
238 throw std::runtime_error(
"Joint state message capture failed");
239 return state->position;
243 ros::ServiceClient& planning_scene_diff_client,
const moveit_msgs::CollisionObject& c_obj)
254 moveit_msgs::ApplyPlanningScene srv;
256 moveit_msgs::PlanningScene planning_scene_msg;
257 planning_scene_msg.world.collision_objects.push_back(c_obj);
258 planning_scene_msg.is_diff =
true;
259 srv.request.scene = planning_scene_msg;
261 if(planning_scene_diff_client.
call(srv))
277 double scale = 0.001;
278 Eigen::Vector3d scale_vector(scale, scale, scale);
279 moveit_msgs::AttachedCollisionObject attached_full_eef;
280 attached_full_eef.link_name =
"eef_base_link";
283 geometry_msgs::Pose pose;
284 pose.position.x = 0.0;
285 pose.position.y = 0.0;
286 pose.position.z = 0.0;
287 pose.orientation.w= 0.0;
288 pose.orientation.x= 0.0;
289 pose.orientation.y= 0.0;
290 pose.orientation.z= 0.0;
294 "package://asw_end_effector/meshes/collision/asw_hotend_end_effector_hull_bulky.stl", scale_vector);
295 shape_msgs::Mesh mesh;
298 mesh = boost::get<shape_msgs::Mesh>(mesh_msg);
300 attached_full_eef.object.header.frame_id =
"eef_tcp_frame";
301 attached_full_eef.object.id =
"full_eef";
303 attached_full_eef.object.meshes.resize(1);
304 attached_full_eef.object.mesh_poses.resize(1);
306 attached_full_eef.object.meshes[0] = mesh;
307 attached_full_eef.object.mesh_poses[0] = pose;
311 attached_full_eef.object.operation = moveit_msgs::CollisionObject::ADD;
315 attached_full_eef.object.operation = moveit_msgs::CollisionObject::REMOVE;
318 return attached_full_eef;
327 if(!planning_scene_fetch_client.waitForExistence())
329 ROS_ERROR_STREAM(
"[Process Planning] cannot connect with get fetch planning scene server...");
332 moveit_msgs::GetPlanningScene fetch_srv;
333 fetch_srv.request.components.components = moveit_msgs::PlanningSceneComponents::WORLD_OBJECT_GEOMETRY;
335 if(!planning_scene_fetch_client.call(fetch_srv))
346 moveit_msgs::ApplyPlanningScene srv;
348 moveit_msgs::PlanningScene planning_scene_msg;
349 for(
auto& existing_obj : fetch_srv.response.scene.world.collision_objects)
351 existing_obj.operation = moveit_msgs::CollisionObject::REMOVE;
352 planning_scene_msg.world.collision_objects.push_back(existing_obj);
357 planning_scene_msg.is_diff =
true;
358 srv.request.scene = planning_scene_msg;
360 if(planning_scene_diff_client.
call(srv))
374 const std::vector<double>& stop,
double dtheta)
378 for (std::size_t i = 0; i < path.size(); ++i)
380 result.push_back(boost::make_shared<descartes_trajectory::JointTrajectoryPt>(path[i]));
386 const std::string& group_name,
const std::vector<double>& joints_start,
387 const std::vector<double>& joints_stop, moveit::core::RobotModelConstPtr model)
390 robot_state::RobotState goal_state(model);
391 goal_state.setJointGroupPositions(group, joints_stop);
393 moveit_msgs::GetMotionPlan::Request req;
394 req.motion_plan_request.group_name = group_name;
400 req.motion_plan_request.workspace_parameters.header.frame_id = model->getRootLinkName();
401 req.motion_plan_request.workspace_parameters.header.stamp =
ros::Time::now();
405 moveit_msgs::RobotState start_state;
406 start_state.is_diff =
false;
407 sensor_msgs::JointState joint_state;
409 joint_state.position = joints_start;
410 start_state.joint_state = joint_state;
411 req.motion_plan_request.start_state = start_state;
415 req.motion_plan_request.goal_constraints.push_back(c);
420 std::string planner_name;
426 if(
"ompl" != planner_name)
428 ROS_INFO_STREAM(
"[Moveit Transition Planning] planner: " << planner_name);
438 trajectory_msgs::JointTrajectory jt;
439 moveit_msgs::GetMotionPlan::Response
res;
440 if (client.
call(req, res))
442 jt = res.motion_plan_response.trajectory.joint_trajectory;
446 ROS_ERROR(
"%s: Unable to call MoveIt path planning service: '%s' or planning failed",
454 const std::string& group_name,
455 const std::vector<double>& joints_start,
456 const std::vector<double>& joints_stop,
457 const std::vector<double>& initial_pose,
458 moveit::core::RobotModelConstPtr model,
459 const bool force_insert_reset)
463 std::string planner_name;
470 robot_state::RobotState goal_state(model);
471 goal_state.setJointGroupPositions(group, joints_stop);
473 moveit_msgs::GetMotionPlan::Request req;
474 req.motion_plan_request.group_name = group_name;
479 if(
"ompl" == planner_name)
484 req.motion_plan_request.workspace_parameters.header.frame_id = model->getRootLinkName();
485 req.motion_plan_request.workspace_parameters.header.stamp =
ros::Time::now();
489 moveit_msgs::RobotState start_state;
490 start_state.is_diff =
false;
491 sensor_msgs::JointState joint_state;
493 joint_state.position = joints_start;
494 start_state.joint_state = joint_state;
495 req.motion_plan_request.start_state = start_state;
499 req.motion_plan_request.goal_constraints.push_back(c_goal);
505 trajectory_msgs::JointTrajectory jt;
506 moveit_msgs::GetMotionPlan::Response
res;
508 if(
"ompl" != planner_name)
510 ROS_INFO_STREAM(
"[Moveit Transition Planning] planner: " << planner_name);
517 bool insert_reset =
false;
519 if(!force_insert_reset)
521 bool plan_call_success = client.
call(req, res);
522 if (plan_call_success && res.motion_plan_response.error_code.SUCCESS == res.motion_plan_response.error_code.val)
525 jt = res.motion_plan_response.trajectory.joint_trajectory;
527 if (
"ompl" == planner_name)
531 ROS_WARN_STREAM(
"[Tr Planning] transition planning jt num overflow:" << jt.points.size()
532 <<
", current threshold: " 534 <<
". reset transition is forced.");
546 if(force_insert_reset || insert_reset)
548 ROS_WARN(
"[Tr Planning] try resetting robot to intial pose and replan");
550 moveit_msgs::GetMotionPlan::Request req_to_reset = req;
551 moveit_msgs::GetMotionPlan::Request req_to_goal = req;
554 robot_state::RobotState reset_goal_state(model);
555 reset_goal_state.setJointGroupPositions(group, initial_pose);
557 req_to_reset.motion_plan_request.goal_constraints.clear();
558 req_to_reset.motion_plan_request.goal_constraints.push_back(c_reset);
561 moveit_msgs::RobotState start_state_to_goal;
562 joint_state.position = initial_pose;
563 start_state_to_goal.is_diff =
false;
564 start_state_to_goal.joint_state = joint_state;
565 req_to_goal.motion_plan_request.start_state = start_state_to_goal;
567 req_to_goal.motion_plan_request.goal_constraints.clear();
568 req_to_goal.motion_plan_request.goal_constraints.push_back(c_goal);
571 trajectory_msgs::JointTrajectory jt_to_reset;
572 trajectory_msgs::JointTrajectory jt_to_goal;
575 bool plan_call_success = client.
call(req_to_reset, res);
576 if (plan_call_success && res.motion_plan_response.error_code.SUCCESS == res.motion_plan_response.error_code.val)
578 jt_to_reset = res.motion_plan_response.trajectory.joint_trajectory;
579 ROS_WARN(
"[Tr Planning] reset planning success.");
583 ROS_ERROR(
"%s: Unable to call MoveIt path planning service: '%s' or planning failed, AFTER RESETTING POSE",
592 plan_call_success = client.
call(req_to_goal, res);
593 if (plan_call_success && res.motion_plan_response.error_code.SUCCESS == res.motion_plan_response.error_code.val)
595 jt_to_goal = res.motion_plan_response.trajectory.joint_trajectory;
596 ROS_WARN(
"[Tr Planning] reset to goal planning success.");
600 ROS_ERROR(
"%s: Unable to call MoveIt path planning service: '%s' or planning failed, AFTER RESETTING POSE",
612 for(
auto pt : jt_to_goal.points)
614 pt.time_from_start += last_time;
615 jt.points.push_back(pt);
622 descartes_core::RobotModel& model,
const std::string& group_name,
623 moveit::core::RobotModelConstPtr moveit_model,
const std::vector<double>& start,
624 const std::vector<double>& stop)
630 bool collision_free =
true;
631 for (std::size_t i = 0; i < joint_approach.size(); ++i)
633 if (!joint_approach[i]->isValid(model))
635 collision_free =
false;
655 const std::vector<double> &target)
657 const double FREE_SPACE_MAX_ANGLE_DELTA =
661 const double FREE_SPACE_ANGLE_PENALTY =
669 for (std::size_t i = 0; i < source.size(); ++i)
671 double diff = std::abs(source[i] - target[i]);
672 if (diff > FREE_SPACE_MAX_ANGLE_DELTA)
673 cost += FREE_SPACE_ANGLE_PENALTY * diff;
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
bool addCollisionObject(ros::ServiceClient &planning_scene, const moveit_msgs::CollisionObject &c_obj)
bool clearAllCollisionObjects(ros::ServiceClient &planning_scene)
void poseMsgToEigen(const geometry_msgs::Pose &m, Eigen::Affine3d &e)
static const std::string GET_PLANNING_SCENE_SERVICE
static const std::string DEFAULT_MOVEIT_PLANNER_ID
Eigen::Affine3d createNominalTransform(const geometry_msgs::Pose &ref_pose, const geometry_msgs::Point &pt)
JointVector interpolateJoint(const std::vector< double > &start, const std::vector< double > &stop, double dtheta)
std::vector< std::vector< double > > JointVector
static const std::string DEFAULT_MOVEIT_FRAME_ID
std::size_t size(custom_string const &s)
bool call(MReq &req, MRes &res)
static const double DEFAULT_MOVEIT_VELOCITY_SCALING
void appendTrajectoryHeaders(const trajectory_msgs::JointTrajectory &orig_traj, trajectory_msgs::JointTrajectory &traj, const double sim_time_scale=0.0)
static const double DEFAULT_TIME_UNDEFINED_VELOCITY
static const double DEFAULT_ANGLE_DISCRETIZATION
trajectory_msgs::JointTrajectory toROSTrajectory(const DescartesTraj &solution, const descartes_core::RobotModel &model)
std::vector< double > getCurrentJointState(const std::string &topic)
static const double DEFAULT_JOINT_VELOCITY
static const double DEFAULT_JOINT_WAIT_TIME
DescartesTraj createJointPath(const std::vector< double > &start, const std::vector< double > &stop, double dtheta=M_PI/180.0)
static const int DEFAULT_MOVEIT_NUM_PLANNING_ATTEMPTS
static double calcDefaultTime(const std::vector< double > &a, const std::vector< double > &b, double max_joint_vel)
moveit_msgs::AttachedCollisionObject addFullEndEffectorCollisionObject(bool is_add)
void fillTrajectoryHeaders(const std::vector< std::string > &joints, trajectory_msgs::JointTrajectory &traj, const std::string world_frame)
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
ROSCPP_DECL bool get(const std::string &key, std::string &s)
moveit_msgs::Constraints constructGoalConstraints(const robot_state::RobotState &state, const robot_model::JointModelGroup *jmg, double tolerance_below, double tolerance_above)
static const int RRT_WEIRD_SOL_NUM_THRESHOLD
const std::vector< std::string > & getActiveJointModelNames() const
void pointMsgToEigen(const geometry_msgs::Point &m, Eigen::Vector3d &e)
trajectory_msgs::JointTrajectory getMoveitPlan(const std::string &group_name, const std::vector< double > &joints_start, const std::vector< double > &joints_stop, moveit::core::RobotModelConstPtr model)
static const double DEFAULT_MOVEIT_PLANNING_TIME
#define ROS_WARN_STREAM(args)
trajectory_msgs::JointTrajectory getMoveitTransitionPlan(const std::string &group_name, const std::vector< double > &joints_start, const std::vector< double > &joints_stop, const std::vector< double > &initial_pose, moveit::core::RobotModelConstPtr model, const bool force_insert_reset=false)
trajectory_msgs::JointTrajectory planFreeMove(descartes_core::RobotModel &model, const std::string &group_name, moveit::core::RobotModelConstPtr moveit_model, const std::vector< double > &start, const std::vector< double > &stop)
std::vector< descartes_core::TrajectoryPtPtr > DescartesTraj
bool constructMsgFromShape(const Shape *shape, ShapeMsg &shape_msg)
Mesh * createMeshFromResource(const std::string &resource)
#define ROS_INFO_STREAM(args)
static const std::string DEFAULT_MOVEIT_PLANNING_SERVICE_NAME
double freeSpaceCostFunction(const std::vector< double > &source, const std::vector< double > &target)
static const std::string DEFAULT_FRAME_ID
static const std::string GET_PLANNER_PARAM_SERVICE
#define ROS_ERROR_STREAM(args)
boost::variant< shape_msgs::SolidPrimitive, shape_msgs::Mesh, shape_msgs::Plane > ShapeMsg