我正在尝试在 C 中实现基于观察者的状态反馈,但无法弄清楚。
下面是算法的伪代码:
//Initialize Stuff
loop:
//read process variable
y = read(ch1)
//compute control variable
u = -K * xhat
//set process input
write(u)
//update state estimate
xhat = xhat + Ts * (A*xhat + B*u + L*(y-C*xhat))
// repeat loop
- xhat 是估计的状态
- K 是反馈增益
- L 是观察者增益
- y 是系统输出
- u 是系统输入
- Ts 是采样周期
- A、B 和 C 是状态空间矩阵
这是应该如何实施观察者反馈的吗?
这是我为循环所拥有的实际代码:
// Get Process Output from Sensors:
y[0] = sensor1();
y[1] = sensor2();
// Compute Control Variable:
u = 0;
for (i=0; i<4; i++)
u += K[i]*xhat[i];
// Set Process Input:
write_to_DAC(u);
// Calculate: A*xhat + B*u
for (i=0; i<4; i++) {
Ax_Bu[i] = 0;
for (j=0; j<4; j++)
Ax_Bu[i] += A[i*4+j]*xhat[j];
Ax_Bu[i] += B[i]*u;
}
// Calculate: y-y_hat
for (i=0; i<2; i++) {
yhat[i] = 0;
y_yhat[i] = 0;
for (j=0; j<4; j++) {
yhat[i] += C[i*4+j]*xhat[j]; // Calculate yhat = C*xhat
y_yhat[i] = y[i]-yhat[i]; // Calculate the 'Innovation'
}
}
// Calculate: L*(y-y_hat)
for (i=0; i<4; i++) {
L_y_yhat[i] = 0;
for (j=0; j<2; j++)
L_y_yhat[i] += L[i*2+j]*y_yhat[j];
}
// Update State Estimate:
for (i=0; i<4; i++)
xhat[i] += Ts*(Ax_Bu[i] + L_y_yhat[i]);