5 #ifndef LODESTAR_STATESPACE_HPP
6 #define LODESTAR_STATESPACE_HPP
8 #include "SystemStateless.hpp"
10 #include "Lodestar/aux/CompileTimeQualifiers.hpp"
14 template<
typename TScalar =
double, const
int TStateDim = Eigen::Dynamic, const
int TInputDim = Eigen::Dynamic, const
int TOutputDim = Eigen::Dynamic>
17 typedef Eigen::Matrix<TScalar, TStateDim, TStateDim> TDStateMatrix;
18 typedef Eigen::Matrix<TScalar, TStateDim, TInputDim> TDInputMatrix;
19 typedef Eigen::Matrix<TScalar, TOutputDim, TStateDim> TDOutputMatrix;
20 typedef Eigen::Matrix<TScalar, TOutputDim, TInputDim> TDFeedforwardMatrix;
26 B_(new TDInputMatrix),
27 C_(new TDOutputMatrix),
28 D_(new TDFeedforwardMatrix),
49 TDOutputMatrix *C, TDOutputMatrix *D);
65 template<
typename DerivedA,
typename DerivedB,
typename DerivedC,
typename DerivedD>
66 StateSpace(
const Eigen::EigenBase<DerivedA> &A,
const Eigen::EigenBase<DerivedB> &B,
67 const Eigen::EigenBase<DerivedC> &C,
const Eigen::EigenBase<DerivedD> &D);
81 const TDStateMatrix &
getA()
const;
88 void setA(TDStateMatrix *A);
90 template<
typename Derived>
91 void setA(Eigen::EigenBase<Derived> *A);
98 void setA(
const TDStateMatrix &A);
100 template<
typename Derived>
101 void setA(
const Eigen::EigenBase<Derived> &A);
108 const TDInputMatrix &
getB()
const;
115 void setB(TDInputMatrix *B);
117 template<
typename Derived>
118 void setB(Eigen::EigenBase<Derived> *B);
125 void setB(
const TDInputMatrix &B);
127 template<
typename Derived>
128 void setB(
const Eigen::EigenBase<Derived> &B);
135 const TDOutputMatrix &
getC()
const;
142 void setC(TDOutputMatrix *C);
144 template<
typename Derived>
145 void setC(Eigen::EigenBase<Derived> *C);
152 void setC(
const TDOutputMatrix &C);
154 template<
typename Derived>
155 void setC(
const Eigen::EigenBase<Derived> &C);
162 const TDFeedforwardMatrix &
getD()
const;
169 void setD(TDFeedforwardMatrix *D);
171 template<
typename Derived>
172 void setD(Eigen::EigenBase<Derived> *D);
179 void setD(
const TDFeedforwardMatrix &D);
181 template<
typename Derived>
182 void setD(
const Eigen::EigenBase<Derived> &D);
234 IF_DYNAMIC_RETURN(TStateDim, TInputDim, TOutputDim,
long)
237 return stateDimDynamic();
240 long inline stateDimDynamic()
const
245 IF_STATIC_RETURN(TStateDim, TInputDim, TOutputDim,
long)
248 return stateDimStatic();
251 long inline stateDimStatic()
const
261 IF_DYNAMIC_RETURN(TStateDim, TInputDim, TOutputDim,
long)
264 return inputDimDynamic();
267 long inline inputDimDynamic()
const
272 IF_STATIC_RETURN(TStateDim, TInputDim, TOutputDim,
long)
275 return inputDimStatic();
278 long inline inputDimStatic()
const
288 IF_DYNAMIC_RETURN(TStateDim, TInputDim, TOutputDim,
long)
291 return outputDimDynamic();
294 long inline outputDimDynamic()
const
299 IF_STATIC_RETURN(TStateDim, TInputDim, TOutputDim,
long)
302 return outputDimStatic();
305 long inline outputDimStatic()
const
318 template<
int TStateDim2,
int TOutputDim2>
319 void append(
const StateSpace<TScalar, TStateDim2, TOutputDim, TOutputDim2> &ss);
331 inline bool isStable(
double tolerance = 0)
const;
351 template<
typename T_TScalar = TScalar, const
int T_TStateDim = TStateDim, const
int T_TInputDim = TInputDim, const
int T_TOutputDim = TOutputDim>
353 T_TStateDim + T_TOutputDim), T_TInputDim, T_TOutputDim>
354 addIntegralAction(LS_IS_DYNAMIC_DEFAULT(T_TStateDim, T_TInputDim, T_TOutputDim))
const;
374 template<
typename T_TScalar = TScalar, const
int T_TStateDim = TStateDim, const
int T_TInputDim = TInputDim, const
int T_TOutputDim = TOutputDim>
376 T_TStateDim + T_TOutputDim), T_TInputDim, T_TOutputDim>
377 addIntegralAction(LS_IS_STATIC_DEFAULT(T_TStateDim, T_TInputDim, T_TOutputDim))
const;
383 TDFeedforwardMatrix *
D_;
390 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
393 A_(new TDStateMatrix(other.getA())),
394 B_(new TDInputMatrix(other.getB())),
395 C_(new TDOutputMatrix(other.getC())),
396 D_(new TDFeedforwardMatrix(other.getD())),
397 dt_(other.getSamplingPeriod()),
398 isDiscrete_(other.isDiscrete())
402 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
404 TDStateMatrix *A, TDInputMatrix *B,
405 TDOutputMatrix *C, TDOutputMatrix *D):
406 A_(new TDStateMatrix(*A)),
407 B_(new TDInputMatrix(*B)),
408 C_(new TDOutputMatrix(*C)),
409 D_(new TDFeedforwardMatrix(*D))
433 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
434 template<
typename DerivedA,
typename DerivedB,
typename DerivedC,
typename DerivedD>
436 const Eigen::EigenBase<DerivedB> &B,
437 const Eigen::EigenBase<DerivedC> &C,
438 const Eigen::EigenBase<DerivedD> &D):
439 A_(new TDStateMatrix(A)),
440 B_(new TDInputMatrix(B)),
441 C_(new TDOutputMatrix(C)),
442 D_(new TDFeedforwardMatrix(D))
452 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
453 const typename ls::systems::StateSpace<TScalar, TStateDim, TInputDim, TOutputDim>::TDStateMatrix &
459 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
465 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
466 template<
typename Derived>
472 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
474 const TDStateMatrix &A)
479 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
480 template<
typename Derived>
486 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
487 const typename ls::systems::StateSpace<TScalar, TStateDim, TInputDim, TOutputDim>::TDInputMatrix &
493 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
499 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
500 template<
typename Derived>
506 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
508 const TDInputMatrix &B)
513 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
514 template<
typename Derived>
520 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
521 const typename ls::systems::StateSpace<TScalar, TStateDim, TInputDim, TOutputDim>::TDOutputMatrix &
527 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
534 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
535 template<
typename Derived>
541 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
543 const TDOutputMatrix &C)
548 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
549 template<
typename Derived>
555 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
556 const typename ls::systems::StateSpace<TScalar, TStateDim, TInputDim, TOutputDim>::TDFeedforwardMatrix &
562 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
564 TDFeedforwardMatrix *D)
569 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
570 template<
typename Derived>
576 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
578 const TDFeedforwardMatrix &D)
583 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
584 template<
typename Derived>
590 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
596 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
602 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
608 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
615 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
619 isDiscrete_ = discrete;
622 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
623 template<
int TStateDim2,
int TOutputDim2>
630 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
633 auto eig = A_->eigenvalues();
636 double tol = (tolerance < 0 ? -tolerance * tolerance : tolerance * tolerance);
638 for (
int i = 0; i < eig.size(); i++) {
639 if (eig(i).real() * eig(i).real() + eig(i).imag() * eig(i).imag() > 1 + tol)
643 for (
int i = 0; i < eig.size(); i++) {
644 if (eig(i).real() > -tolerance)
652 template<
typename TScalar,
int TStateDim,
int TInputDim,
int TOutputDim>
663 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
664 template<
typename T_TScalar, const
int T_TStateDim, const
int T_TInputDim, const
int T_TOutputDim>
665 ls::systems::StateSpace<T_TScalar, LS_STATIC_UNLESS_DYNAMIC(T_TStateDim + T_TOutputDim), T_TInputDim, T_TOutputDim>
667 LS_IS_DYNAMIC(T_TStateDim, T_TInputDim, T_TOutputDim))
const
669 Eigen::Matrix<TScalar, Eigen::Dynamic, Eigen::Dynamic> A(stateDim() + outputDim(), stateDim() + outputDim());
671 A.topLeftCorner(stateDim(), stateDim()) = getA();
672 A.bottomLeftCorner(outputDim(), stateDim()) = getC();
673 A.bottomRightCorner(outputDim(), outputDim()).setIdentity();
675 Eigen::Matrix<TScalar, Eigen::Dynamic, Eigen::Dynamic> B(stateDim() + outputDim(), inputDim());
677 B.topRows(stateDim()) = getB();
679 Eigen::Matrix<TScalar, Eigen::Dynamic, Eigen::Dynamic> C(outputDim(), stateDim() + outputDim());
681 C.leftCols(stateDim()) = getC();
682 C.rightCols(outputDim()).setIdentity();
684 Eigen::Matrix<TScalar, Eigen::Dynamic, Eigen::Dynamic> D(outputDim(), inputDim());
688 T_TStateDim + T_TOutputDim), T_TInputDim, T_TOutputDim> ssi{A, B, C, D};
693 template<
typename TScalar, const
int TStateDim, const
int TInputDim, const
int TOutputDim>
694 template<
typename T_TScalar, const
int T_TStateDim, const
int T_TInputDim, const
int T_TOutputDim>
695 ls::systems::StateSpace<T_TScalar, LS_STATIC_UNLESS_DYNAMIC(T_TStateDim + T_TOutputDim), T_TInputDim, T_TOutputDim>
697 LS_IS_STATIC(T_TStateDim, T_TInputDim, T_TOutputDim))
const
699 Eigen::Matrix<TScalar, TStateDim + TOutputDim, TStateDim + TOutputDim> A{};
701 A.template topLeftCorner<TStateDim, TStateDim>() = getA();
702 A.template bottomLeftCorner<TOutputDim, TStateDim>() = getC();
703 A.template bottomRightCorner<TOutputDim, TOutputDim>().setIdentity();
705 Eigen::Matrix<TScalar, TStateDim + TOutputDim, TInputDim> B{};
707 B.template topRows<TStateDim>() = getB();
709 Eigen::Matrix<TScalar, TOutputDim, TStateDim + TOutputDim> C{};
711 C.template leftCols<TStateDim>() = getC();
712 C.template rightCols<TOutputDim>().setIdentity();
714 Eigen::Matrix<TScalar, TOutputDim, TInputDim> D{};
718 T_TStateDim + T_TOutputDim), T_TInputDim, T_TOutputDim> ssi{A, B, C, D};
723 #endif //LODESTAR_STATESPACE_HPP