ingreso de parametros a controlador PID con un ENCODER .
Dependencies: DebounceIn Debounced QEI TextLCD2 mbed
Diff: main.cpp
- Revision:
- 2:7fd0465280d2
- Parent:
- 1:30696e4d196b
- Child:
- 3:c87c88124d37
--- a/main.cpp Wed Aug 11 09:15:10 2010 +0000 +++ b/main.cpp Mon Nov 18 15:41:52 2013 +0000 @@ -1,16 +1,174 @@ +#include "mbed.h" #include "QEI.h" - -Serial pc(USBTX, USBRX); -//Use X4 encoding. -//QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING); -//Use X2 encoding by default. -QEI wheel (p29, p30, NC, 624); +#include "TextLCD.h" +#include "DebouncedIn.h" +#include "DebounceIn.h" +AnalogIn Vin(PTC2); +AnalogOut Vo(PTE30); +QEI wheel (PTD5, PTD0, NC, 624); +TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 +DebouncedIn b1(PTC9); +DebounceIn b2(PTC17); +int x=1,i,d,f,g,h=0,m=0,l=0,k=0,j=0; +int C1=0x0e; +float kp, ki, kd, sp ,err, med, yr, pid, ap, ai, ad, err_v ,Sp; +int main() +{ +lcd.writeCommand(C1); // escribe el comando C1 +lcd.locate(0,0); +lcd.printf("Sp:%d",i); +lcd.locate(8,0); +lcd.printf("Kp:%d",d); +lcd.locate(0,1); +lcd.printf("Ki:%d",f); +lcd.locate(8,1); +lcd.printf("Kd:%d",g); -int main() { + while(1) { + j=wheel.getPulses(); + if(b1.falling()) + { + x=x+1; + } + if(j<0) + { + wheel.reset(); + j=0; + } + +//condicionales para ubicación en pantalla y escritura + if(x==1) + { + i=j+h; + if(i<0) + { + i=0; + } + wait(0.1); + lcd.locate(3,0); + lcd.printf(" "); + lcd.locate(3,0); + lcd.printf("%d",i); + } + if(x==2) + { + d=(j)-(i)+h+m; + if(d<0) + { + d=0; + } + + wait(0.1); + lcd.locate(11,0); + lcd.printf(" "); + lcd.locate(11,0); + lcd.printf("%d",d); +} + if(x==3) + { + f=j-d-i+h+m+l; + if(f<0) + { + f=0; + } + wait(0.1); + lcd.locate(3,1); + lcd.printf(" "); + lcd.locate(3,1); + lcd.printf("%d",f); + } + if(x==4) + { + g=j-(i+d+f)+h+m+l+k; + if(g<0) + { + g=0; + } + wait(0.1); + lcd.locate(11,1); + lcd.printf(" "); + lcd.locate(11,1); + lcd.printf("%d",g); + } + if(x>4) + { + x=1; + h=i; + m=d; + l=f; + k=g; + wheel.reset(); + } + if(b2.read()==0) + { + goto PID; + } +}//while + PID: + + wait(0.5); // espera 1 segundo + lcd.cls(); // borra lcd + wait(0.2); + lcd.printf("DATOS GUARDADOS!"); + wait(1); + lcd.cls(); + + lcd.locate(0,0); + lcd.printf("Er:"); + lcd.locate(8,0); + lcd.printf("Me:"); + lcd.locate(0,1); + lcd.printf("Sp:"); + lcd.locate(8,1); + lcd.printf("CO:"); + + while(1) + {//whilePID + wait(0.1); + sp=h; + kp=m; + kd=l; + ki=g; + + Sp=(sp*3.3)/100; + med=Vin*3.3; + err = (Sp-med); + ap = kp*err; + ai =(ki*err)+ai; - while(1){ - wait(0.1); - pc.printf("Pulses is: %i\n", wheel.getPulses()); - } + ad = kd*(err-err_v); + pid = (ap+ai+ad); + if (pid<0) + { + pid=0; + } + if (pid>99) + { + pid=100; + } + + Vo=pid/100; + + + lcd.locate(3,0); + lcd.printf(" "); + lcd.locate(3,0); + lcd.printf("%.1f",err); + lcd.locate(11,0); + lcd.printf(" "); + lcd.locate(11,0); + lcd.printf("%.1f",med); + lcd.locate(3,1); + lcd.printf(" "); + lcd.locate(3,1); + lcd.printf("%.1f",Sp); + lcd.locate(11,1); + lcd.printf(" "); + lcd.locate(11,1); + lcd.printf("%.1f",pid); + + err_v = err; + }//whilePID -} + +}//main \ No newline at end of file