增量式PID控制C++实现

使用了积分分离的增量式pid控制
P:
D:使结果趋于稳定,相当于加了一个阻尼项,减少输出的变化速度
I:消除系统偏差,消除稳态误差

header

#include <iostream>
#include <cmath>
#include <libconfig.h++>
using namespace libconfig;

typedef struct _pid{
    float target;
    float actual;
    float curr_err;
    float err_last1;
    float err_last2;
    float err_integral;
    float Kp, Ki, Kd;
    float control_value;
    int Ki_detach_flag;
    int Ki_detach_threshould;
}Pid;

class Pid_control{
    public:
    void PID_init();
    float PID_control(float target_value, float actual_value);
    void show_PID_params();
    Pid pid;
    private:


};

src

void  Pid_control::PID_init(){
    pid.target = 0.0;
    pid.actual = 0.0;

    pid.curr_err = 0.0;
    pid.err_last1 = 0.0;
    pid.err_last2 = 0.0;

    pid.err_integral = 0.0;
    pid.control_value = 0.0;

    pid.Kp = 0;
    pid.Ki = 0;
    pid.Kd = 0;

    pid.Ki_detach_flag = 1;
    pid.Ki_detach_threshould = 1;
}
 
float Pid_control::PID_control(float target_value, float actual_value){

    pid.target = target_value;
    pid.actual = actual_value;

    float err_curr_tmp = pid.curr_err;
    float err_last1_tmp = pid.err_last1;
    //error
    pid.curr_err = pid.target - pid.actual;
    
    if (abs(pid.curr_err) > pid.Ki_detach_threshould){
        pid.Ki_detach_flag = 0;
        pid.err_integral = 0;
        pid.err_integral += pid.curr_err;
        pid.err_integral += pid.err_last1;
        pid.err_integral += pid.err_last2;

    }
    else{
        pid.Ki_detach_flag = 1;
        pid.err_integral = 0;
        pid.err_integral += pid.curr_err;
        pid.err_integral += pid.err_last1;
        pid.err_integral += pid.err_last2;
    }

    pid.control_value = pid.Kp*pid.curr_err + pid.Ki_detach_flag*pid.Ki*pid.err_integral + pid.Kd*(pid.curr_err - 2*pid.err_last1 + pid.err_last2);

    pid.err_last1 = err_curr_tmp;
    pid.err_last2 = err_last1_tmp;

    return pid.control_value;
}
void Pid_control::show_PID_params(){
    cout << "--------------pid parameters-------------------" << endl;
    cout << "pid.target:" << pid.target << endl;
    cout << "pid.actual:" << pid.actual << endl;
    cout << "pid.curr_err:" << pid.curr_err << endl;
    cout << "pid.err_last1:" << pid.err_last1 << endl;
    cout << "pid.err_last2:" << pid.err_last2 << endl;
    cout << "pid.err_integral:" << pid.err_integral << endl;
    cout << "pid.control_value:" << pid.control_value << endl;
    cout << "pid.Kp:" << pid.Kp << endl;
    cout << "pid.Ki:" << pid.Ki << endl;
    cout << "pid.Kd:" << pid.Kd << endl;
    cout << "pid.Ki_detach_flag:" << pid.Ki_detach_flag << endl;
    cout << "pid.Ki_detach_threshould:" << pid.Ki_detach_threshould << endl;
    cout << "----------------------------------------------" << endl << endl;
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值