Skip to content

Instantly share code, notes, and snippets.

@joaoantoniocardoso
Last active November 24, 2019 01:50
Show Gist options
  • Save joaoantoniocardoso/2d6da69f355be98692b0960673128d57 to your computer and use it in GitHub Desktop.
Save joaoantoniocardoso/2d6da69f355be98692b0960673128d57 to your computer and use it in GitHub Desktop.
PI CONTROL ALGORITHM - SERIES IMPLEMENTATION
/* PI CONTROL ALGORITHM - SERIES IMPLEMENTATION
** /desc Algoritimo para Controlador Proporcional Integrativo Diferencial.
** ref1: https://e2e.ti.com/cfs-file/__key/communityserver-discussions-components-files/902/PI-controller-equations.pdf
** /var r é o valor desejado para a saída, o 'set-point'.
** /var y é o valor da saída.
** /ret retorna a ação de controle u.
*/
float pi(float r, float y){
// PI CONFIGURATIONS:
const float Kp = 0.08*0.8; // analog series proportional gain
const float Ti = 5e-3; // analog series integration period
const float Ts = PERIOD; // digital sampling period
// INTERNAL CONSTANTS COMPUTATION:
const float a0 = -Kp; // IIR coefficient for old sample
const float a1 = Kp*(1+Ts/Ti); // IIR coefficient for new sample
// CONTROLLER STATIC VARIABLES
static float e0 = 0; // old error
static float e1 = 0; // new error
static float u = 0; // control action
// Compute error:
e0 = e1;
e1 = r -y;
// Compute control action:
u += + a1*e1 + a0*e0;
// Anti windup
//if(u < D_MIN) u = D_MIN;
//else if(u > D_MAX) u = D_MAX;
return u;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment