Mid_pid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include "Mid_pid.h"
  2. #include "Com_parameter.h"
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include <string.h>
  6. #include <stdlib.h>
  7. static pid_handle_t g_pid_handle;
  8. /* SET_ALL 载荷缓冲: 0~23=PTC, 24~27=进风, 28~31=出风 */
  9. static uint8_t g_output_duty[PID_OUTPUT_DUTY_LEN];
  10. #define PID_SAMPLE_TIME_MS 100
  11. /**
  12. * @brief PID 初始化 (全部通道一次性初始化)
  13. * @note 默认参数: 使能、NORMAL 模式、kp=2.6/ki=0.125/kd=0.04、限幅 0~100
  14. * @param 无
  15. * @return 无
  16. */
  17. void Mid_PID_Init(void)
  18. {
  19. uint8_t i;
  20. memset(&g_pid_handle, 0, sizeof(pid_handle_t));
  21. memset(g_output_duty, 0, sizeof(g_output_duty));
  22. g_pid_handle.total_ch = PID_CHANNEL_MAX;
  23. for(i = 0; i < PID_CHANNEL_MAX; i++) {
  24. g_pid_handle.channels[i].ch = i;
  25. g_pid_handle.channels[i].enable = 1;
  26. g_pid_handle.channels[i].mode = PID_MODE_NORMAL;
  27. g_pid_handle.channels[i].gain.kp = 2.6f;
  28. g_pid_handle.channels[i].gain.ki = 0.125f;
  29. g_pid_handle.channels[i].gain.kd = 0.04f;
  30. g_pid_handle.channels[i].limit.output_min = 0.0f;
  31. g_pid_handle.channels[i].limit.output_max = 100.0f;
  32. g_pid_handle.channels[i].deadband = 0.0f;
  33. g_pid_handle.channels[i].sample_time = PID_SAMPLE_TIME_MS;
  34. g_pid_handle.channels[i].last_update = 0;
  35. }
  36. }
  37. /**
  38. * @brief PID 反初始化 (清空所有通道数据)
  39. * @note 会同时清空输出占空比缓冲外的句柄数据
  40. * @param 无
  41. * @return 无
  42. */
  43. void Mid_PID_DeInit(void)
  44. {
  45. memset(&g_pid_handle, 0, sizeof(pid_handle_t));
  46. }
  47. /**
  48. * @brief 设置指定 PID 通道的增益与限幅参数
  49. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  50. * @param gain : 增益参数指针 (kp/ki/kd), 可为 NULL 表示不修改
  51. * @param limit : 输出限幅指针 (min/max), 可为 NULL 表示不修改
  52. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  53. */
  54. pid_status_t Mid_PID_SetChannel(uint8_t ch, pid_gain_t *gain, pid_limit_t *limit)
  55. {
  56. if(ch >= PID_CHANNEL_MAX) {
  57. return PID_STA_ERR_CHANNEL;
  58. }
  59. if(gain != NULL) {
  60. g_pid_handle.channels[ch].gain.kp = gain->kp;
  61. g_pid_handle.channels[ch].gain.ki = gain->ki;
  62. g_pid_handle.channels[ch].gain.kd = gain->kd;
  63. }
  64. if(limit != NULL) {
  65. g_pid_handle.channels[ch].limit.output_min = limit->output_min;
  66. g_pid_handle.channels[ch].limit.output_max = limit->output_max;
  67. }
  68. return PID_STA_OK;
  69. }
  70. /**
  71. * @brief 获取指定 PID 通道的完整信息
  72. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  73. * @param info : [out] 通道信息结构体指针
  74. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  75. */
  76. pid_status_t Mid_PID_GetChannel(uint8_t ch, pid_ch_t *info)
  77. {
  78. if(ch >= PID_CHANNEL_MAX) {
  79. return PID_STA_ERR_CHANNEL;
  80. }
  81. if(info != NULL) {
  82. memcpy(info, &g_pid_handle.channels[ch], sizeof(pid_ch_t));
  83. }
  84. return PID_STA_OK;
  85. }
  86. /**
  87. * @brief 设置指定 PID 通道的目标值 (setpoint)
  88. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  89. * @param setpoint : 目标值
  90. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  91. */
  92. pid_status_t Mid_PID_SetPoint(uint8_t ch, float setpoint)
  93. {
  94. if(ch >= PID_CHANNEL_MAX) {
  95. return PID_STA_ERR_CHANNEL;
  96. }
  97. g_pid_handle.channels[ch].calc.setpoint = setpoint;
  98. return PID_STA_OK;
  99. }
  100. /**
  101. * @brief 设置指定 PID 通道的工作模式
  102. * @note PID_MODE_AUTO 尚未实现, 不可设置
  103. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  104. * @param mode : 工作模式 (NORMAL / MANUAL)
  105. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL / PID_STA_ERR_MODE)
  106. */
  107. pid_status_t Mid_PID_SetMode(uint8_t ch, pid_mode_t mode)
  108. {
  109. if(ch >= PID_CHANNEL_MAX) {
  110. return PID_STA_ERR_CHANNEL;
  111. }
  112. if(mode >= PID_MODE_AUTO) {
  113. return PID_STA_ERR_MODE;
  114. }
  115. g_pid_handle.channels[ch].mode = mode;
  116. return PID_STA_OK;
  117. }
  118. /**
  119. * @brief 设置指定 PID 通道的手动输出值
  120. * @note 仅 MANUAL 模式下生效, 输出会自动限幅
  121. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  122. * @param output : 手动输出值
  123. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL / PID_STA_ERR_MODE)
  124. */
  125. pid_status_t Mid_PID_SetManualOutput(uint8_t ch, float output)
  126. {
  127. if(ch >= PID_CHANNEL_MAX) {
  128. return PID_STA_ERR_CHANNEL;
  129. }
  130. if(g_pid_handle.channels[ch].mode != PID_MODE_MANUAL) {
  131. return PID_STA_ERR_MODE;
  132. }
  133. /* 限幅 */
  134. if(output < g_pid_handle.channels[ch].limit.output_min) {
  135. output = g_pid_handle.channels[ch].limit.output_min;
  136. }
  137. if(output > g_pid_handle.channels[ch].limit.output_max) {
  138. output = g_pid_handle.channels[ch].limit.output_max;
  139. }
  140. g_pid_handle.channels[ch].calc.output = output;
  141. return PID_STA_OK;
  142. }
  143. /**
  144. * @brief PID 计算: 输入过程值, 返回通道输出值
  145. * @note 支持使能检查、MANUAL 直通、死区、输出限幅与退饱和积分
  146. * @param ch : PID 通道号 (0~PID_CHANNEL_MAX-1)
  147. * @param pv : 过程值 (反馈值, 如实测温度)
  148. * @return PID 输出值; 通道无效/未使能返回 0.0f
  149. */
  150. float Mid_PID_Calc(uint8_t ch, float pv)
  151. {
  152. pid_ch_t *pch;
  153. float error, p_term, i_term, d_term;
  154. float output;
  155. if(ch >= PID_CHANNEL_MAX) {
  156. return 0.0f;
  157. }
  158. pch = &g_pid_handle.channels[ch];
  159. if(!pch->enable) {
  160. return 0.0f;
  161. }
  162. if(pch->mode == PID_MODE_MANUAL) {
  163. return pch->calc.output;
  164. }
  165. /* 计算误差 */
  166. error = pch->calc.setpoint - pv;
  167. /* 死区处理 */
  168. if(pch->deadband > 0.0f) {
  169. if((error > 0 && error < pch->deadband) ||
  170. (error < 0 && error > -pch->deadband)) {
  171. error = 0;
  172. }
  173. }
  174. /* PID计算 */
  175. p_term = pch->gain.kp * error;
  176. i_term = pch->calc.integral;
  177. d_term = pch->gain.kd * (error - pch->calc.prev_error);
  178. pch->calc.prev_error = error;
  179. output = p_term + i_term + d_term;
  180. /* 输出限幅 */
  181. if(output < pch->limit.output_min) {
  182. output = pch->limit.output_min;
  183. }
  184. if(output > pch->limit.output_max) {
  185. output = pch->limit.output_max;
  186. }
  187. /* 退饱和积分: 仅在输出未饱和时累加积分 */
  188. if(output > pch->limit.output_min && output < pch->limit.output_max) {
  189. pch->calc.integral += pch->gain.ki * error;
  190. if(pch->calc.integral < pch->limit.output_min) {
  191. pch->calc.integral = pch->limit.output_min;
  192. }
  193. if(pch->calc.integral > pch->limit.output_max) {
  194. pch->calc.integral = pch->limit.output_max;
  195. }
  196. }
  197. /* 更新状态 */
  198. pch->calc.process_value = pv;
  199. pch->calc.error = error;
  200. pch->calc.output = output;
  201. return output;
  202. }
  203. /**
  204. * @brief 周期更新所有通道 (占位)
  205. * @note 实际使用需由外部循环调用 Mid_PID_Calc 并关联温度源
  206. * @param 无
  207. * @return 无
  208. */
  209. void Mid_PID_UpdateAll(void)
  210. {
  211. uint8_t i;
  212. for(i = 0; i < g_pid_handle.total_ch; i++) {
  213. /* 占位 - 实际使用时需要在外部循环调用Mid_PID_Calc */
  214. }
  215. }
  216. /**
  217. * @brief 使能/禁用指定 PID 通道
  218. * @note 禁用时清除该通道的积分与历史误差
  219. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  220. * @param enable : 1=使能, 0=禁用
  221. * @return PID 状态 (PID_STA_OK / PID_STA_ERR_CHANNEL)
  222. */
  223. pid_status_t Mid_PID_Enable(uint8_t ch, uint8_t enable)
  224. {
  225. if(ch >= PID_CHANNEL_MAX) {
  226. return PID_STA_ERR_CHANNEL;
  227. }
  228. g_pid_handle.channels[ch].enable = enable ? 1 : 0;
  229. if(!enable) {
  230. /* 禁用时清除积分和历史 */
  231. g_pid_handle.channels[ch].calc.integral = 0;
  232. g_pid_handle.channels[ch].calc.prev_error = 0;
  233. }
  234. return PID_STA_OK;
  235. }
  236. /**
  237. * @brief 查询指定 PID 通道是否使能
  238. * @param ch : 通道号 (0~PID_CHANNEL_MAX-1)
  239. * @return 1=使能, 0=禁用 (通道无效返回 0)
  240. */
  241. uint8_t Mid_PID_IsEnabled(uint8_t ch)
  242. {
  243. if(ch >= PID_CHANNEL_MAX) {
  244. return 0;
  245. }
  246. return g_pid_handle.channels[ch].enable;
  247. }
  248. /**
  249. * @brief PID 输出直接给到加热器功率 (24 个加热片独立加热)
  250. * @note 仅写入共享输出缓冲, 由 CPLD 任务统一封装 SET_ALL 帧经串口下发
  251. * @param ch : 通道号 (0~23, 对应 24 路 PTC)
  252. * @param output : PID 输出值 (0~100)
  253. * @return 无
  254. */
  255. void Mid_PID_HeaterControl(uint8_t ch, float output)
  256. {
  257. if(ch >= PID_CHANNEL_MAX) {
  258. return;
  259. }
  260. if(output < 0.0f) {
  261. output = 0.0f;
  262. }
  263. if(output > 100.0f) {
  264. output = 100.0f;
  265. }
  266. g_output_duty[ch] = (uint8_t)(output + 0.5f);
  267. }
  268. /**
  269. * @brief 根据实测温度控制风扇转速 (6400 八个风扇)
  270. * @note ch 0~3=进风, ch 4~7=出风; 由 Mid_PID_FanZoneUpdate 每 500ms
  271. * 按 4 个腔体温度各调一次 (调用方须持输出锁); 无迟滞、无斜率限制
  272. * @note 分档以目标控制温度 (EEPROM 参数) 为基准: temp>=target+5 → 55/55;
  273. * temp>=target+1 → 进35/出40; 其余 (含 temp<target 与无效 -127) → 20/25
  274. * @param ch : 通道号 (0~7)
  275. * @param temp : 实测温度 (°C)
  276. * @return 无
  277. */
  278. void Mid_PID_FanControl(uint8_t ch, float temp)
  279. {
  280. uint8_t front_duty;
  281. uint8_t rear_duty;
  282. float target;
  283. if(ch >= 8) {
  284. return;
  285. }
  286. /* 目标控制温度: 设置只有目标温度, 无独立风扇温度 */
  287. target = (float)Com_parameter_get_target();
  288. /* 三档判定 (从高到低, 消除 target..target+1 中间带死区) */
  289. if(temp >= (target + 5.0f)) {
  290. front_duty = 55;
  291. rear_duty = 55;
  292. } else if(temp >= (target + 1.0f)) {
  293. front_duty = 35;
  294. rear_duty = 40;
  295. } else {
  296. front_duty = 20;
  297. rear_duty = 25;
  298. }
  299. if(ch < 4) {
  300. /* 进风 Front: ch0~3 -> offset 24~27 */
  301. g_output_duty[PID_FAN_FRONT_BASE + ch] = front_duty;
  302. } else {
  303. /* 出风 Rear: ch4~7 -> offset 28~31 */
  304. g_output_duty[PID_FAN_REAR_BASE + (ch - 4)] = rear_duty;
  305. }
  306. }
  307. /**
  308. * @brief 直接设定风扇占空比 (定值输出, 用于预加热等场景)
  309. * @note ch 0~3=进风 (SET_ALL offset 24~27), ch 4~7=出风 (offset 28~31)
  310. * @param ch : 风扇通道号 (0~7)
  311. * @param duty : 占空比 (0~100)
  312. * @return 无
  313. */
  314. void Mid_PID_FanSetDuty(uint8_t ch, uint8_t duty)
  315. {
  316. if(ch >= 8) {
  317. return;
  318. }
  319. if(duty > 100) {
  320. duty = 100;
  321. }
  322. if(ch < 4) {
  323. g_output_duty[PID_FAN_FRONT_BASE + ch] = duty;
  324. } else {
  325. g_output_duty[PID_FAN_REAR_BASE + (ch - 4)] = duty;
  326. }
  327. }
  328. /**
  329. * @brief 分区风扇映射表
  330. * @note 分区0: PTC 0-5 / 进风 ch0 / 出风 ch4 (腔体温度 idx0)
  331. * 分区1: PTC 6-11 / 进风 ch1 / 出风 ch5 (腔体温度 idx1)
  332. * 分区2: PTC 12-17 / 进风 ch2 / 出风 ch6 (腔体温度 idx2)
  333. * 分区3: PTC 18-23 / 进风 ch3 / 出风 ch7 (腔体温度 idx3)
  334. */
  335. static const pid_fan_zone_t g_fan_zones[PID_FAN_ZONE_MAX] =
  336. {
  337. { 0, 6, 0, 0, 4 },
  338. { 6, 6, 1, 1, 5 },
  339. { 12, 6, 2, 2, 6 },
  340. { 18, 6, 3, 3, 7 },
  341. };
  342. /**
  343. * @brief 获取分区风扇映射表
  344. * @param count : [out] 分区数量
  345. * @return 指向只读分区表的指针
  346. */
  347. const pid_fan_zone_t *Mid_PID_GetFanZones(uint8_t *count)
  348. {
  349. if(count != NULL) {
  350. *count = PID_FAN_ZONE_MAX;
  351. }
  352. return g_fan_zones;
  353. }
  354. /**
  355. * @brief 按分区温度控制对应进/出风风扇
  356. * @note 分区 i 取 sensor4[i] 实测温度, 经 Mid_PID_FanControl 三档分档
  357. * 驱动该分区进风 (ch i) 与出风 (ch i+4) 风扇
  358. * @param sensor4 : 4 路分区温度指针 (越界不读入)
  359. * @return 无
  360. */
  361. void Mid_PID_FanZoneUpdate(const int16_t *sensor4)
  362. {
  363. uint8_t z;
  364. if(sensor4 == NULL) {
  365. return;
  366. }
  367. for(z = 0; z < PID_FAN_ZONE_MAX; z++) {
  368. Mid_PID_FanControl((uint8_t)(g_fan_zones[z].front_fan_ch), (float)sensor4[z]);
  369. Mid_PID_FanControl((uint8_t)(g_fan_zones[z].rear_fan_ch), (float)sensor4[z]);
  370. }
  371. }
  372. /**
  373. * @brief 获取 SET_ALL 32 字节输出占空比缓冲指针
  374. * @note [0..23]=PTC, [24..27]=进风, [28..31]=出风
  375. * @note 返回的指针仅在退出临界区之前有效, 跨任务读取须用
  376. * Mid_PID_LockOutput()/Mid_PID_UnlockOutput() 保护
  377. * @param 无
  378. * @return 指向 g_output_duty 的只读指针
  379. */
  380. const uint8_t *Mid_PID_GetOutputDuty(void)
  381. {
  382. return g_output_duty;
  383. }
  384. /**
  385. * @brief 锁定输出占空比缓冲 (进入临界区)
  386. * @note 批量写缓冲或读取缓冲+封装 SET_ALL 帧前调用, 防止
  387. * 高优先级 CPLD 任务抢占读取/写入到一半的缓冲。
  388. * 临界区长度应尽量短, 发送帧 (可能阻塞) 必须在 Unlock 后进行。
  389. * @param 无
  390. * @return 无
  391. */
  392. void Mid_PID_LockOutput(void)
  393. {
  394. taskENTER_CRITICAL();
  395. }
  396. /**
  397. * @brief 解锁输出占空比缓冲 (退出临界区)
  398. * @note 与 Mid_PID_LockOutput() 成对使用
  399. * @param 无
  400. * @return 无
  401. */
  402. void Mid_PID_UnlockOutput(void)
  403. {
  404. taskEXIT_CRITICAL();
  405. }