#include "Mid_pid.h" #include "Com_parameter.h" #include "FreeRTOS.h" #include "task.h" #include #include static pid_handle_t g_pid_handle; /* SET_ALL 载荷缓冲: 0~23=PTC, 24~27=进风, 28~31=出风 */ static uint8_t g_output_duty[PID_OUTPUT_DUTY_LEN]; #define PID_SAMPLE_TIME_MS 100 /** * @brief PID 初始化 (全部通道一次性初始化) * @note 默认参数: 使能、NORMAL 模式、kp=2.6/ki=0.125/kd=0.04、限幅 0~100 * @param 无 * @return 无 */ void Mid_PID_Init(void) { uint8_t i; memset(&g_pid_handle, 0, sizeof(pid_handle_t)); memset(g_output_duty, 0, sizeof(g_output_duty)); g_pid_handle.total_ch = PID_CHANNEL_MAX; for(i = 0; i < PID_CHANNEL_MAX; i++) { g_pid_handle.channels[i].ch = i; g_pid_handle.channels[i].enable = 1; g_pid_handle.channels[i].mode = PID_MODE_NORMAL; g_pid_handle.channels[i].gain.kp = 2.6f; g_pid_handle.channels[i].gain.ki = 0.125f; g_pid_handle.channels[i].gain.kd = 0.04f; g_pid_handle.channels[i].limit.output_min = 0.0f; g_pid_handle.channels[i].limit.output_max = 100.0f; g_pid_handle.channels[i].deadband = 0.0f; g_pid_handle.channels[i].sample_time = PID_SAMPLE_TIME_MS; g_pid_handle.channels[i].last_update = 0; } } /** * @brief PID 反初始化 (清空所有通道数据) * @note 会同时清空输出占空比缓冲外的句柄数据 * @param 无 * @return 无 */ void Mid_PID_DeInit(void) { memset(&g_pid_handle, 0, sizeof(pid_handle_t)); } /** * @brief 设置指定 PID 通道的增益与限幅参数 * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @param gain : 增益参数指针 (kp/ki/kd), 可为 NULL 表示不修改 * @param limit : 输出限幅指针 (min/max), 可为 NULL 表示不修改 * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL) */ pid_status_t Mid_PID_SetChannel(uint8_t ch, pid_gain_t *gain, pid_limit_t *limit) { if(ch >= PID_CHANNEL_MAX) { return PID_STA_ERR_CHANNEL; } if(gain != NULL) { g_pid_handle.channels[ch].gain.kp = gain->kp; g_pid_handle.channels[ch].gain.ki = gain->ki; g_pid_handle.channels[ch].gain.kd = gain->kd; } if(limit != NULL) { g_pid_handle.channels[ch].limit.output_min = limit->output_min; g_pid_handle.channels[ch].limit.output_max = limit->output_max; } return PID_STA_OK; } /** * @brief 获取指定 PID 通道的完整信息 * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @param info : [out] 通道信息结构体指针 * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL) */ pid_status_t Mid_PID_GetChannel(uint8_t ch, pid_ch_t *info) { if(ch >= PID_CHANNEL_MAX) { return PID_STA_ERR_CHANNEL; } if(info != NULL) { memcpy(info, &g_pid_handle.channels[ch], sizeof(pid_ch_t)); } return PID_STA_OK; } /** * @brief 设置指定 PID 通道的目标值 (setpoint) * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @param setpoint : 目标值 * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL) */ pid_status_t Mid_PID_SetPoint(uint8_t ch, float setpoint) { if(ch >= PID_CHANNEL_MAX) { return PID_STA_ERR_CHANNEL; } g_pid_handle.channels[ch].calc.setpoint = setpoint; return PID_STA_OK; } /** * @brief 设置指定 PID 通道的工作模式 * @note PID_MODE_AUTO 尚未实现, 不可设置 * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @param mode : 工作模式 (NORMAL / MANUAL) * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL / PID_STA_ERR_MODE) */ pid_status_t Mid_PID_SetMode(uint8_t ch, pid_mode_t mode) { if(ch >= PID_CHANNEL_MAX) { return PID_STA_ERR_CHANNEL; } if(mode >= PID_MODE_AUTO) { return PID_STA_ERR_MODE; } g_pid_handle.channels[ch].mode = mode; return PID_STA_OK; } /** * @brief 设置指定 PID 通道的手动输出值 * @note 仅 MANUAL 模式下生效, 输出会自动限幅 * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @param output : 手动输出值 * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL / PID_STA_ERR_MODE) */ pid_status_t Mid_PID_SetManualOutput(uint8_t ch, float output) { if(ch >= PID_CHANNEL_MAX) { return PID_STA_ERR_CHANNEL; } if(g_pid_handle.channels[ch].mode != PID_MODE_MANUAL) { return PID_STA_ERR_MODE; } /* 限幅 */ if(output < g_pid_handle.channels[ch].limit.output_min) { output = g_pid_handle.channels[ch].limit.output_min; } if(output > g_pid_handle.channels[ch].limit.output_max) { output = g_pid_handle.channels[ch].limit.output_max; } g_pid_handle.channels[ch].calc.output = output; return PID_STA_OK; } /** * @brief PID 计算: 输入过程值, 返回通道输出值 * @note 支持使能检查、MANUAL 直通、死区、输出限幅与退饱和积分 * @param ch : PID 通道号 (0~PID_CHANNEL_MAX-1) * @param pv : 过程值 (反馈值, 如实测温度) * @return PID 输出值; 通道无效/未使能返回 0.0f */ float Mid_PID_Calc(uint8_t ch, float pv) { pid_ch_t *pch; float error, p_term, i_term, d_term; float output; if(ch >= PID_CHANNEL_MAX) { return 0.0f; } pch = &g_pid_handle.channels[ch]; if(!pch->enable) { return 0.0f; } if(pch->mode == PID_MODE_MANUAL) { return pch->calc.output; } /* 计算误差 */ error = pch->calc.setpoint - pv; /* 死区处理 */ if(pch->deadband > 0.0f) { if((error > 0 && error < pch->deadband) || (error < 0 && error > -pch->deadband)) { error = 0; } } /* PID计算 */ p_term = pch->gain.kp * error; i_term = pch->calc.integral; d_term = pch->gain.kd * (error - pch->calc.prev_error); pch->calc.prev_error = error; output = p_term + i_term + d_term; /* 输出限幅 */ if(output < pch->limit.output_min) { output = pch->limit.output_min; } if(output > pch->limit.output_max) { output = pch->limit.output_max; } /* 退饱和积分: 仅在输出未饱和时累加积分 */ if(output > pch->limit.output_min && output < pch->limit.output_max) { pch->calc.integral += pch->gain.ki * error; if(pch->calc.integral < pch->limit.output_min) { pch->calc.integral = pch->limit.output_min; } if(pch->calc.integral > pch->limit.output_max) { pch->calc.integral = pch->limit.output_max; } } /* 更新状态 */ pch->calc.process_value = pv; pch->calc.error = error; pch->calc.output = output; return output; } /** * @brief 周期更新所有通道 (占位) * @note 实际使用需由外部循环调用 Mid_PID_Calc 并关联温度源 * @param 无 * @return 无 */ void Mid_PID_UpdateAll(void) { uint8_t i; for(i = 0; i < g_pid_handle.total_ch; i++) { /* 占位 - 实际使用时需要在外部循环调用Mid_PID_Calc */ } } /** * @brief 使能/禁用指定 PID 通道 * @note 禁用时清除该通道的积分与历史误差 * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @param enable : 1=使能, 0=禁用 * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL) */ pid_status_t Mid_PID_Enable(uint8_t ch, uint8_t enable) { if(ch >= PID_CHANNEL_MAX) { return PID_STA_ERR_CHANNEL; } g_pid_handle.channels[ch].enable = enable ? 1 : 0; if(!enable) { /* 禁用时清除积分和历史 */ g_pid_handle.channels[ch].calc.integral = 0; g_pid_handle.channels[ch].calc.prev_error = 0; } return PID_STA_OK; } /** * @brief 查询指定 PID 通道是否使能 * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @return 1=使能, 0=禁用 (通道无效返回 0) */ uint8_t Mid_PID_IsEnabled(uint8_t ch) { if(ch >= PID_CHANNEL_MAX) { return 0; } return g_pid_handle.channels[ch].enable; } /** * @brief PID 输出直接给到加热器功率 (24 个加热片独立加热) * @note 仅写入共享输出缓冲, 由 CPLD 任务统一封装 SET_ALL 帧经串口下发 * @param ch : 通道号 (0~23, 对应 24 路 PTC) * @param output : PID 输出值 (0~100) * @return 无 */ void Mid_PID_HeaterControl(uint8_t ch, float output) { if(ch >= PID_CHANNEL_MAX) { return; } if(output < 0.0f) { output = 0.0f; } if(output > 100.0f) { output = 100.0f; } g_output_duty[ch] = (uint8_t)(output + 0.5f); } /** * @brief 根据实测温度控制风扇转速 (6400 八个风扇) * @note ch 0~3=进风 Front, ch 4~7=出风 Rear * @note 分档以目标控制温度 (EEPROM 参数, 见 Com_parameter) 为基准: * temp>=target+5 → 进55/出55; temp>=target+1 → 进35/出40; * 其余(含 temp= 8) { return; } /* 目标控制温度: 设置只有目标温度, 无独立风扇温度 */ target = (float)Com_parameter_get_target(); /* 三档判定 (从高到低, 消除 target..target+1 中间带死区) */ if(temp >= (target + 5.0f)) { front_duty = 55; rear_duty = 55; } else if(temp >= (target + 1.0f)) { front_duty = 35; rear_duty = 40; } else { front_duty = 20; rear_duty = 25; } if(ch < 4) { /* 进风 Front: ch0~3 -> offset 24~27 */ g_output_duty[PID_FAN_FRONT_BASE + ch] = front_duty; } else { /* 出风 Rear: ch4~7 -> offset 28~31 */ g_output_duty[PID_FAN_REAR_BASE + (ch - 4)] = rear_duty; } } /** * @brief 直接设定风扇占空比 (定值输出, 用于预加热等场景) * @note ch 0~3=进风 (SET_ALL offset 24~27), ch 4~7=出风 (offset 28~31) * @param ch : 风扇通道号 (0~7) * @param duty : 占空比 (0~100) * @return 无 */ void Mid_PID_FanSetDuty(uint8_t ch, uint8_t duty) { if(ch >= 8) { return; } if(duty > 100) { duty = 100; } if(ch < 4) { g_output_duty[PID_FAN_FRONT_BASE + ch] = duty; } else { g_output_duty[PID_FAN_REAR_BASE + (ch - 4)] = duty; } } /** * @brief 分区风扇映射表 * @note 分区0: PTC 0-5 / 进风 ch0 / 出风 ch4 (sensor idx0) * 分区1: PTC 6-11 / 进风 ch1 / 出风 ch5 (sensor idx1) * 分区2: PTC 12-17 / 进风 ch2 / 出风 ch6 (sensor idx2) * 分区3: PTC 18-23 / 进风 ch3 / 出风 ch7 (sensor idx3) */ static const pid_fan_zone_t g_fan_zones[PID_FAN_ZONE_MAX] = { { 0, 6, 0, 0, 4 }, { 6, 6, 1, 1, 5 }, { 12, 6, 2, 2, 6 }, { 18, 6, 3, 3, 7 }, }; /** * @brief 获取分区风扇映射表 * @param count : [out] 分区数量 * @return 指向只读分区表的指针 */ const pid_fan_zone_t *Mid_PID_GetFanZones(uint8_t *count) { if(count != NULL) { *count = PID_FAN_ZONE_MAX; } return g_fan_zones; } /** * @brief 按分区温度控制对应进/出风风扇 * @note 分区 i 取 sensor4[i] 实测温度, 经 Mid_PID_FanControl 三档分档 * 驱动该分区进风 (ch i) 与出风 (ch i+4) 风扇 * @param sensor4 : 4 路分区温度指针 (越界不读入) * @return 无 */ void Mid_PID_FanZoneUpdate(const int16_t *sensor4) { uint8_t z; if(sensor4 == NULL) { return; } for(z = 0; z < PID_FAN_ZONE_MAX; z++) { Mid_PID_FanControl((uint8_t)(g_fan_zones[z].front_fan_ch), (float)sensor4[z]); Mid_PID_FanControl((uint8_t)(g_fan_zones[z].rear_fan_ch), (float)sensor4[z]); } } /** * @brief 获取 SET_ALL 32 字节输出占空比缓冲指针 * @note [0..23]=PTC, [24..27]=进风, [28..31]=出风 * @note 返回的指针仅在退出临界区之前有效, 跨任务读取须用 * Mid_PID_LockOutput()/Mid_PID_UnlockOutput() 保护 * @param 无 * @return 指向 g_output_duty 的只读指针 */ const uint8_t *Mid_PID_GetOutputDuty(void) { return g_output_duty; } /** * @brief 锁定输出占空比缓冲 (进入临界区) * @note 批量写缓冲或读取缓冲+封装 SET_ALL 帧前调用, 防止 * 高优先级 CPLD 任务抢占读取/写入到一半的缓冲。 * 临界区长度应尽量短, 发送帧 (可能阻塞) 必须在 Unlock 后进行。 * @param 无 * @return 无 */ void Mid_PID_LockOutput(void) { taskENTER_CRITICAL(); } /** * @brief 解锁输出占空比缓冲 (退出临界区) * @note 与 Mid_PID_LockOutput() 成对使用 * @param 无 * @return 无 */ void Mid_PID_UnlockOutput(void) { taskEXIT_CRITICAL(); }