- 3.0.2 optimal control module.
LinearSpliner.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 
10 
11 namespace ct {
12 namespace optcon {
13 
21 template <class T, typename SCALAR = double>
22 class LinearSpliner : public SplinerBase<T, SCALAR>
23 {
24 public:
25  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 
27  typedef T vector_t;
28  typedef std::vector<vector_t, Eigen::aligned_allocator<vector_t>> vector_array_t;
29  typedef Eigen::Matrix<SCALAR, T::DIM, T::DIM> matrix_t;
30 
31  LinearSpliner() = delete;
32 
38  LinearSpliner(std::shared_ptr<tpl::TimeGrid<SCALAR>> grid) : timeGrid_(grid) {}
39  ~LinearSpliner() override = default;
40  void computeSpline(const vector_array_t& points) override { nodes_ = points; }
41  // evaluate spline and return vector at interpolation time
42  vector_t evalSpline(const SCALAR time, const size_t shotIdx) override
43  {
44  Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> result;
45  result.resize(T::DIM);
46 
47  // int shotIdx = timeGrid_->getShotIndex(time);
48  SCALAR t_shot = timeGrid_->getShotDuration(shotIdx); /* current duration of a whole shot*/
49  SCALAR t_s_start = timeGrid_->getShotStartTime(shotIdx); /* time when this particular shot started */
50  SCALAR t_s_end = timeGrid_->getShotEndTime(shotIdx); /* time when this particular shot ends */
51 
52  assert(shotIdx < nodes_.size());
53 
54  result = nodes_[shotIdx] * (t_s_end - time) / t_shot + nodes_[shotIdx + 1] * (time - t_s_start) / t_shot;
55 
56  return result;
57  }
58 
59 
60  vector_t splineDerivative_t(const SCALAR time, const size_t shotIdx) const override
61  {
62  vector_t result;
63 
64  SCALAR t_shot = timeGrid_->getShotDuration(shotIdx); /* current duration of a whole shot*/
65 
66  result = (nodes_[shotIdx + 1] - nodes_[shotIdx]) / t_shot;
67 
68  return result;
69  }
70 
71 
72  vector_t splineDerivative_h_i(const SCALAR time, const size_t shotIdx) const override
73  {
74  vector_t result;
75 
76  SCALAR t_shot = timeGrid_->getShotDuration(shotIdx); /* current duration of a whole shot*/
77  SCALAR t_s_start = timeGrid_->getShotStartTime(shotIdx); /* time when this particular shot started */
78 
79  result = (time - t_s_start) * (nodes_[shotIdx] - nodes_[shotIdx + 1]) / (t_shot * t_shot);
80 
81  return result;
82  }
83 
84  matrix_t splineDerivative_q_i(const SCALAR time, const size_t shotIdx) const override
85  {
86  matrix_t drv;
87 
88  SCALAR t_shot = timeGrid_->getShotDuration(shotIdx); /* current duration of a the shot*/
89  SCALAR t_s_end = timeGrid_->getShotEndTime(shotIdx); /* time when this particular shot ends */
90 
91  drv.setIdentity();
92  drv *= (t_s_end - time) / t_shot;
93 
94  return drv;
95  }
96 
97 
98  matrix_t splineDerivative_q_iplus1(const SCALAR time, const size_t shotIdx) const override
99  {
100  matrix_t drv;
101 
102  // int shotIdx = timeGrid_->getShotIndex(time);
103  SCALAR t_shot = timeGrid_->getShotDuration(shotIdx); /* current duration of the shot*/
104  SCALAR t_s_start = timeGrid_->getShotStartTime(shotIdx); /* time when this particular shot started */
105 
106  drv.setIdentity();
107  drv *= (time - t_s_start) / t_shot;
108 
109  return drv;
110  }
111 
112 
113 private:
114  vector_array_t nodes_; // an array of references to grid points between which is interpolated
115 
116  std::shared_ptr<tpl::TimeGrid<SCALAR>> timeGrid_;
117 };
118 
119 } // namespace optcon
120 } // namespace ct
Eigen::Matrix< SCALAR, T::DIM, T::DIM > matrix_t
Definition: LinearSpliner.h:29
void grid(bool flag)
Abstract base class for the control input splining between the DMS shots.
Definition: SplinerBase.h:20
matrix_t splineDerivative_q_i(const SCALAR time, const size_t shotIdx) const override
Return the spline derivative with respect to the control input at shot i.
Definition: LinearSpliner.h:84
The linear spline implementation.
Definition: LinearSpliner.h:22
vector_t evalSpline(const SCALAR time, const size_t shotIdx) override
Depending on the spline type, this method evaluates the control input between the shots...
Definition: LinearSpliner.h:42
~LinearSpliner() override=default
LinearSpliner(std::shared_ptr< tpl::TimeGrid< SCALAR >> grid)
Custom constructor.
Definition: LinearSpliner.h:38
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void computeSpline(const vector_array_t &points) override
Updates the vector on the shots.
Definition: LinearSpliner.h:40
std::vector< vector_t, Eigen::aligned_allocator< vector_t > > vector_array_t
Definition: LinearSpliner.h:28
Definition: TimeGrid.h:27
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef T vector_t
Definition: LinearSpliner.h:27
matrix_t splineDerivative_q_iplus1(const SCALAR time, const size_t shotIdx) const override
Returns the spline derivative with respect to the control input at shot i+1.
Definition: LinearSpliner.h:98
vector_t splineDerivative_t(const SCALAR time, const size_t shotIdx) const override
Returns the spline derivatives with respect to time.
Definition: LinearSpliner.h:60
vector_t splineDerivative_h_i(const SCALAR time, const size_t shotIdx) const override
Returns the spline derivatives with respect to the time segment between the shots.
Definition: LinearSpliner.h:72