Mid_pid.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef MID_PID_H
  2. #define MID_PID_H
  3. #include "stdint.h"
  4. /* PID通道数量定义 */
  5. #define PID_CHANNEL_MAX 24
  6. /* PID模式 */
  7. typedef enum {
  8. PID_MODE_NORMAL = 0, //正常模式
  9. PID_MODE_MANUAL, //手动模式
  10. PID_MODE_AUTO //自动模式未实现
  11. } pid_mode_t;
  12. /* PID输出限幅 */
  13. typedef struct {
  14. float output_min; //输出最小值
  15. float output_max; //输出最大值
  16. } pid_limit_t;
  17. /* PID增益结构 */
  18. typedef struct {
  19. float kp; //比例
  20. float ki; //积分
  21. float kd; //微分
  22. } pid_gain_t;
  23. /* PID计算结果 */
  24. typedef struct {
  25. float setpoint; //目标值
  26. float process_value; //测量值
  27. float output; //输出值
  28. float error; //当前误差值
  29. float prev_error; //上一次的误差
  30. float integral; //积分累加值
  31. } pid_calc_t;
  32. /* PID通道状态 */
  33. typedef struct {
  34. uint8_t ch; //通道号
  35. uint8_t enable; //使能标志(1运行、0停止)
  36. pid_mode_t mode; //工作模式
  37. pid_gain_t gain; //增益参数
  38. pid_limit_t limit; //输出限幅
  39. pid_calc_t calc; //计算相关
  40. float deadband; //死区范围
  41. float sample_time; //采样时间(ms 目前未使用)
  42. uint32_t last_update; //上次更新的时间戳(目前未使用)
  43. } pid_ch_t;
  44. /* PID全局配置 */
  45. typedef struct {
  46. uint8_t total_ch; //总通道数
  47. pid_ch_t channels[PID_CHANNEL_MAX]; //总定义
  48. } pid_handle_t; //句柄
  49. /* PID状态 */
  50. typedef enum {
  51. PID_STA_OK = 0, //操作成功
  52. PID_STA_ERR_PARAM, //参数错误
  53. PID_STA_ERR_CHANNEL, //通道号错误
  54. PID_STA_ERR_MODE //模式错误
  55. } pid_status_t;
  56. /* API */
  57. void Mid_PID_Init(void);
  58. void Mid_PID_DeInit(void);
  59. pid_status_t Mid_PID_SetChannel(uint8_t ch, pid_gain_t *gain, pid_limit_t *limit);
  60. pid_status_t Mid_PID_GetChannel(uint8_t ch, pid_ch_t *info);
  61. pid_status_t Mid_PID_SetPoint(uint8_t ch, float setpoint);
  62. pid_status_t Mid_PID_SetMode(uint8_t ch, pid_mode_t mode);
  63. pid_status_t Mid_PID_SetManualOutput(uint8_t ch, float output);
  64. float Mid_PID_Calc(uint8_t ch, float pv);
  65. void Mid_PID_UpdateAll(void);
  66. pid_status_t Mid_PID_Enable(uint8_t ch, uint8_t enable);
  67. uint8_t Mid_PID_IsEnabled(uint8_t ch);
  68. /* 执行器控制 */
  69. void Mid_PID_HeaterControl(uint8_t ch, float output);
  70. void Mid_PID_FanControl(uint8_t ch, float temp);
  71. #endif