FERSAT-ZAD7-PID_Refactoring/Code/main/pid_reg.cpp
float PID_Regulate(PID_Handle_t *hpid, float error) {
    /* transform PID coeff from Parralel to Standard form. See documentation */
    static const float Kr = hpid->Kp;
    static const float Ti = Kr / hpid->Ki;
    static const float Td = hpid->Kd / Kr;
    static const float T  = hpid->Ts;
    static const float v  = hpid->V;

    /* e(k) = error */
    hpid->e0 = error;

    /* up(k)  = Kr *   e(k) */
    hpid->up0 = Kr * hpid->e0;

    /* ui(k)  = ui(k-1)   + Kr * T / Ti * (  e(k)   + e(k-1)  ) / 2.0f */
    hpid->ui0 = hpid->ui1 + Kr * T / Ti * (hpid->e0 + hpid->e1) / 2.0f;

    /* ud(k)  = Td / (Td + v * T) * ud(k-1)   + Kr * v * Td / (Td + v * T) * (  e(k)   - e(k-1)  ) */
    hpid->ud0 = Td / (Td + v * T) * hpid->ud1 + Kr * v * Td / (Td + v * T) * (hpid->e0 - hpid->e1);

    /* u(k)  =    up(k)  +    ui(k)  +    ud(k) */
    hpid->u0 = hpid->up0 + hpid->ui0 + hpid->ud0; 

    /* u(k-1) = u(k) */
    /* e(k-1) = e(k) */
    hpid->ui1 = hpid->ui0;
    hpid->ud1 = hpid->ud0;
    hpid->e1  = hpid->e0;

    /* Return regulator output value */
    return hpid->u0;    
}
/**
 * Function for the PID regulator output.
 * 
 * PID_Handle_t *hpid - PID regulator handle
 * float error - defined as Set_point_value minus Measured_value
 * 
 * return float - Output of the PID regulator
 * 
 * */
Function for the PID regulator output.

PID_Handle_t *hpid - PID regulator handle
float error - defined as Set_point_value minus Measured_value

return float - Output of the PID regulator
Function for the PID regulator output . PID_Handle_t * hpid - PID regulator handle float error - defined as Set_point_value minus Measured_value return float - Output of the PID regulator
float PID_Regulate(PID_Handle_t *hpid, float error) {
    static const float Kr = hpid->Kp;
    static const float Ti = Kr / hpid->Ki;
    static const float Td = hpid->Kd / Kr;
    static const float T  = hpid->Ts;
    static const float v  = hpid->V;
    hpid->e0 = error;
    hpid->up0 = Kr * hpid->e0;
    hpid->ui0 = hpid->ui1 + Kr * T / Ti * (hpid->e0 + hpid->e1) / 2.0f;
    hpid->ud0 = Td / (Td + v * T) * hpid->ud1 + Kr * v * Td / (Td + v * T) * (hpid->e0 - hpid->e1);
    hpid->u0 = hpid->up0 + hpid->ui0 + hpid->ud0; 
    hpid->ui1 = hpid->ui0;
    hpid->ud1 = hpid->ud0;
    hpid->e1  = hpid->e0;
    return hpid->u0;    
}
float PID_Regulate ( PID_Handle_t * hpid , float error ) { static const float Kr = hpid -> Kp ; static const float Ti = Kr / hpid -> Ki ; static const float Td = hpid -> Kd / Kr ; static const float T = hpid -> Ts ; static const float v = hpid -> V ; hpid -> e0 = error ; hpid -> up0 = Kr * hpid -> e0 ; hpid -> ui0 = hpid -> ui1 + Kr * T / Ti * ( hpid -> e0 + hpid -> e1 ) / 2.0f ; hpid -> ud0 = Td / ( Td + v * T ) * hpid -> ud1 + Kr * v * Td / ( Td + v * T ) * ( hpid -> e0 - hpid -> e1 ) ; hpid -> u0 = hpid -> up0 + hpid -> ui0 + hpid -> ud0 ; hpid -> ui1 = hpid -> ui0 ; hpid -> ud1 = hpid -> ud0 ; hpid -> e1 = hpid -> e0 ; return hpid -> u0 ; }
Function for the PID regulator output.
Function for the PID regulator output .
/* transform PID coeff from Parralel to Standard form. See documentation */ /* e(k) = error */ /* up(k)  = Kr *   e(k) */ /* ui(k)  = ui(k-1)   + Kr * T / Ti * (  e(k)   + e(k-1)  ) / 2.0f */ /* ud(k)  = Td / (Td + v * T) * ud(k-1)   + Kr * v * Td / (Td + v * T) * (  e(k)   - e(k-1)  ) */ /* u(k)  =    up(k)  +    ui(k)  +    ud(k) */ /* u(k-1) = u(k) */ /* e(k-1) = e(k) */ /* Return regulator output value */