App_pidControl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 将输出缓冲封装 SET_ALL 帧并立即下发
  32. * @param 无
  33. * @return 无
  34. */
  35. static void pid_send_set_all(void)
  36. {
  37. uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
  38. uint16_t flen;
  39. flen = Com_cpld_set_all(Mid_PID_GetOutputDuty(), frame);
  40. if (flen > 0)
  41. {
  42. Com_cpld_usart_send(frame, flen);
  43. }
  44. }
  45. /**
  46. * @brief PID 控温任务体
  47. * @note s_running=0 时仅休眠 (预热期间输出由 preheat 任务掌控)
  48. * @param argument : 任务入参 (未使用)
  49. * @return 无 (任务不可返回)
  50. */
  51. static void Task_PidControl(void *argument)
  52. {
  53. (void)argument;
  54. uint8_t i;
  55. printf("PID control task running...\r\n");
  56. while (1)
  57. {
  58. if (s_running)
  59. {
  60. int16_t sensor4[MID_TEMP_PREHEAT_NUM];
  61. /* 24 路 PTC 闭环: 过程值 = BMC 24 路温度 */
  62. for (i = 0; i < PID_CHANNEL_MAX; i++)
  63. {
  64. int16_t pv = Mid_Temp_GetBmcTemp(i);
  65. float out;
  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. /* 风扇: 按分区对应的 4 路温度分档 */
  77. for (i = 0; i < MID_TEMP_PREHEAT_NUM; i++)
  78. {
  79. sensor4[i] = Mid_Temp_GetPreheat(i);
  80. }
  81. Mid_PID_FanZoneUpdate(sensor4);
  82. pid_send_set_all();
  83. }
  84. vTaskDelay(pdMS_TO_TICKS(APP_PIDCONTROL_PERIOD_MS));
  85. }
  86. }
  87. void App_pidControl_Start(uint8_t target)
  88. {
  89. uint8_t i;
  90. for (i = 0; i < PID_CHANNEL_MAX; i++)
  91. {
  92. Mid_PID_SetPoint(i, (float)target);
  93. Mid_PID_Enable(i, 0); /* 清积分与历史误差, 防预加热满功率积分饱和 */
  94. Mid_PID_Enable(i, 1);
  95. }
  96. s_running = 1;
  97. printf("[PID] start target=%d\r\n", (int)target);
  98. }
  99. void App_pidControl_Stop(void)
  100. {
  101. uint8_t i;
  102. s_running = 0;
  103. for (i = 0; i < PID_CHANNEL_MAX; i++)
  104. {
  105. Mid_PID_HeaterControl(i, 0.0f);
  106. }
  107. for (i = 0; i < 8; i++)
  108. {
  109. Mid_PID_FanSetDuty(i, 0);
  110. }
  111. pid_send_set_all();
  112. printf("[PID] stop\r\n");
  113. }
  114. uint8_t App_pidControl_is_running(void)
  115. {
  116. return (s_running != 0) ? 1 : 0;
  117. }
  118. /**
  119. * @brief 创建 PID 控温任务
  120. * @note 在 vTaskStartScheduler() 之前调用一次; 句柄存于 xPidControlTaskHandle。
  121. * @param 无
  122. * @return 无
  123. */
  124. void App_pidControl_task_create(void)
  125. {
  126. BaseType_t ret;
  127. ret = xTaskCreate(Task_PidControl, "pidCtrlTask", APP_PIDCONTROL_TASK_STACK,
  128. NULL, APP_PIDCONTROL_TASK_PRIORITY, &xPidControlTaskHandle);
  129. if (ret != pdPASS)
  130. {
  131. printf("[ERR] PID control task create failed\r\n");
  132. }
  133. }