Alarma de pico y placa usando módulo RTC.

Dependencies:   QEI TextLCD mbed

Fork of grafica_PID_android by Nicolás Villegas Echavarría

Committer:
NicolasV
Date:
Sat Jun 03 01:07:49 2017 +0000
Revision:
5:82ac4b34c3d9
Parent:
3:2e823b16a4a1
Gr?fica PID (Android)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NicolasV 2:00db719ebd81 1 /*
NicolasV 2:00db719ebd81 2 * Código por: Laura Álvila
NicolasV 2:00db719ebd81 3 * Daniela López
NicolasV 2:00db719ebd81 4 * Nicolás Villegas
NicolasV 2:00db719ebd81 5 */
NicolasV 2:00db719ebd81 6
tony63 0:4e0dfcf0e7ce 7 #include "mbed.h"
tony63 0:4e0dfcf0e7ce 8 #include "QEI.h"
tony63 0:4e0dfcf0e7ce 9 #include "TextLCD.h"
tony63 0:4e0dfcf0e7ce 10
NicolasV 2:00db719ebd81 11 Serial GSM(PTE0,PTE1); // Módulo Bluetooh
tony63 0:4e0dfcf0e7ce 12 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
NicolasV 3:2e823b16a4a1 13 QEI encoder (PTA13, PTD5, NC, 10);
NicolasV 3:2e823b16a4a1 14 Timer t;
NicolasV 2:00db719ebd81 15
NicolasV 2:00db719ebd81 16 AnalogIn y(PTC2); // Entrada análoga: Salida del sistema
NicolasV 2:00db719ebd81 17 AnalogOut u(PTE30); // Salida análoga: Señal de control
NicolasV 2:00db719ebd81 18
tony63 0:4e0dfcf0e7ce 19 DigitalOut led1(LED1);
tony63 0:4e0dfcf0e7ce 20 DigitalOut led2(LED2);
tony63 0:4e0dfcf0e7ce 21 DigitalOut led3(LED3);
tony63 0:4e0dfcf0e7ce 22
NicolasV 2:00db719ebd81 23 DigitalIn button3(PTC16); // Botón del encoder
NicolasV 2:00db719ebd81 24 DigitalIn button4(PTC17); // Pulsador
tony63 0:4e0dfcf0e7ce 25
NicolasV 2:00db719ebd81 26 int C1=0x0F;
NicolasV 5:82ac4b34c3d9 27 int C2=0x18;
NicolasV 5:82ac4b34c3d9 28 int C3=0x1A;
NicolasV 5:82ac4b34c3d9 29 int C4=0x0C;
tony63 0:4e0dfcf0e7ce 30
NicolasV 2:00db719ebd81 31 int cambio = 0, diferencia = 0;
NicolasV 3:2e823b16a4a1 32 float pid, o, ai, ad, ap, med, err, setpoint;
tony63 1:058b8f5c135d 33 float err_v;
NicolasV 2:00db719ebd81 34 int spnum = 0, kinum = 0, kpnum = 0 ,kdnum = 0, pos = 1;
NicolasV 2:00db719ebd81 35 int j,k;
NicolasV 3:2e823b16a4a1 36 long t_pid = 0, t_btn = 0;
tony63 0:4e0dfcf0e7ce 37
NicolasV 2:00db719ebd81 38 int main() {
NicolasV 3:2e823b16a4a1 39
NicolasV 2:00db719ebd81 40 // Se asigna baudrate y se configura el puerto serie de la USART
NicolasV 3:2e823b16a4a1 41
NicolasV 2:00db719ebd81 42 GSM.baud(9600);
NicolasV 2:00db719ebd81 43 GSM.format(8,Serial::None,1);
NicolasV 3:2e823b16a4a1 44 t.start();
NicolasV 3:2e823b16a4a1 45
NicolasV 5:82ac4b34c3d9 46
NicolasV 5:82ac4b34c3d9 47 // Animación de bienvenida:
NicolasV 5:82ac4b34c3d9 48
NicolasV 5:82ac4b34c3d9 49 for (int i = 3; i < 15; ++i) {
NicolasV 3:2e823b16a4a1 50 lcd.cls();
NicolasV 3:2e823b16a4a1 51 lcd.locate(0,0);
NicolasV 3:2e823b16a4a1 52 lcd.printf("**Control PID**");
NicolasV 3:2e823b16a4a1 53 wait_ms(1000/i);
NicolasV 3:2e823b16a4a1 54
NicolasV 3:2e823b16a4a1 55 ++i;
NicolasV 3:2e823b16a4a1 56 lcd.cls();
NicolasV 3:2e823b16a4a1 57 lcd.locate(1,0);
NicolasV 3:2e823b16a4a1 58 lcd.printf("**Control PID**");
NicolasV 3:2e823b16a4a1 59 wait_ms(1000/i);
NicolasV 3:2e823b16a4a1 60
NicolasV 3:2e823b16a4a1 61 ++i;
NicolasV 3:2e823b16a4a1 62 lcd.cls();
NicolasV 3:2e823b16a4a1 63 lcd.locate(1,1);
NicolasV 3:2e823b16a4a1 64 lcd.printf("**Control PID**");
NicolasV 3:2e823b16a4a1 65 wait_ms(1000/i);
NicolasV 3:2e823b16a4a1 66
NicolasV 3:2e823b16a4a1 67 ++i;
NicolasV 3:2e823b16a4a1 68 lcd.cls();
NicolasV 3:2e823b16a4a1 69 lcd.locate(0,1);
NicolasV 3:2e823b16a4a1 70 lcd.printf("**Control PID**");
NicolasV 3:2e823b16a4a1 71 wait_ms(1000/i);
NicolasV 3:2e823b16a4a1 72 }
NicolasV 3:2e823b16a4a1 73
NicolasV 3:2e823b16a4a1 74 init:
NicolasV 3:2e823b16a4a1 75
NicolasV 3:2e823b16a4a1 76 lcd.cls();
NicolasV 3:2e823b16a4a1 77 lcd.writeCommand(C1);
NicolasV 3:2e823b16a4a1 78
NicolasV 3:2e823b16a4a1 79 lcd.locate(8,0);
NicolasV 3:2e823b16a4a1 80 lcd.printf("Ki=%d", kinum);
NicolasV 2:00db719ebd81 81
tony63 0:4e0dfcf0e7ce 82 lcd.locate(0,1);
NicolasV 3:2e823b16a4a1 83 lcd.printf("Kd=%d", kdnum);
NicolasV 2:00db719ebd81 84
NicolasV 3:2e823b16a4a1 85 lcd.locate(8,1);
NicolasV 3:2e823b16a4a1 86 lcd.printf("**PID**");
NicolasV 3:2e823b16a4a1 87
NicolasV 3:2e823b16a4a1 88 lcd.locate(0,0);
NicolasV 2:00db719ebd81 89 lcd.printf("Kp=%d", kpnum);
NicolasV 3:2e823b16a4a1 90
NicolasV 5:82ac4b34c3d9 91 bool state = 1; // Esta variable determina si se están modificando las constantes del controlador o el Set-Point.
NicolasV 3:2e823b16a4a1 92
NicolasV 2:00db719ebd81 93 while(true) {
tony63 0:4e0dfcf0e7ce 94
NicolasV 3:2e823b16a4a1 95 diferencia=(encoder.getPulses() - cambio)*3;
tony63 0:4e0dfcf0e7ce 96 cambio=encoder.getPulses();
NicolasV 2:00db719ebd81 97
NicolasV 3:2e823b16a4a1 98 if (diferencia != 0 && state) {
NicolasV 2:00db719ebd81 99 switch (pos) {
NicolasV 2:00db719ebd81 100 case 1:
NicolasV 2:00db719ebd81 101 kpnum += diferencia;
NicolasV 2:00db719ebd81 102
NicolasV 2:00db719ebd81 103 if (kpnum >= 999)
NicolasV 2:00db719ebd81 104 kpnum = 999;
NicolasV 2:00db719ebd81 105 else if (kpnum < 0)
NicolasV 2:00db719ebd81 106 kpnum = 0;
NicolasV 3:2e823b16a4a1 107
NicolasV 3:2e823b16a4a1 108 lcd.locate(3,0);
tony63 0:4e0dfcf0e7ce 109 lcd.printf(" ");
NicolasV 3:2e823b16a4a1 110 lcd.locate(3,0);
tony63 0:4e0dfcf0e7ce 111 lcd.printf("%d", kpnum);
NicolasV 2:00db719ebd81 112 break;
NicolasV 3:2e823b16a4a1 113 case 2:
NicolasV 2:00db719ebd81 114 kinum += diferencia;
NicolasV 2:00db719ebd81 115
NicolasV 2:00db719ebd81 116 if (kinum >= 999)
NicolasV 2:00db719ebd81 117 kinum = 999;
NicolasV 2:00db719ebd81 118 else if (kinum < 0)
NicolasV 2:00db719ebd81 119 kinum = 0;
NicolasV 2:00db719ebd81 120
NicolasV 3:2e823b16a4a1 121 lcd.locate(11,0);
tony63 0:4e0dfcf0e7ce 122 lcd.printf(" ");
NicolasV 3:2e823b16a4a1 123 lcd.locate(11,0);
tony63 0:4e0dfcf0e7ce 124 lcd.printf("%d", kinum);
NicolasV 2:00db719ebd81 125 break;
NicolasV 3:2e823b16a4a1 126 case 3:
NicolasV 2:00db719ebd81 127 kdnum += diferencia;
NicolasV 2:00db719ebd81 128
NicolasV 2:00db719ebd81 129 if (kdnum >= 999)
NicolasV 2:00db719ebd81 130 kdnum = 999;
NicolasV 2:00db719ebd81 131 else if (kdnum < 0)
NicolasV 2:00db719ebd81 132 kdnum = 0;
NicolasV 2:00db719ebd81 133
NicolasV 3:2e823b16a4a1 134 lcd.locate(3,1);
tony63 0:4e0dfcf0e7ce 135 lcd.printf(" ");
NicolasV 3:2e823b16a4a1 136 lcd.locate(3,1);
tony63 0:4e0dfcf0e7ce 137 lcd.printf("%d", kdnum);
NicolasV 2:00db719ebd81 138 break;
NicolasV 3:2e823b16a4a1 139 }
NicolasV 3:2e823b16a4a1 140 }
NicolasV 3:2e823b16a4a1 141
NicolasV 3:2e823b16a4a1 142 if (diferencia != 0 && !state) {
NicolasV 3:2e823b16a4a1 143
NicolasV 3:2e823b16a4a1 144 spnum += diferencia;
NicolasV 3:2e823b16a4a1 145
NicolasV 3:2e823b16a4a1 146 if (spnum >= 999) spnum = 999;
NicolasV 3:2e823b16a4a1 147 else if (spnum < 0) spnum = 0;
NicolasV 3:2e823b16a4a1 148
NicolasV 3:2e823b16a4a1 149 lcd.locate(10,0);
NicolasV 3:2e823b16a4a1 150 lcd.printf(" ");
NicolasV 3:2e823b16a4a1 151 lcd.locate(10,0);
NicolasV 3:2e823b16a4a1 152 lcd.printf("%d", spnum);
NicolasV 3:2e823b16a4a1 153 }
NicolasV 3:2e823b16a4a1 154
NicolasV 2:00db719ebd81 155
NicolasV 2:00db719ebd81 156 if (!button3) { // Cambia la posición de ingreso de parámetros
NicolasV 5:82ac4b34c3d9 157 led2 = !led2;
NicolasV 2:00db719ebd81 158 switch(pos++) {
NicolasV 3:2e823b16a4a1 159 case 3:
NicolasV 2:00db719ebd81 160 pos = 1;
tony63 0:4e0dfcf0e7ce 161 lcd.locate(3,0);
NicolasV 3:2e823b16a4a1 162 lcd.printf("%d", kpnum);
NicolasV 2:00db719ebd81 163 break;
NicolasV 2:00db719ebd81 164 case 1:
tony63 0:4e0dfcf0e7ce 165 lcd.locate(11,0);
NicolasV 3:2e823b16a4a1 166 lcd.printf("%d", kinum);
NicolasV 2:00db719ebd81 167 break;
NicolasV 2:00db719ebd81 168 case 2:
tony63 0:4e0dfcf0e7ce 169 lcd.locate(3,1);
tony63 0:4e0dfcf0e7ce 170 lcd.printf("%d", kdnum);
NicolasV 2:00db719ebd81 171 break;
NicolasV 3:2e823b16a4a1 172 }
NicolasV 3:2e823b16a4a1 173 wait(0.2);
NicolasV 3:2e823b16a4a1 174 }
tony63 0:4e0dfcf0e7ce 175
NicolasV 3:2e823b16a4a1 176 if (!button4 && state) {
NicolasV 3:2e823b16a4a1 177 lcd.cls();
NicolasV 3:2e823b16a4a1 178 lcd.locate(8,1);
NicolasV 3:2e823b16a4a1 179 lcd.printf("**PID**");
NicolasV 3:2e823b16a4a1 180 lcd.locate(0,0);
NicolasV 3:2e823b16a4a1 181 lcd.printf("Set-Point=");
NicolasV 3:2e823b16a4a1 182 lcd.locate(10,0);
NicolasV 3:2e823b16a4a1 183 lcd.printf("%d", spnum);
NicolasV 3:2e823b16a4a1 184 wait(0.2);
NicolasV 3:2e823b16a4a1 185 state = 0;
NicolasV 3:2e823b16a4a1 186 }
NicolasV 3:2e823b16a4a1 187 else if (!button4 && !state) {
NicolasV 3:2e823b16a4a1 188 wait(0.2);
NicolasV 3:2e823b16a4a1 189 break;
tony63 0:4e0dfcf0e7ce 190 }
tony63 0:4e0dfcf0e7ce 191 wait(0.1);
tony63 0:4e0dfcf0e7ce 192 }
NicolasV 3:2e823b16a4a1 193
tony63 0:4e0dfcf0e7ce 194
NicolasV 3:2e823b16a4a1 195 // Transición
NicolasV 2:00db719ebd81 196 lcd.writeCommand(C4); // Escribimos un comando segun el manual del módulo LCD para quitar cursor bajo
tony63 0:4e0dfcf0e7ce 197 lcd.cls();
NicolasV 3:2e823b16a4a1 198 lcd.locate(0,0); lcd.printf("y=");
NicolasV 3:2e823b16a4a1 199 lcd.locate(8,0); lcd.printf("e=");
NicolasV 3:2e823b16a4a1 200 lcd.locate(0,1); lcd.printf("r=");
NicolasV 3:2e823b16a4a1 201 lcd.locate(8,1); lcd.printf("u=");
tony63 0:4e0dfcf0e7ce 202
NicolasV 3:2e823b16a4a1 203 loop:
NicolasV 2:00db719ebd81 204
NicolasV 3:2e823b16a4a1 205 if (t.read_ms() - t_pid > 10) {
NicolasV 3:2e823b16a4a1 206
NicolasV 3:2e823b16a4a1 207 med = y.read()*999;
NicolasV 5:82ac4b34c3d9 208 err = spnum - med; // Se calcula el error
NicolasV 5:82ac4b34c3d9 209 ap = kpnum*err*0.01f; // Se calcula la acción proporcional
NicolasV 5:82ac4b34c3d9 210 ai += kinum*err*0.01f; // Cálculo de la integral del error
NicolasV 2:00db719ebd81 211 ad = kdnum*(err - err_v)*0.01f; // Cálculo de la acción derivativa
NicolasV 2:00db719ebd81 212 pid = ap + ai + ad;
NicolasV 3:2e823b16a4a1 213
NicolasV 3:2e823b16a4a1 214 if (pid <= 0) pid = 0;
NicolasV 3:2e823b16a4a1 215 if (pid > 999) pid = 999;
NicolasV 2:00db719ebd81 216
NicolasV 3:2e823b16a4a1 217 setpoint = spnum;
NicolasV 5:82ac4b34c3d9 218 GSM.printf("#%3.0f%3.0f&", med, setpoint); // Se envía una cadena de caracteres por el puerto serial. Se agregan identificadores #...& que denotan el comienzo y la terminación de la misma.
NicolasV 5:82ac4b34c3d9 219 // Esto se hace con el fin de evitar errores en la transmisión. Estos identificadores se usan en la aplicación de AppInventor.
NicolasV 3:2e823b16a4a1 220 lcd.locate(3,0); lcd.printf("%3.0f ", med);
NicolasV 3:2e823b16a4a1 221 lcd.locate(3,1); lcd.printf("%3d ", spnum);
NicolasV 3:2e823b16a4a1 222 lcd.locate(11,0); lcd.printf("%3.0f ", err);
NicolasV 3:2e823b16a4a1 223 lcd.locate(11,1); lcd.printf("%3.0f ", pid);
tony63 0:4e0dfcf0e7ce 224
tony63 1:058b8f5c135d 225 err_v = err;
tony63 1:058b8f5c135d 226 o = pid/999;
tony63 1:058b8f5c135d 227 u.write(o);
tony63 1:058b8f5c135d 228
NicolasV 3:2e823b16a4a1 229 t_pid = t.read_ms();
NicolasV 3:2e823b16a4a1 230 }
NicolasV 3:2e823b16a4a1 231 else {
NicolasV 3:2e823b16a4a1 232
NicolasV 5:82ac4b34c3d9 233 diferencia=(encoder.getPulses() - cambio)*6; // Es posible cambiar el Set-Point cuando el controlador ya está funcionando.
NicolasV 3:2e823b16a4a1 234 cambio=encoder.getPulses();
NicolasV 3:2e823b16a4a1 235
NicolasV 3:2e823b16a4a1 236 if (diferencia != 0) {
NicolasV 3:2e823b16a4a1 237 spnum += diferencia;
NicolasV 3:2e823b16a4a1 238 if (spnum >= 999) spnum = 999;
NicolasV 3:2e823b16a4a1 239 else if (spnum < 0) spnum = 0;
NicolasV 3:2e823b16a4a1 240 }
NicolasV 3:2e823b16a4a1 241
NicolasV 3:2e823b16a4a1 242 }
NicolasV 3:2e823b16a4a1 243
NicolasV 5:82ac4b34c3d9 244 if (!button4) { // Si se presiona el pulsador se vuelve al menú inicial para configurar de nuevo el controlador.
NicolasV 3:2e823b16a4a1 245 u.write(0);
NicolasV 3:2e823b16a4a1 246 med = 0;
NicolasV 3:2e823b16a4a1 247 err = 0;
NicolasV 3:2e823b16a4a1 248 pid = 0;
NicolasV 3:2e823b16a4a1 249
NicolasV 3:2e823b16a4a1 250 goto init;
NicolasV 3:2e823b16a4a1 251 }
NicolasV 3:2e823b16a4a1 252 else
NicolasV 2:00db719ebd81 253 goto loop;
NicolasV 3:2e823b16a4a1 254
NicolasV 3:2e823b16a4a1 255 }