Proyecto de Tesis en Mecatrónica. Universidad Técnica del Norte. Ernesto Palacios <mecatronica.mid@gmail.com>
Dependencies: EthernetNetIf HTTPServer QEI_hw RPCInterface mbed
Revision 22:d5431fff164b, committed 2012-07-02
- Comitter:
- Yo_Robot
- Date:
- Mon Jul 02 04:35:28 2012 +0000
- Parent:
- 21:353b0fe8fc54
- Child:
- 23:2126e38bb48c
- Commit message:
- v1.9.9 Encoder escrito en cdigo, falta probar en la proto
Changed in this revision
--- a/export.lib Thu Jun 21 03:34:46 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/projects/libraries/svn/mbed/export/trunk@43 \ No newline at end of file
--- a/main.cpp Thu Jun 21 03:34:46 2012 +0000 +++ b/main.cpp Mon Jul 02 04:35:28 2012 +0000 @@ -4,9 +4,9 @@ * * * Archivo: main.cpp * * Autor: Ernesto Palacios <meacatronica.mid@gmail.com> * - * Version: v2.1 * + * Version: v2.2 * * Dependencias: setup.h, setup.cpp * - * Descripci�n: Este es el archivo principal para el microcon- * + * Descripcion: Este es el archivo principal para el microcon- * * trolador mbed para la comunicaci�n y control * * del deslizador * * Fecha: Ibarra, 05 de junio de 2012 * @@ -24,7 +24,7 @@ Serial pc( USBTX, USBRX ); //Comunicacion Serial directa al computador Serial RS_232(p9, p10); //Comunicacion Serial para MAX232 -I2C encoder(p28, p27); //Comunicacion I2C para los encoders +I2C encoder(p28, p27, "i2c_mbed"); //Comunicacion I2C para los encoders DigitalOut pin_son( p7 ); // SON DigitalOut pin_dir( p26 ); // SIGN+ @@ -55,16 +55,16 @@ RPCFunction SetSON (&setSON_eth, "SON"); RPCFunction SetANG (&setANG_eth, "ANG"); RPCFunction SetSpd (&setSPD_eth, "VAN"); -RPCFunction ReadSpd(&getENC_eth, "VLC"); +RPCFunction ReadEnc(&getENC_eth, "ENC"); +RPCFunction ClrEnc (&setENC_eth, "CLR"); int main() { - //*************** Configura Red Ethernet **************************************************// if( isHTTP ) { - // A�ade clases base al protocolo RPC + // Anade clases base al protocolo RPC Base::add_rpc_class<AnalogIn>(); Base::add_rpc_class<AnalogOut>(); Base::add_rpc_class<DigitalIn>(); @@ -77,7 +77,7 @@ Base::add_rpc_class<BusInOut>(); Base::add_rpc_class<Serial>(); - // Configura conexi�n Ethernet + // Configura conexion Ethernet printf("Configurando...\n"); EthernetErr ethErr = eth.setup(); if(ethErr) @@ -107,10 +107,9 @@ //*************** Configura Comunicacion Serial **************************************************// - - int fast_baud = getBaud(); else // Caso Contrario Anadir modo Serial { + int fast_baud = getBaud(); if( isPC ) // Utiliza el Puerto Virtual Incluido. { pc.attach( &ISR_Serial );
--- a/setup.cpp Thu Jun 21 03:34:46 2012 +0000 +++ b/setup.cpp Mon Jul 02 04:35:28 2012 +0000 @@ -17,16 +17,42 @@ // Salida Serial de mbed extern Serial pc; extern Serial RS_232; +extern I2C encoder; extern DigitalIn isPC; extern DigitalOut pin_son; // SON extern DigitalOut pin_dir; // SIGN+ extern InterruptIn pin_alm; // ALM extern AnalogOut aout; // +-10V +extern DigitalOut led_verde; // Led verde del conector Ethernet +extern DigitalOut led_rojo; // Led naranja del conector Ethernet int fq_posicion = 10000; // Variable global donde se almacenara // la velocidad de posicionamiento en Hz +const int addres = 0x36; // Direccion del enconder en modo esclavo + +int read_encoder() +{ + int count; + + char buffer[4]; + encoder.read( addres, buffer, 1 ); // 8bit por ahora + count = (int) buffer[0]; + + return count; +} + +void clear_encoder() +{ + + char buffer[2]; + buffer[0] = 0x01; //Comando para borrar el contador + buffer[1] = 0x02; // Comando para cambiar estado de led + + encoder.write( addres, buffer, 1 ); +} + void setTimer2() { // Encender Timer2 (PCONP[22]) @@ -96,9 +122,11 @@ else if( command == 'S') pin_son = value; + //Setear la velocidad de Posicionamiento else if( command == 'V') fq_posicion = value; - + + // Generar un numero definido de pulsos a la velocidad de posicionamiento else if( command == 'G' ) { int pulsos = value; //Numero de pulsos a generar @@ -110,6 +138,19 @@ wait( t_alto ); //Espera hasta llegar a la posicion stopTimer2(); //Posicion alcanzada ALTO. } + + //Leer encoder + else if( command == 'E' ) + { + if( isPC ) + pc.printf( "%d",read_encoder() ); + else + RS_232.printf( "%d",read_encoder() ); + } + + //Limpiar contador encoder + else if( command == 'Z' ) + clear_encoder(); } @@ -128,6 +169,103 @@ } } + + +void ISR_Alarm() +{ + pin_son = 0 ; + stopTimer2(); + aout = 0.5 ; + + if(isPC) + pc.printf( "AL" ); //ALARMA! solo es AL para que + //sea conciso con el modo ETH y funcione + //bien en LabVIEW. + else + RS_232.printf( "AL" ); +} + +int getMRvalue( int fout ) +{ + int toRegister; + + toRegister = (24000000 /(fout*2.0) ) -1; + return toRegister; +} + + +void setMR2( int newValue ) +{ + LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas + LPC_TIM2->MR3 = newValue; // Para testear el programa. +} + + + +void startTimer2() +{ + // Arrancer el Timer 2 + LPC_TIM2->TCR = 1; +} + +void stopTimer2() +{ + // Detener el Timer 2 + LPC_TIM2->TCR = 0x2; +} + +int getBaud() +{ + int baudios = 115200; //Valor por defecto + + FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp + + if(!fp) // En caso de no encontrarse el archivo + { + printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); + printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n"); + printf("que contenga las lineas:\n\n"); + + printf(" 1\n"); + printf(" 2\n"); + printf(" 3\n"); + printf(" 4\n"); + printf(" baudios: 115200\n"); + + printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n"); + printf("luego reinicie el microcontrolador\n"); + exit(1); + + } + else + { + // Cadenas de caracteres desde el Archivo config.mbd + char notstr [04]; // linea vacia + char baud [40]; // valor en baudios + + // Leer linea a linea el archivo + // cuatro primeras lineas no sirven + fgets( notstr, 4, fp ); + fgets( notstr, 4, fp ); + fgets( notstr, 4, fp ); + fgets( notstr, 4, fp ); + + fgets( baud, 40, fp ); + + printf("Cerrando archivo...\n"); + fclose(fp); + + + // Extraer los valores numericos + sscanf( baud,"%*s %d",&baudios ); + + + } + + return baudios; +} + +// **** Funciones Liberia Ethernet ***** // void setPTO_eth( char * input, char * output ) { int freq = atoi( input ); @@ -181,63 +319,6 @@ } - -void getENC_eth( char * input, char * output ) -{ - //Leer el Encoder ******* F A L T A - if( pin_alm == 0 ) - sprintf( output,"Ok" ); - else - sprintf( output,"AL" ); -} - - -void ISR_Alarm() -{ - pin_son = 0 ; - stopTimer2(); - aout = 0.5 ; - - if(isPC) - pc.printf( "AL" ); //ALARMA! solo es AL para que - //sea conciso con el modo ETH y funcione - //bien en LabVIEW. - else - RS_232.printf( "AL" ); -} - -int getMRvalue( int fout ) -{ - int toRegister; - - toRegister = (24000000 /(fout*2.0) ) -1; - return toRegister; -} - - -void setMR2( int newValue ) -{ - LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas - LPC_TIM2->MR3 = newValue; // Para testear el programa. -} - - - -void startTimer2() -{ - // Arrancer el Timer 2 - LPC_TIM2->TCR = 1; -} - -void stopTimer2() -{ - // Detener el Timer 2 - LPC_TIM2->TCR = 0x2; -} - - -// **** Funciones Liberia Ethernet ***** // - void setAout_eth( char * input, char * output ) { int vout = atoi( input ); @@ -278,58 +359,31 @@ } -int getBaud(); +void getENC_eth( char * input, char * output ) { - int baudios = 115200; //Valor por defecto - - FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp - - if(!fp) // En caso de no encontrarse el archivo - { - printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); - printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n"); - printf("que contenga las líneas:\n\n"); + if( pin_alm == 0 ) + sprintf( output,"%d", read_encoder() ); + else + sprintf( output,"AL" ); + +} - printf(" 1\n"); - printf(" 2\n"); - printf(" 3\n"); - printf(" 4\n"); - printf(" baudios: 115200\n"); - printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n"); - printf("luego reinicie el microcontrolador\n"); - exit(1); - - } - else - { - // Cadenas de caracteres desde el Archivo config.mbd - char notstr [04]; // linea vacia - char baud [40]; // valor en baudios - - // Leer linea a linea el archivo - // cuatro primeras lineas no sirven - fgets( notstr, 4, fp ); - fgets( notstr, 4, fp ); - fgets( notstr, 4, fp ); - fgets( notstr, 4, fp ); - - fgets( baud, 40, fp ); - - printf("Cerrando archivo...\n"); - fclose(fp); - - - // Extraer los valores numericos - sscanf( baud,"%*s %d",&baudios ); - - - } - - return baudios; +void setENC_eth( char * input, char * output ) +{ + clear_encoder(); + + if( pin_alm == 0 ) + sprintf( output,"Ok" ); + else + sprintf( output,"AL" ); + } + + + /* LEGACY FUNCTIONS * * El siguiente codigo no es utilizado por el
--- a/setup.h Thu Jun 21 03:34:46 2012 +0000 +++ b/setup.h Mon Jul 02 04:35:28 2012 +0000 @@ -42,12 +42,6 @@ void setPTO( int freq ); -/** @brief: Esta es la rutina que cambia la frecuencia - * de salida del Micro en comunicacion Ethernet - */ -void setPTO_eth ( char * input, char * output ); - - /** @brief: Esta es la rutina que maneja la interrupcion * de alarma */ @@ -86,6 +80,15 @@ void stopTimer2(); +//________________ FUNCIONES ETHERNET ___________________// + + +/** @brief: Esta es la rutina que cambia la frecuencia + * de salida del Micro en comunicacion Ethernet + */ +void setPTO_eth ( char * input, char * output ); + + /** @brief: Esta Funcion cambia el voltaje de salida el * valor ingresado debe ser en porcentaje. * Desde -100% hasta +100% dependiendo de la velocidad @@ -99,24 +102,43 @@ void setDir_eth ( char * input, char * output ); +/** @brief: Encender el Servo (bloquearlo) + */ +void setSON_eth ( char * input, char * output ); + + +/** @brief: Cambiar el angulo actual del eje del motor + */ +void setANG_eth( char * input, char * output ); + + +/** @brief: Cambiar la velocidad de giro al cambiar el angulo del motor + */ +void setSPD_eth( char * input, char * output ); + + +/** @brief: Leer desde el encoder + */ +void getENC_eth( char * input, char * output ); + + +/** @brief: Leer desde el encoder + */ +void setENC_eth( char * input, char * output ); + + + +//________________ FUNCIONES ENCODER __________________// + /** @brief: Cambiar Direccion @ PTO */ -void setSON_eth ( char * input, char * output ); +int read_encoder(); /** @brief: Cambiar Direccion @ PTO */ -void setANG_eth( char * input, char * output ); - +void clear_encoder(); -/** @brief: Cambiar Direccion @ PTO - */ -void setSPD_eth( char * input, char * output ); - - -/** @brief: Cambiar Direccion @ PTO - */ -void getENC_eth( char * input, char * output ); // Legacy