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