打印
[综合信息]

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

[复制链接]
1533|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zhuotuzi|  楼主 | 2025-1-24 17:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
pi, pid, TE, ST, TI
/*
* 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其实还可以的,不是很麻烦

使用特权

评论回复
5
liu96jp| | 2025-3-18 18:33 | 只看该作者
当然了,PID是比较简单的算法之一了

使用特权

评论回复
6
suw12q| | 2025-3-18 19:45 | 只看该作者
一般调试电机控制的时候,用PID确实方便

使用特权

评论回复
7
p0gon9y| | 2025-3-18 19:50 | 只看该作者
话说,这PID的应用起来,占用多少flash,是不是不多

使用特权

评论回复
8
lix1yr| | 2025-3-19 08:24 | 只看该作者
一般的话是不是M0也可以用PID操作啊

使用特权

评论回复
9
q1ngt12| | 2025-3-19 10:03 | 只看该作者
看代码PID好简单啊,是不是调参也很简单呢?

使用特权

评论回复
10
t1ngus4| | 2025-3-19 11:28 | 只看该作者
楼主PID能以工程打包上来吗,我也想学习学习

使用特权

评论回复
11
g0d5xs| | 2025-3-19 12:41 | 只看该作者
PID还是不错的存在,比较常用而且方便

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

209

主题

3351

帖子

7

粉丝