9 #include "../include/choreo_descartes_planner/pose_verification_helpers.h" 12 #include <descartes_planner/ladder_graph.h> 13 #include <descartes_planner/ladder_graph_dag_search.h> 17 #include <geometry_msgs/Pose.h> 32 const std::vector<ConstrainedSegment>& segs,
33 const std::vector<planning_scene::PlanningScenePtr>& planning_scenes,
34 const std::vector<planning_scene::PlanningScenePtr>& planning_scenes_completed)
37 assert(segs.size() == planning_scenes.size());
39 if(planning_scenes_completed.size() > 0)
41 assert(planning_scenes_completed.size() == planning_scenes.size());
46 for(
size_t i=0; i < segs.size(); i++)
56 cap_rung.
orientations_[0].reserve(segs[i].orientations.size());
58 for (
auto& orient : segs[i].orientations)
67 if(planning_scenes_completed.size() > 0)
83 const std::vector<ConstrainedSegmentPickNPlace>& segs)
87 for(
size_t i=0; i < segs.size(); i++)
92 assert(segs[i].orientations.size() == segs[i].path_pts.size());
93 assert(segs[i].planning_scenes.size() == segs[i].path_pts.size());
96 const int k_family_size = segs[i].path_pts.size();
102 for(
int j = 0; j < k_family_size; j++)
104 const std::vector<Eigen::Vector3d>& pts = segs[i].path_pts[j];
106 assert(pts.size() >= 2);
108 for(
int k = 0; k < pts.size() - 1; k++)
112 cap_rung.
path_pts_[j].insert(cap_rung.
path_pts_[j].end(), points.begin(), points.end());
140 for(
auto& ptr_vert : cap_rung.ptr_cap_verts_)
152 double unit_process_timeout,
153 double rrt_star_timeout)
155 if(unit_process_timeout < 1.0)
157 logWarn(
"[CLT-RRT] unit process sampling timeout %.2f s, smaller than 1.0, set to default timeout %.2f s",
162 if(rrt_star_timeout < 5.0)
165 logWarn(
"[CLT-RRT] rrt star timeout %.2f s, smaller than 5.0s, set to default timeout %.2f s",
166 rrt_star_timeout, rrt_star_timeout);
176 auto unit_search_update = unit_search_start;
182 std::vector<Eigen::Affine3d> poses =
generateSample(cap_rung, *ptr_cap_vert);
189 cap_rung.ptr_cap_verts_.push_back(ptr_cap_vert);
190 ptr_prev_vert = ptr_cap_vert;
196 while((unit_search_update - unit_search_start).toSec() < unit_process_timeout);
198 if(cap_rung.ptr_cap_verts_.empty())
202 <<
" fails to find intial feasible sol within timeout " 203 << unit_process_timeout <<
"secs");
204 return std::numeric_limits<double>::max();
207 ROS_INFO_STREAM(
"[CLTRRT] ik solutions found for process #" << rung_id);
211 double initial_sol_cost = cap_rungs_.back().ptr_cap_verts_.back()->getCost();
212 ROS_INFO_STREAM(
"[CLTRRT] initial sol found! cost: " << initial_sol_cost);
213 ROS_INFO_STREAM(
"[CLTRRT] RRT* improvement starts, computation time: " << rrt_star_timeout);
218 while((
ros::Time::now() - rrt_start_time).toSec() < rrt_star_timeout)
222 auto& cap_rung_sample = cap_rungs_[rung_id_sample];
234 double c_min = std::numeric_limits<double>::max();
237 if(rung_id_sample > 0)
239 for(
auto& ptr_near_vert : this->cap_rungs_[rung_id_sample-1].ptr_cap_verts_)
241 double new_near_cost = ptr_near_vert->getCost() + ptr_new_vert->
distance(ptr_near_vert);
242 if(c_min > new_near_cost)
244 ptr_nearest_vert = ptr_near_vert;
245 c_min = new_near_cost;
251 ptr_new_vert->
rung_id_ = rung_id_sample;
253 cap_rung_sample.ptr_cap_verts_.push_back(ptr_new_vert);
256 if(rung_id_sample < this->cap_rungs_.size()-1)
258 double new_vert_cost = ptr_new_vert->
getCost();
259 for(
auto& ptr_next_vert : this->cap_rungs_[rung_id_sample+1].ptr_cap_verts_)
261 double old_next_cost = ptr_next_vert->getCost();
262 double new_next_cost = new_vert_cost + ptr_next_vert->distance(ptr_new_vert);
263 if(old_next_cost > new_next_cost)
265 ptr_next_vert->setParentVertPtr(ptr_new_vert);
272 CapVert* ptr_last_cap_vert = *std::min_element(this->cap_rungs_.back().ptr_cap_verts_.begin(),
273 this->cap_rungs_.back().ptr_cap_verts_.end(), comparePtrCapVert);
274 double rrt_cost = ptr_last_cap_vert->
getCost();
277 <<
" after " << rrt_star_timeout <<
" secs.");
282 double unit_process_timeout,
283 double rrt_star_timeout)
285 if(unit_process_timeout < 1.0)
287 logWarn(
"[CLT-RRT] unit process sampling timeout %.2f s, smaller than 1.0, set to default timeout %.2f s",
292 if(rrt_star_timeout < 5.0)
294 double updated_rrt_star_timeout = 4*
cap_rungs_.size();
295 logWarn(
"[CLT-RRT] rrt star timeout %.2f s, smaller than 5.0s, set to default timeout %.2f s",
296 rrt_star_timeout, updated_rrt_star_timeout);
306 auto unit_search_update = unit_search_start;
319 cap_rung.ptr_cap_verts_.push_back(ptr_cap_vert);
320 ptr_prev_vert = ptr_cap_vert;
326 while((unit_search_update - unit_search_start).toSec() < unit_process_timeout);
328 if(cap_rung.ptr_cap_verts_.empty())
332 <<
" fails to find intial feasible sol within timeout " 333 << unit_process_timeout <<
"secs");
334 return std::numeric_limits<double>::max();
337 ROS_INFO_STREAM(
"[CLTRRT] ik solutions found for process #" << rung_id);
341 double initial_sol_cost = cap_rungs_.back().ptr_cap_verts_.back()->getCost();
342 ROS_INFO_STREAM(
"[CLTRRT] initial sol found! cost: " << initial_sol_cost);
343 ROS_INFO_STREAM(
"[CLTRRT] RRT* improvement starts, computation time: " << rrt_star_timeout);
348 while((
ros::Time::now() - rrt_start_time).toSec() < rrt_star_timeout)
352 auto& cap_rung_sample = cap_rungs_[rung_id_sample];
364 double c_min = std::numeric_limits<double>::max();
367 if(rung_id_sample > 0)
369 for(
auto& ptr_near_vert : this->cap_rungs_[rung_id_sample-1].ptr_cap_verts_)
371 double new_near_cost = ptr_near_vert->getCost() + ptr_new_vert->
distance(ptr_near_vert);
372 if(c_min > new_near_cost)
374 ptr_nearest_vert = ptr_near_vert;
375 c_min = new_near_cost;
381 ptr_new_vert->
rung_id_ = rung_id_sample;
383 cap_rung_sample.ptr_cap_verts_.push_back(ptr_new_vert);
386 if(rung_id_sample < this->cap_rungs_.size()-1)
388 double new_vert_cost = ptr_new_vert->
getCost();
389 for(
auto& ptr_next_vert : this->cap_rungs_[rung_id_sample+1].ptr_cap_verts_)
391 double old_next_cost = ptr_next_vert->getCost();
392 double new_next_cost = new_vert_cost + ptr_next_vert->distance(ptr_new_vert);
393 if(old_next_cost > new_next_cost)
395 ptr_next_vert->setParentVertPtr(ptr_new_vert);
402 CapVert* ptr_last_cap_vert = *std::min_element(this->cap_rungs_.back().ptr_cap_verts_.begin(),
403 this->cap_rungs_.back().ptr_cap_verts_.end(), comparePtrCapVert);
404 double rrt_cost = ptr_last_cap_vert->
getCost();
407 <<
" after " << rrt_star_timeout <<
" secs.");
412 std::vector<descartes_core::TrajectoryPtPtr>& sol,
413 std::vector<descartes_planner::LadderGraph>& graphs,
414 std::vector<int>& graph_indices,
415 const bool use_saved_graph)
418 const int dof = model.getDOF();
423 graph_indices.clear();
426 CapVert* ptr_last_cap_vert = *std::min_element(this->
cap_rungs_.back().ptr_cap_verts_.begin(),
427 this->
cap_rungs_.back().ptr_cap_verts_.end(), comparePtrCapVert);
428 while (ptr_last_cap_vert !=
NULL)
433 LadderGraph unit_ladder_graph(dof);
438 double traverse_length = (cap_rung.path_pts_[0].front() - cap_rung.path_pts_[0].back()).norm();
439 const auto dt = traverse_length / cap_rung.linear_vel_;
440 model.setPlanningScene(cap_rung.planning_scene_[0]);
450 assert(cap_rung.sub_segment_ids_.size() == cap_rung.path_pts_.size());
451 assert(cap_rung.sub_segment_ids_.size() == cap_rung.orientations_.size());
452 assert(cap_rung.sub_segment_ids_.size() == cap_rung. planning_scene_.size());
454 for(
int i=0; i < cap_rung.sub_segment_ids_.size(); i++)
456 double traverse_length = (cap_rung.path_pts_[i].front() - cap_rung.path_pts_[i].back()).norm();
457 const auto dt = traverse_length / cap_rung.linear_vel_;
459 assert(cap_rung.path_pts_[i].size() == cap_rung.sub_segment_ids_[i]);
461 model.setPlanningScene(cap_rung.planning_scene_[i]);
470 graphs.insert(graphs.begin(), unit_ladder_graph);
471 graph_indices.insert(graph_indices.begin(), unit_ladder_graph.size());
478 graph_indices.clear();
479 for(
const auto& graph : graphs)
481 graph_indices.push_back(graph.size());
486 descartes_planner::LadderGraph unified_graph(model.getDOF());
487 for(
auto& graph : graphs)
489 assert(unified_graph.dof() == graph.dof());
494 descartes_planner::DAGSearch
search(unified_graph);
495 double cost = search.run();
496 auto path_idxs = search.shortestPath();
499 for (
size_t j = 0; j < path_idxs.size(); ++j)
501 const auto idx = path_idxs[j];
502 const auto* data = unified_graph.vertex(j, idx);
503 const auto& tm = unified_graph.getRung(j).timing;
504 auto pt = descartes_core::TrajectoryPtPtr(
new descartes_trajectory::JointTrajectoryPt(
505 std::vector<double>(data, data + dof), tm));
511 << (graph_build_end - graph_build_start).toSec() <<
" seconds");
std::vector< planning_scene::PlanningScenePtr > planning_scene_
std::vector< Eigen::Vector3d > discretizePositions(const Eigen::Vector3d &start, const Eigen::Vector3d &stop, const double ds)
CapVert * getParentVertPtr() const
double solve(descartes_core::RobotModel &model, double clt_rrt_unit_process_timeout, double clt_rrt_timeout)
std::vector< std::vector< Eigen::Matrix3d > > orientations_
bool checkFeasibility(descartes_core::RobotModel &model, const std::vector< Eigen::Affine3d > &poses, descartes_planner::CapRung &cap_rung, descartes_planner::CapVert &cap_vert)
CapsulatedLadderTreeRRTstar(const std::vector< ConstrainedSegment > &segs, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes, const std::vector< planning_scene::PlanningScenePtr > &planning_scenes_completed=std::vector< planning_scene::PlanningScenePtr >())
std::vector< std::vector< Eigen::Affine3d > > generateSamplePickNPlace(const descartes_planner::CapRung &cap_rung, descartes_planner::CapVert &cap_vert)
std::vector< std::vector< Eigen::Vector3d > > path_pts_
std::vector< int > sub_segment_ids_
bool checkFeasibilityPickNPlace(descartes_core::RobotModel &model, const std::vector< std::vector< Eigen::Affine3d >> &poses, descartes_planner::CapRung &cap_rung, descartes_planner::CapVert &cap_vert)
double solvePickNPlace(descartes_core::RobotModel &model, double clt_rrt_unit_process_timeout, double clt_rrt_timeout)
std::vector< planning_scene::PlanningScenePtr > planning_scene_completed_
void setParentVertPtr(CapVert *ptr_v)
std::vector< Eigen::Matrix3d > orientation_
int randomSampleInt(int lower, int upper)
#define ROS_INFO_STREAM(args)
void extractSolution(descartes_core::RobotModel &model, std::vector< descartes_core::TrajectoryPtPtr > &sol, std::vector< descartes_planner::LadderGraph > &graphs, std::vector< int > &graph_indices, const bool use_saved_graph)
void appendInTime(LadderGraph ¤t, const LadderGraph &next)
static const double DEFAULT_UNIT_PROCESS_TIMEOUT
LadderGraph sampleSingleConfig(const descartes_core::RobotModel &model, const std::vector< Eigen::Vector3d > &origins, const Eigen::Matrix3d &orientation, const double dt)
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL bool search(const std::string &ns, const std::string &key, std::string &result)
std::vector< Eigen::Affine3d > generateSample(const descartes_planner::CapRung &cap_rung, descartes_planner::CapVert &cap_vert)
std::vector< CapRung > cap_rungs_
double distance(CapVert *v) const
~CapsulatedLadderTreeRRTstar()