Tarea3 procesadores: PID en FRDM-KL25Z con botones incrementales
Dependencies: TextLCD-modificad mbed
main.cpp
- Committer:
- obifandres
- Date:
- 2015-04-08
- Revision:
- 0:6a56212dd414
File content as of revision 0:6a56212dd414:
#include "mbed.h" #include "TextLCD.h" #include "DebouncedIn.h" TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); //configura los puestos PTE0,PTE1,PTE2,PTE3,PTE4,PTE5, donde se conectara el LCD 16x2 DebouncedIn button1(PTC12); DebouncedIn button2(PTC13); DebouncedIn button3(PTC16); DebouncedIn button4(PTC17); AnalogIn Vin(PTB0); AnalogOut Vout(PTE30); Timer t; int j; int flag; int q; int C1=0x0E; // solo muestra el cursor /*int C2=0x18; // desplaza izquierda int C3=0x1A; // desplaza derecha*/ int C4=0x0C; // quito cursor bajo int sp=0; //set point int kp=0; //ganancia proporcional int ki=0; //ganancia integral int kd=0; //ganancia derivativa int k=0; //variable de control} int err, med, yr, ap, ai, ad, err_v, cycle; int pid; int bb; int up(int incremento, int pos){ if(incremento==999){ return incremento; } else if(!button1.read()){ for(int t=0;t<10;t++){ if(button1.read()){ break; } if(incremento>995){ incremento=999; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento++; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.25); } for(int t=0;t<10;t++){ if(button1.read()){ break; } if(incremento>970){ incremento=999; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento+=10; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.25); } for(int t=0;t<20;t++){ if(button1.read()){ break; } if(incremento>920){ incremento=999; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento+=100; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.25); } } return incremento; } int down(int decremento, int pos){ if(decremento==0){ return decremento; } else if(!button2.read()){ for(int t=0;t<10;t++){ if(button2.read()){ break; } if(decremento<4){ decremento=0; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf(" "); if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",decremento); break; } decremento--; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf(" "); if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",decremento); wait(0.25); } for(int t=0;t<10;t++){ if(button2.read()){ break; } if(decremento<40){ decremento=0; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf(" "); if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",decremento); break; } decremento-=10; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf(" "); if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",decremento); wait(0.25); } for(int t=0;t<20;t++){ if(button2.read()){ break; } if(decremento<80){ decremento=0; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf(" "); if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",decremento); break; } decremento-=100; if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf(" "); if (pos==0){ lcd.locate(3,0); } else if(pos==1){ lcd.locate(11,0); } else if (pos==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",decremento); wait(0.25); } } return decremento; } int main() { lcd.cls(); lcd.locate(8,0); lcd.printf("Kp=%d",kp); lcd.locate(0,1); lcd.printf("Ki=%d",ki); lcd.locate(8,1); lcd.printf("Kd=%d",kd); lcd.writeCommand(C1); //permite que el cursor se vea y sea intermitente lcd.locate(0,0); lcd.printf("Sp=%d",sp); while(1) { if (button3.falling()) { ++j; } //INCREMENTA POSICION DEL MENU COB BOTON 3 if (j==0){ lcd.locate(3,0); lcd.printf("%d",sp); if(!button1.read()){ sp=up(sp,j); } if(!button2.read()){ sp=down(sp,j); } } if (j==1) { lcd.locate(11,0); lcd.printf("%d",kp); if(!button1.read()){ kp=up(kp,j); } if(!button2.read()){ kp=down(kp,j); } } if (j==2) { lcd.locate(3,1); lcd.printf("%d",ki); if(!button1.read()){ ki=up(ki,j); } if(!button2.read()){ ki=down(ki,j); } } if (j==3) { lcd.locate(11,1); lcd.printf("%d",kd); if(!button1.read()){ kd=up(kd,j); } if(!button2.read()){ kd=down(kd,j); } } if (j==4) { j=0; } if (button4.falling()){ break; //sale del bucle si pisan suiche4 } } lcd.writeCommand(C4);//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",med); lcd.locate(0,1); lcd.printf("Sp=%d",sp); lcd.locate(8,1); lcd.printf("Co=%d",pid); wait(5); while (1){ med=Vin.read()*1000; err = (sp-med); ap = kp*err; ai =(ki*err)+ai; //calculo de la integral del error ad = kd*(err-err_v); //calculo de la accion derivativa pid = (ap+ai+ad); //****se muestran las variables****************************************** if (pid > 999){ pid=1000; } else if (pid<0){ pid=0; } err_v = err; if(flag==0){ t.start(); flag=1; } if (t>=0.3){ wait(0.02); lcd.locate(3,0); lcd.printf(" "); lcd.locate(3,0); lcd.printf("%d",err); lcd.locate(11,0); lcd.printf(" "); lcd.locate(11,0); lcd.printf("%d",med); lcd.locate(3,1); lcd.printf(" "); lcd.locate(3,1); lcd.printf("%d",sp); lcd.locate(11,1); lcd.printf(" "); lcd.locate(11,1); lcd.printf("%d ",pid); t.reset(); flag=0; } Vout.write(pid/1000); wait(0.01); } }