Proyecto de Tesis en Mecatrónica. Universidad Técnica del Norte. Ernesto Palacios <mecatronica.mid@gmail.com>
Dependencies: EthernetNetIf HTTPServer QEI_hw RPCInterface mbed
main.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:
- 30:413d1a6648b5
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 | 20:4b154134ab20 | 1 | /*******************************************************************\ |
Yo_Robot | 20:4b154134ab20 | 2 | * * |
Yo_Robot | 20:4b154134ab20 | 3 | * DESLIZADOR LINEAL UTN * |
Yo_Robot | 20:4b154134ab20 | 4 | * * |
Yo_Robot | 20:4b154134ab20 | 5 | * Archivo: main.cpp * |
Yo_Robot | 20:4b154134ab20 | 6 | * Autor: Ernesto Palacios <meacatronica.mid@gmail.com> * |
Yo_Robot | 22:d5431fff164b | 7 | * Version: v2.2 * |
Yo_Robot | 20:4b154134ab20 | 8 | * Dependencias: setup.h, setup.cpp * |
Yo_Robot | 22:d5431fff164b | 9 | * Descripcion: Este es el archivo principal para el microcon- * |
Yo_Robot | 23:2126e38bb48c | 10 | * trolador mbed para la comunicacion y control * |
Yo_Robot | 20:4b154134ab20 | 11 | * del deslizador * |
Yo_Robot | 20:4b154134ab20 | 12 | * Fecha: Ibarra, 05 de junio de 2012 * |
Yo_Robot | 20:4b154134ab20 | 13 | * * |
Yo_Robot | 20:4b154134ab20 | 14 | *******************************************************************/ |
Yo_Robot | 3:8d5a9e3cd680 | 15 | |
Yo_Robot | 3:8d5a9e3cd680 | 16 | #include "mbed.h" |
Yo_Robot | 25:1910a55ff0a3 | 17 | #include "qeihw.h" |
Yo_Robot | 3:8d5a9e3cd680 | 18 | #include "setup.h" |
Yo_Robot | 8:958dfe5052b9 | 19 | #include "EthernetNetIf.h" |
Yo_Robot | 8:958dfe5052b9 | 20 | #include "HTTPServer.h" |
Yo_Robot | 8:958dfe5052b9 | 21 | #include "RPCFunction.h" |
Yo_Robot | 8:958dfe5052b9 | 22 | #include "RPCVariable.h" |
Yo_Robot | 20:4b154134ab20 | 23 | |
Yo_Robot | 23:2126e38bb48c | 24 | // Configuraci�n de Entradas / Salidas |
Yo_Robot | 9:6976ac1a430e | 25 | |
Yo_Robot | 25:1910a55ff0a3 | 26 | Serial pc( USBTX, USBRX ); //Comunicacion Serial directa al computador |
Yo_Robot | 25:1910a55ff0a3 | 27 | Serial RS_232(p13, p14); //Comunicacion Serial para MAX232 |
Yo_Robot | 25:1910a55ff0a3 | 28 | //I2C encoder(p28, p27, "i2c_mbed"); //Comunicacion I2C para los encoders |
Yo_Robot | 25:1910a55ff0a3 | 29 | QEIHW encoder( QEI_DIRINV_NONE, QEI_SIGNALMODE_QUAD, QEI_CAPMODE_2X, QEI_INVINX_NONE ); |
Yo_Robot | 23:2126e38bb48c | 30 | Timer crono; // Reloj Interno de mbed |
Yo_Robot | 18:cf1e07d82630 | 31 | |
Yo_Robot | 18:cf1e07d82630 | 32 | DigitalOut pin_son( p7 ); // SON |
Yo_Robot | 18:cf1e07d82630 | 33 | DigitalOut pin_dir( p26 ); // SIGN+ |
Yo_Robot | 18:cf1e07d82630 | 34 | InterruptIn pin_alm( p8 ); // ALM |
Yo_Robot | 20:4b154134ab20 | 35 | AnalogOut aout( p18 ); // VOUT |
Yo_Robot | 24:a1d16835201c | 36 | //PIN_06 pto_generator // PULS+ |
Yo_Robot | 9:6976ac1a430e | 37 | |
Yo_Robot | 16:d50665706f21 | 38 | DigitalIn isHTTP( p15 ); // Modo Ethernet/Serial |
Yo_Robot | 16:d50665706f21 | 39 | DigitalIn isFast( p16 ); // Serial Alta velocidad/Baja Velocidad |
Yo_Robot | 18:cf1e07d82630 | 40 | DigitalIn isPC ( p17 ); // Salida a compuador o MAX232 |
Yo_Robot | 18:cf1e07d82630 | 41 | |
Yo_Robot | 19:c26cf8a48986 | 42 | DigitalOut led_verde( p21 ); // Led verde del conector Ethernet |
Yo_Robot | 19:c26cf8a48986 | 43 | DigitalOut led_rojo( p22 ); // Led naranja del conector Ethernet |
Yo_Robot | 19:c26cf8a48986 | 44 | |
Yo_Robot | 20:4b154134ab20 | 45 | // Objetos Ethernet |
Yo_Robot | 18:cf1e07d82630 | 46 | |
Yo_Robot | 18:cf1e07d82630 | 47 | EthernetNetIf eth; |
Yo_Robot | 18:cf1e07d82630 | 48 | HTTPServer svr; |
Yo_Robot | 17:a4f380bc2516 | 49 | LocalFileSystem fs("local"); |
Yo_Robot | 5:c5aea1eb10bb | 50 | |
Yo_Robot | 8:958dfe5052b9 | 51 | |
Yo_Robot | 20:4b154134ab20 | 52 | // Anadir funciones al Protocolo RPC: |
Yo_Robot | 20:4b154134ab20 | 53 | // |
Yo_Robot | 20:4b154134ab20 | 54 | // estas son las funciones de acceso de la libreria HTTP_ASM sobre |
Yo_Robot | 20:4b154134ab20 | 55 | // el Protocolo RPC |
Yo_Robot | 12:c02b08dacc45 | 56 | RPCFunction SetFQ (&setPTO_eth, "PTO"); |
Yo_Robot | 12:c02b08dacc45 | 57 | RPCFunction SetAOUT(&setAout_eth, "AOUT"); |
Yo_Robot | 12:c02b08dacc45 | 58 | RPCFunction SetDIR (&setDir_eth, "DIR"); |
Yo_Robot | 12:c02b08dacc45 | 59 | RPCFunction SetSON (&setSON_eth, "SON"); |
Yo_Robot | 16:d50665706f21 | 60 | RPCFunction SetANG (&setANG_eth, "ANG"); |
Yo_Robot | 16:d50665706f21 | 61 | RPCFunction SetSpd (&setSPD_eth, "VAN"); |
Yo_Robot | 22:d5431fff164b | 62 | RPCFunction ReadEnc(&getENC_eth, "ENC"); |
Yo_Robot | 22:d5431fff164b | 63 | RPCFunction ClrEnc (&setENC_eth, "CLR"); |
Yo_Robot | 23:2126e38bb48c | 64 | RPCFunction ReadRPM(&getRPM_eth, "RPM"); |
Yo_Robot | 8:958dfe5052b9 | 65 | |
Yo_Robot | 3:8d5a9e3cd680 | 66 | |
Yo_Robot | 3:8d5a9e3cd680 | 67 | int main() { |
Yo_Robot | 20:4b154134ab20 | 68 | //*************** Configura Red Ethernet **************************************************// |
Yo_Robot | 20:4b154134ab20 | 69 | if( isHTTP ) |
Yo_Robot | 13:649543aa8b1d | 70 | { |
Yo_Robot | 20:4b154134ab20 | 71 | |
Yo_Robot | 22:d5431fff164b | 72 | // Anade clases base al protocolo RPC |
Yo_Robot | 13:649543aa8b1d | 73 | Base::add_rpc_class<AnalogIn>(); |
Yo_Robot | 13:649543aa8b1d | 74 | Base::add_rpc_class<AnalogOut>(); |
Yo_Robot | 13:649543aa8b1d | 75 | Base::add_rpc_class<DigitalIn>(); |
Yo_Robot | 13:649543aa8b1d | 76 | Base::add_rpc_class<DigitalOut>(); |
Yo_Robot | 13:649543aa8b1d | 77 | Base::add_rpc_class<DigitalInOut>(); |
Yo_Robot | 13:649543aa8b1d | 78 | Base::add_rpc_class<PwmOut>(); |
Yo_Robot | 13:649543aa8b1d | 79 | Base::add_rpc_class<Timer>(); |
Yo_Robot | 13:649543aa8b1d | 80 | Base::add_rpc_class<BusOut>(); |
Yo_Robot | 13:649543aa8b1d | 81 | Base::add_rpc_class<BusIn>(); |
Yo_Robot | 13:649543aa8b1d | 82 | Base::add_rpc_class<BusInOut>(); |
Yo_Robot | 13:649543aa8b1d | 83 | Base::add_rpc_class<Serial>(); |
Yo_Robot | 17:a4f380bc2516 | 84 | |
Yo_Robot | 24:a1d16835201c | 85 | led_verde = 0; //Esperando configuracion |
Yo_Robot | 24:a1d16835201c | 86 | led_rojo = 1; |
Yo_Robot | 24:a1d16835201c | 87 | |
Yo_Robot | 22:d5431fff164b | 88 | // Configura conexion Ethernet |
Yo_Robot | 19:c26cf8a48986 | 89 | printf("Configurando...\n"); |
Yo_Robot | 18:cf1e07d82630 | 90 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 18:cf1e07d82630 | 91 | if(ethErr) |
Yo_Robot | 13:649543aa8b1d | 92 | { |
Yo_Robot | 21:353b0fe8fc54 | 93 | printf("Error %d en la configuracion.\n", ethErr); |
Yo_Robot | 24:a1d16835201c | 94 | led_rojo = 0; |
Yo_Robot | 18:cf1e07d82630 | 95 | return -1; |
Yo_Robot | 18:cf1e07d82630 | 96 | } |
Yo_Robot | 24:a1d16835201c | 97 | printf("Configuracion correcta!\n"); |
Yo_Robot | 24:a1d16835201c | 98 | |
Yo_Robot | 24:a1d16835201c | 99 | led_verde = 1; //Configuracion correcta led verde encendido |
Yo_Robot | 24:a1d16835201c | 100 | led_rojo = 0; |
Yo_Robot | 24:a1d16835201c | 101 | |
Yo_Robot | 18:cf1e07d82630 | 102 | FSHandler::mount("/local", "/files"); //Monta /local en /files que es el directorio web |
Yo_Robot | 18:cf1e07d82630 | 103 | FSHandler::mount("/local", "/"); //Mount /local en / que es el directorio raiz |
Yo_Robot | 13:649543aa8b1d | 104 | |
Yo_Robot | 18:cf1e07d82630 | 105 | //Handles para la comunicacion |
Yo_Robot | 13:649543aa8b1d | 106 | svr.addHandler<SimpleHandler>("/hello"); |
Yo_Robot | 13:649543aa8b1d | 107 | svr.addHandler<RPCHandler>("/rpc"); |
Yo_Robot | 13:649543aa8b1d | 108 | svr.addHandler<FSHandler>("/files"); |
Yo_Robot | 18:cf1e07d82630 | 109 | svr.addHandler<FSHandler>("/"); |
Yo_Robot | 18:cf1e07d82630 | 110 | |
Yo_Robot | 18:cf1e07d82630 | 111 | svr.bind(80); // Puerto de comunicacion. |
Yo_Robot | 18:cf1e07d82630 | 112 | |
Yo_Robot | 18:cf1e07d82630 | 113 | printf("Listo para comunicacion...\n"); |
Yo_Robot | 13:649543aa8b1d | 114 | |
Yo_Robot | 18:cf1e07d82630 | 115 | // Para probar la correcta configuracion acceder a mbed.htm |
Yo_Robot | 18:cf1e07d82630 | 116 | // http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
Yo_Robot | 18:cf1e07d82630 | 117 | |
Yo_Robot | 13:649543aa8b1d | 118 | } |
Yo_Robot | 20:4b154134ab20 | 119 | |
Yo_Robot | 20:4b154134ab20 | 120 | |
Yo_Robot | 20:4b154134ab20 | 121 | //*************** Configura Comunicacion Serial **************************************************// |
Yo_Robot | 15:a1ffa32ce9d1 | 122 | else // Caso Contrario Anadir modo Serial |
Yo_Robot | 8:958dfe5052b9 | 123 | { |
Yo_Robot | 22:d5431fff164b | 124 | int fast_baud = getBaud(); |
Yo_Robot | 20:4b154134ab20 | 125 | if( isPC ) // Utiliza el Puerto Virtual Incluido. |
Yo_Robot | 19:c26cf8a48986 | 126 | { |
Yo_Robot | 19:c26cf8a48986 | 127 | pc.attach( &ISR_Serial ); |
Yo_Robot | 20:4b154134ab20 | 128 | if( isFast ) //Configurar Serial a alta velocidad |
Yo_Robot | 21:353b0fe8fc54 | 129 | pc.baud( fast_baud ); |
Yo_Robot | 19:c26cf8a48986 | 130 | } |
Yo_Robot | 20:4b154134ab20 | 131 | else // Utiiliza el puerto serial hacia MAX232 |
Yo_Robot | 19:c26cf8a48986 | 132 | { |
Yo_Robot | 19:c26cf8a48986 | 133 | RS_232.attach( &ISR_Serial ); |
Yo_Robot | 20:4b154134ab20 | 134 | if( isFast ) //Configurar Serial a alta velocidad |
Yo_Robot | 21:353b0fe8fc54 | 135 | RS_232.baud( fast_baud ); |
Yo_Robot | 19:c26cf8a48986 | 136 | } |
Yo_Robot | 8:958dfe5052b9 | 137 | } |
Yo_Robot | 8:958dfe5052b9 | 138 | |
Yo_Robot | 20:4b154134ab20 | 139 | //*******************************************************************************************************// |
Yo_Robot | 20:4b154134ab20 | 140 | |
Yo_Robot | 20:4b154134ab20 | 141 | |
Yo_Robot | 24:a1d16835201c | 142 | //________ Ajustes iniciales del Driver _________// |
Yo_Robot | 24:a1d16835201c | 143 | |
Yo_Robot | 20:4b154134ab20 | 144 | setTimer2(); // Configurar PTO |
Yo_Robot | 20:4b154134ab20 | 145 | pin_son = 0; // Servo Apagado por defecto |
Yo_Robot | 13:649543aa8b1d | 146 | aout = 0.5; // Voltaje de salida 0V por defecto( +-10V! ) |
Yo_Robot | 8:958dfe5052b9 | 147 | |
Yo_Robot | 21:353b0fe8fc54 | 148 | |
Yo_Robot | 21:353b0fe8fc54 | 149 | pin_alm.fall( &ISR_Alarm ); // Entrada de la alarma driver |
Yo_Robot | 14:039d070732d5 | 150 | |
Yo_Robot | 13:649543aa8b1d | 151 | while(1) |
Yo_Robot | 13:649543aa8b1d | 152 | { |
Yo_Robot | 13:649543aa8b1d | 153 | if( isHTTP ) |
Yo_Robot | 24:a1d16835201c | 154 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 155 | Net::poll(); // Revisa la red solo si la coneccion es HTTP |
Yo_Robot | 24:a1d16835201c | 156 | led_verde = led_verde ^ 1; // Toggle Led Verde... esperando transmicion |
Yo_Robot | 24:a1d16835201c | 157 | } |
Yo_Robot | 13:649543aa8b1d | 158 | } |
Yo_Robot | 8:958dfe5052b9 | 159 | } |
Yo_Robot | 8:958dfe5052b9 | 160 |