Proyecto de Tesis en Mecatrónica. Universidad Técnica del Norte. Ernesto Palacios <mecatronica.mid@gmail.com>

Dependencies:   EthernetNetIf HTTPServer QEI_hw RPCInterface mbed

Committer:
Yo_Robot
Date:
Fri Jul 06 17:34:16 2012 +0000
Revision:
23:2126e38bb48c
Parent:
22:d5431fff164b
Child:
24:a1d16835201c
Version 2.0 Encoder listo -  faltan alarmas externas de proximidad

Who changed what in which revision?

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