moveit2
The MoveIt Motion Planning Framework for ROS 2.
problem_description.h
Go to the documentation of this file.
1 #pragma once
2 #include <trajopt/common.hpp>
3 #include <trajopt/json_marshal.hpp>
4 #include <trajopt_sco/optimizers.hpp>
5 
8 
9 #include <memory>
10 
11 namespace trajopt_interface
12 {
40 struct TermInfo;
41 MOVEIT_CLASS_FORWARD(TermInfo); // Defines TermInfoPtr, ConstPtr, WeakPtr... etc
42 
43 class TrajOptProblem;
44 MOVEIT_CLASS_FORWARD(TrajOptProblem); // Defines TrajOptProblemPtr, ConstPtr, WeakPtr... etc
45 
46 struct JointPoseTermInfo;
47 MOVEIT_CLASS_FORWARD(JointPoseTermInfo); // Defines JointPoseTermInfoPtr, ConstPtr, WeakPtr... etc
48 
49 struct CartPoseTermInfo;
50 MOVEIT_CLASS_FORWARD(CartPoseTermInfo); // Defines CartPoseTermInfoPtr, ConstPtr, WeakPtr... etc
51 
52 struct JointVelTermInfo;
53 MOVEIT_CLASS_FORWARD(JointVelTermInfo); // Defines JointVelTermInfoPtr, ConstPtr, WeakPtr... etc
54 
55 struct ProblemInfo;
56 TrajOptProblemPtr ConstructProblem(const ProblemInfo&);
57 
59 {
60  TT_COST = 0x1, // 0000 0001
61  TT_CNT = 0x2, // 0000 0010
62  TT_USE_TIME = 0x4, // 0000 0100
63 };
64 
65 struct BasicInfo
66 {
78  int n_steps;
79  sco::IntVec dofs_fixed; // optional
80  sco::ModelType convex_solver; // which convex solver to use
81 
83  bool use_time = false;
85  double dt_upper_lim = 1.0;
87  double dt_lower_lim = 1.0;
88 };
89 
93 struct InitInfo
94 {
104  enum Type
105  {
109  };
115  trajopt::TrajArray data;
116  // Eigen::VectorXd data_vec;
117  // trajopt::TrajArray data_trajectory;
119  double dt = 1.0;
120 };
121 
127 struct TermInfo
128 {
129  std::string name;
132  {
133  return supported_term_types_;
134  }
135  // virtual void fromJson(ProblemConstructionInfo& pci, const Json::Value& v) = 0;
136  virtual void addObjectiveTerms(TrajOptProblem& prob) = 0;
137 
138  static TermInfoPtr fromName(const std::string& type);
139 
144  using MakerFunc = TermInfoPtr (*)(void);
145  static void RegisterMaker(const std::string& type, MakerFunc);
146 
147  TermInfo() = default;
148  TermInfo(const TermInfo&) = default;
149  TermInfo(TermInfo&&) = default;
150  TermInfo& operator=(const TermInfo&) = default;
151  TermInfo& operator=(TermInfo&&) = default;
152  virtual ~TermInfo() = default;
153 
154 protected:
155  TermInfo(int supported_term_types) : supported_term_types_(supported_term_types)
156  {
157  }
158 
159 private:
160  static std::map<std::string, MakerFunc> name_to_maker_;
161  int supported_term_types_;
162 };
163 
165 {
166 public:
168  sco::BasicTrustRegionSQPParameters opt_info;
169  std::vector<TermInfoPtr> cost_infos;
170  std::vector<TermInfoPtr> cnt_infos;
172 
173  planning_scene::PlanningSceneConstPtr planning_scene;
174  std::string planning_group_name;
175 
176  ProblemInfo(planning_scene::PlanningSceneConstPtr ps, const std::string& pg)
178  {
179  }
180 };
181 
186 class TrajOptProblem : public sco::OptProb
187 {
188 public:
189  TrajOptProblem();
190  TrajOptProblem(const ProblemInfo& problem_info);
191  virtual ~TrajOptProblem() = default;
193  sco::VarVector GetVarRow(int i, int start_col, int num_col)
194  {
195  return matrix_traj_vars.rblock(i, start_col, num_col);
196  }
198  sco::VarVector GetVarRow(int i)
199  {
200  return matrix_traj_vars.row(i);
201  }
203  sco::Var& GetVar(int i, int j)
204  {
205  return matrix_traj_vars.at(i, j);
206  }
207  trajopt::VarArray& GetVars()
208  {
209  return matrix_traj_vars;
210  }
213  {
214  return matrix_traj_vars.rows();
215  }
218  int GetNumDOF()
219  {
220  return matrix_traj_vars.cols();
221  }
225  {
226  return dof_;
227  }
228  planning_scene::PlanningSceneConstPtr GetPlanningScene()
229  {
230  return planning_scene_;
231  }
232  void SetInitTraj(const trajopt::TrajArray& x)
233  {
234  matrix_init_traj = x;
235  }
236  trajopt::TrajArray GetInitTraj()
237  {
238  return matrix_init_traj;
239  }
240  // friend TrajOptProbPtr ConstructProblem(const ProblemConstructionInfo&);
242  bool GetHasTime()
243  {
244  return has_time;
245  }
247  void SetHasTime(bool tmp)
248  {
249  has_time = tmp;
250  }
251 
252 private:
254  bool has_time;
256  trajopt::VarArray matrix_traj_vars;
257  planning_scene::PlanningSceneConstPtr planning_scene_;
258  std::string planning_group_;
260  int dof_;
261  trajopt::TrajArray matrix_init_traj;
262 };
263 
268 struct CartPoseTermInfo : public TermInfo
269 {
270  // EIGEN_MAKE_ALIGNED_OPERATOR_NEW
271 
273  int timestep;
277  Eigen::Vector4d wxyz;
281  std::string link;
283  Eigen::Isometry3d tcp;
284 
286 
288  // void fromJson(ProblemConstructionInfo& pci, const Json::Value& v) override;
290  void addObjectiveTerms(TrajOptProblem& prob) override;
291 
292  static TermInfoPtr create()
293  {
294  return std::make_shared<CartPoseTermInfo>();
295  }
296 };
297 
309 {
311  trajopt::DblVec coeffs;
313  trajopt::DblVec targets;
315  trajopt::DblVec upper_tols;
317  trajopt::DblVec lower_tols;
319  int first_step = 0;
321  int last_step = -1;
322 
325  {
326  }
327 
329  void addObjectiveTerms(TrajOptProblem& prob) override;
330 
331  static TermInfoPtr create()
332  {
333  return std::make_shared<JointPoseTermInfo>();
334  }
335 };
336 
337 struct JointVelTermInfo : public TermInfo
338 {
340  trajopt::DblVec coeffs;
342  trajopt::DblVec targets;
344  trajopt::DblVec upper_tols;
346  trajopt::DblVec lower_tols;
348  int first_step = 0;
350  int last_step = -1;
351 
354  {
355  }
356 
358  void addObjectiveTerms(TrajOptProblem& prob) override;
359 
360  static TermInfoPtr create()
361  {
362  return std::make_shared<JointVelTermInfo>();
363  }
364 };
365 
366 void generateInitialTrajectory(const ProblemInfo& pci, const std::vector<double>& current_joint_values,
367  trajopt::TrajArray& init_traj);
368 
369 } // namespace trajopt_interface
planning_scene::PlanningSceneConstPtr GetPlanningScene()
sco::VarVector GetVarRow(int i, int start_col, int num_col)
Returns the values of the specified joints (start_col to num_col) for the specified timestep i.
int GetNumDOF()
Returns the problem DOF. This is the number of columns in the optization matrix. Note that this is no...
bool GetHasTime()
Returns TrajOptProb.has_time.
int GetActiveGroupNumDOF()
Returns the kinematic DOF of the active joint model group.
void SetInitTraj(const trajopt::TrajArray &x)
sco::VarVector GetVarRow(int i)
Returns the values of all joints for the specified timestep i.
int GetNumSteps()
Returns the number of steps in the problem. This is the number of rows in the optimization matrix.
void SetHasTime(bool tmp)
Sets TrajOptProb.has_time
sco::Var & GetVar(int i, int j)
Returns the value of the specified joint j for the specified timestep i.
Vec3fX< details::Vec3Data< double > > Vector3d
Definition: fcl_compat.h:89
x
Definition: pick.py:64
This namespace includes the central class for representing planning contexts.
MOVEIT_CLASS_FORWARD(TermInfo)
TrajOptProblemPtr ConstructProblem(const ProblemInfo &)
void generateInitialTrajectory(const ProblemInfo &pci, const std::vector< double > &current_joint_values, trajopt::TrajArray &init_traj)
int n_steps
Number of time steps (rows) in the optimization matrix.
double dt_upper_lim
The upper limit of 1/dt values allowed in the optimization.
double dt_lower_lim
The lower limit of 1/dt values allowed in the optimization.
bool use_time
If true, the last column in the optimization matrix will be 1/dt.
bool start_fixed
If true, first time step is fixed with a joint level constraint If this is false, the starting point ...
This term is used when the goal frame is fixed in cartesian space.
Eigen::Vector3d pos_coeffs
coefficients for position and rotation
Eigen::Vector3d xyz
Cartesian position.
std::string link
Link which should reach desired pose.
Eigen::Isometry3d tcp
Static transform applied to the link.
Eigen::Vector4d wxyz
Rotation quaternion.
void addObjectiveTerms(TrajOptProblem &prob) override
Used to add term to pci from json.
int timestep
Timestep at which to apply term.
Type
Methods of initializing the optimization matrix. This defines how robot moves from the current state ...
trajopt::TrajArray data
Data used during initialization. Use depends on the initialization selected. This data will be used t...
Type type
Specifies the type of initialization to use.
double dt
Default value the final column of the optimization is initialized too if time is being used.
Joint space position cost Position operates on a single point (unlike velocity, etc)....
int last_step
Last time step to which the term is applied. Default: prob.GetNumSteps() - 1.
trajopt::DblVec upper_tols
Vector of position upper limits. Size should be the DOF of the system. Default: vector of 0's.
trajopt::DblVec coeffs
Vector of coefficients that scale the cost. Size should be the DOF of the system. Default: vector of ...
trajopt::DblVec targets
Vector of position targets. This is a required value. Size should be the DOF of the system.
trajopt::DblVec lower_tols
Vector of position lower limits. Size should be the DOF of the system. Default: vector of 0's.
int first_step
First time step to which the term is applied. Default: 0.
JointPoseTermInfo()
Initialize term with it's supported types.
void addObjectiveTerms(TrajOptProblem &prob) override
Converts term info into cost/constraint and adds it to trajopt problem.
void addObjectiveTerms(TrajOptProblem &prob) override
Converts term info into cost/constraint and adds it to trajopt problem.
trajopt::DblVec coeffs
Vector of coefficients that scale the cost. Size should be the DOF of the system. Default: vector of ...
trajopt::DblVec lower_tols
Vector of velocity lower limits. Size should be the DOF of the system. Default: vector of 0's.
JointVelTermInfo()
Initialize term with it's supported types.
trajopt::DblVec targets
Vector of velocity targets. This is a required value. Size should be the DOF of the system.
trajopt::DblVec upper_tols
Vector of velocity upper limits. Size should be the DOF of the system. Default: vector of 0's.
int last_step
Last time step to which the term is applied. Default: prob.GetNumSteps() - 1.
int first_step
First time step to which the term is applied. Default: 0.
std::vector< TermInfoPtr > cnt_infos
ProblemInfo(planning_scene::PlanningSceneConstPtr ps, const std::string &pg)
std::vector< TermInfoPtr > cost_infos
planning_scene::PlanningSceneConstPtr planning_scene
sco::BasicTrustRegionSQPParameters opt_info
static void RegisterMaker(const std::string &type, MakerFunc)
virtual void addObjectiveTerms(TrajOptProblem &prob)=0
virtual ~TermInfo()=default
TermInfo & operator=(const TermInfo &)=default
TermInfoPtr(*)(void) MakerFunc
TermInfo(TermInfo &&)=default
static TermInfoPtr fromName(const std::string &type)
TermInfo & operator=(TermInfo &&)=default
TermInfo(int supported_term_types)
TermInfo(const TermInfo &)=default