用C/C++实现下列对象的P、PD、PI、PID、积分器饱和PID、不完全微分PID、微分先行PID控制G(s)=(1.151*s+0.1774)/(s^3+0.739*s^2+0.921*s);
时间: 2024-03-12 09:49:53 浏览: 112
实现这些对象需要先确定控制器的采样周期,然后进行离散化,最终得到控制器的差分方程。由于本题中的G(s)为三阶系统,因此需要使用三阶控制器。以下是各种控制器的实现方法:
1. P控制器:
差分方程:u(k) = Kp * e(k)
代码实现:
```c++
double PController(double error, double Kp) {
return Kp * error;
}
```
2. PD控制器:
差分方程:u(k) = Kp * e(k) + Kd * (e(k) - e(k-1))/T
代码实现:
```c++
double PDController(double error, double last_error, double Kp, double Kd, double T) {
return Kp * error + Kd * (error - last_error) / T;
}
```
3. PI控制器:
差分方程:u(k) = Kp * e(k) + Ki * T * (e(k) + e(k-1))/2
代码实现:
```c++
double PIController(double error, double last_error, double Kp, double Ki, double T, double &sum_error) {
sum_error += (error + last_error) * T / 2;
return Kp * error + Ki * sum_error;
}
```
4. PID控制器:
差分方程:u(k) = Kp * e(k) + Ki * T * (e(k) + e(k-1))/2 + Kd * (e(k) - e(k-1))/T
代码实现:
```c++
double PIDController(double error, double last_error, double Kp, double Ki, double Kd, double T, double &sum_error) {
sum_error += (error + last_error) * T / 2;
double derivative = (error - last_error) / T;
return Kp * error + Ki * sum_error + Kd * derivative;
}
```
5. 积分器饱和PID控制器:
差分方程:u(k) = Kp * e(k) + Ki * T * (e(k) + e(k-1))/2 + Kd * (e(k) - e(k-1))/T
其中,Ki * T * (e(k) + e(k-1))/2 部分加入了积分器饱和限制
代码实现:
```c++
double SaturatedPIDController(double error, double last_error, double Kp, double Ki, double Kd, double T, double &sum_error, double max_output) {
sum_error += (error + last_error) * T / 2;
double integral = Ki * sum_error;
if (integral > max_output) {
integral = max_output;
sum_error = integral / Ki;
} else if (integral < -max_output) {
integral = -max_output;
sum_error = integral / Ki;
}
double derivative = (error - last_error) / T;
return Kp * error + integral + Kd * derivative;
}
```
6. 不完全微分PID控制器:
差分方程:u(k) = Kp * e(k) + Ki * T * (e(k) + e(k-1))/2 + Kd * (a*T*e(k) - b*T*e(k-1))/T
代码实现:
```c++
double IncompleteDifferentialPIDController(double error, double last_error, double Kp, double Ki, double Kd, double T, double a, double b, double &last_derivative) {
double derivative = a * T * error - b * T * last_error;
double output = Kp * error + Ki * (error + last_error) * T / 2 + Kd * derivative;
last_derivative = derivative;
return output;
}
```
7. 微分先行PID控制器:
差分方程:u(k) = Kp * e(k) + Ki * T * (e(k) + e(k-1))/2 + Kd * (a*T*e(k) - b*T*e(k-1))/T + Kd * c * u(k-1)
代码实现:
```c++
double PrecedingDifferentialPIDController(double error, double last_error, double Kp, double Ki, double Kd, double T, double a, double b, double c, double last_output, double &last_derivative) {
double derivative = a * T * error - b * T * last_error + c * last_output;
double output = Kp * error + Ki * (error + last_error) * T / 2 + Kd * derivative;
last_derivative = derivative;
return output;
}
```
需要注意的是,这里的变量名与差分方程中的变量名不完全相同,需要根据具体情况进行调整。另外,差分方程中的a、b、c等系数需要根据系统的具体参数进行计算,这里不再赘述。
阅读全文