9 #include <moveit_msgs/CollisionObject.h> 10 #include <moveit_msgs/PlanningSceneComponents.h> 11 #include <moveit_msgs/PlanningScene.h> 14 #include <moveit_msgs/GetPlanningScene.h> 27 std::vector<Eigen::Vector3d> discretizePositions(
const Eigen::Vector3d&
start,
const Eigen::Vector3d& stop,
const double ds)
29 double dist = (stop - start).norm();
31 size_t n_intermediate_points = 0;
34 n_intermediate_points =
static_cast<size_t>(std::lround(dist / ds));
37 const auto total_points = 2 + n_intermediate_points;
39 std::vector<Eigen::Vector3d>
result;
40 result.reserve(total_points);
42 for (std::size_t i = 0; i < total_points; ++i)
44 const double r = i /
static_cast<double>(total_points - 1);
45 Eigen::Vector3d
point = start + (stop - start) * r;
46 result.push_back(point);
52 Eigen::Affine3d makePose(
const Eigen::Vector3d& position,
const Eigen::Matrix3d& orientation,
53 const double z_axis_angle)
55 Eigen::Affine3d
m = Eigen::Affine3d::Identity();
56 m.matrix().block<3,3>(0,0) = orientation;
57 m.matrix().col(3).head<3>() = position;
59 Eigen::AngleAxisd z_rot (z_axis_angle, Eigen::Vector3d::UnitZ());
65 static int randomSampleInt(
int lower,
int upper)
67 std::random_device rd;
68 std::mt19937 gen(rd());
72 std::uniform_int_distribution<int> int_distr(0, upper);
73 return int_distr(gen);
81 static double randomSampleDouble(
double lower,
double upper)
83 std::random_device rd;
84 std::mt19937 gen(rd());
88 std::uniform_real_distribution<double> double_distr(lower, upper);
89 return double_distr(gen);
97 std::vector<Eigen::Affine3d> generateSampleEEFPoses(
98 const std::vector<Eigen::Vector3d>& path_pts,
99 const std::vector<Eigen::Matrix3d>& direction_list)
102 int o_sample = randomSampleInt(0, direction_list.size()-1);
103 Eigen::Matrix3d orientation_sample = direction_list[o_sample];
106 double x_axis_sample = randomSampleDouble(0.0, 1.0) * 2 *
M_PI;
108 std::vector<Eigen::Affine3d> poses;
109 poses.reserve(path_pts.size());
111 for(
const auto& pt : path_pts)
113 poses.push_back(makePose(pt, orientation_sample, x_axis_sample));
119 void convertOrientationVector(
120 const std::vector<Eigen::Vector3d>& vec_orients,
121 std::vector<Eigen::Matrix3d>& m_orients)
125 for(
auto eigen_vec : vec_orients)
129 eigen_vec.normalize();
132 Eigen::Vector3d candidate_dir = Eigen::Vector3d::UnitX();
133 if (
std::abs(eigen_vec.dot(Eigen::Vector3d::UnitX())) > 0.8 )
136 candidate_dir = Eigen::Vector3d::UnitY();
139 Eigen::Vector3d y_vec = eigen_vec.cross(candidate_dir).normalized();
141 Eigen::Vector3d x_vec = y_vec.cross(eigen_vec).normalized();
144 Eigen::Matrix3d
m = Eigen::Matrix3d::Identity();
147 m.col(2) = eigen_vec;
149 m_orients.push_back(m);
161 bool terminal_output,
163 descartes_core::RobotModelPtr hotend_model,
164 moveit::core::RobotModelConstPtr moveit_model,
165 std::string hotend_group_name
168 ptr_frame_ = ptr_dualgraph->ptr_frame_;
169 ptr_dualgraph_ = ptr_dualgraph;
170 ptr_collision_ = ptr_collision;
171 ptr_stiffness_ = ptr_stiffness;
172 ptr_path_ = ptr_path;
174 ptr_wholegraph_ =
new DualGraph(ptr_frame_);
179 D_tol_ = ptr_parm->seq_D_tol_;
183 terminal_output_ = terminal_output;
184 file_output_ = file_output;
186 update_collision_ =
true;
189 hotend_model_ = hotend_model;
190 moveit_model_ = moveit_model;
191 hotend_group_name_ = hotend_group_name;
226 for(
int i = 0; i <
Nd_; i++)
237 if(!planning_scene_client.waitForExistence())
239 ROS_ERROR_STREAM(
"[ts planning] cannot connect with get planning scene server...");
242 moveit_msgs::GetPlanningScene srv;
243 srv.request.components.components =
244 moveit_msgs::PlanningSceneComponents::ROBOT_STATE
245 | moveit_msgs::PlanningSceneComponents::WORLD_OBJECT_GEOMETRY;
247 if(!planning_scene_client.call(srv))
265 multimap<double, WF_edge*, std::greater<double>>base_queue;
266 multimap<double, WF_edge*, std::greater<double>>::iterator it;
267 for (
int dual_i = 0; dual_i <
Nd_; dual_i++)
273 base_queue.insert(make_pair(center.
x(), e));
280 fprintf(stderr,
"Size of base queue: %d, full graph domain pruning in progress.\n", (
int)base_queue.size());
283 for(it = base_queue.begin(); it != base_queue.end(); it++)
288 ROS_INFO_STREAM(
"pillar # " << std::distance(base_queue.begin(), it) <<
" domain pruning.");
297 vector<vector<lld>> tmp_angle(3);
319 D0_.conservativeResize(6 * Ns);
333 if (dual_v != -1 && !v->
isFixed())
336 for (
int i = 0; i < 6; i++)
338 tmp_D[i] =
D0_[6 * dual_v + i];
350 for (
int i = 0; i < 6; i++)
352 D0_[6 * (Ns - 1) + i] = sum_D[i];
367 D0_.block(6 * dual_del, 0, 6 * (Ns - dual_del), 1) =
368 D0_.block(6 * (dual_del + 1), 0, 6 * (Ns - dual_del), 1);
370 D0_.conservativeResize(6 * Ns);
385 for (
int dual_j = 0; dual_j < Nd; dual_j++)
410 for (
int k = 0; k < 3; k++)
427 for(
int dual_j = 0; dual_j < Nd; dual_j++)
438 for(
int k = 0; k < 3; k++)
461 std::vector<moveit_msgs::CollisionObject> added_collision_objs;
466 moveit_msgs::CollisionObject e_collision_obj;
467 e_collision_obj =
frame_msgs_[orig_j].full_collision_object;
468 e_collision_obj.operation = moveit_msgs::CollisionObject::ADD;
473 ROS_WARN_STREAM(
"[ts planning] Failed to add shrinked collision object: edge #" << orig_j);
476 added_collision_objs.push_back(e_collision_obj);
488 std::vector<int> shrink_vert_ids;
492 shrink_vert_ids.push_back(uj);
497 shrink_vert_ids.push_back(vj);
501 for (
const auto &connect_vert_id : shrink_vert_ids)
516 int ne_id = eu->
ID();
517 moveit_msgs::CollisionObject ne_collision_obj;
522 ne_collision_obj =
frame_msgs_[ne_id].st_shrinked_collision_object;
525 if(connect_vert_id == eu->
pvert_->
ID())
527 ne_collision_obj =
frame_msgs_[ne_id].end_shrinked_collision_object;
531 ne_collision_obj.operation = moveit_msgs::CollisionObject::ADD;
536 <<
" Failed to add collision object (shrinked neighnor): edge #" << orig_j);
539 added_collision_objs.push_back(ne_collision_obj);
546 return added_collision_objs;
563 std::vector<moveit_msgs::CollisionObject> recovered_collision_objs;
568 moveit_msgs::CollisionObject e_collision_obj;
569 e_collision_obj =
frame_msgs_[orig_j].full_collision_object;
570 e_collision_obj.operation = moveit_msgs::CollisionObject::REMOVE;
576 <<
"Failed to remove full collision object: edge #" << orig_j);
579 recovered_collision_objs.push_back(e_collision_obj);
591 std::vector<int> shrink_vert_ids;
595 shrink_vert_ids.push_back(uj);
600 shrink_vert_ids.push_back(vj);
604 for (
const auto &connect_vert_id : shrink_vert_ids)
619 int ne_id = eu->
ID();
620 moveit_msgs::CollisionObject ne_collision_obj;
621 ne_collision_obj =
frame_msgs_[ne_id].full_collision_object;
622 ne_collision_obj.operation = moveit_msgs::CollisionObject::ADD;
627 <<
"Failed to replace collision object (shrinked neighnor): edge #" << orig_j);
630 recovered_collision_objs.push_back(ne_collision_obj);
637 return recovered_collision_objs;
656 for (
int k = 0; k < Ns; k++)
659 for (
int t = 0;
t < 3;
t++)
661 offset[
t] = D[k * 6 +
t];
664 if (offset.norm() >=
D_tol_)
685 bool b_success =
false;
689 int orig_j = e->
ID();
700 moveit_msgs::CollisionObject e_collision_obj;
701 e_collision_obj =
frame_msgs_[e->
ID()].both_side_shrinked_collision_object;
702 e_collision_obj.operation = moveit_msgs::CollisionObject::ADD;
705 if (!planning_scene_depart->processCollisionObjectMsg(e_collision_obj))
707 ROS_WARN_STREAM(
"[ts planning robot kinematics] Failed to add shrinked collision object: edge #" << e->
ID());
711 std::vector<Eigen::Vector3d> direction_vec_list =
713 std::vector<Eigen::Matrix3d> direction_matrix_list;
714 convertOrientationVector(direction_vec_list, direction_matrix_list);
717 Eigen::Vector3d st_pt;
718 Eigen::Vector3d end_pt;
733 std::vector<Eigen::Vector3d> path_pts = discretizePositions(st_pt, end_pt, 0.005);
741 std::vector<Eigen::Affine3d> poses = generateSampleEEFPoses(path_pts, direction_matrix_list);
743 bool empty_joint_pose_found =
false;
745 for(std::size_t c_id=0; c_id < poses.size(); c_id++)
747 std::vector<std::vector<double>> joint_poses;
749 if(c_id < poses.size() - 1)
760 empty_joint_pose_found =
true;
765 if(!empty_joint_pose_found)
785 int orig_j = e->
ID();
799 if(uj_pt.
z() > vj_pt.
z())
807 if(exist_uj & exist_vj)
810 int endu_valence = 0;
811 int endv_valence = 0;
819 if (endv_valence > endu_valence)
824 if(endv_valence == endu_valence)
833 if (prev_end_node == uj_pt || prev_end_node == vj_pt)
835 if (prev_end_node == vj_pt)
857 assert(exist_uj || exist_vj);
873 int Nq = print_queue.size();
874 for (
int i = 0; i < Nq; i++)
889 std::vector<choreo_msgs::WireFrameCollisionObject>& collision_objs)
896 ROS_ERROR_STREAM(
"[ts planner] edge id error, not matched to wire frame id. " 897 <<
"Please re-run sequence planner to re-generate sequence result.");
901 collision_objs.clear();
912 moveit_msgs::CollisionObject last_e_collision_obj;
914 collision_objs[i].last_full_obj = last_e_collision_obj;
919 moveit_msgs::CollisionObject e_collision_obj;
921 e_collision_obj =
frame_msgs_[e->
ID()].full_collision_object;
922 collision_objs[i].full_obj = e_collision_obj;
924 e_collision_obj =
frame_msgs_[e->
ID()].both_side_shrinked_collision_object;
925 collision_objs[i].both_side_shrinked_obj = e_collision_obj;
938 for (
int i = 0; i < Nq; i++)
947 planning_result.reserve(Nq);
949 for (
int i = 0; i < Nq; i++)
955 std::vector<Eigen::Vector3d> direction_vec_list;
959 direction_vec_list.push_back(Eigen::Vector3d(0,0,1));
968 assert(direction_vec_list.size() > 0);
972 result.eef_directions_ = direction_vec_list;
974 planning_result.push_back(result);
bool ConstructCollisionObjsInQueue(const std::vector< int > &print_queue_edge_ids, std::vector< choreo_msgs::WireFrameCollisionObject > &collision_objs)
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
descartes_core::RobotModelPtr hotend_model_
void UpdateStructure(WF_edge *e, bool update_collision=false)
Stiffness * ptr_stiffness_
bool CalculateD(VX &D, VX *ptr_x=NULL, bool cond_num=false, int file_id=0, string file_name="")
void ModifyAngle(std::vector< lld > &angle_state, const std::vector< lld > &colli_map)
virtual bool SeqPrintLayer(std::vector< int > layer_id)
moveit::core::RobotModelConstPtr moveit_model_
WF_edge * GetNeighborEdge(int u)
void UpdateStateMap(WF_edge *e, vector< vector< lld >> &state_map)
std::vector< moveit_msgs::CollisionObject > UpdateCollisionObjects(WF_edge *e, bool shrink=false)
WF_edge * RouteEdgeDirection(const WF_edge *prev_e, WF_edge *e)
double CenterDistanceTo(WF_edge *ej) const
void RecoverStructure(WF_edge *e, bool update_collision=false)
bool InputPrintOrder(const std::vector< int > &print_queue)
bool isExistingVert(int u)
bool TestifyStiffness(WF_edge *e)
std::vector< Eigen::Vector3d > ConvertCollisionMapToEigenDirections(const std::vector< lld > &colli_map)
static const T dist(const Vec< D, T > &v1, const Vec< D, T > &v2)
vector< vector< unsigned long long > > angle_state_
planning_scene::PlanningScenePtr planning_scene_
int getExistingVertValence(int u)
void OutputTaskSequencePlanningResult(std::vector< SingleTaskPlanningResult > &planning_result)
SeqAnalyzer(DualGraph *ptr_dualgraph, QuadricCollision *ptr_collision, Stiffness *ptr_stiffness, FiberPrintPARM *ptr_parm, char *ptr_path, bool terminal_output, bool file_output, descartes_core::RobotModelPtr hotend_model, moveit::core::RobotModelConstPtr moveit_model, std::string hotend_group_name) noexcept
bool DetectCollision(WF_edge *target_e, DualGraph *ptr_subgraph, std::vector< lld > &result_map)
DualGraph * ptr_wholegraph_
virtual void PrintOutTimer()
void pointMsgToEigen(const geometry_msgs::Point &m, Eigen::Vector3d &e)
gte::BSRational< UIntegerType > abs(gte::BSRational< UIntegerType > const &number)
DualGraph * ptr_dualgraph_
static const double ROBOT_KINEMATICS_CHECK_TIMEOUT
QuadricCollision * ptr_collision_
#define ROS_WARN_STREAM(args)
std::vector< moveit_msgs::CollisionObject > RecoverCollisionObjects(WF_edge *e, bool shrink=false)
std::vector< WF_edge * > print_queue_
int RemoveUpdation(WF_edge *e)
bool isExistingEdge(WF_edge *e)
#define ROS_INFO_STREAM(args)
int num_p_assign_visited_
bool TestRobotKinematics(WF_edge *e, const std::vector< lld > &colli_map)
const double STATEMAP_UPDATE_DISTANCE
static const std::string GET_PLANNING_SCENE_SERVICE
void Init(vector< lld > &colli_map)
void RecoverStateMap(WF_edge *e, vector< vector< lld >> &state_map)
#define ROS_ERROR_STREAM(args)
std::vector< choreo_msgs::ElementCandidatePoses > frame_msgs_
void OutputPrintOrder(std::vector< WF_edge * > &print_queue)
void UpdateDualization(VectorXd *ptr_x)