17 #include <geometry_msgs/Pose.h> 18 #include <trajectory_msgs/JointTrajectoryPoint.h> 19 #include <choreo_msgs/SubProcess.h> 20 #include <choreo_msgs/ElementCandidatePoses.h> 21 #include <choreo_msgs/WireFrameCollisionObject.h> 22 #include <choreo_msgs/AssemblySequencePickNPlace.h> 23 #include <choreo_msgs/SequencedElement.h> 25 #include <shape_msgs/Mesh.h> 26 #include <moveit_msgs/PlanningScene.h> 29 #include <moveit_msgs/ApplyPlanningScene.h> 33 #include <Eigen/Geometry> 35 #include <boost/filesystem.hpp> 42 const std::vector <planning_scene::PlanningScenePtr> &planning_scenes_approach,
43 const std::vector <planning_scene::PlanningScenePtr> &planning_scenes_depart,
44 const std::vector <descartes_planner::ConstrainedSegment> &segs,
45 std::vector <choreo_msgs::UnitProcessPlan> &plans)
47 if (plans.size() == 0)
49 ROS_ERROR(
"[retraction Planning] plans size = 0!");
55 for (
size_t i = 0; i < plans.size(); i++)
59 const std::vector<double>
60 start_process_joint = plans[i].sub_process_array.back().joint_array.points.front().positions;
61 const std::vector<double> end_process_joint = plans[i].sub_process_array.back().joint_array.points.back().positions;
65 const auto last_process_end_joint = plans[i - 1].sub_process_array.back().joint_array.points.back().positions;
76 model->setPlanningScene(planning_scenes_approach[i]);
78 std::vector <std::vector<double>> approach_retract_traj;
81 model, approach_retract_traj))
83 ROS_ERROR_STREAM(
"[retraction planning] process #" << i <<
" failed to find feasible approach retract motion!");
88 std::reverse(approach_retract_traj.begin(), approach_retract_traj.end());
90 trajectory_msgs::JointTrajectory approach_ros_traj =
93 choreo_msgs::SubProcess sub_process_approach;
95 sub_process_approach.process_type = choreo_msgs::SubProcess::RETRACTION;
96 sub_process_approach.main_data_type = choreo_msgs::SubProcess::CART;
97 sub_process_approach.element_process_type = choreo_msgs::SubProcess::APPROACH;
98 sub_process_approach.joint_array = approach_ros_traj;
100 plans[i].sub_process_array.insert(plans[i].sub_process_array.begin(), sub_process_approach);
103 model->setPlanningScene(planning_scenes_depart[i]);
105 std::vector <std::vector<double>> depart_retract_traj;
107 segs[i].orientations,
108 model, depart_retract_traj))
110 ROS_ERROR_STREAM(
"[retraction planning] process #" << i <<
" failed to find feasible depart retract motion!");
114 trajectory_msgs::JointTrajectory depart_ros_traj =
117 choreo_msgs::SubProcess sub_process_depart;
119 sub_process_depart.process_type = choreo_msgs::SubProcess::RETRACTION;
120 sub_process_depart.main_data_type = choreo_msgs::SubProcess::CART;
121 sub_process_depart.element_process_type = choreo_msgs::SubProcess::DEPART;
122 sub_process_depart.joint_array = depart_ros_traj;
124 plans[i].sub_process_array.insert(plans[i].sub_process_array.end(), sub_process_depart);
129 ROS_INFO_STREAM(
"[retraction planning] Retraction Planning took " << (ret_planning_end - ret_planning_start).toSec()
134 moveit::core::RobotModelConstPtr moveit_model,
136 const std::string &move_group_name,
137 const std::vector<double> &start_state,
138 std::vector <planning_scene::PlanningScenePtr> &planning_scenes)
140 if (plans.size() == 0)
142 ROS_ERROR(
"[transionPlanning] plans size = 0!");
149 bool add_eef_full =
true;
152 std::vector<int> planning_failure_ids;
154 std::vector<double> last_joint_pose = start_state;
155 std::vector<double> current_first_joint_pose;
157 for (
size_t i = 0; i < plans.size(); i++)
163 last_joint_pose = plans[i - 1].sub_process_array.back().joint_array.points.back().positions;
166 current_first_joint_pose = plans[i].sub_process_array.front().joint_array.points.front().positions;
168 if (last_joint_pose == current_first_joint_pose)
177 ROS_ERROR_STREAM(
"[Tr Planning] cannot connect with planning scene diff server...");
180 moveit_msgs::ApplyPlanningScene srv;
181 auto scene_with_attached_eef = planning_scenes[i]->diff();
182 if (!scene_with_attached_eef->processAttachedCollisionObjectMsg(full_eef_collision_obj))
184 ROS_ERROR_STREAM(
"[Tr Planning] planning scene # " << i <<
"fails to add attached full eef collision geometry");
187 scene_with_attached_eef->getPlanningSceneMsg(srv.request.scene);
189 if (!planning_scene_diff_client.
call(srv))
191 ROS_ERROR_STREAM(
"[Tr Planning] Failed to publish planning scene diff srv!");
194 int repeat_planning_call = 0;
195 trajectory_msgs::JointTrajectory ros_trans_traj;
197 bool joint_target_meet =
true;
201 joint_target_meet =
true;
205 current_first_joint_pose,
210 if (repeat_planning_call > 0)
212 ROS_WARN_STREAM(
"[Process Planning] transition planning retry - round " 216 if (0 == ros_trans_traj.points.size())
219 joint_target_meet =
false;
220 repeat_planning_call++;
224 for (
int s = 0;
s < current_first_joint_pose.size();
s++)
226 if (current_first_joint_pose[
s] - ros_trans_traj.points.back().positions[
s] > 0.0001)
228 joint_target_meet =
false;
233 if (joint_target_meet)
235 std::string retry_msg = repeat_planning_call>0 ?
"retry":
"";
237 << retry_msg <<
" succeed!");
241 repeat_planning_call++;
244 if (!joint_target_meet)
246 planning_failure_ids.push_back(i);
247 ROS_ERROR_STREAM(
"[Tr planning] transition planning fails at index #" << i);
251 choreo_msgs::SubProcess sub_process;
253 sub_process.process_type = choreo_msgs::SubProcess::TRANSITION;
254 sub_process.main_data_type = choreo_msgs::SubProcess::JOINT;
255 sub_process.joint_array = ros_trans_traj;
257 plans[i].sub_process_array.insert(plans[i].sub_process_array.begin(), sub_process);
260 for (
auto id : planning_failure_ids)
262 ROS_ERROR_STREAM(
"[Tr planning] transition planning fails at process #" <<
id);
266 ROS_INFO_STREAM(
"[Process Planning] Transition Planning took " << (tr_planning_end - tr_planning_start).toSec()
271 moveit::core::RobotModelConstPtr moveit_model,
273 const std::string &move_group_name,
274 const std::vector<double> &start_state,
275 const std::vector<std::vector<planning_scene::PlanningScenePtr>>& planning_scenes)
277 if (plans.size() == 0)
279 ROS_ERROR(
"[transionPlanning] plans size = 0!");
289 std::vector<int> planning_failure_ids;
291 std::vector<double> last_joint_pose = start_state;
292 std::vector<double> current_first_joint_pose;
294 for (
size_t i = 0; i < plans.size(); i++)
300 assert(planning_scenes[i].
size() == plans[i].sub_process_array.size());
301 std::vector<choreo_msgs::SubProcess> weaved_sub_process;
303 for (
size_t j = 0; j < plans[i].sub_process_array.size(); j++)
310 last_joint_pose = plans[i - 1].sub_process_array.back().joint_array.points.back().positions;
315 last_joint_pose = plans[i].sub_process_array[j-1].joint_array.points.back().positions;
318 current_first_joint_pose = plans[i].sub_process_array[j].joint_array.points.front().positions;
320 if (last_joint_pose != current_first_joint_pose)
325 ROS_ERROR_STREAM(
"[Tr Planning] cannot connect with planning scene diff server...");
328 moveit_msgs::ApplyPlanningScene srv;
329 auto scene = planning_scenes[i][j]->diff();
336 scene->getPlanningSceneMsg(srv.request.scene);
338 if (!planning_scene_diff_client.
call(srv))
340 ROS_ERROR_STREAM(
"[Tr Planning] Failed to publish planning scene diff srv!");
343 int repeat_planning_call = 0;
344 trajectory_msgs::JointTrajectory ros_trans_traj;
346 bool joint_target_meet =
true;
350 joint_target_meet =
true;
354 current_first_joint_pose,
359 if (repeat_planning_call > 0)
361 ROS_WARN_STREAM(
"[Process Planning] transition planning retry - round " 365 if (0 == ros_trans_traj.points.size())
368 joint_target_meet =
false;
369 repeat_planning_call++;
373 for (
int s = 0;
s < current_first_joint_pose.size();
s++)
375 if (current_first_joint_pose[
s] - ros_trans_traj.points.back().positions[
s] > 0.0001)
377 joint_target_meet =
false;
382 if (joint_target_meet)
384 std::string retry_msg = repeat_planning_call > 0 ?
"retry" :
"";
386 << retry_msg <<
" succeed!");
390 repeat_planning_call++;
393 if (!joint_target_meet)
395 planning_failure_ids.push_back(i);
396 ROS_ERROR_STREAM(
"[Tr planning] transition planning fails at index #" << i <<
", subprocess #" << j);
400 choreo_msgs::SubProcess sub_process;
402 sub_process.process_type = choreo_msgs::SubProcess::TRANSITION;
403 sub_process.main_data_type = choreo_msgs::SubProcess::JOINT;
404 sub_process.joint_array = ros_trans_traj;
406 weaved_sub_process.push_back(sub_process);
409 weaved_sub_process.push_back(plans[i].sub_process_array[j]);
413 plans[i].sub_process_array = weaved_sub_process;
416 for (
auto id : planning_failure_ids)
418 ROS_ERROR_STREAM(
"[Tr planning] transition planning fails at process #" <<
id);
422 ROS_INFO_STREAM(
"[Process Planning] Transition Planning took " << (tr_planning_end - tr_planning_start).toSec()
427 const std::vector <std::string> &joint_names,
428 const std::string world_frame)
430 if (plans.size() == 0)
432 ROS_ERROR(
"[ProcessPlanning : adjustTrajectoryTiming] plans size = 0!");
436 if (0 == plans[0].sub_process_array.size())
438 ROS_ERROR(
"[ProcessPlanning : adjustTrajectoryTiming] plans[0] doesn't have sub process!");
443 auto last_filled_jts = plans[0].sub_process_array[0].joint_array;
446 auto adjustTrajectoryHeaders =
447 [](trajectory_msgs::JointTrajectory &last_filled_jts, choreo_msgs::SubProcess &sp,
double sim_speed)
450 last_filled_jts = sp.joint_array;
453 for (
size_t i = 0; i < plans.size(); i++)
455 for (
size_t j = 0; j < plans[i].sub_process_array.size(); j++)
457 plans[i].sub_process_array[j].unit_process_id = i;
458 plans[i].sub_process_array[j].sub_process_id = j;
460 double sim_speed = 1.0;
461 if (2 != plans[i].sub_process_array[j].process_type)
466 adjustTrajectoryHeaders(last_filled_jts, plans[i].sub_process_array[j], sim_speed);
474 const std::vector <planning_scene::PlanningScenePtr> &planning_scenes_shrinked,
475 const std::vector <planning_scene::PlanningScenePtr> &planning_scenes_full,
476 std::vector <choreo_msgs::UnitProcessPlan> &plans)
478 int process_id_count = 0;
479 for (
auto &unit_plan : plans)
481 for (
auto &sub_process : unit_plan.sub_process_array)
483 if (sub_process.process_type == choreo_msgs::SubProcess::TRANSITION)
485 model->setPlanningScene(planning_scenes_full[process_id_count]);
489 model->setPlanningScene(planning_scenes_shrinked[process_id_count]);
492 for (
const auto &jt_pt : sub_process.joint_array.points)
494 Eigen::Affine3d TCP_pose = Eigen::Affine3d::Identity();
495 geometry_msgs::Pose geo_pose_msg;
498 if (!model->getFK(jt_pt.positions, TCP_pose))
500 ROS_ERROR_STREAM(
"FK solution failed at unit process #" << sub_process.unit_process_id
501 <<
", subprocess #" << sub_process.sub_process_id
502 <<
", process type: " << sub_process.process_type
503 <<
" (0: process, 1: near_model, 2: transition)");
509 sub_process.TCP_pose_array.push_back(geo_pose_msg);
518 const std::vector<std::vector<planning_scene::PlanningScenePtr>>& planning_scenes_transition,
519 const std::vector<std::vector<planning_scene::PlanningScenePtr>>& planning_scenes_subprocess,
520 std::vector<choreo_msgs::UnitProcessPlan>& plans)
522 int process_id_count = 0;
523 for (
auto &unit_plan : plans)
528 for (
auto &sub_process : unit_plan.sub_process_array)
530 if (sub_process.process_type == choreo_msgs::SubProcess::TRANSITION)
532 model->setPlanningScene(planning_scenes_transition[process_id_count][tr_count]);
537 model->setPlanningScene(planning_scenes_subprocess[process_id_count][sp_count]);
541 for (
const auto &jt_pt : sub_process.joint_array.points)
543 Eigen::Affine3d TCP_pose = Eigen::Affine3d::Identity();
544 geometry_msgs::Pose geo_pose_msg;
547 if (!model->getFK(jt_pt.positions, TCP_pose))
549 ROS_ERROR_STREAM(
"FK solution failed at unit process #" << sub_process.unit_process_id
550 <<
", subprocess #" << sub_process.sub_process_id
551 <<
", process type: " << sub_process.process_type
552 <<
" (0: process, 1: near_model, 2: transition)");
558 sub_process.TCP_pose_array.push_back(geo_pose_msg);
568 const std::string world_frame,
569 const bool use_saved_graph,
570 const std::string &saved_graph_file_name,
571 const double clt_rrt_unit_process_timeout,
572 const double clt_rrt_timeout,
573 const std::string &move_group_name,
574 const std::vector<double> &start_state,
575 const std::vector <choreo_msgs::ElementCandidatePoses> &task_seq,
576 const std::vector <choreo_msgs::WireFrameCollisionObject> &wf_collision_objs,
577 std::vector <descartes_planner::ConstrainedSegment> &segs,
578 descartes_core::RobotModelPtr model,
579 moveit::core::RobotModelConstPtr moveit_model,
581 std::vector <choreo_msgs::UnitProcessPlan> &plans)
584 if (segs.size() == 0 || task_seq.size() != segs.size())
586 ROS_ERROR_STREAM(
"[Process Planning] input descartes Constrained Segment size" << segs.size());
589 assert(task_seq.size() == wf_collision_objs.size());
591 plans.resize(segs.size());
592 const std::vector <std::string> &joint_names =
593 moveit_model->getJointModelGroup(move_group_name)->getActiveJointModelNames();
596 std::vector <planning_scene::PlanningScenePtr> planning_scenes_shrinked_approach;
597 std::vector <planning_scene::PlanningScenePtr> planning_scenes_shrinked_depart;
598 std::vector <planning_scene::PlanningScenePtr> planning_scenes_full;
600 planning_scenes_shrinked_approach,
601 planning_scenes_shrinked_depart,
602 planning_scenes_full);
606 planning_scenes_shrinked_approach, planning_scenes_shrinked_depart,
607 task_seq, plans, saved_graph_file_name, use_saved_graph);
610 retractionPlanning(model, planning_scenes_shrinked_approach, planning_scenes_shrinked_depart, segs, plans);
614 start_state, planning_scenes_full);
623 ROS_INFO(
"[Process Planning] trajectories solved and packing finished");
635 ROS_ERROR_STREAM(
"[Tr Planning] cannot connect with planning scene diff server...");
638 moveit_msgs::ApplyPlanningScene srv;
640 planning_scene->getPlanningSceneMsg(srv.request.scene);
642 if (!planning_scene_diff_client.
call(srv))
644 ROS_ERROR_STREAM(
"[Tr Planning] Failed to publish TEST planning scene diff srv!");
652 const std::string world_frame,
653 const bool use_saved_graph,
654 const std::string &saved_graph_file_name,
655 const double clt_rrt_unit_process_timeout,
656 const double clt_rrt_timeout,
657 const double& linear_vel,
658 const double& linear_disc,
659 const std::string &move_group_name,
660 const std::vector<double> &start_state,
661 const choreo_msgs::AssemblySequencePickNPlace& as_pnp,
662 descartes_core::RobotModelPtr model,
663 moveit::core::RobotModelConstPtr moveit_model,
665 std::vector <choreo_msgs::UnitProcessPlan> &plans)
667 plans.resize(as_pnp.sequenced_elements.size());
668 const std::vector <std::string> &joint_names =
669 moveit_model->getJointModelGroup(move_group_name)->getActiveJointModelNames();
672 std::vector<std::vector<planning_scene::PlanningScenePtr>> planning_scenes_transition;
673 std::vector<std::vector<planning_scene::PlanningScenePtr>> planning_scenes_subprocess;
678 planning_scenes_transition,
679 planning_scenes_subprocess);
684 clt_rrt_unit_process_timeout,
688 planning_scenes_subprocess,
690 saved_graph_file_name,
697 start_state, planning_scenes_subprocess);
706 ROS_INFO(
"[Process Planning] trajectories solved and packing finished");
void adjustTrajectoryTiming(std::vector< choreo_msgs::UnitProcessPlan > &plans, const std::vector< std::string > &joint_names, const std::string world_frame)
static const int TRANSITION_PLANNING_LOOP_COUNT
void poseEigenToMsg(const Eigen::Affine3d &e, geometry_msgs::Pose &m)
void transitionPlanningPickNPlace(std::vector< choreo_msgs::UnitProcessPlan > &plans, moveit::core::RobotModelConstPtr moveit_model, ros::ServiceClient &planning_scene_diff_client, const std::string &move_group_name, const std::vector< double > &start_state, const std::vector< std::vector< planning_scene::PlanningScenePtr >> &planning_scenes)
void transitionPlanning(std::vector< choreo_msgs::UnitProcessPlan > &plans, moveit::core::RobotModelConstPtr moveit_model, ros::ServiceClient &planning_scene_diff_client, const std::string &move_group_name, const std::vector< double > &start_state, std::vector< planning_scene::PlanningScenePtr > &planning_scenes)
void CLTRRTforProcessROSTraj(descartes_core::RobotModelPtr model, std::vector< descartes_planner::ConstrainedSegment > &segs, const double clt_rrt_unit_process_timeout, const double clt_rrt_timeout, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes_approach, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes_depart, const std::vector< choreo_msgs::ElementCandidatePoses > &task_seq, std::vector< choreo_msgs::UnitProcessPlan > &plans, const std::string &saved_graph_file_name, bool use_saved_graph)
void retractionPlanning(descartes_core::RobotModelPtr model, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes_approach, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes_depart, const std::vector< descartes_planner::ConstrainedSegment > &segs, std::vector< choreo_msgs::UnitProcessPlan > &plans)
std::size_t size(custom_string const &s)
bool call(MReq &req, MRes &res)
void appendTrajectoryHeaders(const trajectory_msgs::JointTrajectory &orig_traj, trajectory_msgs::JointTrajectory &traj, const double sim_time_scale=0.0)
trajectory_msgs::JointTrajectory toROSTrajectory(const DescartesTraj &solution, const descartes_core::RobotModel &model)
bool retractPath(const std::vector< double > &start_joint, double retract_dist, double TCP_speed, const std::vector< Eigen::Matrix3d > &eef_directions, descartes_core::RobotModelPtr &model, std::vector< std::vector< double >> &retract_jt_traj)
void constructPlanningScenes(moveit::core::RobotModelConstPtr moveit_model, const std::string &world_frame, const choreo_msgs::AssemblySequencePickNPlace &as_pnp, std::vector< std::vector< planning_scene::PlanningScenePtr >> &planning_scenes_transition, std::vector< std::vector< planning_scene::PlanningScenePtr >> &planning_scenes_subprocess)
void appendTCPPoseToPlans(const descartes_core::RobotModelPtr model, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes_shrinked, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes_full, std::vector< choreo_msgs::UnitProcessPlan > &plans)
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))
bool generateMotionPlan(const std::string world_frame, const bool use_saved_graph, const std::string &saved_graph_file_name, const double clt_rrt_unit_process_timeout, const double clt_rrt_timeout, const std::string &move_group_name, const std::vector< double > &start_state, const std::vector< choreo_msgs::ElementCandidatePoses > &task_seq, const std::vector< choreo_msgs::WireFrameCollisionObject > &wf_collision_objs, std::vector< descartes_planner::ConstrainedSegment > &segs, descartes_core::RobotModelPtr model, moveit::core::RobotModelConstPtr moveit_model, ros::ServiceClient &planning_scene_diff_client, std::vector< choreo_msgs::UnitProcessPlan > &plans)
#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)
#define ROS_INFO_STREAM(args)
#define ROS_ERROR_STREAM(args)
void appendTCPPoseToPlansPickNPlace(const descartes_core::RobotModelPtr model, const std::vector< std::vector< planning_scene::PlanningScenePtr >> &planning_scenes_transition, const std::vector< std::vector< planning_scene::PlanningScenePtr >> &planning_scenes_subprocess, std::vector< choreo_msgs::UnitProcessPlan > &plans)