Mid_pid.c 12 KB

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