RCAC C++
RCAC.hpp
1 #ifndef _RCAC_HPP_
2 #define _RCAC_HPP_
3 
4 #include "Eigen/Dense"
5 #include "Eigen/Sparse"
6 #include "unsupported/Eigen/KroneckerProduct"
7 #include <deque>
8 #include <string>
9 
10 //Name: Nima Mohseni
11 //Date: 12/29/2019
12 //Purpose: This file contains the general implementation of RCAC to support
13 //derived RCAC classes for RLS, gradient and other RCAC types
14 
15 /*
16 struct rcacGradFlags
17 {
18  //RCAC Flags
19  int Nc;
20  int k_0;
21  Eigen::VectorXd theta_0;
22 };
23 */
24 
38 struct rcacFlags
39 {
40  //Basic RCAC Flags
41  int lz;
42  int ly;
43  int lu;
44  int Nc;
45  int k_0;
46  Eigen::VectorXd theta_0;
47  int filtorder;
48 };
49 
66 struct rcacFilt
67 {
68  Eigen::MatrixXd filtNu;
69  Eigen::MatrixXd filtDu;
70  Eigen::MatrixXd filtNz;
71  Eigen::MatrixXd filtDz;
72 };
73 
81 class RCAC
82 {
83  public:
93  template <typename T>
94  static RCAC* init(
95  T &FLAGS,
96  rcacFilt &FILT,
97  std::string &whichRCAC
98  );
99 
100  /*
101  //Function initRLS: Initializes RCAC with the given flags and
102  //filter values
103  void init(
104  rcacFlags &FLAGS,
105  rcacFilt &FILT
106  );
107  */
108 
109  /*
110  //Function initGrad: Initializes RCAC using gradient descent with the
111  //given flags and filter values
112  void initGrad(
113  rcacGradFlags &FLAGS,
114  rcacFilt &FILT
115  );
116  */
117 
125  //Function oneStep: Compute one step of RCAC
126  void oneStep(
127  Eigen::VectorXd &uIn,
128  Eigen::VectorXd &zIn,
129  Eigen::VectorXd &yIn
130  );
131 
135  //Function getControl: Get the computed control input
136  Eigen::VectorXd getControl()
137  {
138  return uOut;
139  };
140 
144  //Function getCoeff: Get the RCAC coefficients
145  Eigen::VectorXd getCoeff()
146  {
147  return theta;
148  };
149 
153  //Function getlu: Get the number of control inputs
154  int getlu()
155  {
156  return lu;
157  };
158 
162  //Function getly: Get the number of measurements
163  int getly()
164  {
165  return ly;
166  };
167 
171  //Function getlz: Get the number of performance measurements
172  int getlz()
173  {
174  return lz;
175  };
176 
180  //Function getNc: Get the controller order
181  int getNc()
182  {
183  return Nc;
184  };
185 
189  //Function getkk: Get timestep
190  int getkk()
191  {
192  return kk;
193  };
194 
195  protected:
202  //Function coeffUpdate: Compute the RCAC coefficient update
203  virtual void coeffUpdate(
204  Eigen::VectorXd &zIn
205  ) = 0;
206 
210  //Function initRegressor: initialize the regressor variables
211  void initRegressor();
212 
216  //Function initFiltered: initialize filtered variables
217  void initFiltered();
218 
222  //Function computeFiltered: compute the filtered variables
223  void computeFiltered();
224 
225 
226  //RCAC Flags
227  int lz;
228  int ly;
229  int lu;
230  int Nc;
231  int k_0;
232  Eigen::VectorXd theta_0;
233  int filtorder;
234 
235 
236  //rcacGradFlags gradFLAGS;
237 
238  //RCAC Filter
239  /*
240  Eigen::MatrixXd filtNu;
241  Eigen::MatrixXd filtDu;
242  Eigen::MatrixXd filtNz;
243  Eigen::MatrixXd filtDz;
244  */
245 
246  rcacFilt FILT;
247 
248  //RCAC Filtered Variables
249  std::deque<Eigen::VectorXd> uBar;
250  std::deque<Eigen::VectorXd> ufBar;
251  std::deque<Eigen::VectorXd> zBar;
252  std::deque<Eigen::VectorXd> zfBar;
253  //std::deque<Eigen::MatrixXd> uPhiBar;
254  //std::deque<Eigen::MatrixXd> yPhiBar;
255  std::deque<Eigen::MatrixXd> PhiBar;
256  std::deque<Eigen::MatrixXd> PhifBar;
257 
258  //RCAC Working variables
259  Eigen::MatrixXd P;
260  Eigen::VectorXd theta;
261  Eigen::VectorXd uOut;
262  Eigen::VectorXd uIn;
263  Eigen::MatrixXd Phi; //kron([uphi;yphi]', eye_lu)
264  Eigen::VectorXd uphi; //Past Nc u values
265  Eigen::VectorXd yphi; //Past Nc y values
266 
267  //RCAC Types: tell the algorithm what type of RCAC to use
268  bool rcacRLS;
269  bool rcacGrad;
270 
271  //Counter: if kk >= k_0, start RCAC control input
272  int kk = 1;
273 
274  private:
275  bool isFiltIIR = true;
276 };
277 
278 #endif
int getNc()
Definition: RCAC.hpp:181
Eigen::VectorXd getCoeff()
Definition: RCAC.hpp:145
int getlu()
Definition: RCAC.hpp:154
Eigen::VectorXd getControl()
Definition: RCAC.hpp:136
int getkk()
Definition: RCAC.hpp:190
Definition: RCAC.hpp:66
Definition: RCAC.hpp:38
int getly()
Definition: RCAC.hpp:163
int getlz()
Definition: RCAC.hpp:172
Definition: RCAC.hpp:81