[综合信息] PID的算法实现其他 挺简单的

[复制链接]
2355|13
 楼主| zhuotuzi 发表于 2025-1-24 17:55 | 显示全部楼层 |阅读模式
pi, pid, TE, ST, TI
  1. /*
  2. * PID Controller Implementation in C
  3. *
  4. * Created by Joshua Saxby (aka @saxbophone) on 1 Jan, 2016
  5. *
  6. * My own attempt at implementing the PID algorithm in some (hopefully) clean, understandable C.
  7. *
  8. * See LICENSE for licensing details.
  9. */

  10. // protection against multiple includes
  11. #ifndef SAXBOPHONE_PID_H
  12. #define SAXBOPHONE_PID_H

  13. #ifdef __cplusplus
  14. extern "C"{
  15. #endif


  16.     typedef struct pid_calibration {
  17.         /*
  18.          * struct PID_Calibration
  19.          *
  20.          * Struct storing calibrated PID constants for a PID Controller
  21.          * These are used for tuning the algorithm and the values they take are
  22.          * dependent upon the application - (in other words, YMMV...)
  23.          */
  24.         double kp; // Proportional gain
  25.         double ki; // Integral gain
  26.         double kd; // Derivative gain
  27.     } PID_Calibration;


  28.     typedef struct pid_state {
  29.         /*
  30.          * struct PID_State
  31.          *
  32.          * Struct storing the current state of a PID Controller.
  33.          * This is used as the input value to the PID algorithm function, which also
  34.          * returns a PID_State struct reflecting the adjustments suggested by the algorithm.
  35.          *
  36.          * NOTE: The output field in this struct is set by the PID algorithm function, and
  37.          * is ignored in the actual calculations.
  38.          */
  39.         double actual; // The actual reading as measured
  40.         double target; // The desired reading
  41.         double time_delta; // Time since last sample/calculation - should be set when updating state
  42.         // The previously calculated error between actual and target (zero initially)
  43.         double previous_error;
  44.         double integral; // Sum of integral error over time
  45.         double output; // the modified output value calculated by the algorithm, to compensate for error
  46.     } PID_State;


  47.     /*
  48.      * PID Controller Algorithm implementation
  49.      *
  50.      * Given a PID calibration for the P, I and D values and a PID_State for the current
  51.      * state of the PID controller, calculate the new state for the PID Controller and set
  52.      * the output state to compensate for any error as defined by the algorithm
  53.      */
  54.     PID_State pid_iterate(PID_Calibration calibration, PID_State state);


  55. #ifdef __cplusplus
  56. } // extern "C"
  57. #endif

  58. // end of header
  59. #endif
  1. /*
  2. * PID Controller Implementation in C
  3. *
  4. * Created by Joshua Saxby (aka @saxbophone) on 1 Jan, 2016
  5. *
  6. * My own attempt at implementing the PID algorithm in some (hopefully) clean, understandable C.
  7. *
  8. * See LICENSE for licensing details.
  9. */

  10. #include "pid.h"


  11. PID_State pid_iterate(PID_Calibration calibration, PID_State state) {
  12.     // calculate difference between desired and actual values (the error)
  13.     double error = state.target - state.actual;
  14.     // calculate and update integral
  15.     state.integral += (error * state.time_delta);
  16.     // calculate derivative
  17.     double derivative = (error - state.previous_error) / state.time_delta;
  18.     // calculate output value according to algorithm
  19.     state.output = (
  20.         (calibration.kp * error) + (calibration.ki * state.integral) + (calibration.kd * derivative)
  21.     );
  22.     // update state.previous_error to the error value calculated on this iteration
  23.     state.previous_error = error;
  24.     // return the state struct reflecting the calculations
  25.     return state;
  26. }


ex7s4 发表于 2025-3-18 14:53 | 显示全部楼层
所以,您实现了什么
l1uyn9b 发表于 2025-3-18 16:10 | 显示全部楼层
PID确实不复杂,就是调参的时候要费劲儿点
kaif2n9j 发表于 2025-3-18 17:29 | 显示全部楼层
一般有点经验的话,PID其实还可以的,不是很麻烦
liu96jp 发表于 2025-3-18 18:33 | 显示全部楼层
当然了,PID是比较简单的算法之一了
suw12q 发表于 2025-3-18 19:45 | 显示全部楼层
一般调试电机控制的时候,用PID确实方便
p0gon9y 发表于 2025-3-18 19:50 | 显示全部楼层
话说,这PID的应用起来,占用多少flash,是不是不多
lix1yr 发表于 2025-3-19 08:24 | 显示全部楼层
一般的话是不是M0也可以用PID操作啊
q1ngt12 发表于 2025-3-19 10:03 | 显示全部楼层
看代码PID好简单啊,是不是调参也很简单呢?
t1ngus4 发表于 2025-3-19 11:28 | 显示全部楼层
楼主PID能以工程打包上来吗,我也想学习学习
g0d5xs 发表于 2025-3-19 12:41 | 显示全部楼层
PID还是不错的存在,比较常用而且方便
狗啃模拟 发表于 2025-4-23 14:28 | 显示全部楼层
每次PID更新之间的时间差必须准确计算,尤其是当控制循环的时间间隔不固定时。
Amazingxixixi 发表于 2025-4-24 14:51 | 显示全部楼层
学习一下啊!感觉有点知识慌了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

214

主题

3375

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部