full_discretization_grid_base_se2.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020, Christoph Rösmann, All rights reserved.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Authors: Christoph Rösmann
21  *********************************************************************/
22 
23 #ifndef FULL_DISCRETIZATION_GRID_BASE_H_
24 #define FULL_DISCRETIZATION_GRID_BASE_H_
25 
27 
31 
33 
35 
36 #include <memory>
37 
38 namespace mpc_local_planner {
39 
65 {
66  public:
67  using Ptr = std::shared_ptr<FullDiscretizationGridBaseSE2>;
68  using UPtr = std::unique_ptr<FullDiscretizationGridBaseSE2>;
78 
80 
82  virtual ~FullDiscretizationGridBaseSE2() = default;
83 
86 
87  // implements interface method
89  NlpFunctions& nlp_fun, OptimizationEdgeSet& edges, SystemDynamicsInterface::Ptr dynamics, bool new_run,
90  const corbo::Time& t, ReferenceTrajectoryInterface* sref = nullptr, const Eigen::VectorXd* prev_u = nullptr,
91  double prev_u_dt = 0, ReferenceTrajectoryInterface* xinit = nullptr,
92  ReferenceTrajectoryInterface* uinit = nullptr) override;
93 
94  // implements interface method
95  double getFirstDt() const override { return getDt(); }
96  // implements interface method
97  double getFinalTime() const override { return double(getN() - 1) * getDt(); }
98 
99  // implements interface method
100  bool hasConstantControls() const override { return true; }
101  // implements interface method
102  bool hasSingleDt() const override { return true; }
103  // implements interface method
104  bool isTimeVariableGrid() const override { return !isDtFixedIntended(); }
105  // implements interface method
106  bool isUniformGrid() const override { return true; }
107  // implements interface method
108  bool providesStateTrajectory() const override { return true; }
109  // implements interface method
110  bool getFirstControlInput(Eigen::VectorXd& u0) override;
111 
113  const Eigen::VectorXd& getState(int k) const
114  {
115  assert(k <= getN());
116  if (k == _x_seq.size()) return _xf.values();
117  return _x_seq[k].values();
118  }
119 
120  // implements interface method
121  void getStateAndControlTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max = corbo::CORBO_INF_DBL) const override;
122 
123  // implements interface method
124  void clear() override;
125 
126  // implements interface method
127  bool isEmpty() const override { return _x_seq.empty() || _u_seq.empty(); }
128  // implements interface method
129  virtual bool isValid() const { return (_x_seq.size() == _u_seq.size()); }
130 
131  // implements interface method
132  void setN(int n, bool try_resample = true) override;
133  // implements interface method
134  void setInitialDt(double dt) override { setDtRef(dt); }
135  // implements interface method
136  double getInitialDt() const override { return getDtRef(); }
137  // implements interface method
138  int getInitialN() const override { return getNRef(); }
139 
141  int getNRef() const { return _n_ref; }
143  int getN() const override { return _x_seq.size() + 1; }
145  void setNRef(int n);
147  void setDtRef(double dt) { _dt_ref = dt; }
149  double getDtRef() const { return _dt_ref; }
151  double getDt() const { return _dt.value(); }
153  void setWarmStart(bool active) { _warm_start = active; }
156  {
157  _xf_fixed = xf_fixed;
158  setModified(true);
159  }
160 
164  void setCostIntegrationRule(CostIntegrationRule integration) { _cost_integration = integration; }
165 
175  int findClosestPose(double x_ref, double y_ref, int start_idx = 0, double* distance = nullptr) const;
176 
177  // implements interface method
178  std::vector<VertexInterface*>& getActiveVertices() override { return _active_vertices; }
179  // implements interface method
180  void getVertices(std::vector<VertexInterface*>& vertices) override;
181 
182  protected:
183  virtual void initializeSequences(const Eigen::VectorXd& x0, const Eigen::VectorXd& xf, ReferenceTrajectoryInterface& uref, NlpFunctions& nlp_fun);
184  virtual void initializeSequences(const Eigen::VectorXd& x0, const Eigen::VectorXd& xf, ReferenceTrajectoryInterface& xref,
186  virtual void warmStartShifting(const Eigen::VectorXd& x0);
187 
188  virtual bool adaptGrid(bool new_run, NlpFunctions& nlp_fun) { return false; } // A subclass might add a grid adaptation, returns true if adapted
189 
190  int findNearestState(const Eigen::VectorXd& x0);
191 
192  void updateBounds(const NlpFunctions& nlp_fun);
193 
194  virtual void resampleTrajectory(int n_new);
195 
196  bool checkAndInitializeXfFixedFlags(int dim_x);
197 
198  virtual void createEdges(NlpFunctions& nlp_fun, OptimizationEdgeSet& edges, SystemDynamicsInterface::Ptr dynamics) = 0;
199 
200  virtual bool isDtFixedIntended() const { return true; }
201  virtual bool isMovingHorizonWarmStartActive() const { return _warm_start; }
202  virtual bool isGridAdaptActive() const { return false; }
203 
204  void computeActiveVertices() override;
205 
206  corbo::FiniteDifferencesCollocationInterface::Ptr _fd_eval = std::make_shared<corbo::CrankNicolsonDiffCollocation>();
207 
208  std::vector<VectorVertexSE2> _x_seq;
209  std::vector<VectorVertex> _u_seq;
211  std::vector<VertexInterface*> _active_vertices;
212 
213  const NlpFunctions* _nlp_fun = nullptr; // cache -> for bounds
214 
215  int _n_ref = 11;
216  int _n_adapt = 0; // if adaption is on and warmstart off, we might use this n instead of n_ref (only if n_adapt > 0)
217  double _dt_ref = 0.1;
218  ScalarVertex _dt; // we need a ScalarVertex to use the helper methods in stage_functions.cpp
219  bool _warm_start = false;
220  bool _first_run = true;
221 
222  // might be required if the last dt should be fixed or if dt is not fixed
223  Eigen::Matrix<bool, -1, 1> _xf_fixed;
224  double _dt_lb = 0;
226 
228 };
229 
230 } // namespace mpc_local_planner
231 
232 #endif // FULL_DISCRETIZATION_GRID_BASE_H_
std::unique_ptr< DiscretizationGridInterface > UPtr
int findClosestPose(double x_ref, double y_ref, int start_idx=0, double *distance=nullptr) const
Find the closest pose (first part of the state vector) on the grid w.r.t. to a provided reference poi...
virtual bool adaptGrid(bool new_run, NlpFunctions &nlp_fun)
corbo::DiscretizationGridInterface::Ptr getInstance() const override=0
Return a newly created shared instance of the implemented class.
void getVertices(std::vector< VertexInterface *> &vertices) override
void setCostIntegrationRule(CostIntegrationRule integration)
Set cost integration rule.
geometry_msgs::TransformStamped t
VectorVertexSE2 with support for partially fixed components.
void setXfFixed(const Eigen::Matrix< bool, -1, 1 > &xf_fixed)
Set individual components of the final state to fixed or unfixed.
double distance(double x0, double y0, double x1, double y1)
virtual void createEdges(NlpFunctions &nlp_fun, OptimizationEdgeSet &edges, SystemDynamicsInterface::Ptr dynamics)=0
int getN() const override
get current horizon length
list vertices
void setModified(bool modified)
constexpr const double CORBO_INF_DBL
void setWarmStart(bool active)
activate or deactive warmstart
const double & value() const
corbo::GridUpdateResult update(const Eigen::VectorXd &x0, ReferenceTrajectoryInterface &xref, ReferenceTrajectoryInterface &uref, NlpFunctions &nlp_fun, OptimizationEdgeSet &edges, SystemDynamicsInterface::Ptr dynamics, bool new_run, const corbo::Time &t, ReferenceTrajectoryInterface *sref=nullptr, const Eigen::VectorXd *prev_u=nullptr, double prev_u_dt=0, ReferenceTrajectoryInterface *xinit=nullptr, ReferenceTrajectoryInterface *uinit=nullptr) override
corbo::FiniteDifferencesCollocationInterface::Ptr _fd_eval
std::shared_ptr< DiscretizationGridInterface > Ptr
Full discretization grid specialization for SE2.
const Eigen::VectorXd & getState(int k) const
Return state at time stamp k.
double getDt() const
get current temporal resolution
virtual void initializeSequences(const Eigen::VectorXd &x0, const Eigen::VectorXd &xf, ReferenceTrajectoryInterface &uref, NlpFunctions &nlp_fun)
const Eigen::VectorXd & values() const
void setDtRef(double dt)
set reference temporal resolution
std::shared_ptr< TimeSeries > Ptr
void setFiniteDifferencesCollocationMethod(corbo::FiniteDifferencesCollocationInterface::Ptr fd_eval)
Set finite differences collocation method.
PlainMatrixType mat * n
std::shared_ptr< FiniteDifferencesCollocationInterface > Ptr
void getStateAndControlTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max=corbo::CORBO_INF_DBL) const override
std::shared_ptr< SystemDynamicsInterface > Ptr
std::vector< VertexInterface * > & getActiveVertices() override
double getDtRef() const
get current reference temporal resolution


mpc_local_planner
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:53:18