pid_ctrl.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_err.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * @brief PID calculation type
  13. *
  14. */
  15. typedef enum {
  16. PID_CAL_TYPE_INCREMENTAL, /*!< Incremental PID control */
  17. PID_CAL_TYPE_POSITIONAL, /*!< Positional PID control */
  18. } pid_calculate_type_t;
  19. /**
  20. * @brief Type of PID control block handle
  21. *
  22. */
  23. typedef struct pid_ctrl_block_t *pid_ctrl_block_handle_t;
  24. /**
  25. * @brief PID control parameters
  26. *
  27. */
  28. typedef struct {
  29. float kp; // PID Kp parameter
  30. float ki; // PID Ki parameter
  31. float kd; // PID Kd parameter
  32. float max_output; // PID maximum output limitation
  33. float min_output; // PID minimum output limitation
  34. float max_integral; // PID maximum integral value limitation
  35. float min_integral; // PID minimum integral value limitation
  36. pid_calculate_type_t cal_type; // PID calculation type
  37. } pid_ctrl_parameter_t;
  38. /**
  39. * @brief PID control configuration
  40. *
  41. */
  42. typedef struct {
  43. pid_ctrl_parameter_t init_param; // Initial parameters
  44. } pid_ctrl_config_t;
  45. /**
  46. * @brief Create a new PID control session, returns the handle of control block
  47. *
  48. * @param[in] config PID control configuration
  49. * @param[out] ret_pid Returned PID control block handle
  50. * @return
  51. * - ESP_OK: Created PID control block successfully
  52. * - ESP_ERR_INVALID_ARG: Created PID control block failed because of invalid argument
  53. * - ESP_ERR_NO_MEM: Created PID control block failed because out of memory
  54. */
  55. esp_err_t pid_new_control_block(const pid_ctrl_config_t *config, pid_ctrl_block_handle_t *ret_pid);
  56. /**
  57. * @brief Delete the PID control block
  58. *
  59. * @param[in] pid PID control block handle, created by `pid_new_control_block()`
  60. * @return
  61. * - ESP_OK: Delete PID control block successfully
  62. * - ESP_ERR_INVALID_ARG: Delete PID control block failed because of invalid argument
  63. */
  64. esp_err_t pid_del_control_block(pid_ctrl_block_handle_t pid);
  65. /**
  66. * @brief Update PID parameters
  67. *
  68. * @param[in] pid PID control block handle, created by `pid_new_control_block()`
  69. * @param[in] params PID parameters
  70. * @return
  71. * - ESP_OK: Update PID parameters successfully
  72. * - ESP_ERR_INVALID_ARG: Update PID parameters failed because of invalid argument
  73. */
  74. esp_err_t pid_update_parameters(pid_ctrl_block_handle_t pid, const pid_ctrl_parameter_t *params);
  75. /**
  76. * @brief Input error and get PID control result
  77. *
  78. * @param[in] pid PID control block handle, created by `pid_new_control_block()`
  79. * @param[in] input_error error data that feed to the PID controller
  80. * @param[out] ret_result result after PID calculation
  81. * @return
  82. * - ESP_OK: Run a PID compute successfully
  83. * - ESP_ERR_INVALID_ARG: Run a PID compute failed because of invalid argument
  84. */
  85. esp_err_t pid_compute(pid_ctrl_block_handle_t pid, float input_error, float *ret_result);
  86. #ifdef __cplusplus
  87. }
  88. #endif