Procesadores, 2016 - II. Tarea #1. Oswaldo Andrés Giraldo Giraldo, Héctor Andrés Hoyos Ceballos, Jose Fernando Montoya Vargas, María Fernanda Villa Tamayo. Programa para establecer la comunicación con una aplicación en App Inventor por medio de Bluetooth.
Fork of TAREA_1_PID_BLUETOOTH by
main.cpp@0:24c2d41ff63f, 2016-11-16 (annotated)
- Committer:
- joshema216
- Date:
- Wed Nov 16 02:14:33 2016 +0000
- Revision:
- 0:24c2d41ff63f
Programa para establecer la comunicaci?n con una aplicaci?n dise?ada en App Inventor por medio de Bluetooth.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joshema216 | 0:24c2d41ff63f | 1 | // Programa para ejecutar un control PID que lee las constantes del controlador desde la UART, |
joshema216 | 0:24c2d41ff63f | 2 | // que son enviadas por Bluetooth desde un proyecto de App Inventor ejecutado en un dispositivo |
joshema216 | 0:24c2d41ff63f | 3 | // Android como cadena de caracteres. Cuando se inicia el proceso de control se envían los |
joshema216 | 0:24c2d41ff63f | 4 | // parámetros del mismo por Bluetooth hacia el dispositivo Android, el proyecto en App Inventor |
joshema216 | 0:24c2d41ff63f | 5 | // permite recibir la cadena de parámetros y separarlos para asignarlos a su respectivo indicador. |
joshema216 | 0:24c2d41ff63f | 6 | |
joshema216 | 0:24c2d41ff63f | 7 | // Oswaldo Andrés Giraldo Giraldo - C.C.: 1152458465 |
joshema216 | 0:24c2d41ff63f | 8 | // Héctor Andrés Hoyos Ceballos - C.C.: 1039466317 |
joshema216 | 0:24c2d41ff63f | 9 | // Jose Fernando Montoya Vargas - C.C.: 1039468676 |
joshema216 | 0:24c2d41ff63f | 10 | // María Fernanda Villa Tamayo - C.C.: 1152457490 |
joshema216 | 0:24c2d41ff63f | 11 | |
joshema216 | 0:24c2d41ff63f | 12 | #include "mbed.h" |
joshema216 | 0:24c2d41ff63f | 13 | #include "TextLCD.h" |
joshema216 | 0:24c2d41ff63f | 14 | |
joshema216 | 0:24c2d41ff63f | 15 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // RS, E, D4 - D7 |
joshema216 | 0:24c2d41ff63f | 16 | Serial andr(USBTX, USBRX); // TX, RX |
joshema216 | 0:24c2d41ff63f | 17 | Serial device(PTE0, PTE1); // TX, RX |
joshema216 | 0:24c2d41ff63f | 18 | |
joshema216 | 0:24c2d41ff63f | 19 | AnalogIn y(PTB3); // Entrada análoga |
joshema216 | 0:24c2d41ff63f | 20 | AnalogOut u(PTE30); // Salida análoga |
joshema216 | 0:24c2d41ff63f | 21 | DigitalOut led1(LED1); |
joshema216 | 0:24c2d41ff63f | 22 | DigitalOut led2(LED2); |
joshema216 | 0:24c2d41ff63f | 23 | DigitalOut led3(LED3); |
joshema216 | 0:24c2d41ff63f | 24 | |
joshema216 | 0:24c2d41ff63f | 25 | // Códigos movimiento del cursor. |
joshema216 | 0:24c2d41ff63f | 26 | //int C1=0x0E; // Sólo muestra el cursor. |
joshema216 | 0:24c2d41ff63f | 27 | int C2=0x18; // Desplaza hacia la izquierda. |
joshema216 | 0:24c2d41ff63f | 28 | int C3=0x1A; // Desplaza hacia la derecha. |
joshema216 | 0:24c2d41ff63f | 29 | int C4=0x0C; // Quita cursor bajo. |
joshema216 | 0:24c2d41ff63f | 30 | int C1=0x0F; |
joshema216 | 0:24c2d41ff63f | 31 | |
joshema216 | 0:24c2d41ff63f | 32 | float pid,o,ai,ad,ap,med,err,medr; |
joshema216 | 0:24c2d41ff63f | 33 | float err_v; |
joshema216 | 0:24c2d41ff63f | 34 | int spnum = 0, kinum = 0,kpnum = 0, kdnum = 0; |
joshema216 | 0:24c2d41ff63f | 35 | char outmed[16], outmedn[16], outpid[16], outpidn[16], outspnum[16], outspnumn[16], outerr[16], outerrn[16], varout[40]; |
joshema216 | 0:24c2d41ff63f | 36 | |
joshema216 | 0:24c2d41ff63f | 37 | Timer t; |
joshema216 | 0:24c2d41ff63f | 38 | char buffer[4], resp[4], ind; |
joshema216 | 0:24c2d41ff63f | 39 | int count, i; |
joshema216 | 0:24c2d41ff63f | 40 | int k = 0, j = 0, num = 0; |
joshema216 | 0:24c2d41ff63f | 41 | |
joshema216 | 0:24c2d41ff63f | 42 | // Reverses a string 'str' of length 'len' |
joshema216 | 0:24c2d41ff63f | 43 | // driver program to test above funtion. |
joshema216 | 0:24c2d41ff63f | 44 | void reverse(char *str, int len) |
joshema216 | 0:24c2d41ff63f | 45 | { |
joshema216 | 0:24c2d41ff63f | 46 | int i=0, j=len-1, temp; |
joshema216 | 0:24c2d41ff63f | 47 | while (i<j) |
joshema216 | 0:24c2d41ff63f | 48 | { |
joshema216 | 0:24c2d41ff63f | 49 | temp = str[i]; |
joshema216 | 0:24c2d41ff63f | 50 | str[i] = str[j]; |
joshema216 | 0:24c2d41ff63f | 51 | str[j] = temp; |
joshema216 | 0:24c2d41ff63f | 52 | i++; j--; |
joshema216 | 0:24c2d41ff63f | 53 | } |
joshema216 | 0:24c2d41ff63f | 54 | } |
joshema216 | 0:24c2d41ff63f | 55 | |
joshema216 | 0:24c2d41ff63f | 56 | // Converts a given integer x to string str[]. d is the number |
joshema216 | 0:24c2d41ff63f | 57 | // of digits required in output. If d is more than the number |
joshema216 | 0:24c2d41ff63f | 58 | // of digits in x, then 0s are added at the beginning. |
joshema216 | 0:24c2d41ff63f | 59 | int intToStr(int x, char str[], int d) |
joshema216 | 0:24c2d41ff63f | 60 | { |
joshema216 | 0:24c2d41ff63f | 61 | int i = 0; |
joshema216 | 0:24c2d41ff63f | 62 | while (x) |
joshema216 | 0:24c2d41ff63f | 63 | { |
joshema216 | 0:24c2d41ff63f | 64 | str[i++] = (x%10) + '0'; |
joshema216 | 0:24c2d41ff63f | 65 | x = x/10; |
joshema216 | 0:24c2d41ff63f | 66 | } |
joshema216 | 0:24c2d41ff63f | 67 | |
joshema216 | 0:24c2d41ff63f | 68 | // If number of digits required is more, then |
joshema216 | 0:24c2d41ff63f | 69 | // add 0s at the beginning |
joshema216 | 0:24c2d41ff63f | 70 | while (i < d) |
joshema216 | 0:24c2d41ff63f | 71 | str[i++] = '0'; |
joshema216 | 0:24c2d41ff63f | 72 | |
joshema216 | 0:24c2d41ff63f | 73 | reverse(str, i); |
joshema216 | 0:24c2d41ff63f | 74 | str[i] = '\0'; |
joshema216 | 0:24c2d41ff63f | 75 | return i; |
joshema216 | 0:24c2d41ff63f | 76 | } |
joshema216 | 0:24c2d41ff63f | 77 | |
joshema216 | 0:24c2d41ff63f | 78 | // Converts a floating point number to string. |
joshema216 | 0:24c2d41ff63f | 79 | void ftoa(float n, char *res, int afterpoint) |
joshema216 | 0:24c2d41ff63f | 80 | { |
joshema216 | 0:24c2d41ff63f | 81 | // Extract integer part |
joshema216 | 0:24c2d41ff63f | 82 | int ipart = (int)n; |
joshema216 | 0:24c2d41ff63f | 83 | |
joshema216 | 0:24c2d41ff63f | 84 | // Extract floating part |
joshema216 | 0:24c2d41ff63f | 85 | float fpart = n - (float)ipart; |
joshema216 | 0:24c2d41ff63f | 86 | |
joshema216 | 0:24c2d41ff63f | 87 | // convert integer part to string |
joshema216 | 0:24c2d41ff63f | 88 | int i = intToStr(ipart, res, 0); |
joshema216 | 0:24c2d41ff63f | 89 | |
joshema216 | 0:24c2d41ff63f | 90 | // check for display option after point |
joshema216 | 0:24c2d41ff63f | 91 | if (afterpoint != 0) |
joshema216 | 0:24c2d41ff63f | 92 | { |
joshema216 | 0:24c2d41ff63f | 93 | res[i] = '.'; // add dot |
joshema216 | 0:24c2d41ff63f | 94 | |
joshema216 | 0:24c2d41ff63f | 95 | // Get the value of fraction part upto given no. |
joshema216 | 0:24c2d41ff63f | 96 | // of points after dot. The third parameter is needed |
joshema216 | 0:24c2d41ff63f | 97 | // to handle cases like 233.007 |
joshema216 | 0:24c2d41ff63f | 98 | float fp=10; |
joshema216 | 0:24c2d41ff63f | 99 | fpart =fpart * pow(fp,afterpoint); |
joshema216 | 0:24c2d41ff63f | 100 | |
joshema216 | 0:24c2d41ff63f | 101 | intToStr((int)fpart, res + i + 1, afterpoint); |
joshema216 | 0:24c2d41ff63f | 102 | } |
joshema216 | 0:24c2d41ff63f | 103 | } |
joshema216 | 0:24c2d41ff63f | 104 | |
joshema216 | 0:24c2d41ff63f | 105 | // Lee la cadena de caracteres que llegan a la UART. |
joshema216 | 0:24c2d41ff63f | 106 | int readBuffer(char *buffer,int count) |
joshema216 | 0:24c2d41ff63f | 107 | { |
joshema216 | 0:24c2d41ff63f | 108 | int i=0; |
joshema216 | 0:24c2d41ff63f | 109 | t.start(); // start timer |
joshema216 | 0:24c2d41ff63f | 110 | while(1) { |
joshema216 | 0:24c2d41ff63f | 111 | while (device.readable()) { |
joshema216 | 0:24c2d41ff63f | 112 | char c = device.getc(); |
joshema216 | 0:24c2d41ff63f | 113 | if (c == '\r' || c == '\n') c = '$'; |
joshema216 | 0:24c2d41ff63f | 114 | buffer[i++] = c; |
joshema216 | 0:24c2d41ff63f | 115 | if(i > count)break; |
joshema216 | 0:24c2d41ff63f | 116 | } |
joshema216 | 0:24c2d41ff63f | 117 | if(i > count)break; |
joshema216 | 0:24c2d41ff63f | 118 | if(t.read() > 1) { |
joshema216 | 0:24c2d41ff63f | 119 | t.stop(); |
joshema216 | 0:24c2d41ff63f | 120 | t.reset(); |
joshema216 | 0:24c2d41ff63f | 121 | break; |
joshema216 | 0:24c2d41ff63f | 122 | } |
joshema216 | 0:24c2d41ff63f | 123 | } |
joshema216 | 0:24c2d41ff63f | 124 | return 0; |
joshema216 | 0:24c2d41ff63f | 125 | } |
joshema216 | 0:24c2d41ff63f | 126 | |
joshema216 | 0:24c2d41ff63f | 127 | // Limpia una cadena de caracteres. |
joshema216 | 0:24c2d41ff63f | 128 | void cleanBuffer(char *buffer, int count) |
joshema216 | 0:24c2d41ff63f | 129 | { |
joshema216 | 0:24c2d41ff63f | 130 | for(int i=0; i < count; i++) { |
joshema216 | 0:24c2d41ff63f | 131 | buffer[i] = '\0'; |
joshema216 | 0:24c2d41ff63f | 132 | } |
joshema216 | 0:24c2d41ff63f | 133 | } |
joshema216 | 0:24c2d41ff63f | 134 | |
joshema216 | 0:24c2d41ff63f | 135 | int main(void) { |
joshema216 | 0:24c2d41ff63f | 136 | device.baud(9600);// Configura los baudios de la FRDMKL25Z en 9600. |
joshema216 | 0:24c2d41ff63f | 137 | device.format(8,Serial::None,1); // Configura el formato de los datos de la UART. |
joshema216 | 0:24c2d41ff63f | 138 | lcd.locate(1,1); // Se posiciona en col 1 fil 1. |
joshema216 | 0:24c2d41ff63f | 139 | lcd.printf("**Control PID**"); |
joshema216 | 0:24c2d41ff63f | 140 | wait(2); |
joshema216 | 0:24c2d41ff63f | 141 | |
joshema216 | 0:24c2d41ff63f | 142 | lop0: // CICLO DE ASIGNACIÓN DE VARIABLES. |
joshema216 | 0:24c2d41ff63f | 143 | lcd.cls(); // Borrar Pantalla. |
joshema216 | 0:24c2d41ff63f | 144 | lcd.writeCommand(C1);// Escribimos un comando segun el manual del modulo LCD. |
joshema216 | 0:24c2d41ff63f | 145 | lcd.locate(8,0); |
joshema216 | 0:24c2d41ff63f | 146 | lcd.printf("Kp=%d",kpnum); // Constante proporcional. |
joshema216 | 0:24c2d41ff63f | 147 | lcd.locate(0,1); |
joshema216 | 0:24c2d41ff63f | 148 | lcd.printf("Ki=%d",kinum); // Constante integral. |
joshema216 | 0:24c2d41ff63f | 149 | lcd.locate(8,1); |
joshema216 | 0:24c2d41ff63f | 150 | lcd.printf("Kd=%d",kdnum); // Constante derivativa. |
joshema216 | 0:24c2d41ff63f | 151 | lcd.locate(0,0); |
joshema216 | 0:24c2d41ff63f | 152 | lcd.printf("Sp=%d",spnum); // Set point (punto de ajuste) |
joshema216 | 0:24c2d41ff63f | 153 | |
joshema216 | 0:24c2d41ff63f | 154 | while(1){ |
joshema216 | 0:24c2d41ff63f | 155 | if(device.readable()){ // Si llega algún paquete por la UART. |
joshema216 | 0:24c2d41ff63f | 156 | readBuffer(buffer,4); // Leer la cadena de caracteres. |
joshema216 | 0:24c2d41ff63f | 157 | led1 =!led1; |
joshema216 | 0:24c2d41ff63f | 158 | //andr.putc(device.getc()); |
joshema216 | 0:24c2d41ff63f | 159 | andr.printf("%s\r\n",buffer); |
joshema216 | 0:24c2d41ff63f | 160 | ind = buffer[0]; // Las cadenas que llegan tienen un indicador en la primera posición. |
joshema216 | 0:24c2d41ff63f | 161 | |
joshema216 | 0:24c2d41ff63f | 162 | if (ind == '4') { // Si llega una cadena con el indicador '4' (Sp). |
joshema216 | 0:24c2d41ff63f | 163 | resp[0] = buffer[1]; // Toma el valor de la constante ingresada. |
joshema216 | 0:24c2d41ff63f | 164 | resp[1] = buffer[2]; |
joshema216 | 0:24c2d41ff63f | 165 | resp[2] = buffer[3]; |
joshema216 | 0:24c2d41ff63f | 166 | spnum = strtod(resp,NULL); // Asigna la cadena a un entero. |
joshema216 | 0:24c2d41ff63f | 167 | //andr.printf("%s\r\n",resp); |
joshema216 | 0:24c2d41ff63f | 168 | lcd.locate(3,0); |
joshema216 | 0:24c2d41ff63f | 169 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 170 | lcd.locate(3,0); |
joshema216 | 0:24c2d41ff63f | 171 | lcd.printf("%d", spnum); // Imprime la referencia. |
joshema216 | 0:24c2d41ff63f | 172 | led1 =!led1; |
joshema216 | 0:24c2d41ff63f | 173 | } |
joshema216 | 0:24c2d41ff63f | 174 | |
joshema216 | 0:24c2d41ff63f | 175 | if (ind == '1') { // Si llega una cadena con el indicador '1' (Kp). |
joshema216 | 0:24c2d41ff63f | 176 | resp[0] = buffer[1]; // Toma el valor de la constante ingresada. |
joshema216 | 0:24c2d41ff63f | 177 | resp[1] = buffer[2]; |
joshema216 | 0:24c2d41ff63f | 178 | resp[2] = buffer[3]; |
joshema216 | 0:24c2d41ff63f | 179 | kpnum = strtod(resp,NULL); // Asigna la cadena a un entero. |
joshema216 | 0:24c2d41ff63f | 180 | //pc.printf("%s\r\n",resp); |
joshema216 | 0:24c2d41ff63f | 181 | lcd.locate(11,0); |
joshema216 | 0:24c2d41ff63f | 182 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 183 | lcd.locate(11,0); |
joshema216 | 0:24c2d41ff63f | 184 | lcd.printf("%d", kpnum); // Imprime Kp. |
joshema216 | 0:24c2d41ff63f | 185 | led1 =!led1; |
joshema216 | 0:24c2d41ff63f | 186 | } |
joshema216 | 0:24c2d41ff63f | 187 | |
joshema216 | 0:24c2d41ff63f | 188 | if (ind == '2') { // Si llega una cadena con el indicador '2' (Ki). |
joshema216 | 0:24c2d41ff63f | 189 | resp[0] = buffer[1]; // Toma el valor de la constante ingresada. |
joshema216 | 0:24c2d41ff63f | 190 | resp[1] = buffer[2]; |
joshema216 | 0:24c2d41ff63f | 191 | resp[2] = buffer[3]; |
joshema216 | 0:24c2d41ff63f | 192 | kinum = strtod(resp,NULL); // Asigna la cadena a un entero. |
joshema216 | 0:24c2d41ff63f | 193 | //pc.printf("%s\r\n",resp); |
joshema216 | 0:24c2d41ff63f | 194 | lcd.locate(3,1); |
joshema216 | 0:24c2d41ff63f | 195 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 196 | lcd.locate(3,1); |
joshema216 | 0:24c2d41ff63f | 197 | lcd.printf("%d", kinum); //Imprime Ki. |
joshema216 | 0:24c2d41ff63f | 198 | led1 =!led1; |
joshema216 | 0:24c2d41ff63f | 199 | } |
joshema216 | 0:24c2d41ff63f | 200 | |
joshema216 | 0:24c2d41ff63f | 201 | if (ind == '3') { // Si llega una cadena con el indicador '3' (Kd). |
joshema216 | 0:24c2d41ff63f | 202 | resp[0] = buffer[1]; // Toma el valor de la constante ingresada. |
joshema216 | 0:24c2d41ff63f | 203 | resp[1] = buffer[2]; |
joshema216 | 0:24c2d41ff63f | 204 | resp[2] = buffer[3]; |
joshema216 | 0:24c2d41ff63f | 205 | kdnum = strtod(resp,NULL); // Asigna la cadena a un entero. |
joshema216 | 0:24c2d41ff63f | 206 | //pc.printf("%s\r\n",resp); |
joshema216 | 0:24c2d41ff63f | 207 | lcd.locate(11,1); |
joshema216 | 0:24c2d41ff63f | 208 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 209 | lcd.locate(11,1); |
joshema216 | 0:24c2d41ff63f | 210 | lcd.printf("%d", kdnum); // Imprime Kd. |
joshema216 | 0:24c2d41ff63f | 211 | led1 =!led1; |
joshema216 | 0:24c2d41ff63f | 212 | } |
joshema216 | 0:24c2d41ff63f | 213 | |
joshema216 | 0:24c2d41ff63f | 214 | if (ind == '5') { // Si llega una cadena con el indicador '5' (Borrar). |
joshema216 | 0:24c2d41ff63f | 215 | led1 =!led1; |
joshema216 | 0:24c2d41ff63f | 216 | led2 =!led2; |
joshema216 | 0:24c2d41ff63f | 217 | led3 =!led3; |
joshema216 | 0:24c2d41ff63f | 218 | kpnum = 0; // Borra las constantes del controlador. |
joshema216 | 0:24c2d41ff63f | 219 | kinum = 0; |
joshema216 | 0:24c2d41ff63f | 220 | kdnum = 0; |
joshema216 | 0:24c2d41ff63f | 221 | spnum = 0; |
joshema216 | 0:24c2d41ff63f | 222 | lcd.cls(); // Borrar Pantalla. |
joshema216 | 0:24c2d41ff63f | 223 | lcd.locate(1,1); // Se posiciona en col 1 fil 1. |
joshema216 | 0:24c2d41ff63f | 224 | lcd.printf("DATOS BORRADOS"); |
joshema216 | 0:24c2d41ff63f | 225 | wait(2); |
joshema216 | 0:24c2d41ff63f | 226 | lcd.cls(); // Borrar Pantalla. |
joshema216 | 0:24c2d41ff63f | 227 | lcd.writeCommand(C1);// Escribimos un comando segun el manual del modulo LCD. |
joshema216 | 0:24c2d41ff63f | 228 | lcd.locate(8,0); |
joshema216 | 0:24c2d41ff63f | 229 | lcd.printf("Kp=%d",kpnum); |
joshema216 | 0:24c2d41ff63f | 230 | lcd.locate(0,1); |
joshema216 | 0:24c2d41ff63f | 231 | lcd.printf("Ki=%d",kinum); |
joshema216 | 0:24c2d41ff63f | 232 | lcd.locate(8,1); |
joshema216 | 0:24c2d41ff63f | 233 | lcd.printf("Kd=%d",kdnum); |
joshema216 | 0:24c2d41ff63f | 234 | lcd.locate(0,0); |
joshema216 | 0:24c2d41ff63f | 235 | lcd.printf("Sp=%d",spnum); |
joshema216 | 0:24c2d41ff63f | 236 | led2 =!led2; |
joshema216 | 0:24c2d41ff63f | 237 | led3 =!led3; |
joshema216 | 0:24c2d41ff63f | 238 | } |
joshema216 | 0:24c2d41ff63f | 239 | |
joshema216 | 0:24c2d41ff63f | 240 | if (ind == '7') { // Si llega una cadena con el indicador '7' (Detener). |
joshema216 | 0:24c2d41ff63f | 241 | led1 =!led1; // No hacer nada, pues el controlador no se ha ejecutado. |
joshema216 | 0:24c2d41ff63f | 242 | } |
joshema216 | 0:24c2d41ff63f | 243 | |
joshema216 | 0:24c2d41ff63f | 244 | if (ind == '6') { // Si llega una cadena con el indicador '6' (Iniciar). |
joshema216 | 0:24c2d41ff63f | 245 | led1 =!led1; |
joshema216 | 0:24c2d41ff63f | 246 | led3 =!led3; |
joshema216 | 0:24c2d41ff63f | 247 | |
joshema216 | 0:24c2d41ff63f | 248 | // Transición. |
joshema216 | 0:24c2d41ff63f | 249 | lcd.writeCommand(C4);//Escribimos un comando segun el manual del modulo LCD para quitar cursor bajo. |
joshema216 | 0:24c2d41ff63f | 250 | lcd.cls(); // Borra la pantalla. |
joshema216 | 0:24c2d41ff63f | 251 | lcd.locate(1,1); // Se posiciona en col 1 fil 1. |
joshema216 | 0:24c2d41ff63f | 252 | lcd.printf("DATOS GUARDADOS"); |
joshema216 | 0:24c2d41ff63f | 253 | wait(1); |
joshema216 | 0:24c2d41ff63f | 254 | lcd.cls(); |
joshema216 | 0:24c2d41ff63f | 255 | lcd.locate(1,1); // Se posiciona en col 1 fil 1. |
joshema216 | 0:24c2d41ff63f | 256 | lcd.printf("INICIO DEL PID"); |
joshema216 | 0:24c2d41ff63f | 257 | wait(2); |
joshema216 | 0:24c2d41ff63f | 258 | |
joshema216 | 0:24c2d41ff63f | 259 | // Se imprimen los parches del control. |
joshema216 | 0:24c2d41ff63f | 260 | lcd.cls(); |
joshema216 | 0:24c2d41ff63f | 261 | lcd.printf("Er=%3.0f",err); |
joshema216 | 0:24c2d41ff63f | 262 | lcd.locate(8,0); |
joshema216 | 0:24c2d41ff63f | 263 | lcd.printf("Me=%3.0f",med); |
joshema216 | 0:24c2d41ff63f | 264 | lcd.locate(0,1); |
joshema216 | 0:24c2d41ff63f | 265 | lcd.printf("Sp=%3.0f",spnum); |
joshema216 | 0:24c2d41ff63f | 266 | lcd.locate(8,1); |
joshema216 | 0:24c2d41ff63f | 267 | lcd.printf("Co=%3.0f",pid); |
joshema216 | 0:24c2d41ff63f | 268 | wait(1); |
joshema216 | 0:24c2d41ff63f | 269 | |
joshema216 | 0:24c2d41ff63f | 270 | // CICLO PRINCIPAL CONTROLADOR PID. |
joshema216 | 0:24c2d41ff63f | 271 | lop1: |
joshema216 | 0:24c2d41ff63f | 272 | med = y.read()*999; // Se toma la medida actual por el puerto analógico. |
joshema216 | 0:24c2d41ff63f | 273 | //andr.printf("%f", med); |
joshema216 | 0:24c2d41ff63f | 274 | err = (spnum-med); // Se calcula el error. |
joshema216 | 0:24c2d41ff63f | 275 | //andr.printf("%f", err); |
joshema216 | 0:24c2d41ff63f | 276 | ap = kpnum*err*0.01f; // Se calcula la acción proporcinal. |
joshema216 | 0:24c2d41ff63f | 277 | ai =(kinum*err*0.01f)+ai; // Cálculo de la integral del error. |
joshema216 | 0:24c2d41ff63f | 278 | ad = kdnum*(err-err_v)*0.01f; // Cálculo de la acción derivativa. |
joshema216 | 0:24c2d41ff63f | 279 | pid = (ap+ai+ad); // Se actualiza el valor del la acción de control. |
joshema216 | 0:24c2d41ff63f | 280 | //andr.printf("%f", pid); |
joshema216 | 0:24c2d41ff63f | 281 | |
joshema216 | 0:24c2d41ff63f | 282 | // Se verifica que pid sea positivo. |
joshema216 | 0:24c2d41ff63f | 283 | if(pid<=0) { |
joshema216 | 0:24c2d41ff63f | 284 | pid=0; |
joshema216 | 0:24c2d41ff63f | 285 | } |
joshema216 | 0:24c2d41ff63f | 286 | |
joshema216 | 0:24c2d41ff63f | 287 | // Se verifica que pid sea menor o igual la valor máximo. |
joshema216 | 0:24c2d41ff63f | 288 | if (pid > 999) { |
joshema216 | 0:24c2d41ff63f | 289 | pid=999; |
joshema216 | 0:24c2d41ff63f | 290 | } |
joshema216 | 0:24c2d41ff63f | 291 | |
joshema216 | 0:24c2d41ff63f | 292 | // Se envían los datos del control como caracteres. |
joshema216 | 0:24c2d41ff63f | 293 | cleanBuffer(varout, 40); |
joshema216 | 0:24c2d41ff63f | 294 | cleanBuffer(outerr, 16); |
joshema216 | 0:24c2d41ff63f | 295 | cleanBuffer(outmed, 16); |
joshema216 | 0:24c2d41ff63f | 296 | cleanBuffer(outpid, 16); |
joshema216 | 0:24c2d41ff63f | 297 | cleanBuffer(outspnum, 16); |
joshema216 | 0:24c2d41ff63f | 298 | |
joshema216 | 0:24c2d41ff63f | 299 | ftoa(spnum, outspnum, 4); // Se convierte el Set point a caracter. |
joshema216 | 0:24c2d41ff63f | 300 | |
joshema216 | 0:24c2d41ff63f | 301 | if (err < 0 && err > -1){ // Se convierte el Error a caracter. |
joshema216 | 0:24c2d41ff63f | 302 | err = (-1)*err; |
joshema216 | 0:24c2d41ff63f | 303 | strcat(outerr, "0"); |
joshema216 | 0:24c2d41ff63f | 304 | ftoa(err, outerrn, 4); |
joshema216 | 0:24c2d41ff63f | 305 | strcat(outerr, outerrn); |
joshema216 | 0:24c2d41ff63f | 306 | } |
joshema216 | 0:24c2d41ff63f | 307 | else if (err <= -1){ |
joshema216 | 0:24c2d41ff63f | 308 | err = (-1)*err; |
joshema216 | 0:24c2d41ff63f | 309 | ftoa(err, outerr, 4); |
joshema216 | 0:24c2d41ff63f | 310 | } |
joshema216 | 0:24c2d41ff63f | 311 | else if (err > 0 && err < 1){ |
joshema216 | 0:24c2d41ff63f | 312 | strcat(outerr, "0"); |
joshema216 | 0:24c2d41ff63f | 313 | ftoa(err, outerrn, 4); |
joshema216 | 0:24c2d41ff63f | 314 | strcat(outerr, outerrn); |
joshema216 | 0:24c2d41ff63f | 315 | } |
joshema216 | 0:24c2d41ff63f | 316 | else{ |
joshema216 | 0:24c2d41ff63f | 317 | ftoa(err, outerr, 4); |
joshema216 | 0:24c2d41ff63f | 318 | } |
joshema216 | 0:24c2d41ff63f | 319 | |
joshema216 | 0:24c2d41ff63f | 320 | if (med < 1){ // Se convierte la Medida a caracter. |
joshema216 | 0:24c2d41ff63f | 321 | strcat(outmed, "0"); |
joshema216 | 0:24c2d41ff63f | 322 | ftoa(med, outmedn, 4); |
joshema216 | 0:24c2d41ff63f | 323 | strcat(outmed, outmedn); |
joshema216 | 0:24c2d41ff63f | 324 | } |
joshema216 | 0:24c2d41ff63f | 325 | else{ |
joshema216 | 0:24c2d41ff63f | 326 | ftoa(med, outmed, 4); |
joshema216 | 0:24c2d41ff63f | 327 | } |
joshema216 | 0:24c2d41ff63f | 328 | |
joshema216 | 0:24c2d41ff63f | 329 | if (pid < 1){ // Se convierte la Acción de control a caracter. |
joshema216 | 0:24c2d41ff63f | 330 | strcat(outpid, "0"); |
joshema216 | 0:24c2d41ff63f | 331 | ftoa(pid, outpidn, 4); |
joshema216 | 0:24c2d41ff63f | 332 | strcat(outpid, outpidn); |
joshema216 | 0:24c2d41ff63f | 333 | } |
joshema216 | 0:24c2d41ff63f | 334 | else{ |
joshema216 | 0:24c2d41ff63f | 335 | ftoa(pid, outpid, 4); |
joshema216 | 0:24c2d41ff63f | 336 | } |
joshema216 | 0:24c2d41ff63f | 337 | |
joshema216 | 0:24c2d41ff63f | 338 | strcat(varout, " /"); // Se prepara la cadena a ser enviada, se separa cada constante del control por "/". |
joshema216 | 0:24c2d41ff63f | 339 | strcat(varout, outspnum); |
joshema216 | 0:24c2d41ff63f | 340 | strcat(varout, "/"); |
joshema216 | 0:24c2d41ff63f | 341 | strcat(varout, outerr); |
joshema216 | 0:24c2d41ff63f | 342 | strcat(varout, "/"); |
joshema216 | 0:24c2d41ff63f | 343 | strcat(varout, outmed); |
joshema216 | 0:24c2d41ff63f | 344 | strcat(varout, "/"); |
joshema216 | 0:24c2d41ff63f | 345 | strcat(varout, outpid); |
joshema216 | 0:24c2d41ff63f | 346 | |
joshema216 | 0:24c2d41ff63f | 347 | device.printf(varout); |
joshema216 | 0:24c2d41ff63f | 348 | andr.printf("%s\n", varout); |
joshema216 | 0:24c2d41ff63f | 349 | |
joshema216 | 0:24c2d41ff63f | 350 | // Se muestran las variables en la LCD. |
joshema216 | 0:24c2d41ff63f | 351 | lcd.locate(3,0); |
joshema216 | 0:24c2d41ff63f | 352 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 353 | lcd.locate(3,0); |
joshema216 | 0:24c2d41ff63f | 354 | lcd.printf("%3.0f",err); |
joshema216 | 0:24c2d41ff63f | 355 | lcd.locate(11,0); |
joshema216 | 0:24c2d41ff63f | 356 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 357 | lcd.locate(11,0); |
joshema216 | 0:24c2d41ff63f | 358 | lcd.printf("%3.0f",med); |
joshema216 | 0:24c2d41ff63f | 359 | lcd.locate(3,1); |
joshema216 | 0:24c2d41ff63f | 360 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 361 | lcd.locate(3,1); |
joshema216 | 0:24c2d41ff63f | 362 | lcd.printf("%3.0d",spnum); |
joshema216 | 0:24c2d41ff63f | 363 | lcd.locate(11,1); |
joshema216 | 0:24c2d41ff63f | 364 | lcd.printf(" "); |
joshema216 | 0:24c2d41ff63f | 365 | lcd.locate(11,1); |
joshema216 | 0:24c2d41ff63f | 366 | lcd.printf("%3.0f",pid); |
joshema216 | 0:24c2d41ff63f | 367 | |
joshema216 | 0:24c2d41ff63f | 368 | // Normalización de la salida. |
joshema216 | 0:24c2d41ff63f | 369 | // Se actualizan las variables. |
joshema216 | 0:24c2d41ff63f | 370 | err_v = err; |
joshema216 | 0:24c2d41ff63f | 371 | o = pid/999; |
joshema216 | 0:24c2d41ff63f | 372 | u.write(o); // Se envía el valor pid a puerto analogico de salida (D/A). |
joshema216 | 0:24c2d41ff63f | 373 | |
joshema216 | 0:24c2d41ff63f | 374 | // Se repite el ciclo. |
joshema216 | 0:24c2d41ff63f | 375 | wait_ms(300); |
joshema216 | 0:24c2d41ff63f | 376 | |
joshema216 | 0:24c2d41ff63f | 377 | if(device.readable()){ // Si llega alguna cadena de caracteresp por la UART, principalmente para detener el controlador. |
joshema216 | 0:24c2d41ff63f | 378 | //andr.putc(device.getc()); |
joshema216 | 0:24c2d41ff63f | 379 | readBuffer(buffer,4); |
joshema216 | 0:24c2d41ff63f | 380 | //andr.printf("%s\r\n",buffer); |
joshema216 | 0:24c2d41ff63f | 381 | ind = buffer[0]; |
joshema216 | 0:24c2d41ff63f | 382 | |
joshema216 | 0:24c2d41ff63f | 383 | if (ind == '7'){ // Si llega una cadena con el indicador '7' (Dentener). |
joshema216 | 0:24c2d41ff63f | 384 | andr.printf("%s", ind); |
joshema216 | 0:24c2d41ff63f | 385 | pid = 0; // Regresa las variables del proceso de control a cero. |
joshema216 | 0:24c2d41ff63f | 386 | ai = 0; |
joshema216 | 0:24c2d41ff63f | 387 | ad = 0; |
joshema216 | 0:24c2d41ff63f | 388 | ap = 0; |
joshema216 | 0:24c2d41ff63f | 389 | med = 0; |
joshema216 | 0:24c2d41ff63f | 390 | err = 0; |
joshema216 | 0:24c2d41ff63f | 391 | err_v = 0; |
joshema216 | 0:24c2d41ff63f | 392 | |
joshema216 | 0:24c2d41ff63f | 393 | lcd.cls(); // Borrar Pantalla |
joshema216 | 0:24c2d41ff63f | 394 | lcd.locate(1,1); //se posiciona en col 1 fil 1 |
joshema216 | 0:24c2d41ff63f | 395 | lcd.printf("PID DETENIDO"); |
joshema216 | 0:24c2d41ff63f | 396 | wait(2); |
joshema216 | 0:24c2d41ff63f | 397 | lcd.cls(); // Borrar Pantalla |
joshema216 | 0:24c2d41ff63f | 398 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
joshema216 | 0:24c2d41ff63f | 399 | led3 =!led3; |
joshema216 | 0:24c2d41ff63f | 400 | |
joshema216 | 0:24c2d41ff63f | 401 | lcd.locate(8,0); |
joshema216 | 0:24c2d41ff63f | 402 | lcd.printf("Kp=%d",kpnum); |
joshema216 | 0:24c2d41ff63f | 403 | lcd.locate(0,1); |
joshema216 | 0:24c2d41ff63f | 404 | lcd.printf("Ki=%d",kinum); |
joshema216 | 0:24c2d41ff63f | 405 | lcd.locate(8,1); |
joshema216 | 0:24c2d41ff63f | 406 | lcd.printf("Kd=%d",kdnum); |
joshema216 | 0:24c2d41ff63f | 407 | lcd.locate(0,0); |
joshema216 | 0:24c2d41ff63f | 408 | lcd.printf("Sp=%d",spnum); |
joshema216 | 0:24c2d41ff63f | 409 | } |
joshema216 | 0:24c2d41ff63f | 410 | cleanBuffer(buffer,4); |
joshema216 | 0:24c2d41ff63f | 411 | goto lop0; // Si en efecto se detiene el PID vuelve al ciclo de asignación de variables. |
joshema216 | 0:24c2d41ff63f | 412 | } |
joshema216 | 0:24c2d41ff63f | 413 | goto lop1; // Si no se detiene el PID entonces vuelve al ciclo de control. |
joshema216 | 0:24c2d41ff63f | 414 | } |
joshema216 | 0:24c2d41ff63f | 415 | cleanBuffer(buffer,4); |
joshema216 | 0:24c2d41ff63f | 416 | } |
joshema216 | 0:24c2d41ff63f | 417 | //andr.putc(device.getc()); |
joshema216 | 0:24c2d41ff63f | 418 | } |
joshema216 | 0:24c2d41ff63f | 419 | } |