- 暖气片温控器加热冷却双向PID,冷热双执行器控制
- 发布时间:2026-12-02|阅读:次
暖气片温控器:加热/冷却双向PID + 冷热双执行器控制
一、系统架构概览
┌─────────────────────────────────────────────────────┐
│ 双执行器暖气片温控系统 │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ 温度传感器 │───▶│ PID控制器 │───▶│ 加热执行器(阀) │ │
│ │ (NTC/DS18B20)│ │(双向PID) │ └──────────────┘ │
│ └──────────┘ │ │ ┌──────────────┐ │
│ │ ├───▶│ 冷却执行器(阀) │ │
│ ┌──────────┐ │ │ │(风扇/冷水阀) │ │
│ │ 设定温度 │───▶│ │ └──────────────┘ │
│ │ (目标值) │ └──────────┘ │
│ └──────────┘ │
└─────────────────────────────────────────────────────┘
二、双向PID核心算法
2.1 标准PID公式(双向扩展)
误差: e(t) = SetPoint - MeasuredTemp
输出: u(t) = Kp·e(t) + Ki·∫e(τ)dτ + Kd·de(t)/dt
2.2 双向PID区分逻辑
c
// 双向PID输出范围: [-100, +100]
// 正值 → 加热方向
// 负值 → 冷却方向
typedef struct {
float Kp, Ki, Kd;
float integral;
float prev_error;
float output_max;
float output_min;
float deadband; // 死区
} DualPID_t;
float DualPID_Compute(DualPID_t pid, float setpoint, float measured) {
float error = setpoint - measured;
// ====== 死区处理 ======
if (fabsf(error) < pid->deadband) {
pid->integral = 0; // 死区内清除积分
return 0.0f; // 双执行器都关闭
}
// ====== 积分项(带限幅) ======
pid->integral += error;
// 积分限幅防止 windup
if (pid->integral > pid->output_max / pid->Ki)
pid->integral = pid->output_max / pid->Ki;
if (pid->integral < pid->output_min / pid->Ki)
pid->integral = pid->output_min / pid->Ki;
// ====== 微分项 ======
float derivative = error - pid->prev_error;
pid->prev_error = error;
// ====== PID计算 ======
float output = pid->Kp error
+ pid->Ki pid->integral
+ pid->Kd derivative;
// ====== 输出限幅 ======
if (output > pid->output_max) output = pid->output_max;
if (output < pid->output_min) output = pid->output_min;
return output; // +100~0:加热 0~-100:冷却
}
2.3 关键参数说明
| 参数 | 说明 | 典型值 |
|||--|
| `Kp` | 比例增益 | 2.0 ~ 5.0 |
| `Ki` | 积分时间常数 | 0.1 ~ 0.5 |
| `Kd` | 微分时间常数 | 0.5 ~ 2.0 |
| `deadband` | 死区 ±℃ | ±0.3 ~ 0.5 |
| `output_max` | 最大加热输出 | +100 |
| `output_min` | 最大冷却输出 | -100 |
三、冷热双执行器控制策略
3.1 执行器映射关系
PID输出 u:
+100 ────────── 0 ────────── -100
│ │ │
▼ ▼ ▼
加热阀100% 双阀全关 冷却阀100%
(全开热水) (待机状态) (全开冷水/风扇)
3.2 执行器驱动实现
c
typedef enum {
ACTUATOR_HEAT, // 加热:热水阀 / 电加热
ACTUATOR_COOL, // 冷却:冷水阀 / 风扇 / 半导体制冷
ACTUATOR_NONE
} ActuatorType_t;
typedef struct {
uint8_t pwm_channel; // PWM通道
float current_duty; // 当前占空比 0~100%
uint32_t min_pulse; // 最小脉宽 (μs)
uint32_t max_pulse; // 最大脉宽 (μs)
} Actuator_t;
void DualActuator_Control(float pid_output, Actuator_t heat, Actuator_t cool) {
if (pid_output > 0) {
// ====== 加热模式 ======
float duty = pid_output; // 0~100
heat->current_duty = duty;
cool->current_duty = 0; // 冷却阀关闭
// PWM设置
uint32_t pulse = heat->min_pulse
+ (heat->max_pulse - heat->min_pulse) duty / 100.0f;
PWM_SetDuty(heat->pwm_channel, pulse);
} else if (pid_output < 0) {
// ====== 冷却模式 ======
float duty = fabsf(pid_output); // 0~100
heat->current_duty = 0; // 加热阀关闭
cool->current_duty = duty;
// PWM设置
uint32_t pulse = cool->min_pulse
+ (cool->max_pulse - cool->min_pulse) duty / 100.0f;
PWM_SetDuty(cool->pwm_channel, pulse);
} else {
// ====== 双执行器关闭 ======
heat->current_duty = 0;
cool->current_duty = 0;
PWM_SetDuty(heat->pwm_channel, 0);
PWM_SetDuty(cool->pwm_channel, 0);
}
}
3.3 执行器硬件接口
┌─────────────────────────────────────┐
│ 硬件接线示意 │
│ │
│ MCU PWM1 ──────▶ 加热阀驱动(MOSFET) │
│ │ │
│ ▼ │
│ ┌─────────┐ │
│ │热水电磁阀│ │
│ └─────────┘ │
│ │
│ MCU PWM2 ──────▶ 冷却阀/风扇驱动 │
│ │ │
│ ▼ │
│ ┌─────────┐ │
│ │冷水电磁阀│ 或 风扇 │
│ └─────────┘ │
│ │
│ NTC/DS18B20 ──▶ ADC / 1-Wire │
│ 按键 ──────▶ GPIO (设定温度) │
│ 显示屏 ────▶ I2C/SPI │
└─────────────────────────────────────┘
四、完整控制主循环
c
define SAMPLE_INTERVAL_MS 200 // PID采样周期200ms
define TEMP_FILTER_N 8 // 温度滤波点数
int main(void) {
// ====== 初始化 ======
DualPID_t pid = {
.Kp = 3.0f, .Ki = 0.2f, .Kd = 1.0f,
.integral = 0, .prev_error = 0,
.output_max = 100, .output_min = -100,
.deadband = 0.3f
};
Actuator_t heat_act = {.pwm_channel = 0, .min_pulse = 1000, .max_pulse = 2000};
Actuator_t cool_act = {.pwm_channel = 1, .min_pulse = 1000, .max_pulse = 2000};
float setpoint = 22.0f; // 默认设定温度 22℃
uint32_t last_tick = 0;
while (1) {
uint32_t now = GetTickMs();
// ====== 定时采样与控制 ======
if (now - last_tick >= SAMPLE_INTERVAL_MS) {
last_tick = now;
// 1. 读取温度(多次采样取平均)
float temp_raw = Temperature_Read();
float measured = LowPassFilter(temp_raw, TEMP_FILTER_N);
// 2. 双向PID计算
float pid_output = DualPID_Compute(&pid, setpoint, measured);
// 3. 双执行器控制
DualActuator_Control(pid_output, &heat_act, &cool_act);
// 4. 显示/通信
Display_Update(setpoint, measured, pid_output);
}
// ====== 按键扫描(修改设定温度) ======
Key_Scan(&setpoint);
// ====== 安全保护 ======
Safety_Check(&measured, &setpoint, &heat_act, &cool_act);
}
}
五、安全保护机制
c
void Safety_Check(float temp, float setpoint,
Actuator_t heat, Actuator_t cool) {
// 1. 温度超高保护(防烫伤/防冻管爆裂)
if (temp > 85.0f) {
heat->current_duty = 0;
cool->current_duty = 100; // 强制全冷却
Alarm_Trigger(OVERHEAT);
}
// 2. 温度过低保护(防冻裂)
if (temp < 5.0f) {
heat->current_duty = 100; // 强制全加热
cool->current_duty = 0;
Alarm_Trigger(FREEZE_RISK);
}
// 3. 传感器故障检测
if (IsSensorFault(temp)) {
heat->current_duty = 0;
cool->current_duty = 0;
Alarm_Trigger(SENSOR_FAULT);
}
// 4. 执行器互锁(防止同时加热冷却)
if (heat->current_duty > 5 && cool->current_duty > 5) {
// 强制关闭一个
heat->current_duty = 0;
cool->current_duty = 0;
}
}
六、PID参数整定建议
┌──────────────────────────────────────────────────┐
│ 整定步骤(双向分步法) │
│ │
│ Step 1: 只调加热方向 │
│ → 设 Ki=0, Kd=0, 逐步增大 Kp 直到有稳态振荡 │
│ → 再加入 Ki 消除静差 │
│ → 最后加 Kd 抑制超调 │
│ │
│ Step 2: 只调冷却方向 │
│ → 同样方法,但 Kp/Ki/Kd 可能需要不同值 │
│ → 冷却通常响应更快,Kd 可适当减小 │
│ │
│ Step 3: 联合调试 │
│ → 验证在设定点附近切换时无剧烈抖动 │
│ → 调整 deadband 避免执行器频繁切换 │
│ │
│ 经验比例: 冷却Kp ≈ 加热Kp × 0.7~1.2 │
│ (取决于冷热系统响应速度差异) │
└──────────────────────────────────────────────────┘
七、系统框图总结
┌─────────────┐
│ 用户设定温度 │
│ (按键/App) │
└──────┬──────┘
│
┌──────▼──────┐
│ 双向PID计算 │◄──── 温度传感器(滤波)
│ +100~0~-100│
└──────┬──────┘
┌──────┴──────┐
┌─────▼─────┐ ┌─────▼─────┐
│ 加热执行器 │ │ 冷却执行器 │
│ PWM驱动阀 │ │ PWM驱动阀 │
│ /电加热丝 │ │ /风扇/冷阀│
└─────┬─────┘ └─────┬─────┘
│ │
┌─────▼─────┐ ┌─────▼─────┐
│ 热水/电热 │ │ 冷水/风扇 │
│ 输入 │ │ 输入 │
└─────┬─────┘ └─────┬─────┘
└──────┬──────┘
┌──────▼──────┐
│ 暖气片体 │────▶ 房间温度
└─────────────┘
> 核心要点:双向PID的关键在于一个输出同时控制两个互斥执行器,通过正负符号区分方向,配合死区和互锁逻辑保证安全性。冷却侧如果用的是风扇而非冷水阀,还需考虑风速-散热的非线性关系,可能需要在PID输出后加非线性映射表。