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