#ifndef MID_PID_H #define MID_PID_H #include "stdint.h" /* PID通道数量定义 */ #define PID_CHANNEL_MAX 24 /* PID模式 */ typedef enum { PID_MODE_NORMAL = 0, //正常模式 PID_MODE_MANUAL, //手动模式 PID_MODE_AUTO //自动模式未实现 } pid_mode_t; /* PID输出限幅 */ typedef struct { float output_min; //输出最小值 float output_max; //输出最大值 } pid_limit_t; /* PID增益结构 */ typedef struct { float kp; //比例 float ki; //积分 float kd; //微分 } pid_gain_t; /* PID计算结果 */ typedef struct { float setpoint; //目标值 float process_value; //测量值 float output; //输出值 float error; //当前误差值 float prev_error; //上一次的误差 float integral; //积分累加值 } pid_calc_t; /* PID通道状态 */ typedef struct { uint8_t ch; //通道号 uint8_t enable; //使能标志(1运行、0停止) pid_mode_t mode; //工作模式 pid_gain_t gain; //增益参数 pid_limit_t limit; //输出限幅 pid_calc_t calc; //计算相关 float deadband; //死区范围 float sample_time; //采样时间(ms 目前未使用) uint32_t last_update; //上次更新的时间戳(目前未使用) } pid_ch_t; /* PID全局配置 */ typedef struct { uint8_t total_ch; //总通道数 pid_ch_t channels[PID_CHANNEL_MAX]; //总定义 } pid_handle_t; //句柄 /* SET_ALL 输出占空比缓冲长度 (24 PTC + 4 进风 + 4 出风) */ #define PID_OUTPUT_DUTY_LEN 32 /* 风扇通道在 SET_ALL 载荷中的映射 */ #define PID_FAN_FRONT_BASE 24 /* ch0~3 进风 Front, 对应 SET_ALL offset 24~27 */ #define PID_FAN_REAR_BASE 28 /* ch4~7 出风 Rear, 对应 SET_ALL offset 28~31 */ /* 分区风扇与 PTC/传感器关联 * 分区分组 (按规律补齐): * 分区0: PTC 0-5 ↔ 传感器 idx0 ↔ 进风 ch0 / 出风 ch4 * 分区1: PTC 6-11 ↔ 传感器 idx1 ↔ 进风 ch1 / 出风 ch5 * 分区2: PTC 12-17 ↔ 传感器 idx2 ↔ 进风 ch2 / 出风 ch6 * 分区3: PTC 18-23 ↔ 传感器 idx3 ↔ 进风 ch3 / 出风 ch7 */ #define PID_FAN_ZONE_MAX 4 typedef struct { uint8_t ptc_start; /* 分区起始 PTC 通道 (0~23) */ uint8_t ptc_count; /* 分区 PTC 通道数 */ uint8_t sensor_idx; /* 关联的预加热温度序号 (0~3) */ uint8_t front_fan_ch; /* 进风风扇通道 (0~3 -> SET_ALL offset 24~27) */ uint8_t rear_fan_ch; /* 出风风扇通道 (4~7 -> SET_ALL offset 28~31) */ } pid_fan_zone_t; /* PID状态 */ typedef enum { PID_STA_OK = 0, //操作成功 PID_STA_ERR_PARAM, //参数错误 PID_STA_ERR_CHANNEL, //通道号错误 PID_STA_ERR_MODE //模式错误 } pid_status_t; /* API */ /** * @brief PID 初始化 (全部通道一次性初始化) * @note 默认参数: 使能、NORMAL 模式、kp=2.6/ki=0.125/kd=0.04、限幅 0~100 * @param 无 * @return 无 */ void Mid_PID_Init(void); /** * @brief PID 反初始化 (清空所有通道数据) * @param 无 * @return 无 */ void Mid_PID_DeInit(void); /** * @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); /** * @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); /** * @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); /** * @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); /** * @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); /** * @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); /** * @brief 周期更新所有通道 (占位) * @note 实际使用需由外部循环调用 Mid_PID_Calc 并关联温度源 * @param 无 * @return 无 */ void Mid_PID_UpdateAll(void); /** * @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); /** * @brief 查询指定 PID 通道是否使能 * @param ch : 通道号 (0~PID_CHANNEL_MAX-1) * @return 1=使能, 0=禁用 (通道无效返回 0) */ uint8_t Mid_PID_IsEnabled(uint8_t ch); /* 执行器控制 */ /** * @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); /** * @brief 根据实测温度控制风扇转速 (6400 八个风扇) * @note ch 0~3=进风 Front, ch 4~7=出风 Rear * @note 分档以目标控制温度 (EEPROM 参数) 为基准: * temp>=target+5 → 进55/出55; temp>=target+1 → 进35/出40; * 其余(含 temp