Robowflex  v0.1
Making MoveIt Easy
trajopt_planner.h
Go to the documentation of this file.
1 /* Author: Carlos Quintero Pena */
2 
3 #ifndef ROBOWFLEX_TRAJOPT_PLANNER_
4 #define ROBOWFLEX_TRAJOPT_PLANNER_
5 
9 #include <tesseract_planning/trajopt/trajopt_planner.h>
10 #include <tesseract_ros/kdl/kdl_env.h>
11 
12 namespace robowflex
13 {
14  /** \cond IGNORE */
17  ROBOWFLEX_CLASS_FORWARD(RobotTrajectory);
18  ROBOWFLEX_CLASS_FORWARD(MotionRequestBuilder);
19  /** \endcond */
20 
21  /** \cond IGNORE */
22  ROBOWFLEX_CLASS_FORWARD(TrajOptPlanner);
23  /** \endcond */
24 
25  /** \class robowflex::TrajOptPlannerPtr
26  \brief A shared pointer wrapper for robowflex::TrajOptPlanner. */
27 
28  /** \class robowflex::TrajOptPlannerConstPtr
29  \brief A const shared pointer wrapper for robowflex::TrajOptPlanner. */
30 
31  /** \brief Robowflex Tesseract TrajOpt Planner.
32  */
33  class TrajOptPlanner : public Planner
34  {
35  public:
36  /** \brief Options structure with parameter values for TrajOpt planner.
37  */
38  struct Options
39  {
40  sco::ModelType backend_optimizer{sco::ModelType::AUTO_SOLVER}; ///< Optimizer to use.
41  bool perturb_init_traj{false}; ///< Whether the initial trajectory should be randomly perturbed
42  ///< or not.
43  double noise_init_traj{0.09}; ///< Max and (negative) min amount of uniform noise added to each
44  ///< joint value for all waypoints of an initial trajectory.
45  bool verbose{true}; ///< Verbosity.
46  bool return_first_sol{true}; ///< Whether the planner runs only once or not. This has higher
47  ///< piority than return_until_timeout. Choosing false will set
48  ///< perturb_init_traj to true.
49  bool return_after_timeout{false}; ///< Whether the planner returns after timeout or after the
50  ///< first feasible solution.
51  double max_planning_time{1.0}; ///< Maximum amount of time the planner is allowed to search for a
52  ///< feasible solution.
53  bool use_cont_col_avoid{true}; ///< Whether to use continuous collision avoidance or not.
54  int num_waypoints{20}; ///< Number of waypoints.
55  double dt_lower_lim{2.0}; ///< 1/max_dt.
56  double dt_upper_lim{100.0}; ///< 1/min_dt.
57  bool start_fixed{false}; ///< Whether to use the current env state as a fixed initial state.
58  bool use_time{false}; ///< Whether any cost/cnt use time.
59  double init_info_dt{0.5}; ///< Value of dt in init_info.
60  double joint_vel_coeffs{5.0}; ///< Coefficients for joint_vel costs.
61  int collision_gap{1}; ///< For continuous collision avoidance, compute swept-volume between
62  ///< timestep t and t+gap.
63  double default_safety_margin{0.025}; ///< Default safety margin for collision avoidance.
64  double default_safety_margin_coeffs{50.0}; ///< Coefficients for safety margin.
65  double joint_pose_safety_margin_coeffs{50.0}; ///< Coefficients for safety margin when using
66  ///< joint pose costs/cnts.
67  double joint_state_safety_margin_coeffs{20.0}; ///< Coefficients for safety margin when using
68  ///< joint state costs/cnts.
69  double pose_cnt_pos_coeffs{10.0}; ///< Coefficients for pose constraints (position).
70  double pose_cnt_rot_coeffs{10.0}; ///< Coefficients for pose constraints (rotation).
71  double joint_pos_cnt_coeffs{1.0}; ///< Coefficients for joint position constraints.
72  double improve_ratio_threshold{0.25}; ///< Minimum ratio true_improve/approx_improve to accept
73  ///< step.
74  double min_trust_box_size{1e-4}; ///< If trust region gets any smaller, exit and report
75  ///< convergence.
76  double min_approx_improve{1e-4}; ///< If model improves less than this, exit and report
77  ///< convergence.
79  -std::numeric_limits<double>::infinity()}; ///< If model improves less than this, exit and
80  ///< report convergence.
81  double max_iter{50.0}; ///< The max number of iterations.
82  double trust_shrink_ratio{0.1}; // If improvement is less than improve_ratio_threshold, shrink
83  // trust region by this ratio.
84  double trust_expand_ratio{1.5}; ///< If improvement is greater than improve_ratio_threshold,
85  ///< expand trust region by this ratio.
86  double cnt_tolerance{1e-4}; ///< After convergence of penalty subproblem, if constraint violation
87  ///< is less than this, we're done.
88  double max_merit_coeff_increases{5.0}; ///< Number of times that we jack up penalty coefficient.
89  double merit_coeff_increase_ratio{10.0}; ///< Ratio that we increate coeff each time.
90  double merit_error_coeff{10.0}; ///< Initial penalty coefficient.
91  double trust_box_size{1e-1}; ///< Current size of trust region (component-wise).
93 
94  /** \brief Planner result: first->converged, second->collision_free
95  */
97 
98  /** \brief Constructor.
99  * \param[in] robot Robot to plan for.
100  * \param[in] group_name Name of the (joint) group to plan for.
101  * \param[in] name Name of planner.
102  */
103  TrajOptPlanner(const RobotPtr &robot, const std::string &group_name,
104  const std::string &name = "trajopt");
105 
106  /** \brief Initialize planner. The user must specify a chain group defined in the robot's srdf
107  * that contains all the links of the manipulator.
108  * \param[in] manip Name of chain group with all the links of the manipulator.
109  * \return True if initialization succeded.
110  */
111  bool initialize(const std::string &manip);
112 
113  /** \brief Initialize planner. All links between \a base_link and \a tip_link will be added to
114  * the manipulator.
115  * \param[in] base_link Base link of the \a manip.
116  * \param[in] tip_link Tip link of the \a manip.
117  * \return True if initialization succeded.
118  */
119  bool initialize(const std::string &base_link, const std::string &tip_link);
120 
121  /** \name Set and get TrajOpt parameters
122  \{*/
123 
124  /** \brief Set initial trajectory for TrajOpt and set init_type to GIVEN_TRAJ
125  * \param[in] init_trajectory Trajectory to initialize TrajOpt.
126  */
127  void setInitialTrajectory(const robot_trajectory::RobotTrajectoryPtr &init_trajectory);
128 
129  /** \brief Set initial trajectory for TrajOpt and set init_type to GIVEN_TRAJ
130  * \param[in] init_trajectory Trajectory to initialize TrajOpt.
131  */
132  void setInitialTrajectory(const trajopt::TrajArray &init_trajectory);
133 
134  /** \brief Set type of initialization to use for the trajectory optimization.
135  * Current options are:
136  * STATIONARY
137  * JOINT_INTERPOLATED
138  * The user can also provide their own trajectory using setInitialTrajectory(). In
139  * such case, there is no need to call setInitType().
140  * \param[in] init_type Type of initial trajectory to be used.
141  */
142  void setInitType(const trajopt::InitInfo::Type &init_type);
143 
144  /** \brief Get the trajectory that resulted in the last call to plan().
145  * \return Last trajectory computed using plan().
146  */
147  const robot_trajectory::RobotTrajectoryPtr &getTrajectory() const;
148 
149  /** \brief Get the trajectory that resulted in the last call to plan() in Tesseract
150  * format.
151  * \return Last trajectory computed using plan().
152  */
153  const trajopt::TrajArray &getTesseractTrajectory() const;
154 
155  /** \brief Get the link names in the tesseract KDL environment.
156  * \return Name of links in the KDL environment.
157  */
159 
160  /** \brief Get the link names of the env manipulator.
161  * \return Name of links in the manipulator.
162  */
164 
165  /** \brief Get the joint names of the env manipulator.
166  * \return Name of joints in the manipulator.
167  */
169 
170  /** \brief Get the time spent by the planner the last time it was called.
171  * \return Planning time.
172  */
173  double getPlanningTime() const;
174 
175  /** \brief Constrain certain joints during optimization to their initial value.
176  * \param[in] joints Vector of joints to freeze.
177  */
178  void fixJoints(const std::vector<std::string> &joints);
179 
180  /** \} */
181 
182  /** \name Planning functions
183  \{ */
184 
185  /** \brief Plan a motion given a \a request and a \a scene.
186  * \param[in] scene A planning scene to compute the plan in.
187  * \param[in] request The motion planning request to solve.
188  * \return The motion planning response generated by the planner.
189  */
191  plan(const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) override;
192 
193  /** \brief Plan a motion using a \a scene from \a start_state to a \a goal_state.
194  * \param[in] scene Scene to plan for.
195  * \param[in] start_state Start state for the robot.
196  * \param[in] goal_state Goal state for the robot.
197  * \return Planner result with convergence and collision status.
198  */
199  PlannerResult plan(const SceneConstPtr &scene, const robot_state::RobotStatePtr &start_state,
200  const robot_state::RobotStatePtr &goal_state);
201 
202  /** \brief Plan a motion given a \a start_state, a \a goal_pose for a \a link and a \a
203  * scene.
204  * \param[in] scene A planning scene for the same robot to compute the plan in.
205  * \param[in] start_state Start state for the robot.
206  * \param[in] goal_pose Cartesian goal pose for \a link.
207  * \param[in] link Link to find pose for.
208  * \return Planner result with convergence and collision status.
209  */
210  PlannerResult plan(const SceneConstPtr &scene, const robot_state::RobotStatePtr &start_state,
211  const RobotPose &goal_pose, const std::string &link);
212 
213  /** \brief Plan a motion given a \a start_state, a \a goal_pose for a \a link and a \a
214  * scene.
215  * \param[in] scene A planning scene for the same robot to compute the plan in.
216  * \param[in] start_state Start state for the robot.
217  * \param[in] goal_pose Cartesian goal pose for \a link.
218  * \param[in] link Link to find pose for.
219  * \return Planner result with convergence and collision status.
220  */
222  const std::unordered_map<std::string, double> &start_state,
223  const RobotPose &goal_pose, const std::string &link);
224 
225  /** \brief Plan a motion given a \a start_pose for \a start_link and a \a goal_pose
226  * for \a goal_link.
227  * \param[in] scene A planning scene to compute the plan in.
228  * \param[in] start_pose Cartesian start pose for \a start_link.
229  * \param[in] start_link Robot's link with \a start_pose.
230  * \param[in] goal_pose Cartesian goal pose for \a goal_link.
231  * \param[in] goal_link Robot's link with \a goal_pose.
232  * \return Planner result with convergence and collision status.
233  */
234  PlannerResult plan(const SceneConstPtr &scene, const RobotPose &start_pose,
235  const std::string &start_link, const RobotPose &goal_pose,
236  const std::string &goal_link);
237 
238  /** \brief Plan a motion using custom terms specified by the user.
239  * \param[in] scene A planning scene to compute the plan in.
240  * \param[in] start_state Start state for the robot.
241  * \return Planner result with convergence and collision status.
242  */
243  virtual PlannerResult plan(const SceneConstPtr &scene, const robot_state::RobotStatePtr &start_state);
244 
245  /** \brief Get planner configurations offered by this planner.
246  * Any of the configurations returned can be set as the planner for a motion planning
247  * query sent to plan().
248  * \return A vector of strings of planner configuration names.
249  */
251  /** \} */
252 
253  /** \name Debugging Options
254  \{ */
255 
256  /** \brief Set whether a report file (for the optimization) should be written or not.
257  * \param[in] file_write_cb Whether to write the file or not.
258  * \param[in] file_path File path
259  */
260  void setWriteFile(bool file_write_cb, const std::string &file_path = "");
261 
262  /** \} */
263 
264  protected:
265  /** \brief Create a TrajOpt problem construction info object with default values.
266  * \param[out] pci Pointer to problem construction info initialized.
267  */
269 
270  /** \brief Add velocity cost to the trajectory optimization.
271  * \param[out] pci Pointer to problem construction info with velocity cost added.
272  */
274 
275  /** \brief Add collision avoidance cost to the trajectory optimization for all waypoints.
276  * \param[out] pci Pointer to problem construction info with collision avoidance added.
277  */
279 
280  /** \brief Add a (joint) configuration constraint in the first waypoint taken from \a request.
281  * \param[in] request Request motion planning problem.
282  * \param[out] pci Pointer to problem construction info with start state constraint added.
283  */
286 
287  /** \brief Add a (joint) configuration constraint \a start_state in the first waypoint.
288  * \param[in] start_state Desired robot's start state.
289  * \param[out] pci Pointer to problem construction info with start state constraint added.
290  */
291  void addStartState(const robot_state::RobotStatePtr &start_state,
293 
294  /** \brief Add a (joint) configuration constraint \a start_state in the first waypoint.
295  * \param[in] start_state Desired start manipulator's joint names/values.
296  * \param[out] pci Pointer to problem construction info with start state constraint added.
297  */
300 
301  /** \brief Add a cartesian pose constraint \a start_pose for \a link in the first waypoint.
302  * \param[in] start_pose Desired start cartesian pose of \a link.
303  * \param[in] link Link that will be constrained to be in \a start_pose in the first waypoint.
304  * \param[out] pci Pointer to problem construction info with start pose constraint added.
305  */
306  void addStartPose(const RobotPose &start_pose, const std::string &link,
308 
309  /** \brief Add a (joint) configuration constraint in the last waypoint taken from \a request.
310  * \param[in] request Request motion planning problem.
311  * \param[out] pci Pointer to problem construction info with goal state constraint added.
312  */
315 
316  /** \brief Add a (joint) configuration constraint \a goal_state in the last waypoint.
317  * \param[in] goal_state Desired robot's goal state.
318  * \param[out] pci Pointer to problem construction info with goal state constraint added.
319  */
320  void addGoalState(const robot_state::RobotStatePtr &goal_state,
322 
323  /** \brief Add a (joint) configuration constraint \a goal_state in the last waypoint.
324  * \param[in] goal_state Desired goal manipulator's joint values.
325  * \param[out] pci Pointer to problem construction info with goal state constraint added.
326  */
327  void addGoalState(const std::vector<double> goal_state,
329 
330  /** \brief Add a cartesian pose constraint \a goal_pose for \a link in the last waypoint.
331  * \param[in] goal_pose Desired goal cartesian pose of \a link.
332  * \param[in] link Link that will be constrained to be in \a goal_pose in the last waypoint.
333  * \param[out] pci Pointer to problem construction info with goal pose constraint added.
334  */
335  void addGoalPose(const RobotPose &goal_pose, const std::string &link,
337 
338  /** \brief Solve SQP optimization problem.
339  * \param[in] scene Scene to plan for.
340  * \param[in] pci Pointer to problem construction info initialized.
341  * \return Planner result with convergence and collision status.
342  */
345 
346  /** \brief Get parameters of the SQP.
347  * \return SQP parameters.
348  */
349  sco::BasicTrustRegionSQPParameters getTrustRegionSQPParameters() const;
350 
351  robot_trajectory::RobotTrajectoryPtr trajectory_; ///< Last successful trajectory generated by the
352  ///< planner.
353  trajopt::TrajArray tesseract_trajectory_; ///< Last successful trajectory generated by the
354  ///< planner in Tesseract format.
355  tesseract::tesseract_ros::KDLEnvPtr env_; ///< KDL environment.
356  std::string group_; ///< Name of group to plan for.
357  std::string manip_; ///< Name of manipulator chain to check for collisions.
358  bool cont_cc_{true}; ///< Use continuous collision checking.
360  std::string file_path_; ///< Path of debug file.
361  bool file_write_cb_{false}; ///< Whether to write a debug file or not.
362  trajopt::InitInfo::Type init_type_{trajopt::InitInfo::Type::STATIONARY}; ///< Type of initial
363  ///< trajectory.
364  trajopt::TrajArray initial_trajectory_; ///< Initial trajectory (if any).
365  double time_{0.0}; ///< Time taken by the optimizer the last time it was called.
366  std::vector<int> fixed_joints_; ///< List of joints that need to be fixed, indexed in the order they
367  ///< appear in the manipulator.
368  robot_state::RobotStatePtr ref_state_; ///< Reference state to build moveit trajectory waypoints.
369  };
370 } // namespace robowflex
371 
372 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
A shared pointer wrapper for robowflex::MotionRequestBuilder.
An abstract interface to a motion planning algorithm.
A shared pointer wrapper for robowflex::Robot.
A const shared pointer wrapper for robowflex::Scene.
Robowflex Tesseract TrajOpt Planner.
const trajopt::TrajArray & getTesseractTrajectory() const
Get the trajectory that resulted in the last call to plan() in Tesseract format.
void addGoalState(const MotionRequestBuilderPtr &request, std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Add a (joint) configuration constraint in the last waypoint taken from request.
trajopt::TrajArray tesseract_trajectory_
std::shared_ptr< std::ofstream > stream_ptr_
Debug file stream.
std::string manip_
Name of manipulator chain to check for collisions.
void addGoalState(const robot_state::RobotStatePtr &goal_state, std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Add a (joint) configuration constraint goal_state in the last waypoint.
robot_state::RobotStatePtr ref_state_
Reference state to build moveit trajectory waypoints.
double getPlanningTime() const
Get the time spent by the planner the last time it was called.
const std::vector< std::string > & getManipulatorJoints() const
Get the joint names of the env manipulator.
const std::vector< std::string > & getEnvironmentLinks() const
Get the link names in the tesseract KDL environment.
sco::BasicTrustRegionSQPParameters getTrustRegionSQPParameters() const
Get parameters of the SQP.
double time_
Time taken by the optimizer the last time it was called.
void addCollisionAvoidance(std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Add collision avoidance cost to the trajectory optimization for all waypoints.
struct robowflex::TrajOptPlanner::Options options
void setWriteFile(bool file_write_cb, const std::string &file_path="")
Set whether a report file (for the optimization) should be written or not.
void setInitialTrajectory(const robot_trajectory::RobotTrajectoryPtr &init_trajectory)
Set initial trajectory for TrajOpt and set init_type to GIVEN_TRAJ.
void addGoalPose(const RobotPose &goal_pose, const std::string &link, std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Add a cartesian pose constraint goal_pose for link in the last waypoint.
void addVelocityCost(std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Add velocity cost to the trajectory optimization.
const std::vector< std::string > & getManipulatorLinks() const
Get the link names of the env manipulator.
std::vector< std::string > getPlannerConfigs() const override
Get planner configurations offered by this planner. Any of the configurations returned can be set as ...
bool file_write_cb_
Whether to write a debug file or not.
bool cont_cc_
Use continuous collision checking.
void addStartState(const robot_state::RobotStatePtr &start_state, std::shared_ptr< trajopt::ProblemConstructionInfo > pci)
Add a (joint) configuration constraint start_state in the first waypoint.
void problemConstructionInfo(std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Create a TrajOpt problem construction info object with default values.
void setInitialTrajectory(const trajopt::TrajArray &init_trajectory)
Set initial trajectory for TrajOpt and set init_type to GIVEN_TRAJ.
trajopt::TrajArray initial_trajectory_
Initial trajectory (if any).
void fixJoints(const std::vector< std::string > &joints)
Constrain certain joints during optimization to their initial value.
std::pair< bool, bool > PlannerResult
Planner result: first->converged, second->collision_free.
void addStartState(const std::unordered_map< std::string, double > &start_state, std::shared_ptr< trajopt::ProblemConstructionInfo > pci)
Add a (joint) configuration constraint start_state in the first waypoint.
PlannerResult solve(const SceneConstPtr &scene, const std::shared_ptr< trajopt::ProblemConstructionInfo > &pci)
Solve SQP optimization problem.
trajopt::InitInfo::Type init_type_
robot_trajectory::RobotTrajectoryPtr trajectory_
const robot_trajectory::RobotTrajectoryPtr & getTrajectory() const
Get the trajectory that resulted in the last call to plan().
std::string group_
Name of group to plan for.
std::string file_path_
Path of debug file.
void addStartState(const MotionRequestBuilderPtr &request, std::shared_ptr< trajopt::ProblemConstructionInfo > pci)
Add a (joint) configuration constraint in the first waypoint taken from request.
void addStartPose(const RobotPose &start_pose, const std::string &link, std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Add a cartesian pose constraint start_pose for link in the first waypoint.
planning_interface::MotionPlanResponse plan(const SceneConstPtr &scene, const planning_interface::MotionPlanRequest &request) override
Plan a motion given a request and a scene.
bool initialize(const std::string &manip)
Initialize planner. The user must specify a chain group defined in the robot's srdf that contains all...
void setInitType(const trajopt::InitInfo::Type &init_type)
Set type of initialization to use for the trajectory optimization. Current options are: STATIONARY JO...
std::vector< int > fixed_joints_
TrajOptPlanner(const RobotPtr &robot, const std::string &group_name, const std::string &name="trajopt")
Constructor.
tesseract::tesseract_ros::KDLEnvPtr env_
KDL environment.
void addGoalState(const std::vector< double > goal_state, std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
Add a (joint) configuration constraint goal_state in the last waypoint.
T infinity(T... args)
moveit_msgs::MotionPlanRequest MotionPlanRequest
Functions for loading and animating robots in Blender.
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Definition: adapter.h:24
Functions for loading and animating scenes in Blender.
Options structure with parameter values for TrajOpt planner.
double pose_cnt_rot_coeffs
Coefficients for pose constraints (rotation).
bool use_time
Whether any cost/cnt use time.
bool use_cont_col_avoid
Whether to use continuous collision avoidance or not.
double merit_coeff_increase_ratio
Ratio that we increate coeff each time.
sco::ModelType backend_optimizer
Optimizer to use.
double joint_vel_coeffs
Coefficients for joint_vel costs.
double max_merit_coeff_increases
Number of times that we jack up penalty coefficient.
double joint_pos_cnt_coeffs
Coefficients for joint position constraints.
double default_safety_margin
Default safety margin for collision avoidance.
double merit_error_coeff
Initial penalty coefficient.
double init_info_dt
Value of dt in init_info.
bool start_fixed
Whether to use the current env state as a fixed initial state.
int num_waypoints
Number of waypoints.
double trust_box_size
Current size of trust region (component-wise).
double max_iter
The max number of iterations.
double pose_cnt_pos_coeffs
Coefficients for pose constraints (position).
double default_safety_margin_coeffs
Coefficients for safety margin.