my new gear...

Dependencies:   mbed

control_theory/pid.cpp

Committer:
yootee
Date:
2022-03-27
Revision:
2:e7b09385d197
Parent:
pid.cpp@ 0:1456b6f84c75
Child:
11:20418879650b

File content as of revision 2:e7b09385d197:

#include<pid.hpp>

Pid::Pid(double get_Kp,double get_Ki,double get_Kd,int plumi){
    setGain(get_Kp,get_Ki,get_Kd);
    e_o = 0;
    e_c = 0;
    plusminus=plumi;
}

double Pid::returnVal(double get_target,double input_val,double time){
    target = get_target;
    e_c = target - input_val;
    Give_P = Kp * e_c;
    Give_I=Ki*((e_o+e_c)/2.0/time);
    Give_D=Kd*(e_c-e_o)*time;
    e_o = e_c;
    Operation_amount = plusminus * Operation_amount + Give_P+Give_I-Give_D;
    if(Operation_amount > max_val){
        Operation_amount = max_val;
    }else if(Operation_amount < minimum_val){
        Operation_amount = minimum_val;
    }
    if(target == input_val){
        Operation_amount = 0;
    }
    return Operation_amount;
}

void Pid::setGain(double get_Kp,double get_Ki,double get_Kd){
    Kp = get_Kp;
    Ki = get_Ki;
    Kd = get_Kd;
}

void Pid::setMax(double get_max,double get_min){
    max_val = get_max;
    minimum_val = get_min;    
}