Lodestar
An integrated real-time control package in C++
DiscreteSystem.hpp
1 //
2 // Created by Hamza El-Kebir on 4/21/21.
3 //
4 
5 #ifndef LODESTAR_DISCRETESYSTEM_HPP
6 #define LODESTAR_DISCRETESYSTEM_HPP
7 
8 #include <Eigen/Dense>
9 #include "StateSpace.hpp"
10 #include "SystemStateful.hpp"
11 #include "Lodestar/aux/CompileTimeQualifiers.hpp"
12 
13 namespace ls {
14  namespace systems {
15  template<class SYS>
16  class DiscreteSystem : public SystemStateful {
17  public:
18  typedef SYS TDSystem;
19  DiscreteSystem() : system(new SYS), state(new Eigen::VectorXd),
20  input(
21  nullptr), time(0)
22  {}
23 
24  DiscreteSystem(SYS *sys) : system(sys), state(new Eigen::VectorXd),
25  input(nullptr), time(0)
26  {}
27 
28  void advance();
29 
30 // void advance(void *control);
31 
32  void advanceFree();
33 
34  void advanceForced();
35 
36 // void advanceForced(void *control);
37 
38  SYS *system;
39  void *state;
40  void *input;
41 
42  double time;
43  };
44 
45  template <template <typename, const int, const int, const int> class TSystem, typename TScalar, const int TStateDim, const int TInputDim, const int TOutputDim>
46  class DiscreteSystem<TSystem<TScalar, TStateDim, TInputDim, TOutputDim>>;
47 
48  template <typename TScalar, const int TStateDim, const int TInputDim, const int TOutputDim>
49  class DiscreteSystem<StateSpace<TScalar, TStateDim, TInputDim, TOutputDim>> {
50  public:
52  typedef Eigen::Matrix<TScalar, TStateDim, LS_STATIC_UNLESS_DYNAMIC_VAL(TStateDim, 1)> TDStateVector;
53  typedef Eigen::Matrix<TScalar, TInputDim, LS_STATIC_UNLESS_DYNAMIC_VAL(TInputDim, 1)> TDInputVector;
54 
55  DiscreteSystem() : system(new TDSystem), state(new TDStateVector),
56  input(nullptr), time(0)
57  {}
58 
59  DiscreteSystem(TDSystem *sys) : system(sys), state(new TDStateVector),
60  input(nullptr), time(0)
61  {}
62 
63  IF_DYNAMIC_RETURN(TStateDim, TInputDim, TOutputDim, void)
64  initialize() {
65  state->conservativeResize(system->getA().rows());
66 
67  if (input == nullptr)
68  input = new Eigen::VectorXd;
69 
70  input->conservativeResize(system->getB().cols());
71  }
72 
73  IF_STATIC_RETURN(TStateDim, TInputDim, TOutputDim, void)
74  initialize() {
75  // NOTE: No action since no memory may be allocated.
76  return;
77  }
78 
79  void advance();
80 
81  void advance(TDInputVector *control);
82 
83  void advanceFree();
84 
85  void advanceForced();
86 
87  void advanceForced(TDInputVector *control);
88 
89  TDSystem *system;
90  TDStateVector *state;
91  TDInputVector *input;
92 
93  double time;
94  };
95  }
96 }
97 
98 template<typename TScalar, int TStateDim, int TInputDim, int TOutputDim>
100 {
101  if (input != nullptr) {
102  *state = (*system->getA()) * (*state) + (*system->getB()) * (*input);
103  } else {
104  *state = (*system->getA()) * (*state);
105  }
106 
107  time += system->getSamplingPeriod();
108 }
109 
110 template<typename TScalar, int TStateDim, int TInputDim, int TOutputDim>
112  Eigen::Matrix<TScalar, TInputDim, Kmax2(-1, TInputDim, 1)> *control)
113 {
114  *state = (*system->getA()) * (*state) + (*system->getB()) * (*control);
115 
116  time = system->getSamplingPeriod();
117 }
118 
119 template<typename TScalar, int TStateDim, int TInputDim, int TOutputDim>
121 {
122  *state = (*system->getA()) * (*state);
123 
124  time += system->getSamplingPeriod();
125 }
126 
127 template<typename TScalar, int TStateDim, int TInputDim, int TOutputDim>
129 {
130  *state = (*system->getA()) * (*state) + (*system->getB()) * (*input);
131 
132  time += system->getSamplingPeriod();
133 }
134 
135 template<typename TScalar, int TStateDim, int TInputDim, int TOutputDim>
137  Eigen::Matrix<TScalar, TInputDim, Kmax2(-1, TInputDim, 1)> *control)
138 {
139  *state = (*system->getA()) * (*state) + (*system->getB()) * (*control);
140 
141  time += system->getSamplingPeriod();
142 }
143 
144 #endif //LODESTAR_DISCRETESYSTEM_HPP
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::systems::StateSpace
Definition: StateSpace.hpp:15
ls::systems::SystemStateful
Definition: SystemStateful.hpp:11
ls::systems::DiscreteSystem
Definition: DiscreteSystem.hpp:16