Tarea4-Versión1. Uso de un encoder para cambiar los parametros de un PID, cada vez que se presiona el botón del encoder se produce un sonido
Dependencies: QEI TextLCD mbed
main.cpp@0:ac77a888ee22, 2014-05-12 (annotated)
- Committer:
- juniorACA
- Date:
- Mon May 12 05:39:20 2014 +0000
- Revision:
- 0:ac77a888ee22
Tarea4-V1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
juniorACA | 0:ac77a888ee22 | 1 | #include "mbed.h" |
juniorACA | 0:ac77a888ee22 | 2 | #include "QEI.h" |
juniorACA | 0:ac77a888ee22 | 3 | #include "TextLCD.h" |
juniorACA | 0:ac77a888ee22 | 4 | |
juniorACA | 0:ac77a888ee22 | 5 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 |
juniorACA | 0:ac77a888ee22 | 6 | QEI encoder (PTD6, PTD7, PTA17, 624); |
juniorACA | 0:ac77a888ee22 | 7 | AnalogIn Vin(PTB0); |
juniorACA | 0:ac77a888ee22 | 8 | PwmOut sonido(PTA12); |
juniorACA | 0:ac77a888ee22 | 9 | AnalogOut Vout(PTE30); |
juniorACA | 0:ac77a888ee22 | 10 | DigitalIn button3(PTA17); |
juniorACA | 0:ac77a888ee22 | 11 | |
juniorACA | 0:ac77a888ee22 | 12 | int C2=0x18; // Desplazamiento Izquierda |
juniorACA | 0:ac77a888ee22 | 13 | int C3=0x1A; // Desplazamiento Derecha |
juniorACA | 0:ac77a888ee22 | 14 | int C4=0x0C; // Quitar cursor bajo |
juniorACA | 0:ac77a888ee22 | 15 | int C1=0x0F; |
juniorACA | 0:ac77a888ee22 | 16 | int err, med, yr, pid, ap, ai, ad, err_v, cambio=0, diferencia=0; |
juniorACA | 0:ac77a888ee22 | 17 | int spnum=0,kinum=0,kpnum=0,kdnum=0,pos=1; |
juniorACA | 0:ac77a888ee22 | 18 | int flagt=0; |
juniorACA | 0:ac77a888ee22 | 19 | float Dd=10; |
juniorACA | 0:ac77a888ee22 | 20 | float pp=30; |
juniorACA | 0:ac77a888ee22 | 21 | float Fi; |
juniorACA | 0:ac77a888ee22 | 22 | |
juniorACA | 0:ac77a888ee22 | 23 | |
juniorACA | 0:ac77a888ee22 | 24 | int main() |
juniorACA | 0:ac77a888ee22 | 25 | { |
juniorACA | 0:ac77a888ee22 | 26 | lcd.cls(); // Borrar Pantalla |
juniorACA | 0:ac77a888ee22 | 27 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
juniorACA | 0:ac77a888ee22 | 28 | |
juniorACA | 0:ac77a888ee22 | 29 | lcd.locate(8,0); |
juniorACA | 0:ac77a888ee22 | 30 | lcd.printf("Kp=%d",kpnum); |
juniorACA | 0:ac77a888ee22 | 31 | lcd.locate(0,1); |
juniorACA | 0:ac77a888ee22 | 32 | lcd.printf("Ki=%d",kinum); |
juniorACA | 0:ac77a888ee22 | 33 | lcd.locate(8,1); |
juniorACA | 0:ac77a888ee22 | 34 | lcd.printf("Kd=%d",kdnum); |
juniorACA | 0:ac77a888ee22 | 35 | lcd.locate(0,0); |
juniorACA | 0:ac77a888ee22 | 36 | lcd.printf("Sp=%d",spnum); |
juniorACA | 0:ac77a888ee22 | 37 | |
juniorACA | 0:ac77a888ee22 | 38 | while(1) |
juniorACA | 0:ac77a888ee22 | 39 | { diferencia=encoder.getPulses()-cambio; |
juniorACA | 0:ac77a888ee22 | 40 | cambio=encoder.getPulses(); |
juniorACA | 0:ac77a888ee22 | 41 | |
juniorACA | 0:ac77a888ee22 | 42 | if (diferencia==0) |
juniorACA | 0:ac77a888ee22 | 43 | { |
juniorACA | 0:ac77a888ee22 | 44 | //no hace nada |
juniorACA | 0:ac77a888ee22 | 45 | } |
juniorACA | 0:ac77a888ee22 | 46 | else if(diferencia>0) |
juniorACA | 0:ac77a888ee22 | 47 | { |
juniorACA | 0:ac77a888ee22 | 48 | if(pos==1) |
juniorACA | 0:ac77a888ee22 | 49 | { |
juniorACA | 0:ac77a888ee22 | 50 | if(spnum+diferencia>=999) |
juniorACA | 0:ac77a888ee22 | 51 | { |
juniorACA | 0:ac77a888ee22 | 52 | spnum=999; |
juniorACA | 0:ac77a888ee22 | 53 | lcd.locate(3,0); |
juniorACA | 0:ac77a888ee22 | 54 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 55 | lcd.locate(3,0); |
juniorACA | 0:ac77a888ee22 | 56 | lcd.printf("%d", spnum); |
juniorACA | 0:ac77a888ee22 | 57 | } |
juniorACA | 0:ac77a888ee22 | 58 | else |
juniorACA | 0:ac77a888ee22 | 59 | { |
juniorACA | 0:ac77a888ee22 | 60 | spnum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 61 | lcd.locate(3,0); |
juniorACA | 0:ac77a888ee22 | 62 | lcd.printf("%d", spnum); |
juniorACA | 0:ac77a888ee22 | 63 | } |
juniorACA | 0:ac77a888ee22 | 64 | } |
juniorACA | 0:ac77a888ee22 | 65 | else if(pos==2) |
juniorACA | 0:ac77a888ee22 | 66 | { |
juniorACA | 0:ac77a888ee22 | 67 | if(kpnum+diferencia>=999) |
juniorACA | 0:ac77a888ee22 | 68 | { |
juniorACA | 0:ac77a888ee22 | 69 | kpnum=999; |
juniorACA | 0:ac77a888ee22 | 70 | lcd.locate(11,0); |
juniorACA | 0:ac77a888ee22 | 71 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 72 | lcd.locate(11,0); |
juniorACA | 0:ac77a888ee22 | 73 | lcd.printf("%d", kpnum); |
juniorACA | 0:ac77a888ee22 | 74 | } |
juniorACA | 0:ac77a888ee22 | 75 | else |
juniorACA | 0:ac77a888ee22 | 76 | { |
juniorACA | 0:ac77a888ee22 | 77 | kpnum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 78 | lcd.locate(11,0); |
juniorACA | 0:ac77a888ee22 | 79 | lcd.printf("%d", kpnum); |
juniorACA | 0:ac77a888ee22 | 80 | } |
juniorACA | 0:ac77a888ee22 | 81 | } |
juniorACA | 0:ac77a888ee22 | 82 | else if(pos==3) |
juniorACA | 0:ac77a888ee22 | 83 | { |
juniorACA | 0:ac77a888ee22 | 84 | if(kinum+diferencia>=999) |
juniorACA | 0:ac77a888ee22 | 85 | { |
juniorACA | 0:ac77a888ee22 | 86 | kinum=999; |
juniorACA | 0:ac77a888ee22 | 87 | lcd.locate(3,1); |
juniorACA | 0:ac77a888ee22 | 88 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 89 | lcd.locate(3,1); |
juniorACA | 0:ac77a888ee22 | 90 | lcd.printf("%d", kinum); |
juniorACA | 0:ac77a888ee22 | 91 | } |
juniorACA | 0:ac77a888ee22 | 92 | else |
juniorACA | 0:ac77a888ee22 | 93 | { |
juniorACA | 0:ac77a888ee22 | 94 | kinum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 95 | lcd.locate(3,1); |
juniorACA | 0:ac77a888ee22 | 96 | lcd.printf("%d", kinum); |
juniorACA | 0:ac77a888ee22 | 97 | } |
juniorACA | 0:ac77a888ee22 | 98 | } |
juniorACA | 0:ac77a888ee22 | 99 | else if(pos==4) |
juniorACA | 0:ac77a888ee22 | 100 | { |
juniorACA | 0:ac77a888ee22 | 101 | if(kdnum+diferencia>=999) |
juniorACA | 0:ac77a888ee22 | 102 | { |
juniorACA | 0:ac77a888ee22 | 103 | kdnum=999; |
juniorACA | 0:ac77a888ee22 | 104 | lcd.locate(11,1); |
juniorACA | 0:ac77a888ee22 | 105 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 106 | lcd.locate(11,1); |
juniorACA | 0:ac77a888ee22 | 107 | lcd.printf("%d", kdnum); |
juniorACA | 0:ac77a888ee22 | 108 | } |
juniorACA | 0:ac77a888ee22 | 109 | else |
juniorACA | 0:ac77a888ee22 | 110 | { |
juniorACA | 0:ac77a888ee22 | 111 | kdnum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 112 | lcd.locate(11,1); |
juniorACA | 0:ac77a888ee22 | 113 | lcd.printf("%d", kdnum); |
juniorACA | 0:ac77a888ee22 | 114 | } |
juniorACA | 0:ac77a888ee22 | 115 | } |
juniorACA | 0:ac77a888ee22 | 116 | } |
juniorACA | 0:ac77a888ee22 | 117 | |
juniorACA | 0:ac77a888ee22 | 118 | else if(diferencia<0) |
juniorACA | 0:ac77a888ee22 | 119 | { |
juniorACA | 0:ac77a888ee22 | 120 | if(pos==1) |
juniorACA | 0:ac77a888ee22 | 121 | { |
juniorACA | 0:ac77a888ee22 | 122 | if(spnum+diferencia<0) |
juniorACA | 0:ac77a888ee22 | 123 | { |
juniorACA | 0:ac77a888ee22 | 124 | //No hace nada |
juniorACA | 0:ac77a888ee22 | 125 | } |
juniorACA | 0:ac77a888ee22 | 126 | else |
juniorACA | 0:ac77a888ee22 | 127 | { |
juniorACA | 0:ac77a888ee22 | 128 | spnum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 129 | lcd.locate(3,0); |
juniorACA | 0:ac77a888ee22 | 130 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 131 | lcd.locate(3,0); |
juniorACA | 0:ac77a888ee22 | 132 | lcd.printf("%d", spnum); |
juniorACA | 0:ac77a888ee22 | 133 | } |
juniorACA | 0:ac77a888ee22 | 134 | } |
juniorACA | 0:ac77a888ee22 | 135 | else if(pos==2) |
juniorACA | 0:ac77a888ee22 | 136 | { |
juniorACA | 0:ac77a888ee22 | 137 | if(kpnum+diferencia<0) |
juniorACA | 0:ac77a888ee22 | 138 | { |
juniorACA | 0:ac77a888ee22 | 139 | //No hace nada |
juniorACA | 0:ac77a888ee22 | 140 | } |
juniorACA | 0:ac77a888ee22 | 141 | else |
juniorACA | 0:ac77a888ee22 | 142 | { |
juniorACA | 0:ac77a888ee22 | 143 | kpnum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 144 | lcd.locate(11,0); |
juniorACA | 0:ac77a888ee22 | 145 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 146 | lcd.locate(11,0); |
juniorACA | 0:ac77a888ee22 | 147 | lcd.printf("%d", kpnum); |
juniorACA | 0:ac77a888ee22 | 148 | } |
juniorACA | 0:ac77a888ee22 | 149 | } |
juniorACA | 0:ac77a888ee22 | 150 | else if(pos==3) |
juniorACA | 0:ac77a888ee22 | 151 | { |
juniorACA | 0:ac77a888ee22 | 152 | if(kinum+diferencia<0) |
juniorACA | 0:ac77a888ee22 | 153 | { |
juniorACA | 0:ac77a888ee22 | 154 | //No hace nada |
juniorACA | 0:ac77a888ee22 | 155 | } |
juniorACA | 0:ac77a888ee22 | 156 | else |
juniorACA | 0:ac77a888ee22 | 157 | { |
juniorACA | 0:ac77a888ee22 | 158 | kinum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 159 | lcd.locate(3,1); |
juniorACA | 0:ac77a888ee22 | 160 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 161 | lcd.locate(3,1); |
juniorACA | 0:ac77a888ee22 | 162 | lcd.printf("%d", kinum); |
juniorACA | 0:ac77a888ee22 | 163 | } |
juniorACA | 0:ac77a888ee22 | 164 | } |
juniorACA | 0:ac77a888ee22 | 165 | else if(pos==4) |
juniorACA | 0:ac77a888ee22 | 166 | { |
juniorACA | 0:ac77a888ee22 | 167 | if(kdnum+diferencia<0) |
juniorACA | 0:ac77a888ee22 | 168 | { |
juniorACA | 0:ac77a888ee22 | 169 | //No hace nada |
juniorACA | 0:ac77a888ee22 | 170 | } |
juniorACA | 0:ac77a888ee22 | 171 | else |
juniorACA | 0:ac77a888ee22 | 172 | { |
juniorACA | 0:ac77a888ee22 | 173 | kdnum+=diferencia; |
juniorACA | 0:ac77a888ee22 | 174 | lcd.locate(11,1); |
juniorACA | 0:ac77a888ee22 | 175 | lcd.printf(" "); |
juniorACA | 0:ac77a888ee22 | 176 | lcd.locate(11,1); |
juniorACA | 0:ac77a888ee22 | 177 | lcd.printf("%d", kdnum); |
juniorACA | 0:ac77a888ee22 | 178 | } |
juniorACA | 0:ac77a888ee22 | 179 | } |
juniorACA | 0:ac77a888ee22 | 180 | } |
juniorACA | 0:ac77a888ee22 | 181 | |
juniorACA | 0:ac77a888ee22 | 182 | if (!button3) |
juniorACA | 0:ac77a888ee22 | 183 | { Fi=(Dd)*100; |
juniorACA | 0:ac77a888ee22 | 184 | pp=(1/Fi); |
juniorACA | 0:ac77a888ee22 | 185 | sonido.period(pp); |
juniorACA | 0:ac77a888ee22 | 186 | { |
juniorACA | 0:ac77a888ee22 | 187 | |
juniorACA | 0:ac77a888ee22 | 188 | sonido.write(0.3);; |
juniorACA | 0:ac77a888ee22 | 189 | wait(0.1); |
juniorACA | 0:ac77a888ee22 | 190 | sonido=0.0; |
juniorACA | 0:ac77a888ee22 | 191 | |
juniorACA | 0:ac77a888ee22 | 192 | } |
juniorACA | 0:ac77a888ee22 | 193 | if(pos==4) |
juniorACA | 0:ac77a888ee22 | 194 | { |
juniorACA | 0:ac77a888ee22 | 195 | pos=1; |
juniorACA | 0:ac77a888ee22 | 196 | lcd.locate(3,0); |
juniorACA | 0:ac77a888ee22 | 197 | lcd.printf("%d", spnum); |
juniorACA | 0:ac77a888ee22 | 198 | } |
juniorACA | 0:ac77a888ee22 | 199 | else if (pos==1) |
juniorACA | 0:ac77a888ee22 | 200 | { |
juniorACA | 0:ac77a888ee22 | 201 | pos++; |
juniorACA | 0:ac77a888ee22 | 202 | lcd.locate(11,0); |
juniorACA | 0:ac77a888ee22 | 203 | lcd.printf("%d", kpnum); |
juniorACA | 0:ac77a888ee22 | 204 | } |
juniorACA | 0:ac77a888ee22 | 205 | else if(pos==2) |
juniorACA | 0:ac77a888ee22 | 206 | { |
juniorACA | 0:ac77a888ee22 | 207 | pos++; |
juniorACA | 0:ac77a888ee22 | 208 | lcd.locate(3,1); |
juniorACA | 0:ac77a888ee22 | 209 | lcd.printf("%d", kinum); |
juniorACA | 0:ac77a888ee22 | 210 | } |
juniorACA | 0:ac77a888ee22 | 211 | else if(pos==3) |
juniorACA | 0:ac77a888ee22 | 212 | { |
juniorACA | 0:ac77a888ee22 | 213 | pos++; |
juniorACA | 0:ac77a888ee22 | 214 | lcd.locate(11,1); |
juniorACA | 0:ac77a888ee22 | 215 | lcd.printf("%d", kdnum); |
juniorACA | 0:ac77a888ee22 | 216 | } |
juniorACA | 0:ac77a888ee22 | 217 | wait(0.25); |
juniorACA | 0:ac77a888ee22 | 218 | |
juniorACA | 0:ac77a888ee22 | 219 | } |
juniorACA | 0:ac77a888ee22 | 220 | |
juniorACA | 0:ac77a888ee22 | 221 | } |
juniorACA | 0:ac77a888ee22 | 222 | } |
juniorACA | 0:ac77a888ee22 | 223 |