PID_TECLAS
Dependencies: DebouncedIn TextLCD mbed
Fork of PID_Encoder by
main.cpp
- Committer:
- sagilar
- Date:
- 2015-04-10
- Revision:
- 5:a582c64fce81
- Parent:
- 4:d42fe3777735
File content as of revision 5:a582c64fce81:
#include "mbed.h" #include "DebouncedIn.h" #include "TextLCD.h" AnalogIn Vin(PTC2); AnalogOut u(PTE30); TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 DigitalOut led1(LED1); DigitalOut led2(LED2); DebouncedIn button1(PTC12); DebouncedIn button2(PTC13); DebouncedIn button3(PTC16); DebouncedIn button4(PTC17); Timer t; //se define la función del timer interno que ofrece la plataforma mbed /* codigos movimiento del curzor 18 para izquierda 1A para derecha */ int C1=0x0E; // solo muestra el curzor int C2=0x18; // desplaza izquierda int C3=0x1A; // desplaza derecha int C4=0x0C; // quito cursor bajo int i; // indice de la variable int j; int kp,ki,kd,sp; float med,pid,pidn,ap,ai,ad,err,err_v,err_v2; int count; int esc=1; int main() { lcd.cls(); lcd.locate(0,0); lcd.printf("Sp=%d",sp); lcd.locate(6,0); lcd.printf("Kp=%d",kp); lcd.locate(0,1); lcd.printf("Ki=%d",ki); lcd.locate(6,1); lcd.printf("Kd=%d",kd); lcd.locate(12,1); lcd.printf("E%d",esc); lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD lcd.locate(0,0); lcd.printf("Sp=%d",sp); while(1) { if (button3.falling()) { ++j; } //INCREMENTA POSICION DEL MENU CON BOTON 3 if (j==0){ lcd.locate(3,0); lcd.printf("%d",sp); if (button1.falling()) { t.stop(); t.reset(); t.start(); while (button1==0){ if (sp<999){ if (t<=2){++sp;} wait(0.2); if (t>2&&t<=5){sp=sp+5;} wait(0.2); if (t>5&&t<=10){sp=sp+10;} wait(0.2); if (t>10){sp=sp+100;} wait(0.2); if (sp>999){sp=999;} lcd.locate(3,0); lcd.printf(" "); lcd.locate(3,0); lcd.printf("%d",sp); } } } if (button2.falling()) { t.stop(); t.reset(); t.start(); while (button2==0) if (sp>0){ if (t<=2){--sp;} wait(0.2); if (t>2&&t<=5){sp=sp-5;} wait(0.2); if (t>5&&t<=10){sp=sp-10;} wait(0.2); if (t>10){sp=sp-100;} wait(0.2); if (sp<0){sp=0;} lcd.locate(3,0); lcd.printf(" "); lcd.locate(3,0); lcd.printf("%d",sp); } } } if (j==1) { if (button1.falling()) { t.stop(); t.reset(); t.start(); while (button1==0){ if (kp<1000){ if (t<=2){++kp;} wait(0.2); if (t>2&&t<=5){kp=kp+5;} wait(0.2); if (t>5&&t<=10){kp=kp+10;} wait(0.2); if (t>10){kp=kp+100;} wait(0.2); if (kp>999){kp=999;} lcd.locate(9,0); lcd.printf(" "); lcd.locate(9,0); lcd.printf("%d",kp); } } } if (button2.falling()) { t.stop(); t.reset(); t.start(); while (button2==0){ if (kp>=0){ if (t<=2){--kp;} wait(0.2); if (t>2&&t<=5){kp=kp-5;} wait(0.2); if (t>5&&t<=10){kp=kp-10;} wait(0.2); if (t>10){kp=kp-100;} wait(0.2); if(kp<0){kp=0;} lcd.locate(9,0); lcd.printf(" "); lcd.locate(9,0); lcd.printf("%d",kp); } } } } if (j==2) { if (button1.falling()) { t.stop(); t.reset(); t.start(); while (button1==0){ if (ki<1000){ if (t<=2){++ki;} wait(0.2); if (t>2&&t<=5){ki=ki+5;} wait(0.2); if (t>5&&t<=10){ki=ki+10;} wait(0.2); if (t>10){ki=ki+100;} wait(0.2); if (ki>999){ki=999;} lcd.locate(3,1); lcd.printf(" "); lcd.locate(3,1); lcd.printf("%d",ki); } } } if (button2.falling()) { t.stop(); t.reset(); t.start(); while (button2==0){ if (ki>=0){ if (t<=2){--ki;} wait(0.2); if (t>2&&t<=5){ki=ki-5;} wait(0.2); if (t>5&&t<=10){ki=ki-10;} wait(0.2); if (t>10){ki=ki-100;} wait(0.2); if (ki<0){ki=0;} lcd.locate(3,1); lcd.printf(" "); lcd.locate(3,1); lcd.printf("%d",ki); } } } } if (j==3) { if (button1.falling()) { t.stop(); t.reset(); t.start(); while (button1==0){ if (kd<1000){ if (t<=2){++kd;} wait(0.2); if (t>2&&t<=5){kd=kd+5;} wait(0.2); if (t>5&&t<=10){kd=kd+10;} wait(0.2); if (t>10){kd=kd+100;} wait(0.2); if (kd>999){kd=999;} lcd.locate(9,1); lcd.printf(" "); lcd.locate(9,1); lcd.printf("%d",kd); } } } if (button2.falling()) { t.stop(); t.reset(); t.start(); while (button2==0){ if (kd>=0){ if (t<=2){--kd;} wait(0.2); if (t>2&&t<=5){kd=kd-5;} wait(0.2); if (t>5&&t<=10){kd=kd-10;} wait(0.2); if (t>10){kd=kd-100;} wait(0.2); if (kd<0){kd=0;} lcd.locate(9,1); lcd.printf(" "); lcd.locate(9,1); lcd.printf("%d",kd); } } } } if (j==4) { if (button1.falling()){ if (esc<1000){ esc=esc*10; lcd.locate(13,1); lcd.printf(" "); lcd.locate(13,1); lcd.printf("%d",esc); } } if (button2.falling()){ if (esc>1){ esc=esc/10; lcd.locate(13,1); lcd.printf(" "); lcd.locate(13,1); lcd.printf("%d",esc); } } } if (j==5) { 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%01.3f",err); lcd.locate(8,0); lcd.printf("Me%01.3f",med); lcd.locate(0,1); lcd.printf("Sp%d",sp); lcd.locate(8,1); lcd.printf("Co%01.3f",pid); wait(5); // CICLO PRINCIPAL CONTROLADOR PID while(1) { med=Vin.read()/esc; //leer puerto analogo y asignar a med err = (sp-med); ap = kp*(err-err_v2); if(ai<100) { ai =(ki*(err-err_v))+ai; //calculo de la integral del error } // se verifica que la accion integral no sea muy grande ad = kd*(err-(2*err_v)+err_v2); //calculo de la accion derivativa pid = (ap+ai+ad); //****se muestran las variables****************************************** if (pid > 999){ pid=1000; } if (pid < 0){ pid=0; } wait(.5); lcd.locate(2,0); lcd.printf("%01.3f",err); lcd.locate(10,0); lcd.printf("%01.3f",med); lcd.locate(2,1); lcd.printf("%01.3f",sp); lcd.locate(10,1); lcd.printf("%01.3f",pid); // se actualizan las variables ******************************************* err_v2=err_v; err_v = err; pidn=pid/esc; // se verifica que pid sea positivo ************************************** u.write(pidn); // se verifica que pid sea menor o igual la valor maximo ***************** // se envia el valor pid a puerto analogico de salida (D/A) ************** // se repite el ciclo } }