zhuotuzi 发表于 2025-1-24 17:55

PID的算法实现其他 挺简单的

/*
* PID Controller Implementation in C
*
* Created by Joshua Saxby (aka @saxbophone) on 1 Jan, 2016
*
* My own attempt at implementing the PID algorithm in some (hopefully) clean, understandable C.
*
* See LICENSE for licensing details.
*/

// protection against multiple includes
#ifndef SAXBOPHONE_PID_H
#define SAXBOPHONE_PID_H

#ifdef __cplusplus
extern "C"{
#endif


    typedef struct pid_calibration {
      /*
         * struct PID_Calibration
         *
         * Struct storing calibrated PID constants for a PID Controller
         * These are used for tuning the algorithm and the values they take are
         * dependent upon the application - (in other words, YMMV...)
         */
      double kp; // Proportional gain
      double ki; // Integral gain
      double kd; // Derivative gain
    } PID_Calibration;


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


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


#ifdef __cplusplus
} // extern "C"
#endif

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

#include "pid.h"


PID_State pid_iterate(PID_Calibration calibration, PID_State state) {
    // calculate difference between desired and actual values (the error)
    double error = state.target - state.actual;
    // calculate and update integral
    state.integral += (error * state.time_delta);
    // calculate derivative
    double derivative = (error - state.previous_error) / state.time_delta;
    // calculate output value according to algorithm
    state.output = (
      (calibration.kp * error) + (calibration.ki * state.integral) + (calibration.kd * derivative)
    );
    // update state.previous_error to the error value calculated on this iteration
    state.previous_error = error;
    // return the state struct reflecting the calculations
    return state;
}


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

学习一下啊!感觉有点知识慌了
页: [1]
查看完整版本: PID的算法实现其他 挺简单的