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