6 #include <unsupported/Eigen/MatrixFunctions>
20 template<
int StateDim,
int InputDim,
int OutputDim>
34 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42 StateSpaceModel(
int state_dim = StateDim,
int input_dim = InputDim,
int output_dim = OutputDim)
48 throw std::runtime_error(
"state_dim must be positive: " + std::to_string(
state_dim_) +
" <= 0");
52 throw std::runtime_error(
"input_dim must be non-negative: " + std::to_string(
input_dim_) +
" < 0");
56 throw std::runtime_error(
"output_dim must be non-negative: " + std::to_string(
output_dim_) +
" < 0");
60 if constexpr(StateDim != Eigen::Dynamic)
64 throw std::runtime_error(
"state_dim is inconsistent with template parameter: " + std::to_string(
state_dim_)
65 +
" != " + std::to_string(StateDim));
68 if constexpr(InputDim != Eigen::Dynamic)
72 throw std::runtime_error(
"input_dim is inconsistent with template parameter: " + std::to_string(
input_dim_)
73 +
" != " + std::to_string(InputDim));
76 if constexpr(OutputDim != Eigen::Dynamic)
80 throw std::runtime_error(
"output_dim is inconsistent with template parameter: " + std::to_string(
output_dim_)
81 +
" != " + std::to_string(OutputDim));
125 return A_ * x +
B_ * u +
E_;
154 return C_ * x +
D_ * u +
F_;
170 if constexpr(StateDim == Eigen::Dynamic || InputDim == Eigen::Dynamic)
177 Eigen::MatrixXd ABZeroExp = ABZero.exp();
187 Eigen::MatrixXd ABEZeroExp = ABEZero.exp();
198 Eigen::Matrix<double, StateDim + InputDim, StateDim + InputDim> ABZero;
199 ABZero << dt_ * A_, dt_ * B_, Eigen::Matrix<double, InputDim, StateDim + InputDim>::Zero();
200 Eigen::Matrix<double, StateDim + InputDim, StateDim + InputDim> ABZeroExp = ABZero.exp();
201 Ad_ = ABZeroExp.template block<StateDim, StateDim>(0, 0);
202 Bd_ = ABZeroExp.template block<StateDim, InputDim>(0, StateDim);
208 Eigen::Matrix<double, StateDim + InputDim + 1, StateDim + InputDim + 1> ABEZero;
209 ABEZero << dt_ * A_, dt_ * B_, dt_ * E_, Eigen::Matrix<double, InputDim + 1, StateDim + InputDim + 1>::Zero();
210 Eigen::Matrix<double, StateDim + InputDim + 1, StateDim + InputDim + 1> ABEZeroExp = ABEZero.exp();
211 Ad_ = ABEZeroExp.template block<StateDim, StateDim>(0, 0);
212 Bd_ = ABEZeroExp.template block<StateDim, InputDim>(0, StateDim);
213 Ed_ = ABEZeroExp.template block<StateDim, 1>(0, StateDim + InputDim);
229 Eigen::Matrix<double, StateDim, StateDim>
A_;
232 Eigen::Matrix<double, StateDim, InputDim>
B_;
235 Eigen::Matrix<double, OutputDim, StateDim>
C_;
238 Eigen::Matrix<double, OutputDim, InputDim>
D_;
250 Eigen::Matrix<double, StateDim, StateDim>
Ad_;
253 Eigen::Matrix<double, StateDim, InputDim>
Bd_;