GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
RLS.hpp
1#include <cstdio>
2#include <ctime>
3#include <stdexcept>
4
5#include "Matrix.hpp"
6#include "core/logger.h"
7
8#pragma once
9
10namespace roboctrl::utils
11{
12 template<uint32_t dim>
13 class RLS
14 {
15 public:
19 RLS() = delete;
20
26 constexpr RLS(float delta_, float lambda_)
27 : dimension(dim),
28 lambda(lambda_),
29 delta(delta_),
30 lastUpdate(0),
31 updateCnt(0),
32 defaultParamsVector(Matrixf<dim, 1>::zeros()) {
33 this->reset();
34 this->validate();
35 }
36
37 constexpr RLS(float delta_, float lambda_, Matrixf<dim, 1> initParam) : RLS(delta_, lambda_) {
38 defaultParamsVector = initParam;
39 paramsVector = initParam;
40 }
41
46 void reset() {
47 transMatrix = Matrixf<dim, dim>::eye() * delta;
48 gainVector = Matrixf<dim, 1>::zeros();
49 paramsVector = defaultParamsVector;
50 output = 0.0f;
51 lastUpdate = 0;
52 updateCnt = 0;
53 }
54
61 const Matrixf<dim, 1> &update(const Matrixf<dim, 1> &sampleVector, float actualOutput) {
62 const float previousOutput = (sampleVector.trans() * paramsVector)[0][0];
63 gainVector = (transMatrix * sampleVector) /
64 (1.0f + (sampleVector.trans() * transMatrix * sampleVector)[0][0] / lambda) /
65 lambda; // Get gain vector
66 paramsVector +=
67 gainVector * (actualOutput - previousOutput); // Get params vector
68 transMatrix =
69 (transMatrix - gainVector * sampleVector.trans() * transMatrix) / lambda; // Get transferred matrix
70 output = (sampleVector.trans() * paramsVector)[0][0];
71
72 updateCnt++;
73 lastUpdate = std::clock();
74 return paramsVector;
75 }
76
82 void setParamVector(const Matrixf<dim, 1> &updatedParams) {
83 paramsVector = updatedParams;
84 defaultParamsVector = updatedParams;
85 }
92 return paramsVector;
93 }
94
95 constexpr const Matrixf<dim, 1>& getParamsVector() const {
96 return paramsVector;
97 }
98
104 const float &getOutput() const {
105 return output;
106 }
107
108 private:
114 void validate() const {
115
116 if (!(lambda > 0.0f && lambda <= 1.0f))
117 throw std::invalid_argument("RLS lambda must be in (0, 1]");
118
119 if(!(delta > 0))
120 throw std::invalid_argument("RLS delta must be positive");
121 }
122
123 uint32_t dimension; // Dimension of the RLS space
124 float lambda; // The forget index
125 float delta; // Intialized value of the transferred matrix
126
127 size_t lastUpdate; // Last update tick
128 uint32_t updateCnt; // Total update Count
129
130 /*RLS relvant matrix*/
131 Matrixf<dim, dim> transMatrix; // Transfer matrix instance
132 Matrixf<dim, 1> gainVector; // Gain vector for params update
133 Matrixf<dim, 1> paramsVector; // Params vector
134 Matrixf<dim, 1> defaultParamsVector;
135 float output {0.0f}; // Estimated / filtered output
136 };
137}
constexpr Matrix< _cols, _rows, T > trans() const
Get the transpose of the matrix
Definition Matrix.hpp:344
static constexpr Matrix eye()
Returns a _rows * columns matrix
Definition Matrix.hpp:476
static constexpr Matrix zeros()
Returns a _rows x _cols zero matrix
Definition Matrix.hpp:454
RLS()=delete
Delete the default constructor
void reset()
Reset the RLS module
Definition RLS.hpp:46
constexpr RLS(float delta_, float lambda_)
The constructor
Definition RLS.hpp:26
constexpr Matrixf< dim, 1 > & getParamsVector()
The getter function of the params vector
Definition RLS.hpp:91
const Matrixf< dim, 1 > & update(const Matrixf< dim, 1 > &sampleVector, float actualOutput)
Proccess a cycle of RLS update
Definition RLS.hpp:61
const float & getOutput() const
The getter function of the output vector
Definition RLS.hpp:104
void setParamVector(const Matrixf< dim, 1 > &updatedParams)
Set the default regression parameters
Definition RLS.hpp:82
用于日志输出的组件。
用于存放工具函数的命名空间。
Definition ballistics.hpp:6