PID
Dependencies: TextLCD-modificad mbed
main.cpp
- Committer:
- salondonog
- Date:
- 2013-11-15
- Revision:
- 0:9d62b34e332d
File content as of revision 0:9d62b34e332d:
#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 DigitalIn mas(PTC12); DigitalIn menos(PTC13); DebouncedIn salto(PTC16); DebouncedIn set(PTC17); AnalogIn Vin(PTB0); AnalogOut Vout(PTE30); Timer t; int flag; int q; int C1=0x0E; //configurar el lcd para mostrar el guin bajo int C2=0x0C; //configurar el lcd para no mostrar el guion bajo int s=0; //set point int p=0; //ganancia proporcional int i=0; //ganancia integral int d=0; //ganancia derivativa int k=0; //variable de control} int err, med, yr, ap, ai, ad, err_v, cycle; int pid; int conteo_creciente(int incremento){ if(incremento==999){ return incremento; } else if(!mas.read()){ for(int t=0;t<10;t++){ if(mas.read()){ break; } if(incremento>=999){ incremento=999; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento++; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.5); } for(int t=0;t<10;t++){ if(mas.read()){ break; } if(incremento>=990){ incremento=999; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento+=10; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.5); } for(int t=0;t<10;t++){ if(mas.read()){ break; } if(incremento>=900){ incremento=999; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento+=100; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.5); } } return incremento; } int conteo_decreciente(int incremento){ if (incremento==0){ return incremento; } else if(!menos.read()){ for(int t=0;t<10;t++){ if(menos.read()){ break; } if(incremento<=1){ incremento=0; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d",incremento); break; } --incremento; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.5); } for(int t=0;t<10;t++){ if(menos.read()){ break; } if(incremento<=10){ incremento=0; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento-=10; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.5); } for(int t=0;t<10;t++){ if(menos.read()){ break; } if(incremento<=100){ incremento=0; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); break; } incremento-=100; if (k==0){ lcd.locate(3,0); } else if(k==1){ lcd.locate(11,0); } else if (k==2){ lcd.locate(3,1); } else { lcd.locate(11,1); } lcd.printf("%d ",incremento); wait(0.5); } } return incremento; } void def_posicion(int h){ if (h==0){ lcd.locate(3,0); lcd.printf("%d",s); lcd.locate(3,0); } else if(h==1){ lcd.locate(11,0); lcd.printf("%d",p); lcd.locate(11,0); } else if (h==2){ lcd.locate(3,1); lcd.printf("%d",i); lcd.locate(3,1); } else { lcd.locate(11,1); lcd.printf("%d",d); lcd.locate(11,1); } } void decremento(int u){ //Funcion que decrementa los valores if(u==0){ s=conteo_decreciente(s); } else if(u==1){ p=conteo_decreciente(p) ; } else if(u==2){ i=conteo_decreciente(i); } else{ d=conteo_decreciente(d); } } void incremento(int h){ //Funcion que incremente los valores if(h==0){ s=conteo_creciente(s); } else if(h==1){ p=conteo_creciente(p) ; } else if(h==2){ i=conteo_creciente(i); } else{ d=conteo_creciente(d); } } void star_patch1(void){ //funcion que imprime los caracteres que no van a variar en el display lcd.cls(); lcd.locate(8,0); lcd.printf("Kp=%d",p); lcd.locate(0,1); lcd.printf("Ki=%d",i); lcd.locate(8,1); lcd.printf("Kd=%d",d); lcd.writeCommand(C1); //permite que el cursor se vea y sea intermitente lcd.locate(0,0); lcd.printf("Sp=%d",s); } void star_patch2(void){ //funcion que imprime los caracteres que no van a variar en el display lcd.writeCommand(C2); lcd.cls(); lcd.printf("***INICIA PID***/n"); lcd.printf("***************"); wait(2); lcd.cls(); lcd.printf("Er%=d",err); lcd.locate(8,0); lcd.printf("Me=%d",med); lcd.locate(0,1); lcd.printf("Sp=%d",s); lcd.locate(8,1); lcd.printf("Co=%d",pid); wait(3); } int main(){ star_patch1(); while(1){ if(k>=3 && salto.falling()){ k=0; def_posicion(k); } if(salto.falling()){ ++k; def_posicion(k); } else if(!mas.read()){ incremento(k); } else if(!menos.read()){ decremento(k); } else if(set.falling()){ break; } } star_patch2(); while (1){ med=Vin.read()*1000; err = (s-med); ap = k*err; ai =(i*err)+ai; //calculo de la integral del error // se verifica que la accion integral no sea muy grande ad = d*(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; } 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("%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",s); lcd.locate(11,1); lcd.printf(" "); lcd.locate(11,1); lcd.printf("%d ",pid); t.reset(); flag=0; wait(.3); } // se actualizan las variables ******************************************* err_v = err; } }