This program makes a PID control and the values por control action are entered by matrix keypad 4x4 interface.
Dependencies: FPointer TextLCD keypad mbed
main.cpp
- Committer:
- lcorralesc1
- Date:
- 2013-12-15
- Revision:
- 0:54e8dab91b77
File content as of revision 0:54e8dab91b77:
//Programa para hacer control PID simple, ingresa parámetros con teclado 4x4. Imprime resultados en LCD 16x2 #include "mbed.h" #include "TextLCD.h" #include"keypad.h" //Librería del teclado 4x4 #include"FPointer.h" //Librería complementaria para libreria keypad TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 Keypad keypad(PTA2,PTD4,PTD3,PTD7,PTA13,PTD5,PTD0,PTD2); //Entradas del teclado AnalogIn Vin(PTB0); //Voltaje de alimentación AnalogOut Vout(PTE30); //Marcar la salida analógica //ASIGNACION DE VARIABLES Timer t; int cero; int flag; int q; int k; int C1=0x0E; //configurar el lcd para mostrar el guin bajo int C2=0x0C; //configurar el lcd para QUITAR el guion bajo float sp=0; //set point float kp=0; //ganancia proporcional float ki=0; //ganancia integral float kd=0; //ganancia derivativa int ind=0; //vector de caracteres. float err, med, yr, ap, ai, ad, err_v, cycle; //Variable de control PID float pid; //MATRIZ DEL TECLADO float Keytable[] = {1,2,3,11, 4,5,6,12, 7,8,9,13, 0,0,0,0}; //Se crea una función que incremente los valores void increment(int j){ if(j==0){ if (q!=1 && sp<10){ sp=ind; q=1; } else{ sp=10*sp+ind; } if(sp>999)sp=999; lcd.locate(3,0); lcd.printf(" "); lcd.locate(3,0); lcd.printf("%.0f",sp); } else if(j==1){ if (q!=1 && kp<10){ kp=ind; q=1; } else{ kp=10*kp+ind; } if(kp>999)kp=999; lcd.locate(11,0); lcd.printf(" "); lcd.locate(11,0); lcd.printf("%.0f",kp); } else if(j==2){ if (q!=1 && ki<10){ ki=ind; q=1; } else{ ki=10*ki+ind; } if(ki>999)ki=999; lcd.locate(3,1); lcd.printf(" "); lcd.locate(3,1); lcd.printf("%.0f",ki); } else{ if (q!=1 && kd<10){ kd=ind; q=1; } else{ kd=10*kd+ind; } if(kd>999)kd=999; lcd.locate(11,1); lcd.printf(" "); lcd.locate(11,1); lcd.printf("%.0f",kd); } ind=0; cero=0; } uint32_t cbAfterInput(uint32_t index) { ind=Keytable[index]; cero=index; return 0; } void def_posicion(int j){ if (j==0){ lcd.locate(3,0); lcd.printf("%.0f",sp); lcd.locate(3,0); } else if(j==1){ lcd.locate(11,0); lcd.printf("%.0f",kp); lcd.locate(11,0); } else if (j==2){ lcd.locate(3,1); lcd.printf("%.0f",ki); lcd.locate(3,1); } else { lcd.locate(11,1); lcd.printf("%.0f",kd); lcd.locate(11,1); } } //Dado que hay parámetros que no varía en el display en esta parte del código, se crea una función que los mantenga. void star_patch1(void){ lcd.cls(); lcd.locate(8,0); lcd.printf("Kp=%.0f",kp); lcd.locate(0,1); lcd.printf("Ki=%.0f",ki); lcd.locate(8,1); lcd.printf("Kd=%.0f",kd); lcd.writeCommand(C1); //cursor se vea y sea intermitente lcd.locate(0,0); lcd.printf("Sp=%.0f",sp); } void star_patch2(void){ // uso nuevamente función que imprime los caracteres que no van a variar en el display lcd.writeCommand(C2); lcd.cls(); lcd.printf("Iniciamos el PID |m|"); wait(2); lcd.cls(); lcd.printf("Er%=f",err); lcd.locate(8,0); lcd.printf("Me=%.0f",med); lcd.locate(0,1); lcd.printf("Sp=%.0f",sp); lcd.locate(8,1); lcd.printf("Co=%.0f",pid); wait(3); } int main(){ ini: ind=0; star_patch1(); keypad.CallAfterInput(&cbAfterInput); keypad.Start(); ini1: if(ind==12){ if (k<3) k++; else k=0; def_posicion(k); ind=0; q=0; } if(ind==13){ ind=0; goto PID; } if(ind==11){ if (k==0){ sp=0; lcd.locate(3,0); lcd.printf(" "); lcd.locate(3,0); lcd.printf("%.0f",sp); } else if(k==1){ kp=0; lcd.locate(11,0); lcd.printf(" "); lcd.locate(11,0); lcd.printf("%.0f",kp); } else if (k==2){ ki=0; lcd.locate(3,1); lcd.printf(" "); lcd.locate(3,1); lcd.printf("%.0f",ki); } else { kd=0; lcd.locate(11,1); lcd.printf(" "); lcd.locate(11,1); lcd.printf("%.0f",kd); } q=0; ind=0; } if (ind!=0 && ind!=12 && ind!=13 && ind!=11 || cero==13 ){ increment(k); } goto ini1; PID: star_patch2(); while (1){ med=Vin.read()*1000; err =sp-med; ap = kp*err; ai =(ki*0.001*err)+ai; //calculo de la integral del error // VERIFICAMOS QUE LA ACCION INTEGRAL NO SEA MUY GRANDE!!! if(ai>0.5){ ai=0.5; } ad = kd*(err-err_v); //calculo de la accion derivativa pid =(ap+ai+ad); //MOSTRAMOS LAS VARIABLES if (pid > 999)pid=1000; if (pid<0)pid=0; Vout.write(pid/1000); if(flag==0){ t.start(); flag=1; } if (t>0.3){ lcd.locate(3,0); lcd.printf(" "); lcd.locate(3,0); lcd.printf("%.0f",err); lcd.locate(11,0); lcd.printf(" "); lcd.locate(11,0); lcd.printf("%.0f",med); lcd.locate(3,1); lcd.printf(" "); lcd.locate(3,1); lcd.printf("%.0f",sp); lcd.locate(11,1); lcd.printf(" "); lcd.locate(11,1); lcd.printf("%.2f ",pid/1000); t.reset(); flag=0; wait(.3); } //ACTUALIZACION DE LAS VARIABLES err_v = err; if(ind==13)goto ini; } }