- 3.0.2 optimal control module.
MPC.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 #include <type_traits>
9 
12 
13 #include "MpcSettings.h"
14 #include "MpcTimeKeeper.h"
15 
18 
21 
22 //#define DEBUG_PRINT_MPC //! use this flag to enable debug printouts in the MPC implementation
23 
24 
25 namespace ct {
26 namespace optcon {
27 
28 
54 template <typename OPTCON_SOLVER>
55 class MPC
56 {
57 public:
58  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
59 
60  static const size_t STATE_DIM = OPTCON_SOLVER::STATE_D;
61  static const size_t CONTROL_DIM = OPTCON_SOLVER::CONTROL_D;
62 
63  static const size_t P_DIM = OPTCON_SOLVER::POS_DIM;
64  static const size_t V_DIM = OPTCON_SOLVER::VEL_DIM;
65 
66  using Scalar_t = typename OPTCON_SOLVER::Scalar_t;
67  using Policy_t = typename OPTCON_SOLVER::Policy_t;
69 
70 
72 
87  MPC(const OptConProblem_t& problem,
88  const typename OPTCON_SOLVER::Settings_t& solverSettings,
89  const mpc_settings& mpcsettings = mpc_settings(),
90  std::shared_ptr<PolicyHandler<Policy_t, STATE_DIM, CONTROL_DIM, Scalar_t>> customPolicyHandler = nullptr,
91  std::shared_ptr<tpl::MpcTimeHorizon<Scalar_t>> customTimeHorizon = nullptr);
92 
93 
95 
98  OPTCON_SOLVER& getSolver();
99 
100 
102 
106  void setTimeHorizonStrategy(std::shared_ptr<tpl::MpcTimeHorizon<Scalar_t>> timeHorizonStrategy);
107 
109 
112  void setInitialGuess(const Policy_t& initGuess);
113 
115  bool timeHorizonReached();
116 
117 
119 
123  const Scalar_t timeSinceFirstSuccessfulSolve(const Scalar_t& extTime);
124 
125 
127 
140  void doForwardIntegration(const Scalar_t& t_forward_start,
141  const Scalar_t& t_forward_stop,
143  const std::shared_ptr<core::Controller<STATE_DIM, CONTROL_DIM, Scalar_t>> forwardIntegrationController =
144  nullptr);
145 
146 
151  void prepareIteration(const Scalar_t& ext_ts);
152 
153 
155 
172  const Scalar_t x_ts,
173  Policy_t& newPolicy,
174  Scalar_t& newPolicy_ts,
175  const std::shared_ptr<core::Controller<STATE_DIM, CONTROL_DIM, Scalar_t>> forwardIntegrationController =
176  nullptr);
177 
178 
180  void resetMpc(const Scalar_t& newTimeHorizon);
181 
182 
184 
189  void updateSettings(const mpc_settings& settings);
190 
191 
193  void printMpcSummary();
194 
195 
196 private:
198 
210  void integrateForward(const Scalar_t startTime,
211  const Scalar_t stopTime,
213  const std::shared_ptr<core::Controller<STATE_DIM, CONTROL_DIM, Scalar_t>>& controller);
214 
215  void checkSettings(const mpc_settings& settings);
216 
218  Scalar_t t_forward_start_;
219  Scalar_t t_forward_stop_;
220 
222  OPTCON_SOLVER solver_;
223 
225  mpc_settings mpc_settings_;
226 
228  typename OPTCON_SOLVER::OptConProblem_t::DynamicsPtr_t dynamics_;
229 
232 
234  bool firstRun_;
235 
237  size_t runCallCounter_;
238 
240  std::shared_ptr<PolicyHandler<Policy_t, STATE_DIM, CONTROL_DIM, Scalar_t>> policyHandler_;
241 
243  Policy_t currentPolicy_;
244 
246  std::shared_ptr<tpl::MpcTimeHorizon<Scalar_t>> timeHorizonStrategy_;
247 
249  tpl::MpcTimeKeeper<Scalar_t> timeKeeper_;
250 };
251 
252 
253 } //namespace optcon
254 } //namespace ct
void printMpcSummary()
printout simple statistical data
Definition: MPC-impl.h:300
Definition: PolicyHandler.h:12
const Scalar_t timeSinceFirstSuccessfulSolve(const Scalar_t &extTime)
retrieve the time that elapsed since the first successful solve() call to an Optimal Control Problem ...
Definition: MPC-impl.h:108
bool finishIteration(const core::StateVector< STATE_DIM, Scalar_t > &x, const Scalar_t x_ts, Policy_t &newPolicy, Scalar_t &newPolicy_ts, const std::shared_ptr< core::Controller< STATE_DIM, CONTROL_DIM, Scalar_t >> forwardIntegrationController=nullptr)
finish MPC iteration
Definition: MPC-impl.h:177
bool timeHorizonReached()
Check if final time horizon for this task was reached.
Definition: MPC-impl.h:101
MPC(const OptConProblem_t &problem, const typename OPTCON_SOLVER::Settings_t &solverSettings, const mpc_settings &mpcsettings=mpc_settings(), std::shared_ptr< PolicyHandler< Policy_t, STATE_DIM, CONTROL_DIM, Scalar_t >> customPolicyHandler=nullptr, std::shared_ptr< tpl::MpcTimeHorizon< Scalar_t >> customTimeHorizon=nullptr)
MPC solver constructor.
Definition: MPC-impl.h:12
void prepareIteration(const Scalar_t &ext_ts)
Definition: MPC-impl.h:138
static const size_t CONTROL_DIM
Definition: MPC.h:61
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const size_t STATE_DIM
Definition: MPC.h:60
typename OPTCON_SOLVER::Policy_t Policy_t
Definition: MPC.h:67
void doForwardIntegration(const Scalar_t &t_forward_start, const Scalar_t &t_forward_stop, core::StateVector< STATE_DIM, Scalar_t > &x_start, const std::shared_ptr< core::Controller< STATE_DIM, CONTROL_DIM, Scalar_t >> forwardIntegrationController=nullptr)
perform forward integration of the measured system state, to compensate for expected or already occur...
Definition: MPC-impl.h:115
MPC Settings struct.
Definition: MpcSettings.h:45
Main MPC class.
Definition: MPC.h:55
void setTimeHorizonStrategy(std::shared_ptr< tpl::MpcTimeHorizon< Scalar_t >> timeHorizonStrategy)
Additional method to insert a custom time horizon strategy, independent from the constructor.
Definition: MPC-impl.h:85
Definition: MpcTimeHorizon.h:20
typename OPTCON_SOLVER::Scalar_t Scalar_t
Definition: MPC.h:66
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
static const size_t P_DIM
Definition: MPC.h:63
void resetMpc(const Scalar_t &newTimeHorizon)
reset the mpc problem and provide new problem time horizon (mandatory)
Definition: MPC-impl.h:270
void setInitialGuess(const Policy_t &initGuess)
set a new initial guess for the policy
Definition: MPC-impl.h:92
Definition: OptConProblemBase.h:40
void updateSettings(const mpc_settings &settings)
update the mpc settings in all instances (main class, time keeper class, etc)
Definition: MPC-impl.h:286
static const size_t V_DIM
Definition: MPC.h:64
OPTCON_SOLVER & getSolver()
Allows access to the solver member, required mainly for unit testing.
Definition: MPC-impl.h:78