App_pidControl.c 4.9 KB

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