semi_constrained_cartesian_planning.cpp
Go to the documentation of this file.
1 //
2 // Created by yijiangh on 4/10/18.
3 //
4 
6 //namespace descartes_planner
7 //{
8 //
11 //
12 //typedef boost::function<std::vector<Eigen::Affine3d>(const CapRung& cap_rung, const CapVert& cap_vert)> SegmentSamplingFunction;
13 //
14 //}
15 
17 #include "common_utils.h"
18 #include"path_transitions.h"
19 
20 // cap ladder tree RRTstar
23 
24 #include <descartes_planner/ladder_graph_dag_search_lazy_collision.h>
25 #include <descartes_planner/ladder_graph_dag_search.h>
26 #include <descartes_planner/dense_planner.h>
27 #include <descartes_core/trajectory_pt.h>
28 
30 
31 // for serializing ladder graph
32 #include <descartes_parser/descartes_parser.h>
33 
34 // serialization
36 
37 #include <descartes_msgs/LadderGraphList.h>
38 
39 #include <trajectory_msgs/JointTrajectoryPoint.h>
40 
41 namespace {
42 
43 bool saveLadderGraph(const std::string &filename, const descartes_msgs::LadderGraphList &graph_list_msg)
44 {
45  if (!choreo_param_helpers::toFile(filename, graph_list_msg))
46  {
47  ROS_WARN_STREAM("Unable to save ladder graph list to: " << filename);
48  }
49 }
50 
51 } // anon util namespace
52 
54 {
55 
56 // TODO: overhead for spatial extrusion
57 void CLTRRTforProcessROSTraj(descartes_core::RobotModelPtr model,
58  std::vector <descartes_planner::ConstrainedSegment> &segs,
59  const double clt_rrt_unit_process_timeout,
60  const double clt_rrt_timeout,
61  const std::vector <planning_scene::PlanningScenePtr> &planning_scenes_approach,
62  const std::vector <planning_scene::PlanningScenePtr> &planning_scenes_depart,
63  const std::vector <choreo_msgs::ElementCandidatePoses> &task_seq,
64  std::vector <choreo_msgs::UnitProcessPlan> &plans,
65  const std::string &saved_graph_file_name,
66  bool use_saved_graph)
67 {
68  // sanity check
69  assert(segs.size() == task_seq.size());
70 
71  const auto clt_start = ros::Time::now();
72 
74 
75  std::vector <descartes_planner::LadderGraph> graphs;
76  std::vector<int> graph_indices;
77  descartes_msgs::LadderGraphList graph_list_msg;
78  double clt_cost = 0;
79 
80  if (use_saved_graph)
81  {
82  if (choreo_param_helpers::fromFile(saved_graph_file_name, graph_list_msg))
83  {
84  ROS_INFO_STREAM("[CLR RRT*] use saved ladder graph.");
85 
86  //parse saved ladder graph
87  graphs = descartes_parser::convertToLadderGraphList(graph_list_msg);
88  }
89  else
90  {
91  ROS_WARN_STREAM("[CLT RRT*] read saved ladder graph list fails. Reconstruct graph.");
92  // reading msg fails, reconstruct ladder graph
93  use_saved_graph = false;
94  }
95  }
96 
97  descartes_planner::CapsulatedLadderTreeRRTstar CLT_RRT(segs, planning_scenes_approach, planning_scenes_depart);
98 
99  if (!use_saved_graph || segs.size() > graphs.size())
100  {
101  // reconstruct and search, output sol, graphs, graph_indices
102  clt_cost = CLT_RRT.solve(*model, clt_rrt_unit_process_timeout, clt_rrt_timeout);
103  CLT_RRT.extractSolution(*model,
104  sol,
105  graphs,
106  graph_indices,
107  use_saved_graph);
108 
109  graph_list_msg = descartes_parser::convertToLadderGraphListMsg(graphs);
110  saveLadderGraph(saved_graph_file_name, graph_list_msg);
111  }
112  else
113  {
114  // default start from begin
115  std::vector <descartes_planner::LadderGraph> chosen_graphs(graphs.begin(), graphs.begin() + segs.size());
116  CLT_RRT.extractSolution(*model, sol,
117  chosen_graphs,
118  graph_indices, use_saved_graph);
119  }
120 
121  const auto clt_end = ros::Time::now();
122  ROS_INFO_STREAM("[CLT RRT*] CLT RRT* Search took " << (clt_end - clt_start).toSec()
123  << " seconds");
124 
125  trajectory_msgs::JointTrajectory ros_traj = choreo_process_planning::toROSTrajectory(sol, *model);
126 
127  auto it = ros_traj.points.begin();
128  for (size_t i = 0; i < task_seq.size(); i++)
129  {
130  choreo_msgs::SubProcess sub_process;
131 
132  sub_process.unit_process_id = i;
133  sub_process.process_type = choreo_msgs::SubProcess::PROCESS;
134  sub_process.main_data_type = choreo_msgs::SubProcess::CART;
135 
136  switch (task_seq[i].type)
137  {
138  case choreo_msgs::ElementCandidatePoses::SUPPORT:
139  {
140  sub_process.element_process_type = choreo_msgs::SubProcess::SUPPORT;
141  break;
142  }
143  case choreo_msgs::ElementCandidatePoses::CREATE:
144  {
145  sub_process.element_process_type = choreo_msgs::SubProcess::CREATE;
146  break;
147  }
148  case choreo_msgs::ElementCandidatePoses::CONNECT:
149  {
150  sub_process.element_process_type = choreo_msgs::SubProcess::CONNECT;
151  break;
152  }
153  default:
154  {
155  sub_process.element_process_type = choreo_msgs::SubProcess::NONE;
156  ROS_WARN_STREAM("[Process Planning] printing process #" << i << " have no element process type!");
157  }
158  }
159 
160  sub_process.joint_array.points = std::vector<trajectory_msgs::JointTrajectoryPoint>(it, it + graph_indices[i]);
161 
162  plans[i].sub_process_array.push_back(sub_process);
163 
164  it = it + graph_indices[i];
165  }
166 }
167 
168 // TODO: overload for picknplace
169 void CLTRRTforProcessROSTraj(descartes_core::RobotModelPtr model,
170  const choreo_msgs::AssemblySequencePickNPlace& as_pnp,
171  const double clt_rrt_unit_process_timeout,
172  const double clt_rrt_timeout,
173  const double& linear_vel,
174  const double& linear_disc,
175  const std::vector<std::vector<planning_scene::PlanningScenePtr>>& planning_scenes_subprocess,
176  std::vector <choreo_msgs::UnitProcessPlan>& plans,
177  const std::string &saved_graph_file_name,
178  bool use_saved_graph)
179 {
180  // sanity check, segs' size is used as req.index
181  const auto clt_start = ros::Time::now();
182 
184 
185  std::vector <descartes_planner::LadderGraph> graphs;
186  std::vector<std::vector<int>> graph_indices;
187  std::vector<int> flat_graph_ids; // un-partitioned graph indices
188 
189  descartes_msgs::LadderGraphList graph_list_msg;
190  double clt_cost = 0;
191 
192  if (use_saved_graph)
193  {
194  if(choreo_param_helpers::fromFile(saved_graph_file_name, graph_list_msg))
195  {
196  ROS_INFO_STREAM("[CLR RRT*] use saved ladder graph.");
197 
198  //parse saved ladder graph & partition indices
199  graphs = descartes_parser::convertToLadderGraphList(graph_list_msg, graph_indices);
200  }
201  else
202  {
203  ROS_WARN_STREAM("[CLT RRT*] read saved ladder graph list fails. Reconstruct graph.");
204  // reading msg fails, reconstruct ladder graph
205  use_saved_graph = false;
206  }
207  }
208 
209  // construct segs for descartes & copy chosen task sequence
210  const std::vector<descartes_planner::ConstrainedSegmentPickNPlace> segs =
211  toDescartesConstrainedPath(as_pnp, planning_scenes_subprocess, linear_vel, linear_disc);
212 
213  ROS_INFO_STREAM("[CLT RRT*] Constrained segment constructed.");
214 
215  // partition indices will be init inside CLT_RRT
217 
218  // if request size bigger than the saved one, recompute
219  if (!use_saved_graph || segs.size() > graphs.size())
220  {
221  // TODO: do we need customized extractSolution function?
222  // reconstruct and search, output sol, graphs, graph_indices
223  clt_cost = CLT_RRT.solvePickNPlace(*model, clt_rrt_unit_process_timeout, clt_rrt_timeout);
224  CLT_RRT.extractSolution(*model,
225  sol,
226  graphs,
227  flat_graph_ids,
228  use_saved_graph);
229 
230  graph_indices = CLT_RRT.getGraphPartitionIds();
231 
232  assert(graph_indices.size() == graphs.size());
233  graph_list_msg = descartes_parser::convertToLadderGraphListMsg(graphs, graph_indices);
234  saveLadderGraph(saved_graph_file_name, graph_list_msg);
235  }
236  else
237  {
238  // TODO: do we need customized extractSolution function?
239  // default start from begin
240  std::vector <descartes_planner::LadderGraph> chosen_graphs(graphs.begin(), graphs.begin() + segs.size());
241  CLT_RRT.extractSolution(*model,
242  sol,
243  chosen_graphs,
244  flat_graph_ids,
245  use_saved_graph);
246 
247  graph_indices = CLT_RRT.getGraphPartitionIds();
248 
249  // sanity check
250  assert(graph_indices.size() == flat_graph_ids.size());
251  for(int k=0; k<flat_graph_ids.size(); k++)
252  {
253  int sum = 0;
254  for(auto& chuck_num : graph_indices[k])
255  {
256  sum += chuck_num;
257  }
258  assert(sum == flat_graph_ids[k]);
259  }
260  }
261 
262  const auto clt_end = ros::Time::now();
263  ROS_INFO_STREAM("[CLT RRT*] CLT RRT* Search took " << (clt_end - clt_start).toSec()
264  << " seconds");
265 
266  trajectory_msgs::JointTrajectory ros_traj = choreo_process_planning::toROSTrajectory(sol, *model);
267 
268  auto it = ros_traj.points.begin();
269  for (size_t i = 0; i < segs.size(); i++)
270  {
271  choreo_msgs::SubProcess sub_process;
272 
273  sub_process.unit_process_id = i;
274  sub_process.process_type = choreo_msgs::SubProcess::RETRACTION;
275  sub_process.main_data_type = choreo_msgs::SubProcess::CART;
276 
277  // must contain two index partition: pick approach, pick depart, place approach, place depart
278  assert(graph_indices[i].size() == 4);
279 
280  for(int k = 0; k < 4; k++)
281  {
282  if(k % 2 == 0)
283  {
284  sub_process.element_process_type = choreo_msgs::SubProcess::APPROACH;
285  }
286  else
287  {
288  sub_process.element_process_type = choreo_msgs::SubProcess::DEPART;
289  }
290 
291  if(k <= 1)
292  {
293  sub_process.comment = "pick";
294  }
295  else
296  {
297  sub_process.comment = "place";
298  }
299 
300  sub_process.joint_array.points = std::vector<trajectory_msgs::JointTrajectoryPoint>(it, it + graph_indices[i][k]);
301  plans[i].sub_process_array.push_back(sub_process);
302 
303  it = it + graph_indices[i][k];
304  }
305  }
306 }
307 
308 }
bool fromFile(const std::string &path, T &msg)
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)
std::size_t size(custom_string const &s)
trajectory_msgs::JointTrajectory toROSTrajectory(const DescartesTraj &solution, const descartes_core::RobotModel &model)
double solve(descartes_core::RobotModel &model, double clt_rrt_unit_process_timeout, double clt_rrt_timeout)
bool toFile(const std::string &path, const T &msg)
std::vector< descartes_planner::ConstrainedSegmentPickNPlace > toDescartesConstrainedPath(const choreo_msgs::AssemblySequencePickNPlace &as_pnp, const std::vector< std::vector< planning_scene::PlanningScenePtr >> &planning_scene_subprocess, const double &linear_vel, const double &linear_disc)
double solvePickNPlace(descartes_core::RobotModel &model, double clt_rrt_unit_process_timeout, double clt_rrt_timeout)
#define ROS_WARN_STREAM(args)
std::vector< descartes_core::TrajectoryPtPtr > DescartesTraj
Definition: common_utils.h:25
#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)
static Time now()
std::vector< std::vector< int > > getGraphPartitionIds() const


choreo_process_planning
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:02