NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
gurobi_trajectory.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef TRAJ_OPT_PRO_GUROBI_TRAJECTORY_H_
20 #define TRAJ_OPT_PRO_GUROBI_TRAJECTORY_H_
21 
24 
25 #include <memory>
26 #include <vector>
27 
28 class GRBLinExpr;
29 class GRBQuadExpr;
30 class GRBVar;
31 class GRBModel;
32 
33 namespace traj_opt {
34 
35 enum { TRAJ_X = 0, TRAJ_Y = 1, TRAJ_Z = 2, TRAJ_PSI = 3 };
36 
38  public:
39  TrajSection1D(GRBModel *model_, uint n_p_, uint k_r_, decimal_t dt_,
40  boost::shared_ptr<BasisBundlePro> basis);
42 
43  decimal_t evaluate(decimal_t t, uint derr) const;
44 
45  // gurobi contr functions
46  void getContr(decimal_t x, uint derr, GRBLinExpr &expr);
47  void getLinCost(GRBLinExpr &expr, uint i);
48  void getCost(GRBQuadExpr &cost);
49 
50  // unpacks optimized coefficients
51  void recoverVars(decimal_t ratio = 1.0);
52  void recoverVarsF(decimal_t ratio = 1.0);
53 
55  void setDt(decimal_t t) {
56  // rescale coefficients
57  // for (auto &co : coeffs)
58  // co *= std::pow(dt / t, k_r);
59  dt = t;
60  }
62 
63  uint n_p; // dimension of underlying basis representation
64  uint k_r; // order being minimized i.e. 4 = snap
65  decimal_t dt; // durration of segment
66 
67  boost::shared_ptr<BasisBundlePro> basis;
68  // list of bases // makethis global singleton?
69 
70  std::vector<GRBVar> coeffs_var;
71 
72  std::vector<decimal_t> coeffs;
73 
74  protected:
75  bool generated; // whether or not trajectory has been generated
76 
77  // gurobi model
78  GRBModel *model;
79 };
80 
81 // Trajectory section in 4D
82 
84  public:
85  TrajSection4D(GRBModel *model, uint n_p_, uint k_r_, decimal_t dt_,
86  boost::shared_ptr<BasisBundlePro> basis_,
87  boost::shared_ptr<BasisTransformer> basisT_,
88  boost::shared_ptr<BasisTransformer> basisD_);
90 
91  void evaluate(decimal_t t, uint derr, VecD &out) const;
92 
93  void getContr(decimal_t x, uint derr, uint dim,
94  GRBLinExpr &expr); // dim 0,1,2,3 for x,y,z,psi
95  void getCost(GRBQuadExpr &cost);
96 
98  // unpacks optimized coefficients
99  void recoverVars(decimal_t ratio = 1.0);
100  void recoverVarsF(decimal_t ratio = 1.0);
101  void setDt(decimal_t dt);
102 
103  std::vector<boost::shared_ptr<TrajSection1D>> secs;
104  void addPathCost(const MatD &A, const VecD &b, double epsilon);
105  void addLineCost(const Vec3 &p0, const Vec3 &p1, double upsilon);
106  void getControl(uint point_id, uint dim, GRBLinExpr &expr, bool derr);
107  void addVisionCost(const MatD &norm_pnts, decimal_t weight_loss, VecD &grad);
108  // private:
109  std::vector<GRBVar> const_var;
110 
111  GRBModel *model_;
112  boost::shared_ptr<BasisTransformer> basisT;
113  boost::shared_ptr<BasisTransformer> basisD;
114  boost::shared_ptr<BasisBundlePro> basis_;
115  int dim{4};
116  int n_p;
118 };
121  public:
122  LegendreTrajectory(GRBModel *model_, const std::vector<decimal_t> dts_,
123  int degree, PolyType polytype,
124  int order = 3); // way points and
125  // times each column
126  // represents a time
127  // derrivate: column 0
128  // is position, column
129  // 1 is velocity, etc.
130  // Trajectory(const Trajectory& traj) = delete; // forbid copying because
131  // dependent on gurobi model pointer
133 
134  void addMaximumBound(decimal_t bound,
135  uint derr); // ex (3.5,1) sets maximum velocities to 3.5
136  bool recoverVars(decimal_t ratio = 1.0);
137  bool recoverVarsF(decimal_t ratio = 1.0);
138  bool evaluate(decimal_t t, uint derr, VecD &out)
139  const; // returns false when out of time range, but returns enpoints
140 
141  // get total trajectory length in seconds
142  decimal_t getTotalTime() const;
143 
144  void addVolumeContraints(const Mat4Vec &constr); // adds 3D volume constrains
145  // to the position of the
146  // robot
147  // add constraints of the form Ax <= b
148  void addAxbConstraints(const std::vector<MatD> &A, const std::vector<VecD> &b,
149  const std::vector<decimal_t> &ds);
150 
151  void addWayPointConstrains(const std::vector<Waypoint> &waypnts, bool use_v0);
152 
153  decimal_t getCost();
154 
155  bool adjustTimes(decimal_t epsilon);
156 
157  void adjustTimeToMaxs(decimal_t max_vel, decimal_t max_acc, decimal_t max_jrk,
158  bool use_v0, decimal_t v0);
159  VecD check_max_violation(decimal_t max_vel, decimal_t max_acc,
160  decimal_t max_jrk);
161  void resetConstr(bool lp = false);
162  void setQuadraticCost();
163  void setLinearCost();
164  void setCost(bool lp);
165 
166  virtual TrajData serialize();
167 
168  std::vector<boost::shared_ptr<TrajSection4D>> individual_sections;
169  void addConvexificationConstraints(const MatD &Aobs, const VecD &bobs,
170  const MatD &Aenv, const VecD &benv);
171  void setSlackCost();
172  void setConParam(ConstraintMode mode, int num) {
173  con_mode_ = mode;
174  num_sample_ = num;
175  }
176  void resetConstrVision(const MatD &points, decimal_t weight);
177  void getJacobian(const MatD &norm_pnts, decimal_t weight_loss, VecD &J);
178  decimal_t getCostV(const MatD &points, decimal_t weight);
179 
180  bool check_max(decimal_t max_vel, decimal_t max_acc, decimal_t max_jrk);
181 
182  private:
183  void linkSections(); // links endpoints of trajectories
184 
185  GRBModel *model;
186 
187  std::vector<decimal_t> dts;
188  std::vector<MatD> A_;
189  std::vector<VecD> b_;
190  std::vector<Waypoint> waypnts_;
191 
192  std::vector<decimal_t> old_dts;
193  int n_p_;
194  int k_r_;
195 
196  boost::shared_ptr<BasisTransformer> basisT;
197  boost::shared_ptr<BasisTransformer> basisD;
198  std::vector<GRBVar> slack_var;
199 
200  std::vector<Poly> time_composite;
201  bool use_time_composite{false};
202  ConstraintMode con_mode_{CONTROL};
203  int num_sample_{10};
204  int dim{4};
205 };
206 } // namespace traj_opt
207 #endif // TRAJ_OPT_PRO_GUROBI_TRAJECTORY_H_
mode
uint8_t mode
Definition: signal_lights.h:74
trajectory_solver.h
traj_opt::TrajSection1D::k_r
uint k_r
Definition: gurobi_trajectory.h:64
traj_opt::TrajSection4D::secs
std::vector< boost::shared_ptr< TrajSection1D > > secs
Definition: gurobi_trajectory.h:103
traj_opt::TrajSection1D::generated
bool generated
Definition: gurobi_trajectory.h:75
traj_opt::LegendreTrajectory::setConParam
void setConParam(ConstraintMode mode, int num)
Definition: gurobi_trajectory.h:172
traj_opt::TrajSection4D::getContr
void getContr(decimal_t x, uint derr, uint dim, GRBLinExpr &expr)
traj_opt::CHEBY
@ CHEBY
Definition: gurobi_trajectory.h:119
traj_opt::TrajSection1D::evaluate
decimal_t evaluate(decimal_t t, uint derr) const
traj_opt::PolyType
PolyType
Definition: traj_data.h:27
traj_opt::MatD
Eigen::Matrix< decimal_t, Eigen::Dynamic, Eigen::Dynamic > MatD
Definition: types.h:51
traj_opt::LegendreTrajectory::addMaximumBound
void addMaximumBound(decimal_t bound, uint derr)
traj_opt::Trajectory
Definition: trajectory.h:35
traj_opt::LegendreTrajectory::adjustTimes
bool adjustTimes(decimal_t epsilon)
traj_opt::TrajSection1D::getCost
void getCost(GRBQuadExpr &cost)
traj_opt::TrajSection4D::basis_
boost::shared_ptr< BasisBundlePro > basis_
Definition: gurobi_trajectory.h:114
traj_opt::LegendreTrajectory::setLinearCost
void setLinearCost()
traj_opt::TrajSection4D::dim
int dim
Definition: gurobi_trajectory.h:115
traj_opt::LegendreTrajectory::setQuadraticCost
void setQuadraticCost()
traj_opt::TrajSection4D::const_var
std::vector< GRBVar > const_var
Definition: gurobi_trajectory.h:109
traj_opt::LegendreTrajectory::addConvexificationConstraints
void addConvexificationConstraints(const MatD &Aobs, const VecD &bobs, const MatD &Aenv, const VecD &benv)
traj_opt::TrajSection4D::addPathCost
void addPathCost(const MatD &A, const VecD &b, double epsilon)
traj_opt::TrajData
Definition: traj_data.h:61
traj_opt::TrajSection4D::dt_
decimal_t dt_
Definition: gurobi_trajectory.h:117
traj_opt::TrajSection4D::addLineCost
void addLineCost(const Vec3 &p0, const Vec3 &p1, double upsilon)
traj_opt::TrajSection4D::basisT
boost::shared_ptr< BasisTransformer > basisT
Definition: gurobi_trajectory.h:112
traj_opt::TrajSection1D::coeffs
std::vector< decimal_t > coeffs
Definition: gurobi_trajectory.h:72
traj_opt::LegendreTrajectory::recoverVars
bool recoverVars(decimal_t ratio=1.0)
traj_opt::LegendreTrajectory::getCostV
decimal_t getCostV(const MatD &points, decimal_t weight)
traj_opt::LegendreTrajectory::addWayPointConstrains
void addWayPointConstrains(const std::vector< Waypoint > &waypnts, bool use_v0)
traj_opt::LegendreTrajectory::resetConstrVision
void resetConstrVision(const MatD &points, decimal_t weight)
traj_opt::ConstraintMode
ConstraintMode
Definition: gurobi_trajectory.h:119
traj_opt::TrajSection1D::getBoostPoly
Poly getBoostPoly()
traj_opt::TrajSection1D::getCostDerr
decimal_t getCostDerr()
traj_opt::decimal_t
double decimal_t
Definition: types.h:35
traj_opt::LegendreTrajectory
Definition: gurobi_trajectory.h:120
traj_opt::Vec3
Eigen::Matrix< decimal_t, 3, 1 > Vec3
Definition: types.h:40
polynomial_basis.h
traj_opt::CONTROL
@ CONTROL
Definition: gurobi_trajectory.h:119
traj_opt::TrajSection4D::TrajSection4D
TrajSection4D(GRBModel *model, uint n_p_, uint k_r_, decimal_t dt_, boost::shared_ptr< BasisBundlePro > basis_, boost::shared_ptr< BasisTransformer > basisT_, boost::shared_ptr< BasisTransformer > basisD_)
traj_opt::LegendreTrajectory::adjustTimeToMaxs
void adjustTimeToMaxs(decimal_t max_vel, decimal_t max_acc, decimal_t max_jrk, bool use_v0, decimal_t v0)
traj_opt::LegendreTrajectory::evaluate
bool evaluate(decimal_t t, uint derr, VecD &out) const
traj_opt::LegendreTrajectory::check_max_violation
VecD check_max_violation(decimal_t max_vel, decimal_t max_acc, decimal_t max_jrk)
traj_opt::TrajSection4D::setDt
void setDt(decimal_t dt)
traj_opt::TrajSection1D::coeffs_var
std::vector< GRBVar > coeffs_var
Definition: gurobi_trajectory.h:70
traj_opt::LegendreTrajectory::resetConstr
void resetConstr(bool lp=false)
traj_opt::Mat4Vec
std::vector< Mat4, Eigen::aligned_allocator< Mat4 > > Mat4Vec
Definition: types.h:47
traj_opt::LegendreTrajectory::addVolumeContraints
void addVolumeContraints(const Mat4Vec &constr)
traj_opt::LegendreTrajectory::setCost
void setCost(bool lp)
traj_opt::LegendreTrajectory::serialize
virtual TrajData serialize()
traj_opt::TrajSection1D::dt
decimal_t dt
Definition: gurobi_trajectory.h:65
traj_opt
Definition: msg_traj.h:27
traj_opt::TrajSection1D::setDt
void setDt(decimal_t t)
Definition: gurobi_trajectory.h:55
traj_opt::LegendreTrajectory::check_max
bool check_max(decimal_t max_vel, decimal_t max_acc, decimal_t max_jrk)
traj_opt::LegendreTrajectory::individual_sections
std::vector< boost::shared_ptr< TrajSection4D > > individual_sections
Definition: gurobi_trajectory.h:168
traj_opt::TrajSection4D::model_
GRBModel * model_
Definition: gurobi_trajectory.h:111
traj_opt::TrajSection4D::n_p
int n_p
Definition: gurobi_trajectory.h:116
traj_opt::TrajSection4D::getCost
void getCost(GRBQuadExpr &cost)
traj_opt::LegendreTrajectory::getCost
decimal_t getCost()
traj_opt::SAMPLE
@ SAMPLE
Definition: gurobi_trajectory.h:119
traj_opt::LegendreTrajectory::~LegendreTrajectory
~LegendreTrajectory()
traj_opt::TrajSection1D::recoverVars
void recoverVars(decimal_t ratio=1.0)
traj_opt::TrajSection1D::getContr
void getContr(decimal_t x, uint derr, GRBLinExpr &expr)
traj_opt::TrajSection1D::recoverVarsF
void recoverVarsF(decimal_t ratio=1.0)
traj_opt::TRAJ_PSI
@ TRAJ_PSI
Definition: gurobi_trajectory.h:35
traj_opt::TrajSection4D::addVisionCost
void addVisionCost(const MatD &norm_pnts, decimal_t weight_loss, VecD &grad)
traj_opt::TrajSection4D::recoverVarsF
void recoverVarsF(decimal_t ratio=1.0)
traj_opt::TrajSection1D::TrajSection1D
TrajSection1D(GRBModel *model_, uint n_p_, uint k_r_, decimal_t dt_, boost::shared_ptr< BasisBundlePro > basis)
traj_opt::TrajSection1D
Definition: gurobi_trajectory.h:37
traj_opt::LegendreTrajectory::recoverVarsF
bool recoverVarsF(decimal_t ratio=1.0)
traj_opt::LegendreTrajectory::getJacobian
void getJacobian(const MatD &norm_pnts, decimal_t weight_loss, VecD &J)
traj_opt::TrajSection4D::getControl
void getControl(uint point_id, uint dim, GRBLinExpr &expr, bool derr)
traj_opt::TrajSection1D::n_p
uint n_p
Definition: gurobi_trajectory.h:63
traj_opt::TrajSection4D::evaluate
void evaluate(decimal_t t, uint derr, VecD &out) const
traj_opt::LegendreTrajectory::getTotalTime
decimal_t getTotalTime() const
traj_opt::TRAJ_Y
@ TRAJ_Y
Definition: gurobi_trajectory.h:35
traj_opt::TrajSection1D::getLinCost
void getLinCost(GRBLinExpr &expr, uint i)
traj_opt::TrajSection4D
Definition: gurobi_trajectory.h:83
traj_opt::TRAJ_X
@ TRAJ_X
Definition: gurobi_trajectory.h:35
traj_opt::Poly
boost::math::tools::polynomial< decimal_t > Poly
Definition: polynomial_basis.h:61
traj_opt::LegendreTrajectory::LegendreTrajectory
LegendreTrajectory(GRBModel *model_, const std::vector< decimal_t > dts_, int degree, PolyType polytype, int order=3)
traj_opt::TrajSection1D::basis
boost::shared_ptr< BasisBundlePro > basis
Definition: gurobi_trajectory.h:67
traj_opt::TrajSection4D::getCostDerr
decimal_t getCostDerr()
traj_opt::VecD
Eigen::Matrix< decimal_t, Eigen::Dynamic, 1 > VecD
Definition: types.h:49
traj_opt::TrajSection4D::basisD
boost::shared_ptr< BasisTransformer > basisD
Definition: gurobi_trajectory.h:113
traj_opt::TrajSection1D::~TrajSection1D
~TrajSection1D()
traj_opt::LegendreTrajectory::setSlackCost
void setSlackCost()
traj_opt::LegendreTrajectory::addAxbConstraints
void addAxbConstraints(const std::vector< MatD > &A, const std::vector< VecD > &b, const std::vector< decimal_t > &ds)
traj_opt::TrajSection4D::~TrajSection4D
~TrajSection4D()
traj_opt::TRAJ_Z
@ TRAJ_Z
Definition: gurobi_trajectory.h:35
traj_opt::TrajSection4D::recoverVars
void recoverVars(decimal_t ratio=1.0)
traj_opt::ELLIPSE
@ ELLIPSE
Definition: gurobi_trajectory.h:119
traj_opt::TrajSection1D::model
GRBModel * model
Definition: gurobi_trajectory.h:78