Proyecto de Tesis en Mecatrónica. Universidad Técnica del Norte. Ernesto Palacios <mecatronica.mid@gmail.com>
Dependencies: EthernetNetIf HTTPServer QEI_hw RPCInterface mbed
setup.cpp@30:413d1a6648b5, 2014-02-05 (annotated)
- Committer:
- Yo_Robot
- Date:
- Wed Feb 05 18:55:37 2014 +0000
- Revision:
- 30:413d1a6648b5
- Parent:
- 29:52932326c45a
- Child:
- 31:7e2cdd547cb2
Agregados los pines de alarma optoacopladores, aun sin probar y sin agregar codigo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 4:552beeda4722 | 1 | /** |
Yo_Robot | 4:552beeda4722 | 2 | * @file setup.cpp |
Yo_Robot | 4:552beeda4722 | 3 | * @author Ernesto Palacios |
Yo_Robot | 20:4b154134ab20 | 4 | * @brief Codigo Fuente de las funciones para el deslizador. |
Yo_Robot | 4:552beeda4722 | 5 | * |
Yo_Robot | 4:552beeda4722 | 6 | * Created on 25 de Marzo de 2012 |
Yo_Robot | 4:552beeda4722 | 7 | * |
Yo_Robot | 4:552beeda4722 | 8 | * Licencia GPL v3.0 |
Yo_Robot | 4:552beeda4722 | 9 | * http://www.gnu.org/licenses/gpl-3.0.html |
Yo_Robot | 4:552beeda4722 | 10 | */ |
Yo_Robot | 4:552beeda4722 | 11 | |
Yo_Robot | 25:1910a55ff0a3 | 12 | #include "mbed.h" |
Yo_Robot | 25:1910a55ff0a3 | 13 | #include "qeihw.h" |
Yo_Robot | 3:8d5a9e3cd680 | 14 | #include "setup.h" |
Yo_Robot | 18:cf1e07d82630 | 15 | #include "EthernetNetIf.h" |
Yo_Robot | 25:1910a55ff0a3 | 16 | |
Yo_Robot | 3:8d5a9e3cd680 | 17 | |
Yo_Robot | 24:a1d16835201c | 18 | extern Serial pc; // Salida Serial de mbed |
Yo_Robot | 24:a1d16835201c | 19 | extern Serial RS_232; // Salida Serial a MAX232 |
Yo_Robot | 25:1910a55ff0a3 | 20 | extern QEIHW encoder; |
Yo_Robot | 24:a1d16835201c | 21 | extern Timer crono; // Cronometro interno del microcontrolador |
Yo_Robot | 24:a1d16835201c | 22 | extern DigitalIn isPC; // Bit de configuracion serial en la placa |
Yo_Robot | 12:c02b08dacc45 | 23 | extern DigitalOut pin_son; // SON |
Yo_Robot | 12:c02b08dacc45 | 24 | extern DigitalOut pin_dir; // SIGN+ |
Yo_Robot | 12:c02b08dacc45 | 25 | extern InterruptIn pin_alm; // ALM |
Yo_Robot | 12:c02b08dacc45 | 26 | extern AnalogOut aout; // +-10V |
Yo_Robot | 22:d5431fff164b | 27 | extern DigitalOut led_verde; // Led verde del conector Ethernet |
Yo_Robot | 24:a1d16835201c | 28 | extern DigitalOut led_rojo; // Led naranja del conector Ethernet |
Yo_Robot | 30:413d1a6648b5 | 29 | extern InterruptIn limite_1; |
Yo_Robot | 30:413d1a6648b5 | 30 | extern InterruptIn limite_2; |
Yo_Robot | 30:413d1a6648b5 | 31 | extern InterruptIn limite_3; |
Yo_Robot | 30:413d1a6648b5 | 32 | extern InterruptIn limite_4; |
Yo_Robot | 20:4b154134ab20 | 33 | |
Yo_Robot | 15:a1ffa32ce9d1 | 34 | int fq_posicion = 10000; // Variable global donde se almacenara |
Yo_Robot | 15:a1ffa32ce9d1 | 35 | // la velocidad de posicionamiento en Hz |
Yo_Robot | 24:a1d16835201c | 36 | float t_alto; // para el posicionamiento del motor |
Yo_Robot | 23:2126e38bb48c | 37 | |
Yo_Robot | 26:dad0b2031173 | 38 | int fq_actual = 0; // Ultimo valor seteado para el tren de pulsos |
Yo_Robot | 22:d5431fff164b | 39 | |
Yo_Robot | 29:52932326c45a | 40 | int velocidad_rpm() |
Yo_Robot | 22:d5431fff164b | 41 | { |
Yo_Robot | 25:1910a55ff0a3 | 42 | return encoder.GetPosition(); |
Yo_Robot | 22:d5431fff164b | 43 | } |
Yo_Robot | 22:d5431fff164b | 44 | |
Yo_Robot | 22:d5431fff164b | 45 | void clear_encoder() |
Yo_Robot | 22:d5431fff164b | 46 | { |
Yo_Robot | 25:1910a55ff0a3 | 47 | encoder.Reset( QEI_RESET_POS ); // reset position |
Yo_Robot | 22:d5431fff164b | 48 | } |
Yo_Robot | 22:d5431fff164b | 49 | |
Yo_Robot | 4:552beeda4722 | 50 | void setTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 51 | { |
Yo_Robot | 3:8d5a9e3cd680 | 52 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 3:8d5a9e3cd680 | 53 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 3:8d5a9e3cd680 | 54 | |
Yo_Robot | 3:8d5a9e3cd680 | 55 | // Resetear y parar el Timer |
Yo_Robot | 3:8d5a9e3cd680 | 56 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 3:8d5a9e3cd680 | 57 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 3:8d5a9e3cd680 | 58 | |
Yo_Robot | 4:552beeda4722 | 59 | // Establecer el Preescaler en cero |
Yo_Robot | 4:552beeda4722 | 60 | // SIn Preesclaer |
Yo_Robot | 4:552beeda4722 | 61 | LPC_TIM2->PR = 0; |
Yo_Robot | 3:8d5a9e3cd680 | 62 | |
Yo_Robot | 4:552beeda4722 | 63 | // Calcular el periodo Inicial |
Yo_Robot | 4:552beeda4722 | 64 | uint32_t periodo = ( SystemCoreClock / 400 ); |
Yo_Robot | 3:8d5a9e3cd680 | 65 | |
Yo_Robot | 3:8d5a9e3cd680 | 66 | // Establecer los Registros de Coincidencia |
Yo_Robot | 3:8d5a9e3cd680 | 67 | // ( Match Register ) |
Yo_Robot | 3:8d5a9e3cd680 | 68 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 4:552beeda4722 | 69 | LPC_TIM2->MR3 = periodo; // Legacy, salidas identicas |
Yo_Robot | 3:8d5a9e3cd680 | 70 | |
Yo_Robot | 4:552beeda4722 | 71 | LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 |
Yo_Robot | 3:8d5a9e3cd680 | 72 | |
Yo_Robot | 3:8d5a9e3cd680 | 73 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 3:8d5a9e3cd680 | 74 | // y MAT2.3 |
Yo_Robot | 3:8d5a9e3cd680 | 75 | |
Yo_Robot | 3:8d5a9e3cd680 | 76 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 8:958dfe5052b9 | 77 | // y MAT2.3 como salidas |
Yo_Robot | 8:958dfe5052b9 | 78 | |
Yo_Robot | 6:b4dae934e1ea | 79 | } |
Yo_Robot | 6:b4dae934e1ea | 80 | |
Yo_Robot | 12:c02b08dacc45 | 81 | void ISR_Serial() |
Yo_Robot | 12:c02b08dacc45 | 82 | { |
Yo_Robot | 12:c02b08dacc45 | 83 | int value; // Nuevo Valor |
Yo_Robot | 12:c02b08dacc45 | 84 | char command; // Comando al que aplicar el nuevo valor |
Yo_Robot | 23:2126e38bb48c | 85 | |
Yo_Robot | 12:c02b08dacc45 | 86 | |
Yo_Robot | 19:c26cf8a48986 | 87 | if( isPC ) |
Yo_Robot | 23:2126e38bb48c | 88 | pc.scanf( "%d-%c", &value, &command ); |
Yo_Robot | 19:c26cf8a48986 | 89 | else |
Yo_Robot | 23:2126e38bb48c | 90 | RS_232.scanf( "%d-%c", &value, &command ); |
Yo_Robot | 23:2126e38bb48c | 91 | |
Yo_Robot | 23:2126e38bb48c | 92 | switch( command ) |
Yo_Robot | 23:2126e38bb48c | 93 | { |
Yo_Robot | 19:c26cf8a48986 | 94 | |
Yo_Robot | 29:52932326c45a | 95 | case 'E': // Leer el contador del encoder. |
Yo_Robot | 24:a1d16835201c | 96 | { |
Yo_Robot | 23:2126e38bb48c | 97 | |
Yo_Robot | 27:b8254b76ec57 | 98 | //Leer la posición del encoder |
Yo_Robot | 23:2126e38bb48c | 99 | |
Yo_Robot | 26:dad0b2031173 | 100 | if( isPC ) |
Yo_Robot | 29:52932326c45a | 101 | pc.printf("%d\n\r",encoder.GetPosition()); |
Yo_Robot | 26:dad0b2031173 | 102 | else |
Yo_Robot | 29:52932326c45a | 103 | RS_232.printf("%d\n\r",encoder.GetPosition()); |
Yo_Robot | 23:2126e38bb48c | 104 | break; |
Yo_Robot | 24:a1d16835201c | 105 | } |
Yo_Robot | 27:b8254b76ec57 | 106 | |
Yo_Robot | 23:2126e38bb48c | 107 | case 'H': // Establecer nueva frecuencia |
Yo_Robot | 24:a1d16835201c | 108 | { |
Yo_Robot | 23:2126e38bb48c | 109 | setPTO( value ); |
Yo_Robot | 26:dad0b2031173 | 110 | fq_actual = value; |
Yo_Robot | 26:dad0b2031173 | 111 | |
Yo_Robot | 26:dad0b2031173 | 112 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 113 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 114 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 115 | else |
Yo_Robot | 26:dad0b2031173 | 116 | RS_232.printf("OK\r\n"); |
Yo_Robot | 23:2126e38bb48c | 117 | break; |
Yo_Robot | 24:a1d16835201c | 118 | } |
Yo_Robot | 27:b8254b76ec57 | 119 | |
Yo_Robot | 23:2126e38bb48c | 120 | case 'K': |
Yo_Robot | 24:a1d16835201c | 121 | { |
Yo_Robot | 23:2126e38bb48c | 122 | setPTO( value * 1000 ); |
Yo_Robot | 26:dad0b2031173 | 123 | fq_actual = value; |
Yo_Robot | 26:dad0b2031173 | 124 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 125 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 126 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 127 | else |
Yo_Robot | 26:dad0b2031173 | 128 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 129 | |
Yo_Robot | 23:2126e38bb48c | 130 | break; |
Yo_Robot | 24:a1d16835201c | 131 | } |
Yo_Robot | 23:2126e38bb48c | 132 | case 'A': // Cambiar voltaje de salida |
Yo_Robot | 24:a1d16835201c | 133 | { |
Yo_Robot | 23:2126e38bb48c | 134 | aout = (float)( value + 10000.0 ) / 20000.0; |
Yo_Robot | 26:dad0b2031173 | 135 | |
Yo_Robot | 26:dad0b2031173 | 136 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 137 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 138 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 139 | else |
Yo_Robot | 26:dad0b2031173 | 140 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 141 | |
Yo_Robot | 23:2126e38bb48c | 142 | break; |
Yo_Robot | 24:a1d16835201c | 143 | } |
Yo_Robot | 23:2126e38bb48c | 144 | case 'D': // Cambiar la direccion |
Yo_Robot | 24:a1d16835201c | 145 | { |
Yo_Robot | 26:dad0b2031173 | 146 | |
Yo_Robot | 23:2126e38bb48c | 147 | stopTimer2(); |
Yo_Robot | 23:2126e38bb48c | 148 | pin_dir = value; |
Yo_Robot | 23:2126e38bb48c | 149 | wait_us( 2 ); |
Yo_Robot | 26:dad0b2031173 | 150 | |
Yo_Robot | 26:dad0b2031173 | 151 | if ( fq_actual != 0 ) |
Yo_Robot | 26:dad0b2031173 | 152 | { |
Yo_Robot | 26:dad0b2031173 | 153 | startTimer2(); |
Yo_Robot | 26:dad0b2031173 | 154 | } |
Yo_Robot | 26:dad0b2031173 | 155 | |
Yo_Robot | 26:dad0b2031173 | 156 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 157 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 158 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 159 | else |
Yo_Robot | 26:dad0b2031173 | 160 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 161 | |
Yo_Robot | 23:2126e38bb48c | 162 | break; |
Yo_Robot | 24:a1d16835201c | 163 | } |
Yo_Robot | 23:2126e38bb48c | 164 | case 'V': //Setear la velocidad de Posicionamiento |
Yo_Robot | 24:a1d16835201c | 165 | { |
Yo_Robot | 23:2126e38bb48c | 166 | fq_posicion = value; |
Yo_Robot | 26:dad0b2031173 | 167 | |
Yo_Robot | 26:dad0b2031173 | 168 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 169 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 170 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 171 | else |
Yo_Robot | 26:dad0b2031173 | 172 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 173 | |
Yo_Robot | 23:2126e38bb48c | 174 | break; |
Yo_Robot | 24:a1d16835201c | 175 | } |
Yo_Robot | 27:b8254b76ec57 | 176 | |
Yo_Robot | 27:b8254b76ec57 | 177 | // Generar un numero definido de pulsos a la velocidad de posicionamiento |
Yo_Robot | 29:52932326c45a | 178 | case 'P': |
Yo_Robot | 24:a1d16835201c | 179 | { |
Yo_Robot | 28:b7ded82ee7da | 180 | float pulsos = value; //Numero de pulsos a generar |
Yo_Robot | 28:b7ded82ee7da | 181 | t_alto = (float)(pulsos / fq_posicion); //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 27:b8254b76ec57 | 182 | |
Yo_Robot | 27:b8254b76ec57 | 183 | //DEBUG |
Yo_Robot | 27:b8254b76ec57 | 184 | pc.printf("Tiempo en timer en seg = %f", t_alto); |
Yo_Robot | 27:b8254b76ec57 | 185 | |
Yo_Robot | 23:2126e38bb48c | 186 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 23:2126e38bb48c | 187 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 23:2126e38bb48c | 188 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 23:2126e38bb48c | 189 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 23:2126e38bb48c | 190 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 26:dad0b2031173 | 191 | |
Yo_Robot | 26:dad0b2031173 | 192 | |
Yo_Robot | 26:dad0b2031173 | 193 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 194 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 195 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 196 | else |
Yo_Robot | 26:dad0b2031173 | 197 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 198 | |
Yo_Robot | 26:dad0b2031173 | 199 | |
Yo_Robot | 23:2126e38bb48c | 200 | break; |
Yo_Robot | 24:a1d16835201c | 201 | } |
Yo_Robot | 27:b8254b76ec57 | 202 | |
Yo_Robot | 27:b8254b76ec57 | 203 | // Generar un numero definido de MILES de pulsos a la velocidad de posicionamiento |
Yo_Robot | 29:52932326c45a | 204 | case 'M': |
Yo_Robot | 27:b8254b76ec57 | 205 | { |
Yo_Robot | 28:b7ded82ee7da | 206 | float pulsos = value * 1000; //Numero de pulsos a generar |
Yo_Robot | 28:b7ded82ee7da | 207 | t_alto = (float)(pulsos / fq_posicion); //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 27:b8254b76ec57 | 208 | |
Yo_Robot | 27:b8254b76ec57 | 209 | //DEBUG |
Yo_Robot | 27:b8254b76ec57 | 210 | pc.printf("Tiempo en timer en seg = %f", t_alto); |
Yo_Robot | 27:b8254b76ec57 | 211 | |
Yo_Robot | 27:b8254b76ec57 | 212 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 213 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 27:b8254b76ec57 | 214 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 215 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 27:b8254b76ec57 | 216 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 27:b8254b76ec57 | 217 | |
Yo_Robot | 27:b8254b76ec57 | 218 | |
Yo_Robot | 27:b8254b76ec57 | 219 | // Envia un OK de comando recibido |
Yo_Robot | 27:b8254b76ec57 | 220 | if( isPC ) |
Yo_Robot | 27:b8254b76ec57 | 221 | pc.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 222 | else |
Yo_Robot | 27:b8254b76ec57 | 223 | RS_232.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 224 | |
Yo_Robot | 27:b8254b76ec57 | 225 | |
Yo_Robot | 27:b8254b76ec57 | 226 | break; |
Yo_Robot | 27:b8254b76ec57 | 227 | } |
Yo_Robot | 27:b8254b76ec57 | 228 | |
Yo_Robot | 27:b8254b76ec57 | 229 | // Generar un numero definido de MILLONES pulsos a la velocidad de posicionamiento |
Yo_Robot | 29:52932326c45a | 230 | case 'N': |
Yo_Robot | 27:b8254b76ec57 | 231 | { |
Yo_Robot | 28:b7ded82ee7da | 232 | float pulsos = value * 1000000; //Numero de pulsos a generar |
Yo_Robot | 28:b7ded82ee7da | 233 | t_alto = (float)(pulsos / fq_posicion); //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 27:b8254b76ec57 | 234 | |
Yo_Robot | 27:b8254b76ec57 | 235 | //DEBUG |
Yo_Robot | 27:b8254b76ec57 | 236 | pc.printf("Tiempo en timer en seg = %f", t_alto); |
Yo_Robot | 27:b8254b76ec57 | 237 | |
Yo_Robot | 27:b8254b76ec57 | 238 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 239 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 27:b8254b76ec57 | 240 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 241 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 27:b8254b76ec57 | 242 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 27:b8254b76ec57 | 243 | |
Yo_Robot | 27:b8254b76ec57 | 244 | |
Yo_Robot | 27:b8254b76ec57 | 245 | // Envia un OK de comando recibido |
Yo_Robot | 27:b8254b76ec57 | 246 | if( isPC ) |
Yo_Robot | 27:b8254b76ec57 | 247 | pc.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 248 | else |
Yo_Robot | 27:b8254b76ec57 | 249 | RS_232.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 250 | |
Yo_Robot | 27:b8254b76ec57 | 251 | |
Yo_Robot | 27:b8254b76ec57 | 252 | break; |
Yo_Robot | 27:b8254b76ec57 | 253 | } |
Yo_Robot | 27:b8254b76ec57 | 254 | |
Yo_Robot | 27:b8254b76ec57 | 255 | |
Yo_Robot | 29:52932326c45a | 256 | case 'R': // Leer la velocidd del encoder en RPM's |
Yo_Robot | 24:a1d16835201c | 257 | { |
Yo_Robot | 23:2126e38bb48c | 258 | if( isPC ) |
Yo_Robot | 29:52932326c45a | 259 | pc.printf( "%d",velocidad_rpm() ); |
Yo_Robot | 23:2126e38bb48c | 260 | else |
Yo_Robot | 29:52932326c45a | 261 | RS_232.printf( "%d",velocidad_rpm() ); |
Yo_Robot | 23:2126e38bb48c | 262 | break; |
Yo_Robot | 24:a1d16835201c | 263 | } |
Yo_Robot | 26:dad0b2031173 | 264 | |
Yo_Robot | 23:2126e38bb48c | 265 | case 'Z': //Limpiar contador encoder |
Yo_Robot | 29:52932326c45a | 266 | encoder.Reset(QEI_RESET_POS); |
Yo_Robot | 29:52932326c45a | 267 | encoder.Reset(QEI_RESET_VEL); |
Yo_Robot | 26:dad0b2031173 | 268 | |
Yo_Robot | 26:dad0b2031173 | 269 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 270 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 271 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 272 | else |
Yo_Robot | 26:dad0b2031173 | 273 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 274 | |
Yo_Robot | 23:2126e38bb48c | 275 | break; |
Yo_Robot | 23:2126e38bb48c | 276 | |
Yo_Robot | 23:2126e38bb48c | 277 | case 'S': //Encender el Servo |
Yo_Robot | 26:dad0b2031173 | 278 | |
Yo_Robot | 23:2126e38bb48c | 279 | pin_son = value; |
Yo_Robot | 26:dad0b2031173 | 280 | |
Yo_Robot | 26:dad0b2031173 | 281 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 282 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 283 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 284 | else |
Yo_Robot | 26:dad0b2031173 | 285 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 286 | |
Yo_Robot | 23:2126e38bb48c | 287 | break; |
Yo_Robot | 28:b7ded82ee7da | 288 | |
Yo_Robot | 28:b7ded82ee7da | 289 | default: |
Yo_Robot | 28:b7ded82ee7da | 290 | |
Yo_Robot | 28:b7ded82ee7da | 291 | // EL COMANDO NO SE RECONOCE: No Aplica |
Yo_Robot | 28:b7ded82ee7da | 292 | if( isPC ) |
Yo_Robot | 28:b7ded82ee7da | 293 | pc.printf("NA\r\n"); |
Yo_Robot | 28:b7ded82ee7da | 294 | else |
Yo_Robot | 28:b7ded82ee7da | 295 | RS_232.printf("NA\r\n"); |
Yo_Robot | 28:b7ded82ee7da | 296 | |
Yo_Robot | 28:b7ded82ee7da | 297 | break; |
Yo_Robot | 18:cf1e07d82630 | 298 | } |
Yo_Robot | 23:2126e38bb48c | 299 | |
Yo_Robot | 12:c02b08dacc45 | 300 | } |
Yo_Robot | 12:c02b08dacc45 | 301 | |
Yo_Robot | 12:c02b08dacc45 | 302 | |
Yo_Robot | 12:c02b08dacc45 | 303 | void setPTO( int freq ) |
Yo_Robot | 12:c02b08dacc45 | 304 | { |
Yo_Robot | 12:c02b08dacc45 | 305 | if( freq != 0 ) |
Yo_Robot | 12:c02b08dacc45 | 306 | { |
Yo_Robot | 12:c02b08dacc45 | 307 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 308 | setMR2( getMRvalue( freq ) ); |
Yo_Robot | 12:c02b08dacc45 | 309 | startTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 310 | |
Yo_Robot | 12:c02b08dacc45 | 311 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 312 | |
Yo_Robot | 12:c02b08dacc45 | 313 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 314 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 315 | } |
Yo_Robot | 12:c02b08dacc45 | 316 | } |
Yo_Robot | 12:c02b08dacc45 | 317 | |
Yo_Robot | 22:d5431fff164b | 318 | |
Yo_Robot | 22:d5431fff164b | 319 | |
Yo_Robot | 22:d5431fff164b | 320 | void ISR_Alarm() |
Yo_Robot | 22:d5431fff164b | 321 | { |
Yo_Robot | 22:d5431fff164b | 322 | pin_son = 0 ; |
Yo_Robot | 22:d5431fff164b | 323 | stopTimer2(); |
Yo_Robot | 22:d5431fff164b | 324 | aout = 0.5 ; |
Yo_Robot | 22:d5431fff164b | 325 | |
Yo_Robot | 22:d5431fff164b | 326 | if(isPC) |
Yo_Robot | 26:dad0b2031173 | 327 | pc.printf( "AL\r\n" ); //ALARMA! solo es AL para que |
Yo_Robot | 22:d5431fff164b | 328 | //sea conciso con el modo ETH y funcione |
Yo_Robot | 22:d5431fff164b | 329 | //bien en LabVIEW. |
Yo_Robot | 22:d5431fff164b | 330 | else |
Yo_Robot | 26:dad0b2031173 | 331 | RS_232.printf( "AL\r\n" ); |
Yo_Robot | 22:d5431fff164b | 332 | } |
Yo_Robot | 22:d5431fff164b | 333 | |
Yo_Robot | 30:413d1a6648b5 | 334 | |
Yo_Robot | 30:413d1a6648b5 | 335 | |
Yo_Robot | 30:413d1a6648b5 | 336 | |
Yo_Robot | 30:413d1a6648b5 | 337 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 338 | * del sensor óptico, al recibir una ALARMA de proximidad al encoder |
Yo_Robot | 30:413d1a6648b5 | 339 | */ |
Yo_Robot | 30:413d1a6648b5 | 340 | void ISR_Alm_encoder(){} |
Yo_Robot | 30:413d1a6648b5 | 341 | |
Yo_Robot | 30:413d1a6648b5 | 342 | |
Yo_Robot | 30:413d1a6648b5 | 343 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 344 | * del sensor óptico, al recibir una ALARMA de proximidad al motor |
Yo_Robot | 30:413d1a6648b5 | 345 | */ |
Yo_Robot | 30:413d1a6648b5 | 346 | void ISR_Alm_motor(){} |
Yo_Robot | 30:413d1a6648b5 | 347 | |
Yo_Robot | 30:413d1a6648b5 | 348 | |
Yo_Robot | 30:413d1a6648b5 | 349 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 350 | * del sensor óptico, al recibir una advertencia de proximidad al encoder |
Yo_Robot | 30:413d1a6648b5 | 351 | */ |
Yo_Robot | 30:413d1a6648b5 | 352 | void ISR_Adv_encoder(){} |
Yo_Robot | 30:413d1a6648b5 | 353 | |
Yo_Robot | 30:413d1a6648b5 | 354 | |
Yo_Robot | 30:413d1a6648b5 | 355 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 356 | * del sensor óptico, al recibir una advertencia de proximidad al motor |
Yo_Robot | 30:413d1a6648b5 | 357 | */ |
Yo_Robot | 30:413d1a6648b5 | 358 | void ISR_Adv_motor(){} |
Yo_Robot | 30:413d1a6648b5 | 359 | |
Yo_Robot | 30:413d1a6648b5 | 360 | |
Yo_Robot | 30:413d1a6648b5 | 361 | |
Yo_Robot | 22:d5431fff164b | 362 | int getMRvalue( int fout ) |
Yo_Robot | 22:d5431fff164b | 363 | { |
Yo_Robot | 22:d5431fff164b | 364 | int toRegister; |
Yo_Robot | 22:d5431fff164b | 365 | |
Yo_Robot | 22:d5431fff164b | 366 | toRegister = (24000000 /(fout*2.0) ) -1; |
Yo_Robot | 22:d5431fff164b | 367 | return toRegister; |
Yo_Robot | 22:d5431fff164b | 368 | } |
Yo_Robot | 22:d5431fff164b | 369 | |
Yo_Robot | 22:d5431fff164b | 370 | |
Yo_Robot | 22:d5431fff164b | 371 | void setMR2( int newValue ) |
Yo_Robot | 22:d5431fff164b | 372 | { |
Yo_Robot | 22:d5431fff164b | 373 | LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas |
Yo_Robot | 22:d5431fff164b | 374 | LPC_TIM2->MR3 = newValue; // Para testear el programa. |
Yo_Robot | 22:d5431fff164b | 375 | } |
Yo_Robot | 22:d5431fff164b | 376 | |
Yo_Robot | 22:d5431fff164b | 377 | |
Yo_Robot | 22:d5431fff164b | 378 | |
Yo_Robot | 22:d5431fff164b | 379 | void startTimer2() |
Yo_Robot | 22:d5431fff164b | 380 | { |
Yo_Robot | 22:d5431fff164b | 381 | // Arrancer el Timer 2 |
Yo_Robot | 22:d5431fff164b | 382 | LPC_TIM2->TCR = 1; |
Yo_Robot | 22:d5431fff164b | 383 | } |
Yo_Robot | 22:d5431fff164b | 384 | |
Yo_Robot | 22:d5431fff164b | 385 | void stopTimer2() |
Yo_Robot | 22:d5431fff164b | 386 | { |
Yo_Robot | 22:d5431fff164b | 387 | // Detener el Timer 2 |
Yo_Robot | 22:d5431fff164b | 388 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 22:d5431fff164b | 389 | } |
Yo_Robot | 22:d5431fff164b | 390 | |
Yo_Robot | 22:d5431fff164b | 391 | int getBaud() |
Yo_Robot | 22:d5431fff164b | 392 | { |
Yo_Robot | 22:d5431fff164b | 393 | int baudios = 115200; //Valor por defecto |
Yo_Robot | 22:d5431fff164b | 394 | |
Yo_Robot | 22:d5431fff164b | 395 | FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 22:d5431fff164b | 396 | |
Yo_Robot | 22:d5431fff164b | 397 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 22:d5431fff164b | 398 | { |
Yo_Robot | 22:d5431fff164b | 399 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 22:d5431fff164b | 400 | printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n"); |
Yo_Robot | 22:d5431fff164b | 401 | printf("que contenga las lineas:\n\n"); |
Yo_Robot | 22:d5431fff164b | 402 | |
Yo_Robot | 22:d5431fff164b | 403 | printf(" 1\n"); |
Yo_Robot | 22:d5431fff164b | 404 | printf(" 2\n"); |
Yo_Robot | 22:d5431fff164b | 405 | printf(" 3\n"); |
Yo_Robot | 22:d5431fff164b | 406 | printf(" 4\n"); |
Yo_Robot | 22:d5431fff164b | 407 | printf(" baudios: 115200\n"); |
Yo_Robot | 22:d5431fff164b | 408 | |
Yo_Robot | 22:d5431fff164b | 409 | printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n"); |
Yo_Robot | 22:d5431fff164b | 410 | printf("luego reinicie el microcontrolador\n"); |
Yo_Robot | 22:d5431fff164b | 411 | exit(1); |
Yo_Robot | 22:d5431fff164b | 412 | |
Yo_Robot | 22:d5431fff164b | 413 | } |
Yo_Robot | 22:d5431fff164b | 414 | else |
Yo_Robot | 22:d5431fff164b | 415 | { |
Yo_Robot | 22:d5431fff164b | 416 | // Cadenas de caracteres desde el Archivo config.mbd |
Yo_Robot | 22:d5431fff164b | 417 | char notstr [04]; // linea vacia |
Yo_Robot | 22:d5431fff164b | 418 | char baud [40]; // valor en baudios |
Yo_Robot | 22:d5431fff164b | 419 | |
Yo_Robot | 22:d5431fff164b | 420 | // Leer linea a linea el archivo |
Yo_Robot | 22:d5431fff164b | 421 | // cuatro primeras lineas no sirven |
Yo_Robot | 22:d5431fff164b | 422 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 423 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 424 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 425 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 426 | fgets( baud, 40, fp ); |
Yo_Robot | 22:d5431fff164b | 427 | fclose(fp); |
Yo_Robot | 23:2126e38bb48c | 428 | |
Yo_Robot | 22:d5431fff164b | 429 | // Extraer los valores numericos |
Yo_Robot | 22:d5431fff164b | 430 | sscanf( baud,"%*s %d",&baudios ); |
Yo_Robot | 22:d5431fff164b | 431 | |
Yo_Robot | 22:d5431fff164b | 432 | |
Yo_Robot | 22:d5431fff164b | 433 | } |
Yo_Robot | 22:d5431fff164b | 434 | |
Yo_Robot | 22:d5431fff164b | 435 | return baudios; |
Yo_Robot | 22:d5431fff164b | 436 | } |
Yo_Robot | 22:d5431fff164b | 437 | |
Yo_Robot | 22:d5431fff164b | 438 | // **** Funciones Liberia Ethernet ***** // |
Yo_Robot | 12:c02b08dacc45 | 439 | void setPTO_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 440 | { |
Yo_Robot | 12:c02b08dacc45 | 441 | int freq = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 442 | |
Yo_Robot | 12:c02b08dacc45 | 443 | if( freq != 0 ){ |
Yo_Robot | 12:c02b08dacc45 | 444 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 445 | setMR2( getMRvalue( freq ) ); // Cambiar frefuencia |
Yo_Robot | 12:c02b08dacc45 | 446 | startTimer2(); // Iniciar Timer |
Yo_Robot | 14:039d070732d5 | 447 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 448 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 14:039d070732d5 | 449 | else |
Yo_Robot | 14:039d070732d5 | 450 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 451 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 452 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 453 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 14:039d070732d5 | 454 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 455 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 14:039d070732d5 | 456 | else |
Yo_Robot | 14:039d070732d5 | 457 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 458 | } |
Yo_Robot | 12:c02b08dacc45 | 459 | } |
Yo_Robot | 12:c02b08dacc45 | 460 | |
Yo_Robot | 15:a1ffa32ce9d1 | 461 | void setANG_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 462 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 463 | long int pulsos = atol( input ); //Numero de pulsos a generar |
Yo_Robot | 29:52932326c45a | 464 | float pulsos_f = (float) pulsos; |
Yo_Robot | 29:52932326c45a | 465 | t_alto = pulsos_f / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 15:a1ffa32ce9d1 | 466 | |
Yo_Robot | 15:a1ffa32ce9d1 | 467 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 468 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 15:a1ffa32ce9d1 | 469 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 470 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 18:cf1e07d82630 | 471 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 15:a1ffa32ce9d1 | 472 | |
Yo_Robot | 15:a1ffa32ce9d1 | 473 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 474 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 475 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 476 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 477 | |
Yo_Robot | 15:a1ffa32ce9d1 | 478 | } |
Yo_Robot | 21:353b0fe8fc54 | 479 | |
Yo_Robot | 21:353b0fe8fc54 | 480 | |
Yo_Robot | 15:a1ffa32ce9d1 | 481 | void setSPD_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 482 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 483 | fq_posicion = atoi( input ); |
Yo_Robot | 15:a1ffa32ce9d1 | 484 | // Esta funcion cambia la velocidad con la que se |
Yo_Robot | 15:a1ffa32ce9d1 | 485 | // posicionara el eje del motor en un angulo determinado |
Yo_Robot | 15:a1ffa32ce9d1 | 486 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 487 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 488 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 489 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 490 | |
Yo_Robot | 15:a1ffa32ce9d1 | 491 | } |
Yo_Robot | 21:353b0fe8fc54 | 492 | |
Yo_Robot | 12:c02b08dacc45 | 493 | void setAout_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 494 | { |
Yo_Robot | 12:c02b08dacc45 | 495 | int vout = atoi( input ); |
Yo_Robot | 14:039d070732d5 | 496 | aout = (float)( vout + 10000 ) / 20000; |
Yo_Robot | 14:039d070732d5 | 497 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 498 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 14:039d070732d5 | 499 | else |
Yo_Robot | 14:039d070732d5 | 500 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 501 | } |
Yo_Robot | 12:c02b08dacc45 | 502 | |
Yo_Robot | 12:c02b08dacc45 | 503 | |
Yo_Robot | 12:c02b08dacc45 | 504 | |
Yo_Robot | 12:c02b08dacc45 | 505 | void setDir_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 506 | { |
Yo_Robot | 12:c02b08dacc45 | 507 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 508 | |
Yo_Robot | 12:c02b08dacc45 | 509 | pin_dir = value; |
Yo_Robot | 12:c02b08dacc45 | 510 | |
Yo_Robot | 14:039d070732d5 | 511 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 512 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 12:c02b08dacc45 | 513 | else |
Yo_Robot | 14:039d070732d5 | 514 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 515 | } |
Yo_Robot | 12:c02b08dacc45 | 516 | |
Yo_Robot | 12:c02b08dacc45 | 517 | |
Yo_Robot | 12:c02b08dacc45 | 518 | |
Yo_Robot | 12:c02b08dacc45 | 519 | void setSON_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 520 | { |
Yo_Robot | 12:c02b08dacc45 | 521 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 522 | |
Yo_Robot | 12:c02b08dacc45 | 523 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 524 | |
Yo_Robot | 14:039d070732d5 | 525 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 526 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 12:c02b08dacc45 | 527 | else |
Yo_Robot | 14:039d070732d5 | 528 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 529 | |
Yo_Robot | 12:c02b08dacc45 | 530 | } |
Yo_Robot | 12:c02b08dacc45 | 531 | |
Yo_Robot | 12:c02b08dacc45 | 532 | |
Yo_Robot | 22:d5431fff164b | 533 | void getENC_eth( char * input, char * output ) |
Yo_Robot | 20:4b154134ab20 | 534 | { |
Yo_Robot | 22:d5431fff164b | 535 | if( pin_alm == 0 ) |
Yo_Robot | 29:52932326c45a | 536 | sprintf( output,"%d", encoder.GetPosition() ); |
Yo_Robot | 22:d5431fff164b | 537 | else |
Yo_Robot | 22:d5431fff164b | 538 | sprintf( output,"AL" ); |
Yo_Robot | 22:d5431fff164b | 539 | } |
Yo_Robot | 21:353b0fe8fc54 | 540 | |
Yo_Robot | 21:353b0fe8fc54 | 541 | |
Yo_Robot | 22:d5431fff164b | 542 | void setENC_eth( char * input, char * output ) |
Yo_Robot | 22:d5431fff164b | 543 | { |
Yo_Robot | 29:52932326c45a | 544 | encoder.Reset(QEI_RESET_POS); |
Yo_Robot | 29:52932326c45a | 545 | encoder.Reset(QEI_RESET_VEL); |
Yo_Robot | 22:d5431fff164b | 546 | |
Yo_Robot | 22:d5431fff164b | 547 | if( pin_alm == 0 ) |
Yo_Robot | 26:dad0b2031173 | 548 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 22:d5431fff164b | 549 | else |
Yo_Robot | 22:d5431fff164b | 550 | sprintf( output,"AL" ); |
Yo_Robot | 22:d5431fff164b | 551 | |
Yo_Robot | 20:4b154134ab20 | 552 | } |
Yo_Robot | 12:c02b08dacc45 | 553 | |
Yo_Robot | 23:2126e38bb48c | 554 | void getRPM_eth( char * input, char * output ) |
Yo_Robot | 23:2126e38bb48c | 555 | { |
Yo_Robot | 29:52932326c45a | 556 | int rpm; |
Yo_Robot | 23:2126e38bb48c | 557 | |
Yo_Robot | 29:52932326c45a | 558 | rpm = encoder.CalculateRPM( encoder.GetVelocityCap(), 360); // ultima velocidad leida desde el encoder |
Yo_Robot | 29:52932326c45a | 559 | // numero de revoluciones por vuelta del encoder |
Yo_Robot | 29:52932326c45a | 560 | |
Yo_Robot | 23:2126e38bb48c | 561 | |
Yo_Robot | 23:2126e38bb48c | 562 | if( pin_alm == 0 ) |
Yo_Robot | 29:52932326c45a | 563 | sprintf( output,"%d",rpm ); |
Yo_Robot | 23:2126e38bb48c | 564 | else |
Yo_Robot | 29:52932326c45a | 565 | sprintf( output,"AL" ); |
Yo_Robot | 23:2126e38bb48c | 566 | |
Yo_Robot | 23:2126e38bb48c | 567 | |
Yo_Robot | 23:2126e38bb48c | 568 | } |
Yo_Robot | 23:2126e38bb48c | 569 | |
Yo_Robot | 21:353b0fe8fc54 | 570 | |
Yo_Robot | 22:d5431fff164b | 571 | |
Yo_Robot | 22:d5431fff164b | 572 | |
Yo_Robot | 22:d5431fff164b | 573 | |
Yo_Robot | 4:552beeda4722 | 574 | /* LEGACY FUNCTIONS |
Yo_Robot | 4:552beeda4722 | 575 | * |
Yo_Robot | 20:4b154134ab20 | 576 | * El siguiente codigo no es utilizado por el |
Yo_Robot | 20:4b154134ab20 | 577 | * programa. Sin embargo pueden servir como |
Yo_Robot | 20:4b154134ab20 | 578 | * futuras referencias. |
Yo_Robot | 4:552beeda4722 | 579 | */ |
Yo_Robot | 4:552beeda4722 | 580 | void setMR3( int newValue ) |
Yo_Robot | 4:552beeda4722 | 581 | { |
Yo_Robot | 4:552beeda4722 | 582 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 4:552beeda4722 | 583 | } |
Yo_Robot | 4:552beeda4722 | 584 | |
Yo_Robot | 4:552beeda4722 | 585 | |
Yo_Robot | 4:552beeda4722 | 586 | void setPrescaler( int newValue) |
Yo_Robot | 4:552beeda4722 | 587 | { |
Yo_Robot | 4:552beeda4722 | 588 | LPC_TIM2->PR = newValue; |
Yo_Robot | 4:552beeda4722 | 589 | } |
Yo_Robot | 20:4b154134ab20 | 590 | |
Yo_Robot | 20:4b154134ab20 | 591 | |
Yo_Robot | 20:4b154134ab20 | 592 | EthernetNetIf configurarEthernet() |
Yo_Robot | 20:4b154134ab20 | 593 | { |
Yo_Robot | 20:4b154134ab20 | 594 | //____________ *** ARCHIVO DE CONFIGURACION ***_______________________ // |
Yo_Robot | 20:4b154134ab20 | 595 | |
Yo_Robot | 20:4b154134ab20 | 596 | printf("\n *** CONFIGURACION ETHERNET DE MBED ***\n"); |
Yo_Robot | 20:4b154134ab20 | 597 | printf("Leyendo archivo de configuracion...\n\n"); |
Yo_Robot | 20:4b154134ab20 | 598 | |
Yo_Robot | 20:4b154134ab20 | 599 | FILE *fp = fopen("/local/config.txt", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 20:4b154134ab20 | 600 | |
Yo_Robot | 20:4b154134ab20 | 601 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 20:4b154134ab20 | 602 | { |
Yo_Robot | 20:4b154134ab20 | 603 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 20:4b154134ab20 | 604 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 605 | |
Yo_Robot | 20:4b154134ab20 | 606 | } |
Yo_Robot | 20:4b154134ab20 | 607 | else |
Yo_Robot | 20:4b154134ab20 | 608 | { |
Yo_Robot | 20:4b154134ab20 | 609 | // Cadenas de caracteres desde el Archivo config.txt |
Yo_Robot | 20:4b154134ab20 | 610 | char isDHCP [15]; //Modo Automatico o Manual |
Yo_Robot | 20:4b154134ab20 | 611 | char empty [2]; // Linea vacia |
Yo_Robot | 20:4b154134ab20 | 612 | char ip [40]; // Direccion IP |
Yo_Robot | 20:4b154134ab20 | 613 | char mask [40]; // Mascara de Subred |
Yo_Robot | 20:4b154134ab20 | 614 | char gate [40]; // Puerta de enlace |
Yo_Robot | 20:4b154134ab20 | 615 | char dns [40]; // Direccion DNS |
Yo_Robot | 20:4b154134ab20 | 616 | |
Yo_Robot | 20:4b154134ab20 | 617 | // Valores 'int' para agregar a la configuracion manual |
Yo_Robot | 20:4b154134ab20 | 618 | int DHCP; |
Yo_Robot | 20:4b154134ab20 | 619 | int n_ip[4]; |
Yo_Robot | 20:4b154134ab20 | 620 | int n_mask[4]; |
Yo_Robot | 20:4b154134ab20 | 621 | int n_gate[4]; |
Yo_Robot | 20:4b154134ab20 | 622 | int n_dns[4]; |
Yo_Robot | 20:4b154134ab20 | 623 | |
Yo_Robot | 20:4b154134ab20 | 624 | |
Yo_Robot | 20:4b154134ab20 | 625 | // Leer linea a linea el archivo |
Yo_Robot | 20:4b154134ab20 | 626 | fgets( isDHCP, 15, fp ); |
Yo_Robot | 20:4b154134ab20 | 627 | fgets( empty, 2, fp ); |
Yo_Robot | 20:4b154134ab20 | 628 | fgets( ip, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 629 | fgets( mask, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 630 | fgets( gate, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 631 | fgets( dns, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 632 | |
Yo_Robot | 20:4b154134ab20 | 633 | printf("Cerrando archivo...\n"); |
Yo_Robot | 20:4b154134ab20 | 634 | fclose(fp); |
Yo_Robot | 20:4b154134ab20 | 635 | |
Yo_Robot | 20:4b154134ab20 | 636 | // Extraer los valores numericos |
Yo_Robot | 20:4b154134ab20 | 637 | sscanf( isDHCP,"%*s %d",&DHCP ); |
Yo_Robot | 20:4b154134ab20 | 638 | |
Yo_Robot | 20:4b154134ab20 | 639 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0],&n_ip[1],&n_ip[2],&n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 640 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 641 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 642 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0],&n_dns[1],&n_dns[2],&n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 643 | |
Yo_Robot | 20:4b154134ab20 | 644 | |
Yo_Robot | 20:4b154134ab20 | 645 | if(DHCP) //En caso de modo DHCP |
Yo_Robot | 20:4b154134ab20 | 646 | { |
Yo_Robot | 20:4b154134ab20 | 647 | printf("\n Configurar red de manera automatica\n"); |
Yo_Robot | 20:4b154134ab20 | 648 | EthernetNetIf eth; //Crea la interfaz |
Yo_Robot | 20:4b154134ab20 | 649 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 650 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 651 | { |
Yo_Robot | 20:4b154134ab20 | 652 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 653 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 654 | } |
Yo_Robot | 20:4b154134ab20 | 655 | |
Yo_Robot | 20:4b154134ab20 | 656 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 657 | return eth; |
Yo_Robot | 20:4b154134ab20 | 658 | } |
Yo_Robot | 20:4b154134ab20 | 659 | else |
Yo_Robot | 20:4b154134ab20 | 660 | { |
Yo_Robot | 20:4b154134ab20 | 661 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0], &n_ip[1], &n_ip[2], &n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 662 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 663 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 664 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0], &n_dns[1], &n_dns[2], &n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 665 | printf(" %s %s %s %s %s\n " , isDHCP, ip, mask, gate, dns ); |
Yo_Robot | 20:4b154134ab20 | 666 | |
Yo_Robot | 20:4b154134ab20 | 667 | printf("\n Configurar red de manera manual\n"); |
Yo_Robot | 20:4b154134ab20 | 668 | EthernetNetIf eth( |
Yo_Robot | 20:4b154134ab20 | 669 | IpAddr( n_ip[0], n_ip[1], n_ip[2], n_ip[3]), //IP Address |
Yo_Robot | 20:4b154134ab20 | 670 | IpAddr( n_mask[0],n_mask[1],n_mask[2],n_mask[3]), //Network Mask |
Yo_Robot | 20:4b154134ab20 | 671 | IpAddr( n_gate[0],n_gate[1],n_gate[2],n_gate[3]), //Gateway |
Yo_Robot | 20:4b154134ab20 | 672 | IpAddr( n_dns[0], n_dns[1], n_dns[2], n_dns[3] ) //DNS |
Yo_Robot | 20:4b154134ab20 | 673 | ); |
Yo_Robot | 20:4b154134ab20 | 674 | |
Yo_Robot | 20:4b154134ab20 | 675 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 676 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 677 | { |
Yo_Robot | 20:4b154134ab20 | 678 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 679 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 680 | } |
Yo_Robot | 20:4b154134ab20 | 681 | |
Yo_Robot | 20:4b154134ab20 | 682 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 683 | return eth; |
Yo_Robot | 20:4b154134ab20 | 684 | } |
Yo_Robot | 20:4b154134ab20 | 685 | |
Yo_Robot | 20:4b154134ab20 | 686 | } |
Yo_Robot | 20:4b154134ab20 | 687 | |
Yo_Robot | 20:4b154134ab20 | 688 | EthernetNetIf eth; |
Yo_Robot | 20:4b154134ab20 | 689 | return eth; |
Yo_Robot | 20:4b154134ab20 | 690 | } |