centroidal_control_collection
VariantSequentialExtension.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 {
69 template<int StateDim, template<class, class...> class ListType = std::vector>
71 {
72 public:
75 
76 public:
77  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
78 
83  VariantSequentialExtension(const ListType<std::shared_ptr<_StateSpaceModel>> & model_list,
84  bool extend_for_output = false)
85  : model_list_(model_list)
86  {
87  setup(extend_for_output);
88  }
89 
91  int totalStateDim() const
92  {
93  return total_state_dim_;
94  }
95 
97  int totalInputDim() const
98  {
99  return total_input_dim_;
100  }
101 
103  int totalOutputDim() const
104  {
105  return total_output_dim_;
106  }
107 
108 protected:
110  void setup(bool extend_for_output)
111  {
112  // Check D matrix
113  if(extend_for_output)
114  {
115  for(const auto & model : model_list_)
116  {
117  if(model->D_.norm() > 0)
118  {
119  std::cerr
120  << "[VariantSequentialExtension] Matrix D in state-space model must be zero when extending for output.\n"
121  << " Matrix D:\n"
122  << model->D_;
123  break;
124  }
125  }
126  }
127 
128  // Set total dimension
129  size_t seq_len = model_list_.size();
130  total_state_dim_ = static_cast<int>(seq_len) * StateDim;
131  total_input_dim_ = 0;
132  total_output_dim_ = 0;
133  for(const auto & model : model_list_)
134  {
135  total_input_dim_ += model->inputDim();
136  total_output_dim_ += model->outputDim();
137  }
138 
139  // Resize matrix
140  A_seq_.setZero(total_state_dim_, StateDim);
142  E_seq_.setZero(total_state_dim_);
143 
144  int accum_input_dim = 0;
145  for(size_t i = 0; i < seq_len; i++)
146  {
147  int current_input_dim = model_list_[i]->inputDim();
148 
149  // Setq A_seq_
150  if(i == 0)
151  {
152  A_seq_.template middleRows<StateDim>(i * StateDim) = model_list_[0]->Ad_;
153  }
154  else
155  {
156  (A_seq_.template middleRows<StateDim>(i * StateDim)).noalias() =
157  model_list_[i]->Ad_ * A_seq_.template middleRows<StateDim>((i - 1) * StateDim);
158  }
159 
160  // Setq B_seq_
161  for(size_t j = i; j < seq_len; j++)
162  {
163  if(j == i)
164  {
165  B_seq_.block(j * StateDim, accum_input_dim, StateDim, current_input_dim) = model_list_[i]->Bd_;
166  }
167  else
168  {
169  (B_seq_.block(j * StateDim, accum_input_dim, StateDim, current_input_dim)).noalias() =
170  model_list_[j]->Ad_ * B_seq_.block((j - 1) * StateDim, accum_input_dim, StateDim, current_input_dim);
171  }
172  }
173 
174  // Setq E_seq_
175  if(i == 0)
176  {
177  E_seq_.segment<StateDim>(i * StateDim) = model_list_[0]->Ed_;
178  }
179  else
180  {
181  (E_seq_.segment<StateDim>(i * StateDim)).noalias() =
182  model_list_[i]->Ad_ * E_seq_.segment<StateDim>((i - 1) * StateDim) + model_list_[i]->Ed_;
183  }
184 
185  accum_input_dim += current_input_dim;
186  }
187 
188  // Transform for output
189  if(extend_for_output)
190  {
191  // Set C_seq
192  Eigen::MatrixXd C_seq = Eigen::MatrixXd::Zero(total_output_dim_, total_state_dim_);
193  int accum_output_dim = 0;
194  for(size_t i = 0; i < seq_len; i++)
195  {
196  int current_output_dim = model_list_[i]->outputDim();
197 
198  C_seq.block(accum_output_dim, i * StateDim, current_output_dim, StateDim) = model_list_[i]->C_;
199 
200  accum_output_dim += current_output_dim;
201  }
202 
203  // Apply C_seq
204  A_seq_ = C_seq * A_seq_;
205  B_seq_ = C_seq * B_seq_;
206  E_seq_ = C_seq * E_seq_;
207  }
208  }
209 
210 public:
212  ListType<std::shared_ptr<_StateSpaceModel>> model_list_;
213 
216 
219 
222 
224  Eigen::Matrix<double, Eigen::Dynamic, StateDim> A_seq_;
225 
227  Eigen::MatrixXd B_seq_;
228 
230  Eigen::VectorXd E_seq_;
231 };
232 } // namespace CCC
CCC::VariantSequentialExtension::setup
void setup(bool extend_for_output)
Setup coefficients.
Definition: VariantSequentialExtension.h:110
CCC::VariantSequentialExtension::model_list_
ListType< std::shared_ptr< _StateSpaceModel > > model_list_
State-space model list.
Definition: VariantSequentialExtension.h:212
CCC::VariantSequentialExtension::totalOutputDim
int totalOutputDim() const
Get total output dimension.
Definition: VariantSequentialExtension.h:103
CCC::StateSpaceModel
State-space model.
Definition: StateSpaceModel.h:21
CCC::VariantSequentialExtension::total_input_dim_
int total_input_dim_
Total input dimension.
Definition: VariantSequentialExtension.h:218
CCC
Definition: CommonModels.h:7
CCC::VariantSequentialExtension::total_output_dim_
int total_output_dim_
Total output dimension.
Definition: VariantSequentialExtension.h:221
CCC::VariantSequentialExtension::VariantSequentialExtension
EIGEN_MAKE_ALIGNED_OPERATOR_NEW VariantSequentialExtension(const ListType< std::shared_ptr< _StateSpaceModel >> &model_list, bool extend_for_output=false)
Constructor.
Definition: VariantSequentialExtension.h:83
CCC::VariantSequentialExtension::E_seq_
Eigen::VectorXd E_seq_
Sequential extension vector (i.e., offset vector) .
Definition: VariantSequentialExtension.h:230
CCC::VariantSequentialExtension::totalInputDim
int totalInputDim() const
Get total input dimension.
Definition: VariantSequentialExtension.h:97
StateSpaceModel.h
CCC::VariantSequentialExtension::B_seq_
Eigen::MatrixXd B_seq_
Sequential extension matrix .
Definition: VariantSequentialExtension.h:227
CCC::VariantSequentialExtension::total_state_dim_
int total_state_dim_
Total state dimension.
Definition: VariantSequentialExtension.h:215
CCC::VariantSequentialExtension
Sequential extension for time-variant system.
Definition: VariantSequentialExtension.h:70
CCC::VariantSequentialExtension::A_seq_
Eigen::Matrix< double, Eigen::Dynamic, StateDim > A_seq_
Sequential extension matrix .
Definition: VariantSequentialExtension.h:224
CCC::VariantSequentialExtension::totalStateDim
int totalStateDim() const
Get total state dimension.
Definition: VariantSequentialExtension.h:91