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@25:1910a55ff0a3, 2013-04-11 (annotated)
- Committer:
- Yo_Robot
- Date:
- Thu Apr 11 04:31:37 2013 +0000
- Revision:
- 25:1910a55ff0a3
- Parent:
- 24:a1d16835201c
- Child:
- 26:dad0b2031173
Compila pero falta probar el codigo con el encoder!
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 | 22:d5431fff164b | 35 | const int addres = 0x36; // Direccion del enconder en modo esclavo |
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 | int ang = 0; |
Yo_Robot | 23:2126e38bb48c | 95 | float rpm; |
Yo_Robot | 23:2126e38bb48c | 96 | |
Yo_Robot | 23:2126e38bb48c | 97 | clear_encoder(); // Encerar encoder |
Yo_Robot | 23:2126e38bb48c | 98 | crono.reset(); // Encerar el cronometro; |
Yo_Robot | 23:2126e38bb48c | 99 | |
Yo_Robot | 23:2126e38bb48c | 100 | while( ang == 0 ) // Esperar hasta q exista lectura del encoder |
Yo_Robot | 23:2126e38bb48c | 101 | ang = read_encoder(); // Leer nueva posicion |
Yo_Robot | 23:2126e38bb48c | 102 | |
Yo_Robot | 23:2126e38bb48c | 103 | crono.stop(); // detener cronometro |
Yo_Robot | 23:2126e38bb48c | 104 | rpm = ( ang * 500 ) / (crono.read_ms() * 3 ); |
Yo_Robot | 23:2126e38bb48c | 105 | |
Yo_Robot | 24:a1d16835201c | 106 | // grados 1000 ms 60 s 1 Rev 500 Rev |
Yo_Robot | 24:a1d16835201c | 107 | // ------ * -------- * ------ * ---------- = -------- |
Yo_Robot | 24:a1d16835201c | 108 | // ms 1s 1min 360 grad 3 Min |
Yo_Robot | 23:2126e38bb48c | 109 | |
Yo_Robot | 23:2126e38bb48c | 110 | if( isPC ) |
Yo_Robot | 23:2126e38bb48c | 111 | pc.printf("%f", rpm); |
Yo_Robot | 23:2126e38bb48c | 112 | else |
Yo_Robot | 23:2126e38bb48c | 113 | RS_232.printf("%f", rpm); |
Yo_Robot | 23:2126e38bb48c | 114 | break; |
Yo_Robot | 24:a1d16835201c | 115 | } |
Yo_Robot | 23:2126e38bb48c | 116 | case 'H': // Establecer nueva frecuencia |
Yo_Robot | 24:a1d16835201c | 117 | { |
Yo_Robot | 23:2126e38bb48c | 118 | setPTO( value ); |
Yo_Robot | 23:2126e38bb48c | 119 | break; |
Yo_Robot | 24:a1d16835201c | 120 | } |
Yo_Robot | 23:2126e38bb48c | 121 | case 'K': |
Yo_Robot | 24:a1d16835201c | 122 | { |
Yo_Robot | 23:2126e38bb48c | 123 | setPTO( value * 1000 ); |
Yo_Robot | 23:2126e38bb48c | 124 | break; |
Yo_Robot | 24:a1d16835201c | 125 | } |
Yo_Robot | 23:2126e38bb48c | 126 | case 'A': // Cambiar voltaje de salida |
Yo_Robot | 24:a1d16835201c | 127 | { |
Yo_Robot | 23:2126e38bb48c | 128 | aout = (float)( value + 10000.0 ) / 20000.0; |
Yo_Robot | 23:2126e38bb48c | 129 | break; |
Yo_Robot | 24:a1d16835201c | 130 | } |
Yo_Robot | 23:2126e38bb48c | 131 | case 'D': // Cambiar la direccion |
Yo_Robot | 24:a1d16835201c | 132 | { |
Yo_Robot | 23:2126e38bb48c | 133 | stopTimer2(); |
Yo_Robot | 23:2126e38bb48c | 134 | pin_dir = value; |
Yo_Robot | 23:2126e38bb48c | 135 | wait_us( 2 ); |
Yo_Robot | 23:2126e38bb48c | 136 | startTimer2(); |
Yo_Robot | 23:2126e38bb48c | 137 | break; |
Yo_Robot | 24:a1d16835201c | 138 | } |
Yo_Robot | 23:2126e38bb48c | 139 | case 'V': //Setear la velocidad de Posicionamiento |
Yo_Robot | 24:a1d16835201c | 140 | { |
Yo_Robot | 23:2126e38bb48c | 141 | fq_posicion = value; |
Yo_Robot | 23:2126e38bb48c | 142 | break; |
Yo_Robot | 24:a1d16835201c | 143 | } |
Yo_Robot | 23:2126e38bb48c | 144 | // Generar un numero definido de pulsos a la velocidad de posicionamiento |
Yo_Robot | 23:2126e38bb48c | 145 | case 'G': |
Yo_Robot | 24:a1d16835201c | 146 | { |
Yo_Robot | 23:2126e38bb48c | 147 | int pulsos = value; //Numero de pulsos a generar |
Yo_Robot | 23:2126e38bb48c | 148 | t_alto = pulsos / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 23:2126e38bb48c | 149 | |
Yo_Robot | 23:2126e38bb48c | 150 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 23:2126e38bb48c | 151 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 23:2126e38bb48c | 152 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 23:2126e38bb48c | 153 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 23:2126e38bb48c | 154 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 23:2126e38bb48c | 155 | break; |
Yo_Robot | 24:a1d16835201c | 156 | } |
Yo_Robot | 24:a1d16835201c | 157 | case 'E': //Leer posicion angular relativa del encoder |
Yo_Robot | 24:a1d16835201c | 158 | { |
Yo_Robot | 23:2126e38bb48c | 159 | if( isPC ) |
Yo_Robot | 23:2126e38bb48c | 160 | pc.printf( "%d",read_encoder() ); |
Yo_Robot | 23:2126e38bb48c | 161 | else |
Yo_Robot | 23:2126e38bb48c | 162 | RS_232.printf( "%d",read_encoder() ); |
Yo_Robot | 23:2126e38bb48c | 163 | break; |
Yo_Robot | 24:a1d16835201c | 164 | } |
Yo_Robot | 23:2126e38bb48c | 165 | case 'Z': //Limpiar contador encoder |
Yo_Robot | 23:2126e38bb48c | 166 | clear_encoder(); |
Yo_Robot | 23:2126e38bb48c | 167 | break; |
Yo_Robot | 23:2126e38bb48c | 168 | |
Yo_Robot | 23:2126e38bb48c | 169 | case 'S': //Encender el Servo |
Yo_Robot | 23:2126e38bb48c | 170 | pin_son = value; |
Yo_Robot | 23:2126e38bb48c | 171 | break; |
Yo_Robot | 18:cf1e07d82630 | 172 | } |
Yo_Robot | 23:2126e38bb48c | 173 | |
Yo_Robot | 12:c02b08dacc45 | 174 | } |
Yo_Robot | 12:c02b08dacc45 | 175 | |
Yo_Robot | 12:c02b08dacc45 | 176 | |
Yo_Robot | 12:c02b08dacc45 | 177 | void setPTO( int freq ) |
Yo_Robot | 12:c02b08dacc45 | 178 | { |
Yo_Robot | 12:c02b08dacc45 | 179 | if( freq != 0 ) |
Yo_Robot | 12:c02b08dacc45 | 180 | { |
Yo_Robot | 12:c02b08dacc45 | 181 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 182 | setMR2( getMRvalue( freq ) ); |
Yo_Robot | 12:c02b08dacc45 | 183 | startTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 184 | |
Yo_Robot | 12:c02b08dacc45 | 185 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 186 | |
Yo_Robot | 12:c02b08dacc45 | 187 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 188 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 189 | } |
Yo_Robot | 12:c02b08dacc45 | 190 | } |
Yo_Robot | 12:c02b08dacc45 | 191 | |
Yo_Robot | 22:d5431fff164b | 192 | |
Yo_Robot | 22:d5431fff164b | 193 | |
Yo_Robot | 22:d5431fff164b | 194 | void ISR_Alarm() |
Yo_Robot | 22:d5431fff164b | 195 | { |
Yo_Robot | 22:d5431fff164b | 196 | pin_son = 0 ; |
Yo_Robot | 22:d5431fff164b | 197 | stopTimer2(); |
Yo_Robot | 22:d5431fff164b | 198 | aout = 0.5 ; |
Yo_Robot | 22:d5431fff164b | 199 | |
Yo_Robot | 22:d5431fff164b | 200 | if(isPC) |
Yo_Robot | 22:d5431fff164b | 201 | pc.printf( "AL" ); //ALARMA! solo es AL para que |
Yo_Robot | 22:d5431fff164b | 202 | //sea conciso con el modo ETH y funcione |
Yo_Robot | 22:d5431fff164b | 203 | //bien en LabVIEW. |
Yo_Robot | 22:d5431fff164b | 204 | else |
Yo_Robot | 22:d5431fff164b | 205 | RS_232.printf( "AL" ); |
Yo_Robot | 22:d5431fff164b | 206 | } |
Yo_Robot | 22:d5431fff164b | 207 | |
Yo_Robot | 22:d5431fff164b | 208 | int getMRvalue( int fout ) |
Yo_Robot | 22:d5431fff164b | 209 | { |
Yo_Robot | 22:d5431fff164b | 210 | int toRegister; |
Yo_Robot | 22:d5431fff164b | 211 | |
Yo_Robot | 22:d5431fff164b | 212 | toRegister = (24000000 /(fout*2.0) ) -1; |
Yo_Robot | 22:d5431fff164b | 213 | return toRegister; |
Yo_Robot | 22:d5431fff164b | 214 | } |
Yo_Robot | 22:d5431fff164b | 215 | |
Yo_Robot | 22:d5431fff164b | 216 | |
Yo_Robot | 22:d5431fff164b | 217 | void setMR2( int newValue ) |
Yo_Robot | 22:d5431fff164b | 218 | { |
Yo_Robot | 22:d5431fff164b | 219 | LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas |
Yo_Robot | 22:d5431fff164b | 220 | LPC_TIM2->MR3 = newValue; // Para testear el programa. |
Yo_Robot | 22:d5431fff164b | 221 | } |
Yo_Robot | 22:d5431fff164b | 222 | |
Yo_Robot | 22:d5431fff164b | 223 | |
Yo_Robot | 22:d5431fff164b | 224 | |
Yo_Robot | 22:d5431fff164b | 225 | void startTimer2() |
Yo_Robot | 22:d5431fff164b | 226 | { |
Yo_Robot | 22:d5431fff164b | 227 | // Arrancer el Timer 2 |
Yo_Robot | 22:d5431fff164b | 228 | LPC_TIM2->TCR = 1; |
Yo_Robot | 22:d5431fff164b | 229 | } |
Yo_Robot | 22:d5431fff164b | 230 | |
Yo_Robot | 22:d5431fff164b | 231 | void stopTimer2() |
Yo_Robot | 22:d5431fff164b | 232 | { |
Yo_Robot | 22:d5431fff164b | 233 | // Detener el Timer 2 |
Yo_Robot | 22:d5431fff164b | 234 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 22:d5431fff164b | 235 | } |
Yo_Robot | 22:d5431fff164b | 236 | |
Yo_Robot | 22:d5431fff164b | 237 | int getBaud() |
Yo_Robot | 22:d5431fff164b | 238 | { |
Yo_Robot | 22:d5431fff164b | 239 | int baudios = 115200; //Valor por defecto |
Yo_Robot | 22:d5431fff164b | 240 | |
Yo_Robot | 22:d5431fff164b | 241 | FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 22:d5431fff164b | 242 | |
Yo_Robot | 22:d5431fff164b | 243 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 22:d5431fff164b | 244 | { |
Yo_Robot | 22:d5431fff164b | 245 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 22:d5431fff164b | 246 | printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n"); |
Yo_Robot | 22:d5431fff164b | 247 | printf("que contenga las lineas:\n\n"); |
Yo_Robot | 22:d5431fff164b | 248 | |
Yo_Robot | 22:d5431fff164b | 249 | printf(" 1\n"); |
Yo_Robot | 22:d5431fff164b | 250 | printf(" 2\n"); |
Yo_Robot | 22:d5431fff164b | 251 | printf(" 3\n"); |
Yo_Robot | 22:d5431fff164b | 252 | printf(" 4\n"); |
Yo_Robot | 22:d5431fff164b | 253 | printf(" baudios: 115200\n"); |
Yo_Robot | 22:d5431fff164b | 254 | |
Yo_Robot | 22:d5431fff164b | 255 | printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n"); |
Yo_Robot | 22:d5431fff164b | 256 | printf("luego reinicie el microcontrolador\n"); |
Yo_Robot | 22:d5431fff164b | 257 | exit(1); |
Yo_Robot | 22:d5431fff164b | 258 | |
Yo_Robot | 22:d5431fff164b | 259 | } |
Yo_Robot | 22:d5431fff164b | 260 | else |
Yo_Robot | 22:d5431fff164b | 261 | { |
Yo_Robot | 22:d5431fff164b | 262 | // Cadenas de caracteres desde el Archivo config.mbd |
Yo_Robot | 22:d5431fff164b | 263 | char notstr [04]; // linea vacia |
Yo_Robot | 22:d5431fff164b | 264 | char baud [40]; // valor en baudios |
Yo_Robot | 22:d5431fff164b | 265 | |
Yo_Robot | 22:d5431fff164b | 266 | // Leer linea a linea el archivo |
Yo_Robot | 22:d5431fff164b | 267 | // cuatro primeras lineas no sirven |
Yo_Robot | 22:d5431fff164b | 268 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 269 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 270 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 271 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 272 | fgets( baud, 40, fp ); |
Yo_Robot | 22:d5431fff164b | 273 | fclose(fp); |
Yo_Robot | 23:2126e38bb48c | 274 | |
Yo_Robot | 22:d5431fff164b | 275 | // Extraer los valores numericos |
Yo_Robot | 22:d5431fff164b | 276 | sscanf( baud,"%*s %d",&baudios ); |
Yo_Robot | 22:d5431fff164b | 277 | |
Yo_Robot | 22:d5431fff164b | 278 | |
Yo_Robot | 22:d5431fff164b | 279 | } |
Yo_Robot | 22:d5431fff164b | 280 | |
Yo_Robot | 22:d5431fff164b | 281 | return baudios; |
Yo_Robot | 22:d5431fff164b | 282 | } |
Yo_Robot | 22:d5431fff164b | 283 | |
Yo_Robot | 22:d5431fff164b | 284 | // **** Funciones Liberia Ethernet ***** // |
Yo_Robot | 12:c02b08dacc45 | 285 | void setPTO_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 286 | { |
Yo_Robot | 12:c02b08dacc45 | 287 | int freq = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 288 | |
Yo_Robot | 12:c02b08dacc45 | 289 | if( freq != 0 ){ |
Yo_Robot | 12:c02b08dacc45 | 290 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 291 | setMR2( getMRvalue( freq ) ); // Cambiar frefuencia |
Yo_Robot | 12:c02b08dacc45 | 292 | startTimer2(); // Iniciar Timer |
Yo_Robot | 14:039d070732d5 | 293 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 294 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 295 | else |
Yo_Robot | 14:039d070732d5 | 296 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 297 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 298 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 299 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 14:039d070732d5 | 300 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 301 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 302 | else |
Yo_Robot | 14:039d070732d5 | 303 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 304 | } |
Yo_Robot | 12:c02b08dacc45 | 305 | } |
Yo_Robot | 12:c02b08dacc45 | 306 | |
Yo_Robot | 15:a1ffa32ce9d1 | 307 | void setANG_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 308 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 309 | long int pulsos = atol( input ); //Numero de pulsos a generar |
Yo_Robot | 23:2126e38bb48c | 310 | t_alto = pulsos / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 15:a1ffa32ce9d1 | 311 | |
Yo_Robot | 15:a1ffa32ce9d1 | 312 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 313 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 15:a1ffa32ce9d1 | 314 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 315 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 18:cf1e07d82630 | 316 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 15:a1ffa32ce9d1 | 317 | |
Yo_Robot | 15:a1ffa32ce9d1 | 318 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 319 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 320 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 321 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 322 | |
Yo_Robot | 15:a1ffa32ce9d1 | 323 | } |
Yo_Robot | 21:353b0fe8fc54 | 324 | |
Yo_Robot | 21:353b0fe8fc54 | 325 | |
Yo_Robot | 15:a1ffa32ce9d1 | 326 | void setSPD_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 327 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 328 | fq_posicion = atoi( input ); |
Yo_Robot | 15:a1ffa32ce9d1 | 329 | // Esta funcion cambia la velocidad con la que se |
Yo_Robot | 15:a1ffa32ce9d1 | 330 | // posicionara el eje del motor en un angulo determinado |
Yo_Robot | 15:a1ffa32ce9d1 | 331 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 332 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 333 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 334 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 335 | |
Yo_Robot | 15:a1ffa32ce9d1 | 336 | } |
Yo_Robot | 21:353b0fe8fc54 | 337 | |
Yo_Robot | 12:c02b08dacc45 | 338 | void setAout_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 339 | { |
Yo_Robot | 12:c02b08dacc45 | 340 | int vout = atoi( input ); |
Yo_Robot | 14:039d070732d5 | 341 | aout = (float)( vout + 10000 ) / 20000; |
Yo_Robot | 14:039d070732d5 | 342 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 343 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 344 | else |
Yo_Robot | 14:039d070732d5 | 345 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 346 | } |
Yo_Robot | 12:c02b08dacc45 | 347 | |
Yo_Robot | 12:c02b08dacc45 | 348 | |
Yo_Robot | 12:c02b08dacc45 | 349 | |
Yo_Robot | 12:c02b08dacc45 | 350 | void setDir_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 351 | { |
Yo_Robot | 12:c02b08dacc45 | 352 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 353 | |
Yo_Robot | 12:c02b08dacc45 | 354 | pin_dir = value; |
Yo_Robot | 12:c02b08dacc45 | 355 | |
Yo_Robot | 14:039d070732d5 | 356 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 357 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 358 | else |
Yo_Robot | 14:039d070732d5 | 359 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 360 | } |
Yo_Robot | 12:c02b08dacc45 | 361 | |
Yo_Robot | 12:c02b08dacc45 | 362 | |
Yo_Robot | 12:c02b08dacc45 | 363 | |
Yo_Robot | 12:c02b08dacc45 | 364 | void setSON_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 365 | { |
Yo_Robot | 12:c02b08dacc45 | 366 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 367 | |
Yo_Robot | 12:c02b08dacc45 | 368 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 369 | |
Yo_Robot | 14:039d070732d5 | 370 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 371 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 372 | else |
Yo_Robot | 14:039d070732d5 | 373 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 374 | |
Yo_Robot | 12:c02b08dacc45 | 375 | } |
Yo_Robot | 12:c02b08dacc45 | 376 | |
Yo_Robot | 12:c02b08dacc45 | 377 | |
Yo_Robot | 22:d5431fff164b | 378 | void getENC_eth( char * input, char * output ) |
Yo_Robot | 20:4b154134ab20 | 379 | { |
Yo_Robot | 22:d5431fff164b | 380 | if( pin_alm == 0 ) |
Yo_Robot | 22:d5431fff164b | 381 | sprintf( output,"%d", read_encoder() ); |
Yo_Robot | 22:d5431fff164b | 382 | else |
Yo_Robot | 22:d5431fff164b | 383 | sprintf( output,"AL" ); |
Yo_Robot | 22:d5431fff164b | 384 | } |
Yo_Robot | 21:353b0fe8fc54 | 385 | |
Yo_Robot | 21:353b0fe8fc54 | 386 | |
Yo_Robot | 22:d5431fff164b | 387 | void setENC_eth( char * input, char * output ) |
Yo_Robot | 22:d5431fff164b | 388 | { |
Yo_Robot | 22:d5431fff164b | 389 | clear_encoder(); |
Yo_Robot | 22:d5431fff164b | 390 | |
Yo_Robot | 22:d5431fff164b | 391 | if( pin_alm == 0 ) |
Yo_Robot | 22:d5431fff164b | 392 | sprintf( output,"Ok" ); |
Yo_Robot | 22:d5431fff164b | 393 | else |
Yo_Robot | 22:d5431fff164b | 394 | sprintf( output,"AL" ); |
Yo_Robot | 22:d5431fff164b | 395 | |
Yo_Robot | 20:4b154134ab20 | 396 | } |
Yo_Robot | 12:c02b08dacc45 | 397 | |
Yo_Robot | 23:2126e38bb48c | 398 | void getRPM_eth( char * input, char * output ) |
Yo_Robot | 23:2126e38bb48c | 399 | { |
Yo_Robot | 23:2126e38bb48c | 400 | int ang = 0; |
Yo_Robot | 23:2126e38bb48c | 401 | float rpm; |
Yo_Robot | 23:2126e38bb48c | 402 | |
Yo_Robot | 25:1910a55ff0a3 | 403 | rpm = encoder.CalculateRPM( encoder.GetVelocityCap() , // ultima velocidad leida desde el encoder |
Yo_Robot | 25:1910a55ff0a3 | 404 | 360 // numero de revoluciones por vuelta del encoder |
Yo_Robot | 25:1910a55ff0a3 | 405 | ); |
Yo_Robot | 25:1910a55ff0a3 | 406 | |
Yo_Robot | 23:2126e38bb48c | 407 | |
Yo_Robot | 23:2126e38bb48c | 408 | if( pin_alm == 0 ) |
Yo_Robot | 23:2126e38bb48c | 409 | sprintf( output,"Ok" ); |
Yo_Robot | 23:2126e38bb48c | 410 | else |
Yo_Robot | 23:2126e38bb48c | 411 | sprintf( output,"%f", rpm ); |
Yo_Robot | 23:2126e38bb48c | 412 | |
Yo_Robot | 23:2126e38bb48c | 413 | |
Yo_Robot | 23:2126e38bb48c | 414 | } |
Yo_Robot | 23:2126e38bb48c | 415 | |
Yo_Robot | 21:353b0fe8fc54 | 416 | |
Yo_Robot | 22:d5431fff164b | 417 | |
Yo_Robot | 22:d5431fff164b | 418 | |
Yo_Robot | 22:d5431fff164b | 419 | |
Yo_Robot | 4:552beeda4722 | 420 | /* LEGACY FUNCTIONS |
Yo_Robot | 4:552beeda4722 | 421 | * |
Yo_Robot | 20:4b154134ab20 | 422 | * El siguiente codigo no es utilizado por el |
Yo_Robot | 20:4b154134ab20 | 423 | * programa. Sin embargo pueden servir como |
Yo_Robot | 20:4b154134ab20 | 424 | * futuras referencias. |
Yo_Robot | 4:552beeda4722 | 425 | */ |
Yo_Robot | 4:552beeda4722 | 426 | void setMR3( int newValue ) |
Yo_Robot | 4:552beeda4722 | 427 | { |
Yo_Robot | 4:552beeda4722 | 428 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 4:552beeda4722 | 429 | } |
Yo_Robot | 4:552beeda4722 | 430 | |
Yo_Robot | 4:552beeda4722 | 431 | |
Yo_Robot | 4:552beeda4722 | 432 | void setPrescaler( int newValue) |
Yo_Robot | 4:552beeda4722 | 433 | { |
Yo_Robot | 4:552beeda4722 | 434 | LPC_TIM2->PR = newValue; |
Yo_Robot | 4:552beeda4722 | 435 | } |
Yo_Robot | 20:4b154134ab20 | 436 | |
Yo_Robot | 20:4b154134ab20 | 437 | |
Yo_Robot | 20:4b154134ab20 | 438 | EthernetNetIf configurarEthernet() |
Yo_Robot | 20:4b154134ab20 | 439 | { |
Yo_Robot | 20:4b154134ab20 | 440 | //____________ *** ARCHIVO DE CONFIGURACION ***_______________________ // |
Yo_Robot | 20:4b154134ab20 | 441 | |
Yo_Robot | 20:4b154134ab20 | 442 | printf("\n *** CONFIGURACION ETHERNET DE MBED ***\n"); |
Yo_Robot | 20:4b154134ab20 | 443 | printf("Leyendo archivo de configuracion...\n\n"); |
Yo_Robot | 20:4b154134ab20 | 444 | |
Yo_Robot | 20:4b154134ab20 | 445 | FILE *fp = fopen("/local/config.txt", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 20:4b154134ab20 | 446 | |
Yo_Robot | 20:4b154134ab20 | 447 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 20:4b154134ab20 | 448 | { |
Yo_Robot | 20:4b154134ab20 | 449 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 20:4b154134ab20 | 450 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 451 | |
Yo_Robot | 20:4b154134ab20 | 452 | } |
Yo_Robot | 20:4b154134ab20 | 453 | else |
Yo_Robot | 20:4b154134ab20 | 454 | { |
Yo_Robot | 20:4b154134ab20 | 455 | // Cadenas de caracteres desde el Archivo config.txt |
Yo_Robot | 20:4b154134ab20 | 456 | char isDHCP [15]; //Modo Automatico o Manual |
Yo_Robot | 20:4b154134ab20 | 457 | char empty [2]; // Linea vacia |
Yo_Robot | 20:4b154134ab20 | 458 | char ip [40]; // Direccion IP |
Yo_Robot | 20:4b154134ab20 | 459 | char mask [40]; // Mascara de Subred |
Yo_Robot | 20:4b154134ab20 | 460 | char gate [40]; // Puerta de enlace |
Yo_Robot | 20:4b154134ab20 | 461 | char dns [40]; // Direccion DNS |
Yo_Robot | 20:4b154134ab20 | 462 | |
Yo_Robot | 20:4b154134ab20 | 463 | // Valores 'int' para agregar a la configuracion manual |
Yo_Robot | 20:4b154134ab20 | 464 | int DHCP; |
Yo_Robot | 20:4b154134ab20 | 465 | int n_ip[4]; |
Yo_Robot | 20:4b154134ab20 | 466 | int n_mask[4]; |
Yo_Robot | 20:4b154134ab20 | 467 | int n_gate[4]; |
Yo_Robot | 20:4b154134ab20 | 468 | int n_dns[4]; |
Yo_Robot | 20:4b154134ab20 | 469 | |
Yo_Robot | 20:4b154134ab20 | 470 | |
Yo_Robot | 20:4b154134ab20 | 471 | // Leer linea a linea el archivo |
Yo_Robot | 20:4b154134ab20 | 472 | fgets( isDHCP, 15, fp ); |
Yo_Robot | 20:4b154134ab20 | 473 | fgets( empty, 2, fp ); |
Yo_Robot | 20:4b154134ab20 | 474 | fgets( ip, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 475 | fgets( mask, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 476 | fgets( gate, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 477 | fgets( dns, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 478 | |
Yo_Robot | 20:4b154134ab20 | 479 | printf("Cerrando archivo...\n"); |
Yo_Robot | 20:4b154134ab20 | 480 | fclose(fp); |
Yo_Robot | 20:4b154134ab20 | 481 | |
Yo_Robot | 20:4b154134ab20 | 482 | // Extraer los valores numericos |
Yo_Robot | 20:4b154134ab20 | 483 | sscanf( isDHCP,"%*s %d",&DHCP ); |
Yo_Robot | 20:4b154134ab20 | 484 | |
Yo_Robot | 20:4b154134ab20 | 485 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0],&n_ip[1],&n_ip[2],&n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 486 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 487 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 488 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0],&n_dns[1],&n_dns[2],&n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 489 | |
Yo_Robot | 20:4b154134ab20 | 490 | |
Yo_Robot | 20:4b154134ab20 | 491 | if(DHCP) //En caso de modo DHCP |
Yo_Robot | 20:4b154134ab20 | 492 | { |
Yo_Robot | 20:4b154134ab20 | 493 | printf("\n Configurar red de manera automatica\n"); |
Yo_Robot | 20:4b154134ab20 | 494 | EthernetNetIf eth; //Crea la interfaz |
Yo_Robot | 20:4b154134ab20 | 495 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 496 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 497 | { |
Yo_Robot | 20:4b154134ab20 | 498 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 499 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 500 | } |
Yo_Robot | 20:4b154134ab20 | 501 | |
Yo_Robot | 20:4b154134ab20 | 502 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 503 | return eth; |
Yo_Robot | 20:4b154134ab20 | 504 | } |
Yo_Robot | 20:4b154134ab20 | 505 | else |
Yo_Robot | 20:4b154134ab20 | 506 | { |
Yo_Robot | 20:4b154134ab20 | 507 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0], &n_ip[1], &n_ip[2], &n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 508 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 509 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 510 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0], &n_dns[1], &n_dns[2], &n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 511 | printf(" %s %s %s %s %s\n " , isDHCP, ip, mask, gate, dns ); |
Yo_Robot | 20:4b154134ab20 | 512 | |
Yo_Robot | 20:4b154134ab20 | 513 | printf("\n Configurar red de manera manual\n"); |
Yo_Robot | 20:4b154134ab20 | 514 | EthernetNetIf eth( |
Yo_Robot | 20:4b154134ab20 | 515 | IpAddr( n_ip[0], n_ip[1], n_ip[2], n_ip[3]), //IP Address |
Yo_Robot | 20:4b154134ab20 | 516 | IpAddr( n_mask[0],n_mask[1],n_mask[2],n_mask[3]), //Network Mask |
Yo_Robot | 20:4b154134ab20 | 517 | IpAddr( n_gate[0],n_gate[1],n_gate[2],n_gate[3]), //Gateway |
Yo_Robot | 20:4b154134ab20 | 518 | IpAddr( n_dns[0], n_dns[1], n_dns[2], n_dns[3] ) //DNS |
Yo_Robot | 20:4b154134ab20 | 519 | ); |
Yo_Robot | 20:4b154134ab20 | 520 | |
Yo_Robot | 20:4b154134ab20 | 521 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 522 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 523 | { |
Yo_Robot | 20:4b154134ab20 | 524 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 525 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 526 | } |
Yo_Robot | 20:4b154134ab20 | 527 | |
Yo_Robot | 20:4b154134ab20 | 528 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 529 | return eth; |
Yo_Robot | 20:4b154134ab20 | 530 | } |
Yo_Robot | 20:4b154134ab20 | 531 | |
Yo_Robot | 20:4b154134ab20 | 532 | } |
Yo_Robot | 20:4b154134ab20 | 533 | |
Yo_Robot | 20:4b154134ab20 | 534 | EthernetNetIf eth; |
Yo_Robot | 20:4b154134ab20 | 535 | return eth; |
Yo_Robot | 20:4b154134ab20 | 536 | } |