Mid_pid.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef MID_PID_H
  2. #define MID_PID_H
  3. #include "stdint.h"
  4. /* PID通道数量定义 */
  5. #define PID_CHANNEL_MAX 24
  6. /* PID模式 */
  7. typedef enum {
  8. PID_MODE_NORMAL = 0, //正常模式
  9. PID_MODE_MANUAL, //手动模式
  10. PID_MODE_AUTO //自动模式未实现
  11. } pid_mode_t;
  12. /* PID输出限幅 */
  13. typedef struct {
  14. float output_min; //输出最小值
  15. float output_max; //输出最大值
  16. } pid_limit_t;
  17. /* PID增益结构 */
  18. typedef struct {
  19. float kp; //比例
  20. float ki; //积分
  21. float kd; //微分
  22. } pid_gain_t;
  23. /* PID计算结果 */
  24. typedef struct {
  25. float setpoint; //目标值
  26. float process_value; //测量值
  27. float output; //输出值
  28. float error; //当前误差值
  29. float prev_error; //上一次的误差
  30. float integral; //积分累加值
  31. } pid_calc_t;
  32. /* PID通道状态 */
  33. typedef struct {
  34. uint8_t ch; //通道号
  35. uint8_t enable; //使能标志(1运行、0停止)
  36. pid_mode_t mode; //工作模式
  37. pid_gain_t gain; //增益参数
  38. pid_limit_t limit; //输出限幅
  39. pid_calc_t calc; //计算相关
  40. float deadband; //死区范围
  41. float sample_time; //采样时间(ms 目前未使用)
  42. uint32_t last_update; //上次更新的时间戳(目前未使用)
  43. } pid_ch_t;
  44. /* PID全局配置 */
  45. typedef struct {
  46. uint8_t total_ch; //总通道数
  47. pid_ch_t channels[PID_CHANNEL_MAX]; //总定义
  48. } pid_handle_t; //句柄
  49. /* SET_ALL 输出占空比缓冲长度 (24 PTC + 4 进风 + 4 出风) */
  50. #define PID_OUTPUT_DUTY_LEN 32
  51. /* 风扇通道在 SET_ALL 载荷中的映射 */
  52. #define PID_FAN_FRONT_BASE 24 /* ch0~3 进风 Front, 对应 SET_ALL offset 24~27 */
  53. #define PID_FAN_REAR_BASE 28 /* ch4~7 出风 Rear, 对应 SET_ALL offset 28~31 */
  54. /* 风扇目标温度: 暂定 80°C, 后续改从 EEPROM 读取 */
  55. #define PID_FAN_TARGET_DEFAULT 80.0f
  56. /* PID状态 */
  57. typedef enum {
  58. PID_STA_OK = 0, //操作成功
  59. PID_STA_ERR_PARAM, //参数错误
  60. PID_STA_ERR_CHANNEL, //通道号错误
  61. PID_STA_ERR_MODE //模式错误
  62. } pid_status_t;
  63. /* API */
  64. /**
  65. * @brief PID 初始化 (全部通道一次性初始化)
  66. * @note 默认参数: 使能、NORMAL 模式、kp=2.6/ki=0.125/kd=0.04、限幅 0~100
  67. * @param 无
  68. * @return 无
  69. */
  70. void Mid_PID_Init(void);
  71. /**
  72. * @brief PID 反初始化 (清空所有通道数据)
  73. * @param 无
  74. * @return 无
  75. */
  76. void Mid_PID_DeInit(void);
  77. /**
  78. * @brief 设置指定 PID 通道的增益与限幅参数
  79. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  80. * @param gain : 增益参数指针 (kp/ki/kd), 可为 NULL 表示不修改
  81. * @param limit : 输出限幅指针 (min/max), 可为 NULL 表示不修改
  82. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  83. */
  84. pid_status_t Mid_PID_SetChannel(uint8_t ch, pid_gain_t *gain, pid_limit_t *limit);
  85. /**
  86. * @brief 获取指定 PID 通道的完整信息
  87. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  88. * @param info : [out] 通道信息结构体指针
  89. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  90. */
  91. pid_status_t Mid_PID_GetChannel(uint8_t ch, pid_ch_t *info);
  92. /**
  93. * @brief 设置指定 PID 通道的目标值 (setpoint)
  94. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  95. * @param setpoint : 目标值
  96. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  97. */
  98. pid_status_t Mid_PID_SetPoint(uint8_t ch, float setpoint);
  99. /**
  100. * @brief 设置指定 PID 通道的工作模式
  101. * @note PID_MODE_AUTO 尚未实现, 不可设置
  102. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  103. * @param mode : 工作模式 (NORMAL / MANUAL)
  104. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL / PID_STA_ERR_MODE)
  105. */
  106. pid_status_t Mid_PID_SetMode(uint8_t ch, pid_mode_t mode);
  107. /**
  108. * @brief 设置指定 PID 通道的手动输出值
  109. * @note 仅 MANUAL 模式下生效, 输出会自动限幅
  110. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  111. * @param output : 手动输出值
  112. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL / PID_STA_ERR_MODE)
  113. */
  114. pid_status_t Mid_PID_SetManualOutput(uint8_t ch, float output);
  115. /**
  116. * @brief PID 计算: 输入过程值, 返回通道输出值
  117. * @note 支持使能检查、MANUAL 直通、死区、输出限幅与退饱和积分
  118. * @param ch : PID 通道号 (0~PID_CHANNEL_MAX-1)
  119. * @param pv : 过程值 (反馈值, 如实测温度)
  120. * @return PID 输出值; 通道无效/未使能返回 0.0f
  121. */
  122. float Mid_PID_Calc(uint8_t ch, float pv);
  123. /**
  124. * @brief 周期更新所有通道 (占位)
  125. * @note 实际使用需由外部循环调用 Mid_PID_Calc 并关联温度源
  126. * @param 无
  127. * @return 无
  128. */
  129. void Mid_PID_UpdateAll(void);
  130. /**
  131. * @brief 使能/禁用指定 PID 通道
  132. * @note 禁用时清除该通道的积分与历史误差
  133. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  134. * @param enable : 1=使能, 0=禁用
  135. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  136. */
  137. pid_status_t Mid_PID_Enable(uint8_t ch, uint8_t enable);
  138. /**
  139. * @brief 查询指定 PID 通道是否使能
  140. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  141. * @return 1=使能, 0=禁用 (通道无效返回 0)
  142. */
  143. uint8_t Mid_PID_IsEnabled(uint8_t ch);
  144. /* 执行器控制 */
  145. /**
  146. * @brief PID 输出直接给到加热器功率 (24 个加热片独立加热)
  147. * @note 仅写入共享输出缓冲, 由 CPLD 任务统一封装 SET_ALL 帧经串口下发
  148. * @param ch : 通道号 (0~23, 对应 24 路 PTC)
  149. * @param output : PID 输出值 (0~100)
  150. * @return 无
  151. */
  152. void Mid_PID_HeaterControl(uint8_t ch, float output);
  153. /**
  154. * @brief 根据实测温度控制风扇转速 (6400 八个风扇)
  155. * @note ch 0~3=进风 Front, ch 4~7=出风 Rear
  156. * @note 分档: temp>=target+5 → 进55/出55; temp>=target+1 → 进35/出40;
  157. * 其余(含 temp<target) → 进20/出25; 尚未接入调用点
  158. * @param ch : 通道号 (0~7)
  159. * @param temp : 实测温度 (°C)
  160. * @return 无
  161. */
  162. void Mid_PID_FanControl(uint8_t ch, float temp);
  163. /**
  164. * @brief 设置风扇目标温度 (设定值)
  165. * @note 暂存于 Mid 层; 后续 EEPROM 读取后调用本函数更新
  166. * @param target : 目标温度 (°C)
  167. * @return 无
  168. */
  169. void Mid_PID_SetFanTarget(float target);
  170. /**
  171. * @brief 获取风扇目标温度 (设定值)
  172. * @param 无
  173. * @return 当前风扇目标温度 (°C)
  174. */
  175. float Mid_PID_GetFanTarget(void);
  176. /**
  177. * @brief 获取 SET_ALL 32 字节输出占空比缓冲指针
  178. * @note [0..23]=PTC, [24..27]=进风, [28..31]=出风
  179. * @param 无
  180. * @return 指向输出占空比缓冲的只读指针
  181. */
  182. const uint8_t *Mid_PID_GetOutputDuty(void);
  183. #endif