![](/media/cache/profiles/53015ad3b6bbbf80a4d107e6ef603b1e.jpg.50x50_q85.jpg)
pid y telado matricial en una plante de primer orden
Dependencies: FPointer TextLCD keypad mbed
Diff: main.cpp
- Revision:
- 0:2003274a0cbb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 04 03:27:20 2013 +0000 @@ -0,0 +1,251 @@ +#include "mbed.h" + #include "keypad.h" + #include "TextLCD.h" + + DigitalOut Led(LED2); + TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 + AnalogIn Vin(PTB3); + AnalogOut Vout(PTE30); + //Serial pc(USBTX, USBRX); + int C1=0x0E; // solo muestra el curzor + int C3=0x1A; // desplaza derecha + int a,num=0, i=0 , bt,f=0; + int j; +int Kp, Ki, Kd, Sp, yr, cycle, med2 , c; +float med,pid,ap, err, ai, ad,err_v; + // Define your own keypad values + char Keytable[] = { '1', '2', '3', 'A', + '4', '5', '6', 'B', + '7', '8', '9', 'C', + '*', '0', '#', 'D' + }; + + int Keytablei[] = { 1, 2, 3, 10, + 4, 5, 6, 11, + 7, 8, 9, 12, + 13,0,14, 15 + }; + +uint32_t cbAfterInput(uint32_t key) +{ + if(bt==0) { + if(a<2) { + if(Keytable[key]=='A') { + bt=3; + a=3; + f=1; + } + + + switch(a) { + case 0: + num=num+Keytablei[key]*10; + break; + case 1: + num=num+Keytablei[key]*1; + break; + } + + + + a++; + } + + } + + switch(i) { + case 0: + //a=0; + lcd.locate(2,0); + lcd.printf("= "); + lcd.locate(3,0); + lcd.printf("%d",num); + Sp=num; + //num=0; + //lcd.printf("%d",num); + + break; + case 1: + lcd.locate(10,0); + lcd.printf("= "); + lcd.locate(11,0); + lcd.printf("%d",num); + Kp=num; + //num=0; + //lcd.printf("%d",num); + break; + case 2: + + lcd.locate(2,1); + lcd.printf("= "); + lcd.locate(3,1); + lcd.printf("%d",num); + Kd=num; + //num=0; + //lcd.printf("%d",num); + break; + case 3: + lcd.locate(10,1); + lcd.printf("= "); + lcd.locate(11,1); + lcd.printf("%d",num); + Ki=num; + // num=0; + //lcd.printf("%d",++Kd); + break; + } + + if(f==1) { + a=0; + f=0; + } + if(Keytable[key]=='A') { + bt=3; + a=0; + num=0; + } + if(Keytable[key]=='D') + bt=4; + return 0; +} + + int main() { + Keypad keypad(PTA2,PTD4,PTD3,PTD1,PTD2,PTD0,PTD5,PTA13); + keypad.CallAfterInput(&cbAfterInput); + keypad.Start(); + + lcd.writeCommand(C1); + lcd.printf("Sp= Kp="); + lcd.locate(0,1); + lcd.printf("Ki= Kd="); + lcd.locate(2,0); + lcd.printf("="); + + + while (1) { + + + if(bt==3) { + i++; + if(i>3) { + i=0; + } + + switch (i) { + case 0: + lcd.locate(2,0); + lcd.printf("="); + break; + case 1: + lcd.locate(10,0); + lcd.printf("="); + break; + case 2: + lcd.locate(2,1); + lcd.printf("="); + break; + case 3: + lcd.locate(10,1); + lcd.printf("="); + break; + } + //wait(.1); + + bt=0; + } + + + if (bt==4){ + bt=0; + break; //sale del bucle si pisan suiche4 + } + + } + lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo + lcd.cls(); //borra la pantalla + lcd.printf(" GUARDADOS!"); + wait(2); + lcd.cls(); + lcd.printf(" INICIA EL PID"); + wait(2); + // se imprimen los parches del control ***************************************** + lcd.cls(); + + + lcd.printf("Er%d",err); + lcd.locate(8,0); + lcd.printf("Me%d",med2); + lcd.locate(0,1); + lcd.printf("Sp%d",Sp); + lcd.locate(8,1); + lcd.printf("Pid%d",pid); + //wait(5); + + // CICLO PRINCIPAL CONTROLADOR PID + + while(1) { + //wait(0.01); //leer puerto analogo y asignar a med + med=Vin.read(); + med2=med*100; + err = (Sp-med2); + float kp2; + kp2=Kp*0.001; + ap = kp2*err; + float ki2; + ki2=Ki*0.01; + ai =(ki2*err)+ai; //calculo de la integral del error + float kd2; + kd2=Kd*0.0001; + ad = kd2*(err-err_v); //calculo de la accion derivativa + err_v=err; //guarda el error + pid = (ap+ai+ad); + + // se verifica que pid sea menor o igual la valor maximo ***************** + if (pid > .99999){ + pid=1; + } + + // se verifica que pid sea positivo ************************************** + if (pid <0){ + pid=0; + } + + //wait(.5); + + + + // se verifica que la accion integral no sea muy grande + if (ai > 999){ + ai=1000; + } + + + Vout=pid; + + + //****se muestran las variables****************************************** + if(c>600){ + lcd.locate(2,0); + lcd.printf(" "); + lcd.locate(0,0); + lcd.printf("Er%2.2f",err); + lcd.locate(10,0); + lcd.printf(" "); + lcd.locate(8,0); + lcd.printf("Me%d",med2); + lcd.locate(2,1); + lcd.printf(" "); + lcd.locate(0,1); + lcd.printf("Sp%d",Sp); + lcd.locate(10,1); + lcd.printf(" "); + lcd.locate(8,1); + lcd.printf("Pid%4.3f",pid); + c=0; + } + else + c++; + + + } + } \ No newline at end of file