centroidal_control_collection
InvariantSequentialExtension.h
Go to the documentation of this file.
1 /* Author: Masaki Murooka */
2 
3 #pragma once
4 
5 #include <iostream>
6 #include <memory>
7 
8 #include <CCC/StateSpaceModel.h>
9 
10 namespace CCC
11 {
60 template<int StateDim, int InputDim, int OutputDim>
62 {
63 public:
66 
67 public:
68  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
69 
75  InvariantSequentialExtension(const std::shared_ptr<_StateSpaceModel> & model,
76  int seq_len,
77  bool extend_for_output = false)
78  : model_(model), seq_len_(seq_len)
79  {
80  setup(extend_for_output);
81  }
82 
84  int totalStateDim() const
85  {
86  return seq_len_ * StateDim;
87  }
88 
90  int totalInputDim() const
91  {
92  return seq_len_ * InputDim;
93  }
94 
96  int totalOutputDim() const
97  {
98  return seq_len_ * OutputDim;
99  }
100 
101 protected:
103  void setup(bool extend_for_output)
104  {
105  // Check D matrix
106  if(extend_for_output && model_->D_.norm() > 0)
107  {
108  std::cerr
109  << "[InvariantSequentialExtension] Matrix D in state-space model must be zero when extending for output.\n"
110  << " Matrix D:\n"
111  << model_->D_;
112  }
113 
114  // Resize matrix
115  A_seq_.setZero(seq_len_ * StateDim, StateDim);
116  B_seq_.setZero(seq_len_ * StateDim, seq_len_ * InputDim);
117  E_seq_.setZero(seq_len_ * StateDim);
118 
119  for(int i = 0; i < seq_len_; i++)
120  {
121  // Setq A_seq_
122  if(i == 0)
123  {
124  A_seq_.template middleRows<StateDim>(i * StateDim) = model_->Ad_;
125  }
126  else
127  {
128  (A_seq_.template middleRows<StateDim>(i * StateDim)).noalias() =
129  model_->Ad_ * A_seq_.template middleRows<StateDim>((i - 1) * StateDim);
130  }
131 
132  // Setq B_seq_
133  for(int j = 0; j < seq_len_ - i; j++)
134  {
135  if(j == 0)
136  {
137  if(i == 0)
138  {
139  B_seq_.template block<StateDim, InputDim>(i * StateDim, 0) = model_->Bd_;
140  }
141  else
142  {
143  B_seq_.template block<StateDim, InputDim>(i * StateDim, 0).noalias() =
144  model_->Ad_ * B_seq_.template block<StateDim, InputDim>((i - 1) * StateDim, 0);
145  }
146  }
147  else
148  {
149  B_seq_.template block<StateDim, InputDim>((i + j) * StateDim, j * InputDim) =
150  B_seq_.template block<StateDim, InputDim>(i * StateDim, 0);
151  }
152  }
153 
154  // Setq E_seq_
155  if(i == 0)
156  {
157  E_seq_.template segment<StateDim>(i * StateDim) = model_->Ed_;
158  }
159  else
160  {
161  (E_seq_.template segment<StateDim>(i * StateDim)).noalias() =
162  model_->Ad_ * E_seq_.template segment<StateDim>((i - 1) * StateDim) + model_->Ed_;
163  }
164  }
165 
166  // Transform for output
167  if(extend_for_output)
168  {
169  // Set C_seq
170  Eigen::MatrixXd C_seq = Eigen::MatrixXd::Zero(seq_len_ * OutputDim, seq_len_ * StateDim);
171  for(int i = 0; i < seq_len_; i++)
172  {
173  C_seq.template block<OutputDim, StateDim>(i * OutputDim, i * StateDim) = model_->C_;
174  }
175 
176  // Apply C_seq
177  A_seq_ = C_seq * A_seq_;
178  B_seq_ = C_seq * B_seq_;
179  E_seq_ = C_seq * E_seq_;
180  }
181  }
182 
183 public:
185  std::shared_ptr<_StateSpaceModel> model_;
186 
188  int seq_len_ = 0;
189 
191  Eigen::Matrix<double, Eigen::Dynamic, StateDim> A_seq_;
192 
194  Eigen::MatrixXd B_seq_;
195 
197  Eigen::VectorXd E_seq_;
198 };
199 } // namespace CCC
CCC::InvariantSequentialExtension::totalStateDim
int totalStateDim() const
Get total state dimension.
Definition: InvariantSequentialExtension.h:84
CCC::InvariantSequentialExtension::model_
std::shared_ptr< _StateSpaceModel > model_
State-space model.
Definition: InvariantSequentialExtension.h:185
CCC::InvariantSequentialExtension::totalInputDim
int totalInputDim() const
Get total input dimension.
Definition: InvariantSequentialExtension.h:90
CCC::InvariantSequentialExtension::setup
void setup(bool extend_for_output)
Setup coefficients.
Definition: InvariantSequentialExtension.h:103
CCC::InvariantSequentialExtension::seq_len_
int seq_len_
Sequence length.
Definition: InvariantSequentialExtension.h:188
CCC::InvariantSequentialExtension::InvariantSequentialExtension
EIGEN_MAKE_ALIGNED_OPERATOR_NEW InvariantSequentialExtension(const std::shared_ptr< _StateSpaceModel > &model, int seq_len, bool extend_for_output=false)
Constructor.
Definition: InvariantSequentialExtension.h:75
CCC::StateSpaceModel
State-space model.
Definition: StateSpaceModel.h:21
CCC
Definition: CommonModels.h:7
CCC::InvariantSequentialExtension::totalOutputDim
int totalOutputDim() const
Get total output dimension.
Definition: InvariantSequentialExtension.h:96
CCC::InvariantSequentialExtension::B_seq_
Eigen::MatrixXd B_seq_
Sequential extension matrix .
Definition: InvariantSequentialExtension.h:194
CCC::InvariantSequentialExtension::A_seq_
Eigen::Matrix< double, Eigen::Dynamic, StateDim > A_seq_
Sequential extension matrix .
Definition: InvariantSequentialExtension.h:191
StateSpaceModel.h
CCC::InvariantSequentialExtension
Sequential extension for time-invariant system.
Definition: InvariantSequentialExtension.h:61
CCC::InvariantSequentialExtension::E_seq_
Eigen::VectorXd E_seq_
Sequential extension vector (i.e., offset vector) .
Definition: InvariantSequentialExtension.h:197