App_pidControl.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. ******************************************************************************
  3. * @file App_pidControl.c
  4. * @brief PID 控温任务实现 (App 应用层)
  5. *
  6. * 预加热 4 路温度全部达标后, Task_Preheat 调用 App_pidControl_Start() 移交,
  7. * 本任务每 500ms:
  8. * - 24 路 PTC: Mid_PID_Calc(ch, pv) 过程值 pv = BMC 24 路温度 (快照值即 ℃);
  9. * - 8 路风扇 : 按分区温度 (Mid_PID_FanZoneUpdate) 三档分档;
  10. * 然后立即将 32 字节输出缓冲封装 SET_ALL 帧下发 (兼 30s 喂狗)。
  11. * 传感器无效 (温度 < -90) 时对应通道输出 0, 防止误判全功率加热。
  12. ******************************************************************************
  13. */
  14. #include <stdio.h>
  15. #include "App_pidControl.h"
  16. #include "FreeRTOS.h"
  17. #include "task.h"
  18. #include "Com_cpld_protocol.h"
  19. #include "Com_cpld_usart.h"
  20. #include "Com_usb_cdc.h"
  21. #include "Mid_pid.h"
  22. #include "Mid_temp.h"
  23. /* 任务参数 */
  24. #define APP_PIDCONTROL_TASK_PRIORITY 2
  25. #define APP_PIDCONTROL_TASK_STACK 512
  26. /* 控制周期 (ms) */
  27. #define APP_PIDCONTROL_PERIOD_MS 500
  28. TaskHandle_t xPidControlTaskHandle = NULL;
  29. /* 闭环运行标志 (仅 preheat 任务写, 本任务读) */
  30. static volatile uint8_t s_running = 0;
  31. /**
  32. * @brief PID 控温任务体
  33. * @note s_running=0 时仅休眠 (预热期间输出由 preheat 任务掌控)
  34. * @param argument : 任务入参 (未使用)
  35. * @return 无 (任务不可返回)
  36. */
  37. static void Task_PidControl(void *argument)
  38. {
  39. (void)argument;
  40. uint8_t i;
  41. uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
  42. uint16_t flen;
  43. LOG_PRINTF("[INFO] PID: task running\r\n");
  44. while (1)
  45. {
  46. if (s_running)
  47. {
  48. int16_t sensor4[MID_TEMP_PREHEAT_NUM];
  49. uint8_t abort = 0;
  50. /* 批量写输出缓冲 + 封装 SET_ALL 帧须在同一临界区,
  51. * 防止高优先级 CPLD 任务抢占读取到写了一半的缓冲 */
  52. Mid_PID_LockOutput();
  53. /* 24 路 PTC 闭环: 过程值 = BMC 24 路温度;
  54. * 每路写前复查 s_running, 使 App_pidControl_Stop() 置位后
  55. * 能立即放弃本轮剩余写入, 避免残留占空比被下发 */
  56. for (i = 0; i < PID_CHANNEL_MAX; i++)
  57. {
  58. int16_t pv;
  59. float out;
  60. if (!s_running)
  61. {
  62. abort = 1;
  63. break;
  64. }
  65. pv = Mid_Temp_GetBmcTemp(i);
  66. if (pv < -90)
  67. {
  68. out = 0.0f; /* 传感器无效, 本路不加热 */
  69. }
  70. else
  71. {
  72. out = Mid_PID_Calc(i, (float)pv);
  73. }
  74. Mid_PID_HeaterControl(i, out);
  75. }
  76. if (!abort)
  77. {
  78. /* 风扇: 按分区对应的 4 路温度分档 */
  79. for (i = 0; i < MID_TEMP_PREHEAT_NUM; i++)
  80. {
  81. sensor4[i] = Mid_Temp_GetPreheat(i);
  82. }
  83. Mid_PID_FanZoneUpdate(sensor4);
  84. flen = Com_cpld_set_all(Mid_PID_GetOutputDuty(), frame);
  85. Mid_PID_UnlockOutput();
  86. if (flen > 0)
  87. {
  88. Com_cpld_usart_send(frame, flen);
  89. }
  90. }
  91. else
  92. {
  93. Mid_PID_UnlockOutput();
  94. }
  95. }
  96. vTaskDelay(pdMS_TO_TICKS(APP_PIDCONTROL_PERIOD_MS));
  97. }
  98. }
  99. void App_pidControl_Start(uint8_t target)
  100. {
  101. uint8_t i;
  102. for (i = 0; i < PID_CHANNEL_MAX; i++)
  103. {
  104. Mid_PID_SetPoint(i, (float)target);
  105. Mid_PID_Enable(i, 0); /* 清积分与历史误差, 防预加热满功率积分饱和 */
  106. Mid_PID_Enable(i, 1);
  107. }
  108. s_running = 1;
  109. LOG_PRINTF("[INFO] PID: start target=%d\r\n", (int)target);
  110. }
  111. void App_pidControl_Stop(void)
  112. {
  113. uint8_t i;
  114. uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
  115. uint16_t flen;
  116. s_running = 0;
  117. Mid_PID_LockOutput();
  118. for (i = 0; i < PID_CHANNEL_MAX; i++)
  119. {
  120. Mid_PID_HeaterControl(i, 0.0f);
  121. }
  122. for (i = 0; i < 8; i++)
  123. {
  124. Mid_PID_FanSetDuty(i, 0);
  125. }
  126. flen = Com_cpld_set_all(Mid_PID_GetOutputDuty(), frame);
  127. Mid_PID_UnlockOutput();
  128. if (flen > 0)
  129. {
  130. Com_cpld_usart_send(frame, flen);
  131. }
  132. LOG_PRINTF("[INFO] PID: stop\r\n");
  133. }
  134. uint8_t App_pidControl_is_running(void)
  135. {
  136. return (s_running != 0) ? 1 : 0;
  137. }
  138. /**
  139. * @brief 创建 PID 控温任务
  140. * @note 在 vTaskStartScheduler() 之前调用一次; 句柄存于 xPidControlTaskHandle。
  141. * @param 无
  142. * @return 无
  143. */
  144. void App_pidControl_task_create(void)
  145. {
  146. BaseType_t ret;
  147. ret = xTaskCreate(Task_PidControl, "pidCtrlTask", APP_PIDCONTROL_TASK_STACK,
  148. NULL, APP_PIDCONTROL_TASK_PRIORITY, &xPidControlTaskHandle);
  149. if (ret != pdPASS)
  150. {
  151. LOG_PRINTF("[ERR] PID: task create failed\r\n");
  152. }
  153. }