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@20:4b154134ab20, 2012-06-06 (annotated)
- Committer:
- Yo_Robot
- Date:
- Wed Jun 06 03:08:27 2012 +0000
- Revision:
- 20:4b154134ab20
- Parent:
- 19:c26cf8a48986
- Child:
- 21:353b0fe8fc54
Version 1.9 - Limpiar el cdigo - Se configura la alta velocidad a travez de archivo de configuracion - ; ; Falta I2C Encoder y Alarmas.
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 | 3:8d5a9e3cd680 | 12 | |
Yo_Robot | 3:8d5a9e3cd680 | 13 | #include "setup.h" |
Yo_Robot | 18:cf1e07d82630 | 14 | #include "EthernetNetIf.h" |
Yo_Robot | 3:8d5a9e3cd680 | 15 | #include "mbed.h" |
Yo_Robot | 3:8d5a9e3cd680 | 16 | |
Yo_Robot | 3:8d5a9e3cd680 | 17 | // Salida Serial de mbed |
Yo_Robot | 19:c26cf8a48986 | 18 | extern Serial pc; |
Yo_Robot | 19:c26cf8a48986 | 19 | extern Serial RS_232; |
Yo_Robot | 19:c26cf8a48986 | 20 | extern DigitalIn isPC; |
Yo_Robot | 12:c02b08dacc45 | 21 | extern DigitalOut pin_son; // SON |
Yo_Robot | 12:c02b08dacc45 | 22 | extern DigitalOut pin_dir; // SIGN+ |
Yo_Robot | 12:c02b08dacc45 | 23 | extern InterruptIn pin_alm; // ALM |
Yo_Robot | 12:c02b08dacc45 | 24 | extern AnalogOut aout; // +-10V |
Yo_Robot | 3:8d5a9e3cd680 | 25 | |
Yo_Robot | 20:4b154134ab20 | 26 | |
Yo_Robot | 15:a1ffa32ce9d1 | 27 | int fq_posicion = 10000; // Variable global donde se almacenara |
Yo_Robot | 15:a1ffa32ce9d1 | 28 | // la velocidad de posicionamiento en Hz |
Yo_Robot | 15:a1ffa32ce9d1 | 29 | |
Yo_Robot | 4:552beeda4722 | 30 | void setTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 31 | { |
Yo_Robot | 3:8d5a9e3cd680 | 32 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 3:8d5a9e3cd680 | 33 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 3:8d5a9e3cd680 | 34 | |
Yo_Robot | 3:8d5a9e3cd680 | 35 | // Resetear y parar el Timer |
Yo_Robot | 3:8d5a9e3cd680 | 36 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 3:8d5a9e3cd680 | 37 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 3:8d5a9e3cd680 | 38 | |
Yo_Robot | 4:552beeda4722 | 39 | // Establecer el Preescaler en cero |
Yo_Robot | 4:552beeda4722 | 40 | // SIn Preesclaer |
Yo_Robot | 4:552beeda4722 | 41 | LPC_TIM2->PR = 0; |
Yo_Robot | 3:8d5a9e3cd680 | 42 | |
Yo_Robot | 4:552beeda4722 | 43 | // Calcular el periodo Inicial |
Yo_Robot | 4:552beeda4722 | 44 | uint32_t periodo = ( SystemCoreClock / 400 ); |
Yo_Robot | 3:8d5a9e3cd680 | 45 | |
Yo_Robot | 3:8d5a9e3cd680 | 46 | // Establecer los Registros de Coincidencia |
Yo_Robot | 3:8d5a9e3cd680 | 47 | // ( Match Register ) |
Yo_Robot | 3:8d5a9e3cd680 | 48 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 4:552beeda4722 | 49 | LPC_TIM2->MR3 = periodo; // Legacy, salidas identicas |
Yo_Robot | 3:8d5a9e3cd680 | 50 | |
Yo_Robot | 4:552beeda4722 | 51 | LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 |
Yo_Robot | 3:8d5a9e3cd680 | 52 | |
Yo_Robot | 3:8d5a9e3cd680 | 53 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 3:8d5a9e3cd680 | 54 | // y MAT2.3 |
Yo_Robot | 3:8d5a9e3cd680 | 55 | |
Yo_Robot | 3:8d5a9e3cd680 | 56 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 8:958dfe5052b9 | 57 | // y MAT2.3 como salidas |
Yo_Robot | 8:958dfe5052b9 | 58 | |
Yo_Robot | 6:b4dae934e1ea | 59 | } |
Yo_Robot | 6:b4dae934e1ea | 60 | |
Yo_Robot | 12:c02b08dacc45 | 61 | void ISR_Serial() |
Yo_Robot | 12:c02b08dacc45 | 62 | { |
Yo_Robot | 12:c02b08dacc45 | 63 | int value; // Nuevo Valor |
Yo_Robot | 12:c02b08dacc45 | 64 | char command; // Comando al que aplicar el nuevo valor |
Yo_Robot | 12:c02b08dacc45 | 65 | |
Yo_Robot | 19:c26cf8a48986 | 66 | if( isPC ) |
Yo_Robot | 19:c26cf8a48986 | 67 | pc.scanf( "%d-%c", &value, &command ) ; |
Yo_Robot | 19:c26cf8a48986 | 68 | else |
Yo_Robot | 19:c26cf8a48986 | 69 | RS_232.scanf( "%d-%c", &value, &command ) ; |
Yo_Robot | 19:c26cf8a48986 | 70 | |
Yo_Robot | 12:c02b08dacc45 | 71 | // Establecer nueva frecuencia |
Yo_Robot | 12:c02b08dacc45 | 72 | if( command == 'H') |
Yo_Robot | 12:c02b08dacc45 | 73 | setPTO( value ); |
Yo_Robot | 12:c02b08dacc45 | 74 | |
Yo_Robot | 12:c02b08dacc45 | 75 | else if( command == 'K' ) |
Yo_Robot | 12:c02b08dacc45 | 76 | setPTO( value * 1000 ); |
Yo_Robot | 12:c02b08dacc45 | 77 | |
Yo_Robot | 12:c02b08dacc45 | 78 | // Nuevo voltaje de salida |
Yo_Robot | 12:c02b08dacc45 | 79 | // Alguna formula para calcular el Vout necesario |
Yo_Robot | 12:c02b08dacc45 | 80 | // -100% a +100% |
Yo_Robot | 12:c02b08dacc45 | 81 | else if( command == 'A') |
Yo_Robot | 14:039d070732d5 | 82 | aout = (float)( value + 10000.0 ) / 20000.0; |
Yo_Robot | 12:c02b08dacc45 | 83 | |
Yo_Robot | 12:c02b08dacc45 | 84 | |
Yo_Robot | 12:c02b08dacc45 | 85 | // Cambiar la direccion |
Yo_Robot | 12:c02b08dacc45 | 86 | else if( command == 'D') |
Yo_Robot | 14:039d070732d5 | 87 | { |
Yo_Robot | 14:039d070732d5 | 88 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 89 | pin_dir = value; |
Yo_Robot | 14:039d070732d5 | 90 | wait_us( 2 ); |
Yo_Robot | 14:039d070732d5 | 91 | startTimer2(); |
Yo_Robot | 14:039d070732d5 | 92 | } |
Yo_Robot | 14:039d070732d5 | 93 | |
Yo_Robot | 12:c02b08dacc45 | 94 | |
Yo_Robot | 12:c02b08dacc45 | 95 | //Encender el Servo |
Yo_Robot | 12:c02b08dacc45 | 96 | else if( command == 'S') |
Yo_Robot | 12:c02b08dacc45 | 97 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 98 | |
Yo_Robot | 18:cf1e07d82630 | 99 | else if( command == 'V') |
Yo_Robot | 18:cf1e07d82630 | 100 | fq_posicion = value; |
Yo_Robot | 18:cf1e07d82630 | 101 | |
Yo_Robot | 18:cf1e07d82630 | 102 | else if( command == 'G' ) |
Yo_Robot | 18:cf1e07d82630 | 103 | { |
Yo_Robot | 18:cf1e07d82630 | 104 | int pulsos = value; //Numero de pulsos a generar |
Yo_Robot | 18:cf1e07d82630 | 105 | float t_alto = pulsos / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 12:c02b08dacc45 | 106 | |
Yo_Robot | 18:cf1e07d82630 | 107 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 18:cf1e07d82630 | 108 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 18:cf1e07d82630 | 109 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 18:cf1e07d82630 | 110 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 18:cf1e07d82630 | 111 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 18:cf1e07d82630 | 112 | } |
Yo_Robot | 12:c02b08dacc45 | 113 | } |
Yo_Robot | 12:c02b08dacc45 | 114 | |
Yo_Robot | 12:c02b08dacc45 | 115 | |
Yo_Robot | 12:c02b08dacc45 | 116 | void setPTO( int freq ) |
Yo_Robot | 12:c02b08dacc45 | 117 | { |
Yo_Robot | 12:c02b08dacc45 | 118 | if( freq != 0 ) |
Yo_Robot | 12:c02b08dacc45 | 119 | { |
Yo_Robot | 12:c02b08dacc45 | 120 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 121 | setMR2( getMRvalue( freq ) ); |
Yo_Robot | 12:c02b08dacc45 | 122 | startTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 123 | |
Yo_Robot | 12:c02b08dacc45 | 124 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 125 | |
Yo_Robot | 12:c02b08dacc45 | 126 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 127 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 128 | } |
Yo_Robot | 12:c02b08dacc45 | 129 | } |
Yo_Robot | 12:c02b08dacc45 | 130 | |
Yo_Robot | 12:c02b08dacc45 | 131 | void setPTO_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 132 | { |
Yo_Robot | 12:c02b08dacc45 | 133 | int freq = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 134 | |
Yo_Robot | 12:c02b08dacc45 | 135 | if( freq != 0 ){ |
Yo_Robot | 12:c02b08dacc45 | 136 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 137 | setMR2( getMRvalue( freq ) ); // Cambiar frefuencia |
Yo_Robot | 12:c02b08dacc45 | 138 | startTimer2(); // Iniciar Timer |
Yo_Robot | 14:039d070732d5 | 139 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 140 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 141 | else |
Yo_Robot | 14:039d070732d5 | 142 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 143 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 144 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 145 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 14:039d070732d5 | 146 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 147 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 148 | else |
Yo_Robot | 14:039d070732d5 | 149 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 150 | } |
Yo_Robot | 12:c02b08dacc45 | 151 | } |
Yo_Robot | 12:c02b08dacc45 | 152 | |
Yo_Robot | 15:a1ffa32ce9d1 | 153 | void setANG_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 154 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 155 | long int pulsos = atol( input ); //Numero de pulsos a generar |
Yo_Robot | 15:a1ffa32ce9d1 | 156 | float t_alto = pulsos / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 15:a1ffa32ce9d1 | 157 | |
Yo_Robot | 15:a1ffa32ce9d1 | 158 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 159 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 15:a1ffa32ce9d1 | 160 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 161 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 18:cf1e07d82630 | 162 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 15:a1ffa32ce9d1 | 163 | |
Yo_Robot | 15:a1ffa32ce9d1 | 164 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 165 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 166 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 167 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 168 | |
Yo_Robot | 15:a1ffa32ce9d1 | 169 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 170 | void setSPD_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 171 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 172 | fq_posicion = atoi( input ); |
Yo_Robot | 15:a1ffa32ce9d1 | 173 | // Esta funcion cambia la velocidad con la que se |
Yo_Robot | 15:a1ffa32ce9d1 | 174 | // posicionara el eje del motor en un angulo determinado |
Yo_Robot | 15:a1ffa32ce9d1 | 175 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 176 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 177 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 178 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 179 | |
Yo_Robot | 15:a1ffa32ce9d1 | 180 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 181 | void getENC_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 182 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 183 | //Leer el Encoder ******* F A L T A |
Yo_Robot | 15:a1ffa32ce9d1 | 184 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 185 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 186 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 187 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 188 | |
Yo_Robot | 15:a1ffa32ce9d1 | 189 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 190 | |
Yo_Robot | 12:c02b08dacc45 | 191 | |
Yo_Robot | 12:c02b08dacc45 | 192 | void ISR_Alarm() |
Yo_Robot | 12:c02b08dacc45 | 193 | { |
Yo_Robot | 12:c02b08dacc45 | 194 | pin_son = 0 ; |
Yo_Robot | 12:c02b08dacc45 | 195 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 196 | aout = 0.5 ; |
Yo_Robot | 12:c02b08dacc45 | 197 | |
Yo_Robot | 19:c26cf8a48986 | 198 | if(isPC) |
Yo_Robot | 19:c26cf8a48986 | 199 | pc.printf( "AL" ); //ALARMA! solo es AL para que |
Yo_Robot | 19:c26cf8a48986 | 200 | //sea conciso con el modo ETH y funcione |
Yo_Robot | 19:c26cf8a48986 | 201 | //bien en LabVIEW. |
Yo_Robot | 19:c26cf8a48986 | 202 | else |
Yo_Robot | 19:c26cf8a48986 | 203 | RS_232.printf( "AL" ); |
Yo_Robot | 12:c02b08dacc45 | 204 | } |
Yo_Robot | 6:b4dae934e1ea | 205 | |
Yo_Robot | 4:552beeda4722 | 206 | int getMRvalue( int fout ) |
Yo_Robot | 3:8d5a9e3cd680 | 207 | { |
Yo_Robot | 4:552beeda4722 | 208 | int toRegister; |
Yo_Robot | 4:552beeda4722 | 209 | |
Yo_Robot | 12:c02b08dacc45 | 210 | toRegister = (24000000 /(fout*2.0) ) -1; |
Yo_Robot | 4:552beeda4722 | 211 | return toRegister; |
Yo_Robot | 3:8d5a9e3cd680 | 212 | } |
Yo_Robot | 3:8d5a9e3cd680 | 213 | |
Yo_Robot | 3:8d5a9e3cd680 | 214 | |
Yo_Robot | 3:8d5a9e3cd680 | 215 | void setMR2( int newValue ) |
Yo_Robot | 3:8d5a9e3cd680 | 216 | { |
Yo_Robot | 4:552beeda4722 | 217 | LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas |
Yo_Robot | 4:552beeda4722 | 218 | LPC_TIM2->MR3 = newValue; // Para testear el programa. |
Yo_Robot | 3:8d5a9e3cd680 | 219 | } |
Yo_Robot | 3:8d5a9e3cd680 | 220 | |
Yo_Robot | 3:8d5a9e3cd680 | 221 | |
Yo_Robot | 3:8d5a9e3cd680 | 222 | |
Yo_Robot | 3:8d5a9e3cd680 | 223 | void startTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 224 | { |
Yo_Robot | 8:958dfe5052b9 | 225 | // Arrancer el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 226 | LPC_TIM2->TCR = 1; |
Yo_Robot | 3:8d5a9e3cd680 | 227 | } |
Yo_Robot | 3:8d5a9e3cd680 | 228 | |
Yo_Robot | 3:8d5a9e3cd680 | 229 | void stopTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 230 | { |
Yo_Robot | 4:552beeda4722 | 231 | // Detener el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 232 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 4:552beeda4722 | 233 | } |
Yo_Robot | 4:552beeda4722 | 234 | |
Yo_Robot | 12:c02b08dacc45 | 235 | |
Yo_Robot | 12:c02b08dacc45 | 236 | // **** Funciones Liberia Ethernet ***** // |
Yo_Robot | 12:c02b08dacc45 | 237 | |
Yo_Robot | 12:c02b08dacc45 | 238 | void setAout_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 239 | { |
Yo_Robot | 12:c02b08dacc45 | 240 | int vout = atoi( input ); |
Yo_Robot | 14:039d070732d5 | 241 | aout = (float)( vout + 10000 ) / 20000; |
Yo_Robot | 14:039d070732d5 | 242 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 243 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 244 | else |
Yo_Robot | 14:039d070732d5 | 245 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 246 | } |
Yo_Robot | 12:c02b08dacc45 | 247 | |
Yo_Robot | 12:c02b08dacc45 | 248 | |
Yo_Robot | 12:c02b08dacc45 | 249 | |
Yo_Robot | 12:c02b08dacc45 | 250 | void setDir_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 251 | { |
Yo_Robot | 12:c02b08dacc45 | 252 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 253 | |
Yo_Robot | 12:c02b08dacc45 | 254 | pin_dir = value; |
Yo_Robot | 12:c02b08dacc45 | 255 | |
Yo_Robot | 14:039d070732d5 | 256 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 257 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 258 | else |
Yo_Robot | 14:039d070732d5 | 259 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 260 | } |
Yo_Robot | 12:c02b08dacc45 | 261 | |
Yo_Robot | 12:c02b08dacc45 | 262 | |
Yo_Robot | 12:c02b08dacc45 | 263 | |
Yo_Robot | 12:c02b08dacc45 | 264 | void setSON_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 265 | { |
Yo_Robot | 12:c02b08dacc45 | 266 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 267 | |
Yo_Robot | 12:c02b08dacc45 | 268 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 269 | |
Yo_Robot | 14:039d070732d5 | 270 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 271 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 272 | else |
Yo_Robot | 14:039d070732d5 | 273 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 274 | |
Yo_Robot | 12:c02b08dacc45 | 275 | } |
Yo_Robot | 12:c02b08dacc45 | 276 | |
Yo_Robot | 12:c02b08dacc45 | 277 | |
Yo_Robot | 20:4b154134ab20 | 278 | int configurarBaudios() |
Yo_Robot | 20:4b154134ab20 | 279 | { |
Yo_Robot | 20:4b154134ab20 | 280 | int baudios = 115200; //Valor por defecto |
Yo_Robot | 20:4b154134ab20 | 281 | |
Yo_Robot | 20:4b154134ab20 | 282 | //____________ *** ARCHIVO DE CONFIGURACION ***_______________________ // |
Yo_Robot | 20:4b154134ab20 | 283 | |
Yo_Robot | 20:4b154134ab20 | 284 | printf("\n\n *** CONFIGURACION DE MBED ***\n"); |
Yo_Robot | 20:4b154134ab20 | 285 | printf(" Leyendo archivo de configuracion...\n\n"); |
Yo_Robot | 20:4b154134ab20 | 286 | |
Yo_Robot | 20:4b154134ab20 | 287 | FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 20:4b154134ab20 | 288 | |
Yo_Robot | 20:4b154134ab20 | 289 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 20:4b154134ab20 | 290 | { |
Yo_Robot | 20:4b154134ab20 | 291 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 20:4b154134ab20 | 292 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 293 | |
Yo_Robot | 20:4b154134ab20 | 294 | } |
Yo_Robot | 20:4b154134ab20 | 295 | else |
Yo_Robot | 20:4b154134ab20 | 296 | { |
Yo_Robot | 20:4b154134ab20 | 297 | // Cadenas de caracteres desde el Archivo config.txt |
Yo_Robot | 20:4b154134ab20 | 298 | char notstr [04]; // linea vacia |
Yo_Robot | 20:4b154134ab20 | 299 | char baud [40]; // Direccion DNS |
Yo_Robot | 20:4b154134ab20 | 300 | |
Yo_Robot | 20:4b154134ab20 | 301 | |
Yo_Robot | 20:4b154134ab20 | 302 | // Leer linea a linea el archivo |
Yo_Robot | 20:4b154134ab20 | 303 | // cuatro primeras lineas no sirven |
Yo_Robot | 20:4b154134ab20 | 304 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 305 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 306 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 307 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 308 | |
Yo_Robot | 20:4b154134ab20 | 309 | fgets( baud, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 310 | |
Yo_Robot | 20:4b154134ab20 | 311 | printf("Cerrando archivo...\n"); |
Yo_Robot | 20:4b154134ab20 | 312 | fclose(fp); |
Yo_Robot | 20:4b154134ab20 | 313 | |
Yo_Robot | 20:4b154134ab20 | 314 | |
Yo_Robot | 20:4b154134ab20 | 315 | // Extraer los valores numericos |
Yo_Robot | 20:4b154134ab20 | 316 | sscanf( baud,"%*s %d",&baudios ); |
Yo_Robot | 20:4b154134ab20 | 317 | |
Yo_Robot | 20:4b154134ab20 | 318 | |
Yo_Robot | 20:4b154134ab20 | 319 | } |
Yo_Robot | 20:4b154134ab20 | 320 | |
Yo_Robot | 20:4b154134ab20 | 321 | return baudios; |
Yo_Robot | 20:4b154134ab20 | 322 | } |
Yo_Robot | 12:c02b08dacc45 | 323 | |
Yo_Robot | 4:552beeda4722 | 324 | /* LEGACY FUNCTIONS |
Yo_Robot | 4:552beeda4722 | 325 | * |
Yo_Robot | 20:4b154134ab20 | 326 | * El siguiente codigo no es utilizado por el |
Yo_Robot | 20:4b154134ab20 | 327 | * programa. Sin embargo pueden servir como |
Yo_Robot | 20:4b154134ab20 | 328 | * futuras referencias. |
Yo_Robot | 4:552beeda4722 | 329 | */ |
Yo_Robot | 4:552beeda4722 | 330 | void setMR3( int newValue ) |
Yo_Robot | 4:552beeda4722 | 331 | { |
Yo_Robot | 4:552beeda4722 | 332 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 4:552beeda4722 | 333 | } |
Yo_Robot | 4:552beeda4722 | 334 | |
Yo_Robot | 4:552beeda4722 | 335 | |
Yo_Robot | 4:552beeda4722 | 336 | void setPrescaler( int newValue) |
Yo_Robot | 4:552beeda4722 | 337 | { |
Yo_Robot | 4:552beeda4722 | 338 | LPC_TIM2->PR = newValue; |
Yo_Robot | 4:552beeda4722 | 339 | } |
Yo_Robot | 20:4b154134ab20 | 340 | |
Yo_Robot | 20:4b154134ab20 | 341 | |
Yo_Robot | 20:4b154134ab20 | 342 | EthernetNetIf configurarEthernet() |
Yo_Robot | 20:4b154134ab20 | 343 | { |
Yo_Robot | 20:4b154134ab20 | 344 | //____________ *** ARCHIVO DE CONFIGURACION ***_______________________ // |
Yo_Robot | 20:4b154134ab20 | 345 | |
Yo_Robot | 20:4b154134ab20 | 346 | printf("\n *** CONFIGURACION ETHERNET DE MBED ***\n"); |
Yo_Robot | 20:4b154134ab20 | 347 | printf("Leyendo archivo de configuracion...\n\n"); |
Yo_Robot | 20:4b154134ab20 | 348 | |
Yo_Robot | 20:4b154134ab20 | 349 | FILE *fp = fopen("/local/config.txt", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 20:4b154134ab20 | 350 | |
Yo_Robot | 20:4b154134ab20 | 351 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 20:4b154134ab20 | 352 | { |
Yo_Robot | 20:4b154134ab20 | 353 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 20:4b154134ab20 | 354 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 355 | |
Yo_Robot | 20:4b154134ab20 | 356 | } |
Yo_Robot | 20:4b154134ab20 | 357 | else |
Yo_Robot | 20:4b154134ab20 | 358 | { |
Yo_Robot | 20:4b154134ab20 | 359 | // Cadenas de caracteres desde el Archivo config.txt |
Yo_Robot | 20:4b154134ab20 | 360 | char isDHCP [15]; //Modo Automatico o Manual |
Yo_Robot | 20:4b154134ab20 | 361 | char empty [2]; // Linea vacia |
Yo_Robot | 20:4b154134ab20 | 362 | char ip [40]; // Direccion IP |
Yo_Robot | 20:4b154134ab20 | 363 | char mask [40]; // Mascara de Subred |
Yo_Robot | 20:4b154134ab20 | 364 | char gate [40]; // Puerta de enlace |
Yo_Robot | 20:4b154134ab20 | 365 | char dns [40]; // Direccion DNS |
Yo_Robot | 20:4b154134ab20 | 366 | |
Yo_Robot | 20:4b154134ab20 | 367 | // Valores 'int' para agregar a la configuracion manual |
Yo_Robot | 20:4b154134ab20 | 368 | int DHCP; |
Yo_Robot | 20:4b154134ab20 | 369 | int n_ip[4]; |
Yo_Robot | 20:4b154134ab20 | 370 | int n_mask[4]; |
Yo_Robot | 20:4b154134ab20 | 371 | int n_gate[4]; |
Yo_Robot | 20:4b154134ab20 | 372 | int n_dns[4]; |
Yo_Robot | 20:4b154134ab20 | 373 | |
Yo_Robot | 20:4b154134ab20 | 374 | |
Yo_Robot | 20:4b154134ab20 | 375 | // Leer linea a linea el archivo |
Yo_Robot | 20:4b154134ab20 | 376 | fgets( isDHCP, 15, fp ); |
Yo_Robot | 20:4b154134ab20 | 377 | fgets( empty, 2, fp ); |
Yo_Robot | 20:4b154134ab20 | 378 | fgets( ip, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 379 | fgets( mask, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 380 | fgets( gate, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 381 | fgets( dns, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 382 | |
Yo_Robot | 20:4b154134ab20 | 383 | printf("Cerrando archivo...\n"); |
Yo_Robot | 20:4b154134ab20 | 384 | fclose(fp); |
Yo_Robot | 20:4b154134ab20 | 385 | |
Yo_Robot | 20:4b154134ab20 | 386 | // Extraer los valores numericos |
Yo_Robot | 20:4b154134ab20 | 387 | sscanf( isDHCP,"%*s %d",&DHCP ); |
Yo_Robot | 20:4b154134ab20 | 388 | |
Yo_Robot | 20:4b154134ab20 | 389 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0],&n_ip[1],&n_ip[2],&n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 390 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 391 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 392 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0],&n_dns[1],&n_dns[2],&n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 393 | |
Yo_Robot | 20:4b154134ab20 | 394 | |
Yo_Robot | 20:4b154134ab20 | 395 | if(DHCP) //En caso de modo DHCP |
Yo_Robot | 20:4b154134ab20 | 396 | { |
Yo_Robot | 20:4b154134ab20 | 397 | printf("\n Configurar red de manera automatica\n"); |
Yo_Robot | 20:4b154134ab20 | 398 | EthernetNetIf eth; //Crea la interfaz |
Yo_Robot | 20:4b154134ab20 | 399 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 400 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 401 | { |
Yo_Robot | 20:4b154134ab20 | 402 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 403 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 404 | } |
Yo_Robot | 20:4b154134ab20 | 405 | |
Yo_Robot | 20:4b154134ab20 | 406 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 407 | return eth; |
Yo_Robot | 20:4b154134ab20 | 408 | } |
Yo_Robot | 20:4b154134ab20 | 409 | else |
Yo_Robot | 20:4b154134ab20 | 410 | { |
Yo_Robot | 20:4b154134ab20 | 411 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0], &n_ip[1], &n_ip[2], &n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 412 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 413 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 414 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0], &n_dns[1], &n_dns[2], &n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 415 | printf(" %s %s %s %s %s\n " , isDHCP, ip, mask, gate, dns ); |
Yo_Robot | 20:4b154134ab20 | 416 | |
Yo_Robot | 20:4b154134ab20 | 417 | printf("\n Configurar red de manera manual\n"); |
Yo_Robot | 20:4b154134ab20 | 418 | EthernetNetIf eth( |
Yo_Robot | 20:4b154134ab20 | 419 | IpAddr( n_ip[0], n_ip[1], n_ip[2], n_ip[3]), //IP Address |
Yo_Robot | 20:4b154134ab20 | 420 | IpAddr( n_mask[0],n_mask[1],n_mask[2],n_mask[3]), //Network Mask |
Yo_Robot | 20:4b154134ab20 | 421 | IpAddr( n_gate[0],n_gate[1],n_gate[2],n_gate[3]), //Gateway |
Yo_Robot | 20:4b154134ab20 | 422 | IpAddr( n_dns[0], n_dns[1], n_dns[2], n_dns[3] ) //DNS |
Yo_Robot | 20:4b154134ab20 | 423 | ); |
Yo_Robot | 20:4b154134ab20 | 424 | |
Yo_Robot | 20:4b154134ab20 | 425 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 426 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 427 | { |
Yo_Robot | 20:4b154134ab20 | 428 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 429 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 430 | } |
Yo_Robot | 20:4b154134ab20 | 431 | |
Yo_Robot | 20:4b154134ab20 | 432 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 433 | return eth; |
Yo_Robot | 20:4b154134ab20 | 434 | } |
Yo_Robot | 20:4b154134ab20 | 435 | |
Yo_Robot | 20:4b154134ab20 | 436 | } |
Yo_Robot | 20:4b154134ab20 | 437 | |
Yo_Robot | 20:4b154134ab20 | 438 | EthernetNetIf eth; |
Yo_Robot | 20:4b154134ab20 | 439 | return eth; |
Yo_Robot | 20:4b154134ab20 | 440 | } |