Este programa corre un control PID donde los parámetros se ingresan con un teclado 4x4
Dependencies: FPointer TextLCD keypad mbed
Este programa corre un control PID donde los parámetros se ingresan con un teclado 4x4
el montaje es el siguiente las conexiones se deducen del código. es obligatorio poner resistencias de pull-down entre las cuatro lineas de teclado (filas) de 4.7k
la distribución de este teclado es la siguiente
main.cpp@0:d1b7a566d385, 2016-04-25 (annotated)
- Committer:
- tony63
- Date:
- Mon Apr 25 07:21:57 2016 +0000
- Revision:
- 0:d1b7a566d385
Este programa corre un control PID donde los par?metros se ingresan con un teclado 4x4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 0:d1b7a566d385 | 1 | /* Este programa corre un control PID donde los parametros se ingresan con un teclado 4x4 |
tony63 | 0:d1b7a566d385 | 2 | */ |
tony63 | 0:d1b7a566d385 | 3 | |
tony63 | 0:d1b7a566d385 | 4 | #include "mbed.h" |
tony63 | 0:d1b7a566d385 | 5 | #include "keypad.h" |
tony63 | 0:d1b7a566d385 | 6 | #include "TextLCD.h" |
tony63 | 0:d1b7a566d385 | 7 | #include "stdlib.h" |
tony63 | 0:d1b7a566d385 | 8 | |
tony63 | 0:d1b7a566d385 | 9 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 |
tony63 | 0:d1b7a566d385 | 10 | AnalogIn y(PTB0); |
tony63 | 0:d1b7a566d385 | 11 | AnalogOut u(PTE30); |
tony63 | 0:d1b7a566d385 | 12 | int C1=0x0F; // Cursor |
tony63 | 0:d1b7a566d385 | 13 | int C4=0x0C; // quito cursor bajo |
tony63 | 0:d1b7a566d385 | 14 | int spnum=0,kinum=0,kpnum=0,kdnum=0,pos=1,tecla,flag1=1,num=0; |
tony63 | 0:d1b7a566d385 | 15 | float med, err, pid, ap, ad, ai, yr, err_v; |
tony63 | 0:d1b7a566d385 | 16 | float pidn; |
tony63 | 0:d1b7a566d385 | 17 | int flagt=0; |
tony63 | 0:d1b7a566d385 | 18 | Timer t; |
tony63 | 0:d1b7a566d385 | 19 | char cadena[3]={' ',' ',' '}; |
tony63 | 0:d1b7a566d385 | 20 | int llena=0; //verificar que la entrada es de 3 numeros |
tony63 | 0:d1b7a566d385 | 21 | // Define your own keypad values |
tony63 | 0:d1b7a566d385 | 22 | char Keytable[] = { '1', '2', '3', 'A', |
tony63 | 0:d1b7a566d385 | 23 | '4', '5', '6', 'B', |
tony63 | 0:d1b7a566d385 | 24 | '7', '8', '9', 'C', |
tony63 | 0:d1b7a566d385 | 25 | '*', '0', '#', 'D' |
tony63 | 0:d1b7a566d385 | 26 | }; |
tony63 | 0:d1b7a566d385 | 27 | |
tony63 | 0:d1b7a566d385 | 28 | uint32_t cbAfterInput(uint32_t index) { |
tony63 | 0:d1b7a566d385 | 29 | tecla=index; |
tony63 | 0:d1b7a566d385 | 30 | flag1=0; |
tony63 | 0:d1b7a566d385 | 31 | return 0; |
tony63 | 0:d1b7a566d385 | 32 | } |
tony63 | 0:d1b7a566d385 | 33 | |
tony63 | 0:d1b7a566d385 | 34 | int main() { |
tony63 | 0:d1b7a566d385 | 35 | // f0 f1 f2 f3 c0 c1 c2 c3 |
tony63 | 0:d1b7a566d385 | 36 | Keypad keypad(PTA2, PTD4, PTD3, PTD7,PTA13, PTD5, PTD0, PTD2); |
tony63 | 0:d1b7a566d385 | 37 | ini1: |
tony63 | 0:d1b7a566d385 | 38 | err=0; med=0; yr=0; pid=0; ap=0; ai=0; ad=0; err_v=0;pidn=0; |
tony63 | 0:d1b7a566d385 | 39 | lcd.cls(); // Borrar Pantalla |
tony63 | 0:d1b7a566d385 | 40 | lcd.locate(0,0); |
tony63 | 0:d1b7a566d385 | 41 | lcd.printf("**PID-TECLADO**"); |
tony63 | 0:d1b7a566d385 | 42 | wait(1); |
tony63 | 0:d1b7a566d385 | 43 | lcd.cls(); |
tony63 | 0:d1b7a566d385 | 44 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
tony63 | 0:d1b7a566d385 | 45 | |
tony63 | 0:d1b7a566d385 | 46 | lcd.locate(8,0); |
tony63 | 0:d1b7a566d385 | 47 | lcd.printf("Kp=%d",kpnum); |
tony63 | 0:d1b7a566d385 | 48 | lcd.locate(0,1); |
tony63 | 0:d1b7a566d385 | 49 | lcd.printf("Ki=%d",kinum); |
tony63 | 0:d1b7a566d385 | 50 | lcd.locate(8,1); |
tony63 | 0:d1b7a566d385 | 51 | lcd.printf("Kd=%d",kdnum); |
tony63 | 0:d1b7a566d385 | 52 | lcd.locate(0,0); |
tony63 | 0:d1b7a566d385 | 53 | lcd.printf("Sp=%d",spnum); |
tony63 | 0:d1b7a566d385 | 54 | |
tony63 | 0:d1b7a566d385 | 55 | ini2: |
tony63 | 0:d1b7a566d385 | 56 | keypad.CallAfterInput(&cbAfterInput); |
tony63 | 0:d1b7a566d385 | 57 | keypad.Start(); |
tony63 | 0:d1b7a566d385 | 58 | if(flag1==0){ |
tony63 | 0:d1b7a566d385 | 59 | if(Keytable[tecla]=='B'){ |
tony63 | 0:d1b7a566d385 | 60 | cadena[0]=' ';cadena[1]=' ';cadena[2]=' '; |
tony63 | 0:d1b7a566d385 | 61 | llena=0; |
tony63 | 0:d1b7a566d385 | 62 | num=0; |
tony63 | 0:d1b7a566d385 | 63 | if(pos==4){ |
tony63 | 0:d1b7a566d385 | 64 | pos=1;} |
tony63 | 0:d1b7a566d385 | 65 | else { |
tony63 | 0:d1b7a566d385 | 66 | pos++;} |
tony63 | 0:d1b7a566d385 | 67 | } |
tony63 | 0:d1b7a566d385 | 68 | else if(Keytable[tecla]=='A'){ |
tony63 | 0:d1b7a566d385 | 69 | llena--; |
tony63 | 0:d1b7a566d385 | 70 | cadena[llena]=' '; |
tony63 | 0:d1b7a566d385 | 71 | num=strtod(cadena,NULL); |
tony63 | 0:d1b7a566d385 | 72 | } |
tony63 | 0:d1b7a566d385 | 73 | else if((tecla==0)||(tecla==1)||(tecla==2)||(tecla==4)||(tecla==5)||(tecla==6)||(tecla==8)||(tecla==9)||(tecla==10)||(tecla==13)){ |
tony63 | 0:d1b7a566d385 | 74 | if(llena<3){ |
tony63 | 0:d1b7a566d385 | 75 | cadena[llena]=Keytable[tecla]; |
tony63 | 0:d1b7a566d385 | 76 | num=strtod(cadena,NULL); |
tony63 | 0:d1b7a566d385 | 77 | llena++;} |
tony63 | 0:d1b7a566d385 | 78 | } |
tony63 | 0:d1b7a566d385 | 79 | else if(Keytable[tecla]=='D'){ |
tony63 | 0:d1b7a566d385 | 80 | lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo |
tony63 | 0:d1b7a566d385 | 81 | lcd.cls(); //borra la pantalla |
tony63 | 0:d1b7a566d385 | 82 | lcd.printf(" GUARDADOS!"); |
tony63 | 0:d1b7a566d385 | 83 | wait(1); |
tony63 | 0:d1b7a566d385 | 84 | lcd.cls(); |
tony63 | 0:d1b7a566d385 | 85 | lcd.printf(" INICIA EL PID"); |
tony63 | 0:d1b7a566d385 | 86 | wait(1); |
tony63 | 0:d1b7a566d385 | 87 | // se imprimen los parches del control ***************************************** |
tony63 | 0:d1b7a566d385 | 88 | lcd.cls(); |
tony63 | 0:d1b7a566d385 | 89 | lcd.printf("Er=%3.0f",err); |
tony63 | 0:d1b7a566d385 | 90 | lcd.locate(8,0); |
tony63 | 0:d1b7a566d385 | 91 | lcd.printf("Me=%3.0f",med); |
tony63 | 0:d1b7a566d385 | 92 | lcd.locate(0,1); |
tony63 | 0:d1b7a566d385 | 93 | lcd.printf("Sp=%d",spnum); |
tony63 | 0:d1b7a566d385 | 94 | lcd.locate(8,1); |
tony63 | 0:d1b7a566d385 | 95 | lcd.printf("Co=%3.0f",pid); |
tony63 | 0:d1b7a566d385 | 96 | wait(2); |
tony63 | 0:d1b7a566d385 | 97 | cicloPID: |
tony63 | 0:d1b7a566d385 | 98 | if(Keytable[tecla]=='C'){ |
tony63 | 0:d1b7a566d385 | 99 | spnum=0;kinum=0;kpnum=0;kdnum=0;llena=0;pos=1;flag1=1; |
tony63 | 0:d1b7a566d385 | 100 | cadena[0]=' ';cadena[1]=' ';cadena[2]=' '; |
tony63 | 0:d1b7a566d385 | 101 | goto ini1;} |
tony63 | 0:d1b7a566d385 | 102 | med=y.read()*999; //leer puerto analogo y asignar a med |
tony63 | 0:d1b7a566d385 | 103 | err = (spnum-med); //se calcula el error |
tony63 | 0:d1b7a566d385 | 104 | ap = kpnum*err; //se calcula la accion proporcinal |
tony63 | 0:d1b7a566d385 | 105 | if(ai<300){// se verifica que la accion integral no sea muy grande |
tony63 | 0:d1b7a566d385 | 106 | ai =(kinum*err)+ai;} //calculo de la integral del error |
tony63 | 0:d1b7a566d385 | 107 | ad = kdnum*(err-err_v); //calculo de la accion derivativa |
tony63 | 0:d1b7a566d385 | 108 | pid = (ap+ai+ad); |
tony63 | 0:d1b7a566d385 | 109 | if(pid<=0){// se verifica que pid sea positivo |
tony63 | 0:d1b7a566d385 | 110 | pid=0;} |
tony63 | 0:d1b7a566d385 | 111 | if (pid > 999){// se verifica que pid sea menor o igual la valor maximo |
tony63 | 0:d1b7a566d385 | 112 | pid=999;} |
tony63 | 0:d1b7a566d385 | 113 | |
tony63 | 0:d1b7a566d385 | 114 | if(flagt==0){//se muestran las variables |
tony63 | 0:d1b7a566d385 | 115 | t.start(); |
tony63 | 0:d1b7a566d385 | 116 | flagt=1;} |
tony63 | 0:d1b7a566d385 | 117 | if(t>=0.3){ |
tony63 | 0:d1b7a566d385 | 118 | lcd.locate(3,0);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 119 | lcd.locate(3,0);lcd.printf("%3.0f",err); |
tony63 | 0:d1b7a566d385 | 120 | lcd.locate(11,0);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 121 | lcd.locate(11,0);lcd.printf("%3.0f",med); |
tony63 | 0:d1b7a566d385 | 122 | lcd.locate(3,1);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 123 | lcd.locate(3,1);lcd.printf("%d",spnum); |
tony63 | 0:d1b7a566d385 | 124 | lcd.locate(11,1);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 125 | lcd.locate(11,1);lcd.printf("%3.0f",pid); |
tony63 | 0:d1b7a566d385 | 126 | flagt=0; |
tony63 | 0:d1b7a566d385 | 127 | t.reset(); |
tony63 | 0:d1b7a566d385 | 128 | } |
tony63 | 0:d1b7a566d385 | 129 | |
tony63 | 0:d1b7a566d385 | 130 | pidn=pid/999;//Normalizacion de la salida |
tony63 | 0:d1b7a566d385 | 131 | u.write(pidn);//se envia el valor pid a puerto analogico de salida (D/A) |
tony63 | 0:d1b7a566d385 | 132 | wait_ms(100); |
tony63 | 0:d1b7a566d385 | 133 | err_v = err;// se actualizan las variables |
tony63 | 0:d1b7a566d385 | 134 | goto cicloPID; |
tony63 | 0:d1b7a566d385 | 135 | } |
tony63 | 0:d1b7a566d385 | 136 | if(pos==1){ |
tony63 | 0:d1b7a566d385 | 137 | spnum=num; |
tony63 | 0:d1b7a566d385 | 138 | lcd.locate(3,0);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 139 | lcd.locate(3,0);lcd.printf("%d",spnum); |
tony63 | 0:d1b7a566d385 | 140 | } |
tony63 | 0:d1b7a566d385 | 141 | else if(pos==2){ |
tony63 | 0:d1b7a566d385 | 142 | kpnum=num; |
tony63 | 0:d1b7a566d385 | 143 | lcd.locate(11,0);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 144 | lcd.locate(11,0);lcd.printf("%d",kpnum); |
tony63 | 0:d1b7a566d385 | 145 | } |
tony63 | 0:d1b7a566d385 | 146 | else if(pos==3){ |
tony63 | 0:d1b7a566d385 | 147 | kinum=num; |
tony63 | 0:d1b7a566d385 | 148 | lcd.locate(3,1);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 149 | lcd.locate(3,1);lcd.printf("%d",kinum); |
tony63 | 0:d1b7a566d385 | 150 | } |
tony63 | 0:d1b7a566d385 | 151 | else if(pos==4){ |
tony63 | 0:d1b7a566d385 | 152 | kdnum=num; |
tony63 | 0:d1b7a566d385 | 153 | lcd.locate(11,1);lcd.printf(" "); |
tony63 | 0:d1b7a566d385 | 154 | lcd.locate(11,1);lcd.printf("%d",kdnum); |
tony63 | 0:d1b7a566d385 | 155 | } |
tony63 | 0:d1b7a566d385 | 156 | } |
tony63 | 0:d1b7a566d385 | 157 | flag1=1; |
tony63 | 0:d1b7a566d385 | 158 | goto ini2; |
tony63 | 0:d1b7a566d385 | 159 | } |