finite_differences_variable_grid_se2.cpp
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 
24 
26 
27 #include <corbo-communication/utilities.h>
28 #include <corbo-core/console.h>
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <memory>
33 
34 namespace mpc_local_planner {
35 
36 void FiniteDifferencesVariableGridSE2::setDtBounds(double dt_lb, double dt_ub)
37 {
38  _dt_lb = dt_lb;
39  _dt_ub = dt_ub;
40 }
41 
42 void FiniteDifferencesVariableGridSE2::setGridAdaptTimeBasedSingleStep(int n_max, double dt_hyst_ratio, bool adapt_first_iter)
43 {
45  _n_max = n_max;
46  _dt_hyst_ratio = dt_hyst_ratio;
47  _adapt_first_iter = adapt_first_iter;
48 }
49 
50 void FiniteDifferencesVariableGridSE2::setGridAdaptTimeBasedAggressiveEstimate(int n_max, double dt_hyst_ratio, bool adapt_first_iter)
51 {
53  _n_max = n_max;
54  _dt_hyst_ratio = dt_hyst_ratio;
55  _adapt_first_iter = adapt_first_iter;
56 }
57 
59 {
61  _adapt_first_iter = adapt_first_iter;
62 }
63 
65 {
66  // do not adapt grid in a new run
67  if (new_run && !_adapt_first_iter) return false;
68 
69  bool changed = false;
70  switch (_grid_adapt)
71  {
73  {
74  break;
75  }
77  {
78  changed = adaptGridTimeBasedSingleStep(nlp_fun);
79  break;
80  }
82  {
83  changed = adaptGridTimeBasedAggressiveEstimate(nlp_fun);
84  break;
85  }
87  {
88  changed = adaptGridSimpleShrinkingHorizon(nlp_fun);
89  break;
90  }
91  default:
92  {
93  PRINT_ERROR_NAMED("selected grid adaptation strategy not implemented.");
94  }
95  }
96  return changed;
97 }
98 
100 {
101  PRINT_WARNING_COND_NAMED(!isTimeVariableGrid(), "time based adaptation might only be used with a fixed dt.");
102 
103  _nlp_fun = &nlp_fun;
104 
105  int n = getN();
106 
107  double dt = getDt();
108  if (dt > _dt_ref * (1.0 + _dt_hyst_ratio) && n < _n_max)
109  {
110  resampleTrajectory(n + 1);
111  _n_adapt = n + 1;
112  return true;
113  }
114  else if (dt < _dt_ref * (1.0 - _dt_hyst_ratio) && n > _n_min)
115  {
116  resampleTrajectory(n - 1);
117  _n_adapt = n - 1;
118  return true;
119  }
120  return false;
121 }
122 
124 {
125  PRINT_WARNING_COND_NAMED(!isTimeVariableGrid(), "time based adaptation might only be used with a fixed dt.");
126 
127  _nlp_fun = &nlp_fun;
128  int n = getN();
129  double dt = getDt();
130 
131  // check if hysteresis is satisfied
132  if (dt >= _dt_ref * (1.0 - _dt_hyst_ratio) && dt <= _dt_ref * (1.0 + _dt_hyst_ratio)) return false;
133 
134  // estimate number of samples based on the fraction dt/dt_ref.
135  // dt is the time difference obtained in a previous solution (with a coarser resp. finer trajectory resolution)
136  int new_n = std::round((double)n * (dt / _dt_ref));
137 
138  // bound value
139  if (new_n > _n_max)
140  new_n = _n_max;
141  else if (new_n < _n_min)
142  new_n = _n_min;
143 
144  if (new_n == n) return false;
145 
146  // and now resample
147  resampleTrajectory(new_n);
148  _n_adapt = new_n;
149  return true;
150 }
151 
153 {
154  int n = getN();
155  if (n > _n_min)
156  {
157  resampleTrajectory(n - 1);
158  _n_adapt = n - 1;
159  }
160  return false;
161 }
162 
163 } // namespace mpc_local_planner
#define PRINT_ERROR_NAMED(msg)
#define PRINT_WARNING_COND_NAMED(cond, msg)
void setGridAdaptTimeBasedAggressiveEstimate(int n_max, double dt_hyst_ratio=0.1, bool adapt_first_iter=false)
Set grid adaptation strategy to &#39;aggressive&#39;.
void setGridAdaptSimpleShrinkingHorizon(bool adapt_first_iter=false)
Set grid adaptation strategy to &#39;shrinking horizon&#39;.
void setGridAdaptTimeBasedSingleStep(int n_max, double dt_hyst_ratio=0.1, bool adapt_first_iter=false)
Set grid adaptation strategy to &#39;single step&#39;.
int getN() const override
get current horizon length
bool adaptGrid(bool new_run, NlpFunctions &nlp_fun) override
double getDt() const
get current temporal resolution
PlainMatrixType mat * n
void setDtBounds(double dt_lb, double dt_ub)
Set lower and upper bounds for the temporal resolution.


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