#include <stdio.h>
// PID参数
float Kp = 10.0; // 比例增益
float Ki = 0.1; // 积分增益
float Kd = 1.0; // 微分增益
// PID变量
float integral = 0.0; // 积分项
float previous_error = 0.0; // 上一次的误差
// 采样时间间隔(单位:秒)
float delta_t = 1.0;
/**
* @brief PID控制算法
* @param setpoint: 设定温度
* @param actual: 实际温度
* @return 控制输出(如加热膜的功率)
*/
float PID_Control(float setpoint, float actual) {
// 计算误差
float error = setpoint - actual;
// 计算积分项
integral += error * delta_t;
// 计算微分项
float derivative = (error - previous_error) / delta_t;
// 计算控制输出
float output = Kp * error + Ki * integral + Kd * derivative;
// 保存当前误差
previous_error = error;
return output;
}
int main() {
// 示例:设定温度和实际温度
float setpoint = 37.5; // 设定温度
float actual = 36.0; // 实际温度
// 模拟PID控制
for (int i = 0; i < 10; i++) {
float output = PID_Control(setpoint, actual);
printf("控制输出: %.2f\n", output);
// 模拟实际温度变化
actual += 0.2; // 假设每次采样后温度上升0.2℃
}
return 0;
}
|