31 template <
typename SCALAR =
double>
47 : mpc_settings_(mpc_settings),
49 finalPointReached_(false),
50 lastMeasuredDelay_(0.0),
51 maxDelayMeasured_(0.0),
52 minDelayMeasured_(std::numeric_limits<
SCALAR>::max()),
54 timeHorizonStrategy_(timeHorizonStrategy),
69 lastSolveTimer_.
reset();
70 firstSolveTimer_.
reset();
73 ext_lastSolveTimer_.
reset();
74 ext_firstSolveTimer_.
reset();
76 lastMeasuredDelay_ = 0.0;
77 maxDelayMeasured_ = 0.0;
78 minDelayMeasured_ = std::numeric_limits<SCALAR>::max();
80 finalPointReached_ =
false;
104 if (initialized_ ==
false)
105 throw std::runtime_error(
106 "Error in MPC time keeper: cannot update timings if MpcTimeKeeper not properly initialized.");
109 SCALAR timeSinceEndedLastSolve;
110 SCALAR timeSinceEndedFirstSolve;
116 ext_firstSolveTimer_.
stop(externalTime);
117 ext_lastSolveTimer_.
stop(externalTime);
123 lastSolveTimer_.
stop();
124 firstSolveTimer_.
stop();
132 timeSinceEndedLastSolve = 0.0;
133 timeSinceEndedFirstSolve = 0.0;
138 t_forw_start = timeSinceEndedLastSolve;
144 SCALAR delayToApply = computeDelayToApply();
149 t_forw_stop = t_forw_start + delayToApply;
151 t_forw_stop = t_forw_start;
157 if (t_forw_start > current_T)
159 std::cerr <<
"WARNING: forward integration start time cannot be bigger than last time horizon. Truncating " 160 "forward integration time." 162 t_forw_start = current_T;
164 if (t_forw_stop > current_T)
166 std::cerr <<
"WARNING: forward integration stop time cannot be bigger than last time horizon. Truncating " 167 "forward integration time." 169 t_forw_stop = current_T;
173 finalPointReached_ = timeHorizonStrategy_->computeNewTimeHorizon(timeSinceEndedFirstSolve, t_forw_stop, new_T);
176 #ifdef DEBUG_PRINT_TIMEKEEPER 177 std::cout <<
"DEBUG_PRINT_TIMEKEEPER: Time since first solve(): " << timeSinceEndedFirstSolve << std::endl;
178 std::cout <<
"DEBUG_PRINT_TIMEKEEPER: Time since last solve(): " << timeSinceEndedLastSolve << std::endl;
179 std::cout <<
"DEBUG_PRINT_TIMEKEEPER: t_forward_start: " << t_forw_start << std::endl;
180 std::cout <<
"DEBUG_PRINT_TIMEKEEPER: t_forward_stop: " << t_forw_stop << std::endl;
181 std::cout <<
"DEBUG_PRINT_TIMEKEEPER: New Time Horizon: " << new_T << std::endl;
182 std::cout <<
"DEBUG_PRINT_TIMEKEEPER: final point reached_: " << finalPointReached_ << std::endl;
205 ext_timer_.
start(externalTime);
217 ext_timer_.
stop(externalTime);
220 ext_lastSolveTimer_.
start(externalTime);
227 lastSolveTimer_.
start();
230 maxDelayMeasured_ = std::max(maxDelayMeasured_, lastMeasuredDelay_);
231 minDelayMeasured_ = std::min(minDelayMeasured_, lastMeasuredDelay_);
232 summedDelay_ += lastMeasuredDelay_;
234 if (lastMeasuredDelay_ < 0)
235 throw std::runtime_error(
"Fatal: measured delay cannot be < 0");
237 #ifdef DEBUG_PRINT_TIMEKEEPER 238 std::cout <<
"Measured delay during Solution: " << lastMeasuredDelay_ <<
" seconds" << std::endl;
239 std::cout <<
"Max. measured delay during Solution: " << maxDelayMeasured_ <<
" seconds" << std::endl;
240 std::cout <<
"Min. measured delay during Solution: " << minDelayMeasured_ <<
" seconds" << std::endl;
248 ext_firstSolveTimer_.
start(externalTime);
252 firstSolveTimer_.
start();
273 ext_lastSolveTimer_.
stop(externalTime);
278 lastSolveTimer_.
stop();
298 ext_firstSolveTimer_.
stop(externalTime);
303 firstSolveTimer_.
stop();
325 SCALAR computeDelayToApply()
328 SCALAR variableDelay = 0.0;
335 #ifdef DEBUG_PRINT_TIMEKEEPER 336 std::cout <<
"Accumulated delay to apply: " << fixedDelay + variableDelay <<
" seconds" << std::endl;
339 return fixedDelay + variableDelay;
348 bool finalPointReached_;
364 SCALAR lastMeasuredDelay_;
370 std::shared_ptr<MpcTimeHorizon<SCALAR>> timeHorizonStrategy_;
void computeNewTimings(const SCALAR externalTime, const SCALAR current_T, SCALAR &new_T, SCALAR &t_forw_start, SCALAR &t_forw_stop)
compute new mpc timings, based on current time horizon and the given time horizon strategy ...
Definition: MpcTimeKeeper.h:98
bool measureDelay_
Definition: MpcSettings.h:71
const bool finalPointReached() const
query this in order to find out if the final time horizon has been reached.
Definition: MpcTimeKeeper.h:192
void updateSettings(const mpc_settings &settings)
update mpc settings
Definition: MpcTimeKeeper.h:198
SCALAR getElapsedTime() const
SCALAR timeSincePreviousSuccessfulSolve(const SCALAR &externalTime)
retrieve the time that elapsed since the last successful solve() call to an Optimal Control Problem ...
Definition: MpcTimeKeeper.h:265
Time Keeper Class for Model Predictive Control.
Definition: MpcTimeKeeper.h:32
const SCALAR & getMeasuredDelay() const
obtain the delay which was measured during solving the optimal control problem
Definition: MpcTimeKeeper.h:310
double delayMeasurementMultiplier_
Definition: MpcSettings.h:79
void start(const SCALAR &time)
const SCALAR & getMaxMeasuredDelay() const
get the maximum measured delay (maximum over all cycles)
Definition: MpcTimeKeeper.h:312
SCALAR getElapsedTime() const
const SCALAR & getSummedDelay() const
get the sum of all measured delays
Definition: MpcTimeKeeper.h:316
const SCALAR & getMinMeasuredDelay() const
get the smallest measured delay (minimum over all cycles)
Definition: MpcTimeKeeper.h:314
MpcTimeKeeper()
Standard Constructor MpcTimeKeeper.
Definition: MpcTimeKeeper.h:39
CppAD::AD< CppAD::cg::CG< double > > SCALAR
int additionalDelayUs_
Definition: MpcSettings.h:89
MPC Settings struct.
Definition: MpcSettings.h:45
void stop(const SCALAR &time)
Definition: MpcTimeHorizon.h:20
MpcTimeKeeper(std::shared_ptr< MpcTimeHorizon< SCALAR >> timeHorizonStrategy, const mpc_settings &mpc_settings)
Constructor for Mpc Time Keeper class.
Definition: MpcTimeKeeper.h:46
const SCALAR timeSinceFirstSuccessfulSolve(const SCALAR &externalTime)
retrieve the time that elapsed since the first successful solve() call to an Optimal Control Problem ...
Definition: MpcTimeKeeper.h:290
bool useExternalTiming_
Definition: MpcSettings.h:118
int fixedDelayUs_
Definition: MpcSettings.h:84
void startDelayMeasurement(const SCALAR &externalTime)
start measuring time elapsed during planning / solving the optimal control problem ...
Definition: MpcTimeKeeper.h:200
void initialize()
initialize the Mpc Time Keeper (mandatory)
Definition: MpcTimeKeeper.h:64
void stopDelayMeasurement(const SCALAR &externalTime)
stop measuring time elapsed during solving the optimal control problem
Definition: MpcTimeKeeper.h:213