- 3.0.2 optimal control module.
TimeGrid.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 
7 #pragma once
8 
9 //#define DEBUG_TIMEGRID
10 
11 #include <math.h>
12 #include <cmath>
13 
14 namespace ct {
15 namespace optcon {
16 namespace tpl {
17 
26 template <typename SCALAR>
27 class TimeGrid
28 {
29 public:
30  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
31 
32  TimeGrid() = delete;
33 
40  TimeGrid(const size_t numberOfShots, const SCALAR timeHorizon)
41  : numberOfShots_(numberOfShots), timeHorizon_(timeHorizon), t_(numberOfShots + 1, SCALAR(0.0))
42  {
44  }
45 
51  void changeShotCount(const size_t numberOfShots)
52  {
53  numberOfShots_ = numberOfShots;
54  t_.clear();
55  t_.resize(numberOfShots_ + 1, SCALAR(0.0));
57  }
58 
65  {
66  timeHorizon_ = timeHorizon;
68  }
69 
70 
79  void updateTimeGrid(const Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>& h_segment)
80  {
81  t_[0] = SCALAR(0.0); //by convention (just for documentation)
82 
83  for (size_t i = 0; i < (size_t)h_segment.size(); ++i)
84  t_[i + 1] = t_[i] + h_segment(i);
85 
86 #ifdef DEBUG_TIMEGRID
87  std::cout << " ... in updateTimeGrid(). t_ = ";
88  for (size_t i = 0; i < t_.size(); i++)
89  std::cout << std::setprecision(10) << t_[i] << " ";
90 
91  std::cout << std::endl;
92 #endif
93  }
94 
95 
100  {
101  for (size_t i = 0; i < numberOfShots_ + 1; i++)
102  t_[i] = i * (SCALAR)(timeHorizon_ / (SCALAR)numberOfShots_);
103  }
104 
105 
113  const SCALAR getShotStartTime(const size_t shot_index) const { return t_[shot_index]; }
121  const SCALAR getShotEndTime(const size_t shot_index) const { return t_[shot_index + 1]; }
129  const SCALAR getShotDuration(const size_t shot_index) const { return (t_[shot_index + 1] - t_[shot_index]); }
141  const SCALAR getTimeHorizon() const { return timeHorizon_; }
147  const SCALAR getOptimizedTimeHorizon() const { return t_.back(); }
148 private:
149  const size_t numberOfShots_;
150  SCALAR timeHorizon_;
151 
152  // the individual times of each pair from i=0,..., N
154 };
155 }
156 
158 
159 } // namespace optcon
160 } // namespace ct
const SCALAR getShotEndTime(const size_t shot_index) const
Returns the end time of a shot.
Definition: TimeGrid.h:121
const SCALAR getOptimizedTimeHorizon() const
Returns the optimized timehorizon.
Definition: TimeGrid.h:147
EIGEN_MAKE_ALIGNED_OPERATOR_NEW TimeGrid()=delete
const ct::core::tpl::TimeArray< SCALAR > & toImplementation()
Returns the underlying TimeArray.
Definition: TimeGrid.h:135
const SCALAR getShotDuration(const size_t shot_index) const
Returns to duration of a shot.
Definition: TimeGrid.h:129
CppAD::AD< CppAD::cg::CG< double > > SCALAR
ct::core::Time timeHorizon
Definition: ConstrainedNLOCTest.cpp:15
Definition: TimeGrid.h:27
for i
Definition: mpc_unittest_plotting.m:14
void makeUniformGrid()
Creates a uniform timegrid.
Definition: TimeGrid.h:99
void changeTimeHorizon(const SCALAR timeHorizon)
Updates the timegrid when the timehorizon changes.
Definition: TimeGrid.h:64
const SCALAR getTimeHorizon() const
Returns the initial timehorizon of the problem.
Definition: TimeGrid.h:141
TimeGrid(const size_t numberOfShots, const SCALAR timeHorizon)
Custom constructor.
Definition: TimeGrid.h:40
void changeShotCount(const size_t numberOfShots)
Updates the timegrid when the number of shots changes.
Definition: TimeGrid.h:51
void updateTimeGrid(const Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > &h_segment)
This method updates the timegrid when new optimized time segments arrive from the nlp solver...
Definition: TimeGrid.h:79
const SCALAR getShotStartTime(const size_t shot_index) const
Returns to start time of a shot.
Definition: TimeGrid.h:113