GKD.RoboCtrl
载入中...
搜索中...
未找到
RLS.hpp
1#include <cstdio>
2#include <ctime>
3
4#include "Matrix.hpp"
5#include "core/logger.h"
6
7#pragma once
8
9namespace roboctrl::utils
10{
11 template<uint32_t dim>
12 class RLS
13 {
14 public:
18 RLS() = delete;
19
25 constexpr RLS(float delta_, float lambda_)
26 : dimension(dim),
27 lambda(lambda_),
28 delta(delta_),
29 lastUpdate(0),
30 updateCnt(0),
31 defaultParamsVector(Matrixf<dim, 1>::zeros()) {
32 this->reset();
33 this->validate();
34 }
35
36 constexpr RLS(float delta_, float lambda_, Matrixf<dim, 1> initParam) : RLS(delta_, lambda_) {
37 defaultParamsVector = initParam;
38 }
39
44 void reset() {
45 transMatrix = Matrixf<dim, dim>::eye() * delta;
46 gainVector = Matrixf<dim, 1>::zeros();
47 paramsVector = Matrixf<dim, 1>::zeros();
48 }
49
57 gainVector = (transMatrix * sampleVector) /
58 (1.0f + (sampleVector.trans() * transMatrix * sampleVector)[0][0] / lambda) /
59 lambda; // Get gain vector
60 paramsVector +=
61 gainVector * (actualOutput - (sampleVector.trans() * paramsVector)[0][0]); // Get params vector
62 transMatrix =
63 (transMatrix - gainVector * sampleVector.trans() * transMatrix) / lambda; // Get transferred matrix
64
65 updateCnt++;
66 lastUpdate = clock();
67 return paramsVector;
68 }
69
76 paramsVector = updatedParams;
77 defaultParamsVector = updatedParams;
78 }
84 constexpr Matrixf<dim, 1> &getParamsVector() const {
85 return paramsVector;
86 }
87
93 const float &getOutput() const {
94 return output;
95 }
96
97 private:
103 void validate() const {
104
105 if(!(lambda >= 0.0f || lambda <= 1.0f))
106 LOG_ERROR("!(lambda >= 0.0f || lambda <= 1.0f)");
107
108 if(!(delta > 0))
109 LOG_ERROR("!(delta > 0)");
110 }
111
112 uint32_t dimension; // Dimension of the RLS space
113 float lambda; // The forget index
114 float delta; // Intialized value of the transferred matrix
115
116 size_t lastUpdate; // Last update tick
117 uint32_t updateCnt; // Total update Count
118
119 /*RLS relvant matrix*/
120 Matrixf<dim, dim> transMatrix; // Transfer matrix instance
121 Matrixf<dim, 1> gainVector; // Gain vector for params update
122 Matrixf<dim, 1> paramsVector; // Params vector
123 Matrixf<dim, 1> defaultParamsVector;
124 float output; // Estimated / filtered output
125 };
126}
static constexpr Matrix eye()
Returns a _rows * columns matrix
Definition Matrix.hpp:451
static constexpr Matrix zeros()
Returns a _rows x _cols zero matrix
Definition Matrix.hpp:429
RLS()=delete
Delete the default constructor
constexpr Matrixf< dim, 1 > & getParamsVector() const
The getter function of the params vector
Definition RLS.hpp:84
const Matrixf< dim, 1 > & update(Matrixf< dim, 1 > &sampleVector, float actualOutput)
Proccess a cycle of RLS update
Definition RLS.hpp:56
void reset()
Reset the RLS module
Definition RLS.hpp:44
constexpr RLS(float delta_, float lambda_)
The constructor
Definition RLS.hpp:25
const float & getOutput() const
The getter function of the output vector
Definition RLS.hpp:93
void setParamVector(const Matrixf< dim, 1 > &updatedParams)
Set the default regression parameters
Definition RLS.hpp:75
用于日志输出的组件。
asio::awaitable< T > awaitable
协程任务类型。
Definition async.hpp:42
用于存放工具函数的命名空间。
Definition concepts.hpp:13