Mid_pid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "Mid_pid.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. static pid_handle_t g_pid_handle;
  5. /* SET_ALL 载荷缓冲: 0~23=PTC, 24~27=进风, 28~31=出风 */
  6. static uint8_t g_output_duty[PID_OUTPUT_DUTY_LEN];
  7. /* 风扇目标温度 (设定值): 暂定默认 80°C, 后续从 EEPROM 读取后由 SetFanTarget 更新 */
  8. static float g_fan_target = PID_FAN_TARGET_DEFAULT;
  9. #define PID_SAMPLE_TIME_MS 100
  10. /**
  11. * @brief PID 初始化 (全部通道一次性初始化)
  12. * @note 默认参数: 使能、NORMAL 模式、kp=2.6/ki=0.125/kd=0.04、限幅 0~100
  13. * @param 无
  14. * @return 无
  15. */
  16. void Mid_PID_Init(void)
  17. {
  18. uint8_t i;
  19. memset(&g_pid_handle, 0, sizeof(pid_handle_t));
  20. memset(g_output_duty, 0, sizeof(g_output_duty));
  21. g_pid_handle.total_ch = PID_CHANNEL_MAX;
  22. for(i = 0; i < PID_CHANNEL_MAX; i++) {
  23. g_pid_handle.channels[i].ch = i;
  24. g_pid_handle.channels[i].enable = 1;
  25. g_pid_handle.channels[i].mode = PID_MODE_NORMAL;
  26. g_pid_handle.channels[i].gain.kp = 2.6f;
  27. g_pid_handle.channels[i].gain.ki = 0.125f;
  28. g_pid_handle.channels[i].gain.kd = 0.04f;
  29. g_pid_handle.channels[i].limit.output_min = 0.0f;
  30. g_pid_handle.channels[i].limit.output_max = 100.0f;
  31. g_pid_handle.channels[i].deadband = 0.0f;
  32. g_pid_handle.channels[i].sample_time = PID_SAMPLE_TIME_MS;
  33. g_pid_handle.channels[i].last_update = 0;
  34. }
  35. }
  36. /**
  37. * @brief PID 反初始化 (清空所有通道数据)
  38. * @note 会同时清空输出占空比缓冲外的句柄数据
  39. * @param 无
  40. * @return 无
  41. */
  42. void Mid_PID_DeInit(void)
  43. {
  44. memset(&g_pid_handle, 0, sizeof(pid_handle_t));
  45. }
  46. /**
  47. * @brief 设置指定 PID 通道的增益与限幅参数
  48. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  49. * @param gain : 增益参数指针 (kp/ki/kd), 可为 NULL 表示不修改
  50. * @param limit : 输出限幅指针 (min/max), 可为 NULL 表示不修改
  51. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  52. */
  53. pid_status_t Mid_PID_SetChannel(uint8_t ch, pid_gain_t *gain, pid_limit_t *limit)
  54. {
  55. if(ch >= PID_CHANNEL_MAX) {
  56. return PID_STA_ERR_CHANNEL;
  57. }
  58. if(gain != NULL) {
  59. g_pid_handle.channels[ch].gain.kp = gain->kp;
  60. g_pid_handle.channels[ch].gain.ki = gain->ki;
  61. g_pid_handle.channels[ch].gain.kd = gain->kd;
  62. }
  63. if(limit != NULL) {
  64. g_pid_handle.channels[ch].limit.output_min = limit->output_min;
  65. g_pid_handle.channels[ch].limit.output_max = limit->output_max;
  66. }
  67. return PID_STA_OK;
  68. }
  69. /**
  70. * @brief 获取指定 PID 通道的完整信息
  71. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  72. * @param info : [out] 通道信息结构体指针
  73. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  74. */
  75. pid_status_t Mid_PID_GetChannel(uint8_t ch, pid_ch_t *info)
  76. {
  77. if(ch >= PID_CHANNEL_MAX) {
  78. return PID_STA_ERR_CHANNEL;
  79. }
  80. if(info != NULL) {
  81. memcpy(info, &g_pid_handle.channels[ch], sizeof(pid_ch_t));
  82. }
  83. return PID_STA_OK;
  84. }
  85. /**
  86. * @brief 设置指定 PID 通道的目标值 (setpoint)
  87. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  88. * @param setpoint : 目标值
  89. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  90. */
  91. pid_status_t Mid_PID_SetPoint(uint8_t ch, float setpoint)
  92. {
  93. if(ch >= PID_CHANNEL_MAX) {
  94. return PID_STA_ERR_CHANNEL;
  95. }
  96. g_pid_handle.channels[ch].calc.setpoint = setpoint;
  97. return PID_STA_OK;
  98. }
  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. if(ch >= PID_CHANNEL_MAX) {
  109. return PID_STA_ERR_CHANNEL;
  110. }
  111. if(mode >= PID_MODE_AUTO) {
  112. return PID_STA_ERR_MODE;
  113. }
  114. g_pid_handle.channels[ch].mode = mode;
  115. return PID_STA_OK;
  116. }
  117. /**
  118. * @brief 设置指定 PID 通道的手动输出值
  119. * @note 仅 MANUAL 模式下生效, 输出会自动限幅
  120. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  121. * @param output : 手动输出值
  122. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL / PID_STA_ERR_MODE)
  123. */
  124. pid_status_t Mid_PID_SetManualOutput(uint8_t ch, float output)
  125. {
  126. if(ch >= PID_CHANNEL_MAX) {
  127. return PID_STA_ERR_CHANNEL;
  128. }
  129. if(g_pid_handle.channels[ch].mode != PID_MODE_MANUAL) {
  130. return PID_STA_ERR_MODE;
  131. }
  132. /* 限幅 */
  133. if(output < g_pid_handle.channels[ch].limit.output_min) {
  134. output = g_pid_handle.channels[ch].limit.output_min;
  135. }
  136. if(output > g_pid_handle.channels[ch].limit.output_max) {
  137. output = g_pid_handle.channels[ch].limit.output_max;
  138. }
  139. g_pid_handle.channels[ch].calc.output = output;
  140. return PID_STA_OK;
  141. }
  142. /**
  143. * @brief PID 计算: 输入过程值, 返回通道输出值
  144. * @note 支持使能检查、MANUAL 直通、死区、输出限幅与退饱和积分
  145. * @param ch : PID 通道号 (0~PID_CHANNEL_MAX-1)
  146. * @param pv : 过程值 (反馈值, 如实测温度)
  147. * @return PID 输出值; 通道无效/未使能返回 0.0f
  148. */
  149. float Mid_PID_Calc(uint8_t ch, float pv)
  150. {
  151. pid_ch_t *pch;
  152. float error, p_term, i_term, d_term;
  153. float output;
  154. if(ch >= PID_CHANNEL_MAX) {
  155. return 0.0f;
  156. }
  157. pch = &g_pid_handle.channels[ch];
  158. if(!pch->enable) {
  159. return 0.0f;
  160. }
  161. if(pch->mode == PID_MODE_MANUAL) {
  162. return pch->calc.output;
  163. }
  164. /* 计算误差 */
  165. error = pch->calc.setpoint - pv;
  166. /* 死区处理 */
  167. if(pch->deadband > 0.0f) {
  168. if((error > 0 && error < pch->deadband) ||
  169. (error < 0 && error > -pch->deadband)) {
  170. error = 0;
  171. }
  172. }
  173. /* PID计算 */
  174. p_term = pch->gain.kp * error;
  175. i_term = pch->calc.integral;
  176. d_term = pch->gain.kd * (error - pch->calc.prev_error);
  177. pch->calc.prev_error = error;
  178. output = p_term + i_term + d_term;
  179. /* 输出限幅 */
  180. if(output < pch->limit.output_min) {
  181. output = pch->limit.output_min;
  182. }
  183. if(output > pch->limit.output_max) {
  184. output = pch->limit.output_max;
  185. }
  186. /* 退饱和积分: 仅在输出未饱和时累加积分 */
  187. if(output > pch->limit.output_min && output < pch->limit.output_max) {
  188. pch->calc.integral += pch->gain.ki * error;
  189. if(pch->calc.integral < pch->limit.output_min) {
  190. pch->calc.integral = pch->limit.output_min;
  191. }
  192. if(pch->calc.integral > pch->limit.output_max) {
  193. pch->calc.integral = pch->limit.output_max;
  194. }
  195. }
  196. /* 更新状态 */
  197. pch->calc.process_value = pv;
  198. pch->calc.error = error;
  199. pch->calc.output = output;
  200. return output;
  201. }
  202. /**
  203. * @brief 周期更新所有通道 (占位)
  204. * @note 实际使用需由外部循环调用 Mid_PID_Calc 并关联温度源
  205. * @param 无
  206. * @return 无
  207. */
  208. void Mid_PID_UpdateAll(void)
  209. {
  210. uint8_t i;
  211. for(i = 0; i < g_pid_handle.total_ch; i++) {
  212. /* 占位 - 实际使用时需要在外部循环调用Mid_PID_Calc */
  213. }
  214. }
  215. /**
  216. * @brief 使能/禁用指定 PID 通道
  217. * @note 禁用时清除该通道的积分与历史误差
  218. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  219. * @param enable : 1=使能, 0=禁用
  220. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  221. */
  222. pid_status_t Mid_PID_Enable(uint8_t ch, uint8_t enable)
  223. {
  224. if(ch >= PID_CHANNEL_MAX) {
  225. return PID_STA_ERR_CHANNEL;
  226. }
  227. g_pid_handle.channels[ch].enable = enable ? 1 : 0;
  228. if(!enable) {
  229. /* 禁用时清除积分和历史 */
  230. g_pid_handle.channels[ch].calc.integral = 0;
  231. g_pid_handle.channels[ch].calc.prev_error = 0;
  232. }
  233. return PID_STA_OK;
  234. }
  235. /**
  236. * @brief 查询指定 PID 通道是否使能
  237. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  238. * @return 1=使能, 0=禁用 (通道无效返回 0)
  239. */
  240. uint8_t Mid_PID_IsEnabled(uint8_t ch)
  241. {
  242. if(ch >= PID_CHANNEL_MAX) {
  243. return 0;
  244. }
  245. return g_pid_handle.channels[ch].enable;
  246. }
  247. /**
  248. * @brief PID 输出直接给到加热器功率 (24 个加热片独立加热)
  249. * @note 仅写入共享输出缓冲, 由 CPLD 任务统一封装 SET_ALL 帧经串口下发
  250. * @param ch : 通道号 (0~23, 对应 24 路 PTC)
  251. * @param output : PID 输出值 (0~100)
  252. * @return 无
  253. */
  254. void Mid_PID_HeaterControl(uint8_t ch, float output)
  255. {
  256. if(ch >= PID_CHANNEL_MAX) {
  257. return;
  258. }
  259. if(output < 0.0f) {
  260. output = 0.0f;
  261. }
  262. if(output > 100.0f) {
  263. output = 100.0f;
  264. }
  265. g_output_duty[ch] = (uint8_t)(output + 0.5f);
  266. }
  267. /**
  268. * @brief 根据实测温度控制风扇转速 (6400 八个风扇)
  269. * @note ch 0~3=进风 Front, ch 4~7=出风 Rear
  270. * @note 分档: temp>=target+5 → 进55/出55; temp>=target+1 → 进35/出40;
  271. * 其余(含 temp<target) → 进20/出25; 尚未接入调用点
  272. * @param ch : 通道号 (0~7)
  273. * @param temp : 实测温度 (°C)
  274. * @return 无
  275. */
  276. void Mid_PID_FanControl(uint8_t ch, float temp)
  277. {
  278. uint8_t front_duty;
  279. uint8_t rear_duty;
  280. if(ch >= 8) {
  281. return;
  282. }
  283. /* 三档判定 (从高到低, 消除 target..target+1 中间带死区) */
  284. if(temp >= (g_fan_target + 5.0f)) {
  285. front_duty = 55;
  286. rear_duty = 55;
  287. } else if(temp >= (g_fan_target + 1.0f)) {
  288. front_duty = 35;
  289. rear_duty = 40;
  290. } else {
  291. front_duty = 20;
  292. rear_duty = 25;
  293. }
  294. if(ch < 4) {
  295. /* 进风 Front: ch0~3 -> offset 24~27 */
  296. g_output_duty[PID_FAN_FRONT_BASE + ch] = front_duty;
  297. } else {
  298. /* 出风 Rear: ch4~7 -> offset 28~31 */
  299. g_output_duty[PID_FAN_REAR_BASE + (ch - 4)] = rear_duty;
  300. }
  301. }
  302. /**
  303. * @brief 设置风扇目标温度 (设定值)
  304. * @note 暂存于 Mid 层; 后续 EEPROM 读取后调用本函数更新
  305. * @param target : 目标温度 (°C)
  306. * @return 无
  307. */
  308. void Mid_PID_SetFanTarget(float target)
  309. {
  310. g_fan_target = target;
  311. }
  312. /**
  313. * @brief 获取风扇目标温度 (设定值)
  314. * @param 无
  315. * @return 当前风扇目标温度 (°C)
  316. */
  317. float Mid_PID_GetFanTarget(void)
  318. {
  319. return g_fan_target;
  320. }
  321. /**
  322. * @brief 获取 SET_ALL 32 字节输出占空比缓冲指针
  323. * @note [0..23]=PTC, [24..27]=进风, [28..31]=出风
  324. * @param 无
  325. * @return 指向 g_output_duty 的只读指针
  326. */
  327. const uint8_t *Mid_PID_GetOutputDuty(void)
  328. {
  329. return g_output_duty;
  330. }