- 3.0.2 optimal control module.
MpcTimeHorizon-impl.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4  **********************************************************************************************************************/
5 
6 #pragma once
7 
8 namespace ct {
9 namespace optcon {
10 namespace tpl {
11 
12 template <typename SCALAR>
13 MpcTimeHorizon<SCALAR>::MpcTimeHorizon(const mpc_settings& settings, const SCALAR& initialTimeHorizon)
14  : mpc_settings_(settings), initialTimeHorizon_(initialTimeHorizon)
15 {
16 }
17 
18 template <typename SCALAR>
20 {
21 }
22 
23 template <typename SCALAR>
24 bool MpcTimeHorizon<SCALAR>::computeNewTimeHorizon(const SCALAR& t_since_ended_first_solve,
25  const SCALAR& t_forward_prediction_stop,
26  SCALAR& new_T)
27 {
31  SCALAR timeLeft = initialTimeHorizon_ - (t_since_ended_first_solve + t_forward_prediction_stop);
32  timeLeft = std::max((SCALAR)0.0, timeLeft);
33 
34 
35  switch (mpc_settings_.mpc_mode)
36  {
38  {
39  new_T = initialTimeHorizon_; // leave time horizon unchanged
40 
41  return false; // in this mode, we never reach to the final time
42  }
44  {
45  new_T = timeLeft;
46 
47  if (new_T == 0.0)
48  return true; // reached time horizon, return true
49 
50  return false;
51  }
53  {
54  // std::max() ensures that the time is greater than the mininmum specified time horizon
55  new_T = std::max((SCALAR)mpc_settings_.minimumTimeHorizonMpc_, timeLeft);
56 
58  return true;
59 
60  return false;
61  }
63  {
64  new_T = std::min((SCALAR)mpc_settings_.minimumTimeHorizonMpc_, timeLeft);
65 
66  if (new_T == 0.0)
67  return true;
68 
69  return false;
70  }
71  default:
72  throw std::runtime_error("ERROR in MPC Constructor -- unknown Time Horizon Strategy.");
73  }
74 }
75 
76 template <typename SCALAR>
78 {
79  mpc_settings_ = mpcsettings;
80 }
81 
82 template <typename SCALAR>
84 {
85  initialTimeHorizon_ = initTimeHorizon;
86 }
87 
88 } // namespace tpl
89 } // namespace optcon
90 } // namespace ct
void updateSettings(const mpc_settings &mpcsettings)
Definition: MpcTimeHorizon-impl.h:77
MPC_MODE mpc_mode
Definition: MpcSettings.h:99
SCALAR initialTimeHorizon_
Definition: MpcTimeHorizon.h:50
MpcTimeHorizon(const mpc_settings &settings, const SCALAR &initialTimeHorizon)
Definition: MpcTimeHorizon-impl.h:13
CppAD::AD< CppAD::cg::CG< double > > SCALAR
MPC Settings struct.
Definition: MpcSettings.h:45
void updateInitialTimeHorizon(const SCALAR &initTimeHorizon)
update the time horizon which is used during the first call to the solver
Definition: MpcTimeHorizon-impl.h:83
RECEDING_HORIZON_WITH_FIXED_FINAL_TIME.
Definition: MpcSettings.h:40
FIXED_FINAL_TIME.
Definition: MpcSettings.h:34
CONSTANT_RECEDING_HORIZON.
Definition: MpcSettings.h:36
core::Time minimumTimeHorizonMpc_
Definition: MpcSettings.h:104
virtual ~MpcTimeHorizon()
Definition: MpcTimeHorizon-impl.h:19
FIXED_FINAL_TIME_WITH_MIN_TIME_HORIZON.
Definition: MpcSettings.h:38
virtual bool computeNewTimeHorizon(const SCALAR &t_since_ended_first_solve, const SCALAR &t_forward_prediction_stop, SCALAR &new_T)
compute new MPC time horizon
Definition: MpcTimeHorizon-impl.h:24
mpc_settings mpc_settings_
Definition: MpcTimeHorizon.h:48