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@36:a99dcd4d8f65, 2014-07-07 (annotated)
- Committer:
- Yo_Robot
- Date:
- Mon Jul 07 15:53:02 2014 +0000
- Revision:
- 36:a99dcd4d8f65
- Parent:
- 34:bdf918bc9b59
Intento de evitar que la funcion -P no bloquee comandos, no se logro, control dinamico de posici?n va para LabVIEW
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 QEIHW encoder; |
Yo_Robot | 36:a99dcd4d8f65 | 21 | extern Timeout temporizador; // Cronometro interno del microcontrolador |
Yo_Robot | 24:a1d16835201c | 22 | extern DigitalIn isPC; // Bit de configuracion serial en la placa |
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 | 24:a1d16835201c | 28 | extern DigitalOut led_rojo; // Led naranja del conector Ethernet |
Yo_Robot | 30:413d1a6648b5 | 29 | extern InterruptIn limite_1; |
Yo_Robot | 30:413d1a6648b5 | 30 | extern InterruptIn limite_2; |
Yo_Robot | 30:413d1a6648b5 | 31 | extern InterruptIn limite_3; |
Yo_Robot | 30:413d1a6648b5 | 32 | extern InterruptIn limite_4; |
Yo_Robot | 20:4b154134ab20 | 33 | |
Yo_Robot | 36:a99dcd4d8f65 | 34 | |
Yo_Robot | 36:a99dcd4d8f65 | 35 | |
Yo_Robot | 36:a99dcd4d8f65 | 36 | |
Yo_Robot | 15:a1ffa32ce9d1 | 37 | int fq_posicion = 10000; // Variable global donde se almacenara |
Yo_Robot | 15:a1ffa32ce9d1 | 38 | // la velocidad de posicionamiento en Hz |
Yo_Robot | 24:a1d16835201c | 39 | float t_alto; // para el posicionamiento del motor |
Yo_Robot | 23:2126e38bb48c | 40 | |
Yo_Robot | 26:dad0b2031173 | 41 | int fq_actual = 0; // Ultimo valor seteado para el tren de pulsos |
Yo_Robot | 22:d5431fff164b | 42 | |
Yo_Robot | 32:4483d6c225e5 | 43 | int bandera_inicio = 0; |
Yo_Robot | 33:e6ff02c3e0f5 | 44 | |
Yo_Robot | 36:a99dcd4d8f65 | 45 | |
Yo_Robot | 36:a99dcd4d8f65 | 46 | |
Yo_Robot | 36:a99dcd4d8f65 | 47 | |
Yo_Robot | 36:a99dcd4d8f65 | 48 | void fin_posicion() |
Yo_Robot | 36:a99dcd4d8f65 | 49 | { |
Yo_Robot | 36:a99dcd4d8f65 | 50 | stopTimer2(); |
Yo_Robot | 36:a99dcd4d8f65 | 51 | |
Yo_Robot | 36:a99dcd4d8f65 | 52 | // Envia un OK de comando recibido |
Yo_Robot | 36:a99dcd4d8f65 | 53 | if( isPC ) |
Yo_Robot | 36:a99dcd4d8f65 | 54 | pc.printf("OK\r\n"); |
Yo_Robot | 36:a99dcd4d8f65 | 55 | else |
Yo_Robot | 36:a99dcd4d8f65 | 56 | RS_232.printf("OK\r\n"); |
Yo_Robot | 36:a99dcd4d8f65 | 57 | } |
Yo_Robot | 36:a99dcd4d8f65 | 58 | |
Yo_Robot | 29:52932326c45a | 59 | int velocidad_rpm() |
Yo_Robot | 22:d5431fff164b | 60 | { |
Yo_Robot | 33:e6ff02c3e0f5 | 61 | return encoder.GetVelocityCap(); |
Yo_Robot | 22:d5431fff164b | 62 | } |
Yo_Robot | 22:d5431fff164b | 63 | |
Yo_Robot | 22:d5431fff164b | 64 | void clear_encoder() |
Yo_Robot | 22:d5431fff164b | 65 | { |
Yo_Robot | 25:1910a55ff0a3 | 66 | encoder.Reset( QEI_RESET_POS ); // reset position |
Yo_Robot | 22:d5431fff164b | 67 | } |
Yo_Robot | 22:d5431fff164b | 68 | |
Yo_Robot | 4:552beeda4722 | 69 | void setTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 70 | { |
Yo_Robot | 3:8d5a9e3cd680 | 71 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 3:8d5a9e3cd680 | 72 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 3:8d5a9e3cd680 | 73 | |
Yo_Robot | 3:8d5a9e3cd680 | 74 | // Resetear y parar el Timer |
Yo_Robot | 3:8d5a9e3cd680 | 75 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 3:8d5a9e3cd680 | 76 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 3:8d5a9e3cd680 | 77 | |
Yo_Robot | 4:552beeda4722 | 78 | // Establecer el Preescaler en cero |
Yo_Robot | 4:552beeda4722 | 79 | // SIn Preesclaer |
Yo_Robot | 4:552beeda4722 | 80 | LPC_TIM2->PR = 0; |
Yo_Robot | 3:8d5a9e3cd680 | 81 | |
Yo_Robot | 4:552beeda4722 | 82 | // Calcular el periodo Inicial |
Yo_Robot | 4:552beeda4722 | 83 | uint32_t periodo = ( SystemCoreClock / 400 ); |
Yo_Robot | 3:8d5a9e3cd680 | 84 | |
Yo_Robot | 3:8d5a9e3cd680 | 85 | // Establecer los Registros de Coincidencia |
Yo_Robot | 3:8d5a9e3cd680 | 86 | // ( Match Register ) |
Yo_Robot | 3:8d5a9e3cd680 | 87 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 4:552beeda4722 | 88 | LPC_TIM2->MR3 = periodo; // Legacy, salidas identicas |
Yo_Robot | 3:8d5a9e3cd680 | 89 | |
Yo_Robot | 4:552beeda4722 | 90 | LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 |
Yo_Robot | 3:8d5a9e3cd680 | 91 | |
Yo_Robot | 3:8d5a9e3cd680 | 92 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 3:8d5a9e3cd680 | 93 | // y MAT2.3 |
Yo_Robot | 3:8d5a9e3cd680 | 94 | |
Yo_Robot | 3:8d5a9e3cd680 | 95 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 8:958dfe5052b9 | 96 | // y MAT2.3 como salidas |
Yo_Robot | 8:958dfe5052b9 | 97 | |
Yo_Robot | 6:b4dae934e1ea | 98 | } |
Yo_Robot | 6:b4dae934e1ea | 99 | |
Yo_Robot | 12:c02b08dacc45 | 100 | void ISR_Serial() |
Yo_Robot | 12:c02b08dacc45 | 101 | { |
Yo_Robot | 12:c02b08dacc45 | 102 | int value; // Nuevo Valor |
Yo_Robot | 12:c02b08dacc45 | 103 | char command; // Comando al que aplicar el nuevo valor |
Yo_Robot | 23:2126e38bb48c | 104 | |
Yo_Robot | 12:c02b08dacc45 | 105 | |
Yo_Robot | 19:c26cf8a48986 | 106 | if( isPC ) |
Yo_Robot | 23:2126e38bb48c | 107 | pc.scanf( "%d-%c", &value, &command ); |
Yo_Robot | 19:c26cf8a48986 | 108 | else |
Yo_Robot | 23:2126e38bb48c | 109 | RS_232.scanf( "%d-%c", &value, &command ); |
Yo_Robot | 23:2126e38bb48c | 110 | |
Yo_Robot | 23:2126e38bb48c | 111 | switch( command ) |
Yo_Robot | 23:2126e38bb48c | 112 | { |
Yo_Robot | 19:c26cf8a48986 | 113 | |
Yo_Robot | 29:52932326c45a | 114 | case 'E': // Leer el contador del encoder. |
Yo_Robot | 24:a1d16835201c | 115 | { |
Yo_Robot | 23:2126e38bb48c | 116 | |
Yo_Robot | 27:b8254b76ec57 | 117 | //Leer la posición del encoder |
Yo_Robot | 23:2126e38bb48c | 118 | |
Yo_Robot | 26:dad0b2031173 | 119 | if( isPC ) |
Yo_Robot | 29:52932326c45a | 120 | pc.printf("%d\n\r",encoder.GetPosition()); |
Yo_Robot | 26:dad0b2031173 | 121 | else |
Yo_Robot | 29:52932326c45a | 122 | RS_232.printf("%d\n\r",encoder.GetPosition()); |
Yo_Robot | 23:2126e38bb48c | 123 | break; |
Yo_Robot | 24:a1d16835201c | 124 | } |
Yo_Robot | 27:b8254b76ec57 | 125 | |
Yo_Robot | 23:2126e38bb48c | 126 | case 'H': // Establecer nueva frecuencia |
Yo_Robot | 24:a1d16835201c | 127 | { |
Yo_Robot | 23:2126e38bb48c | 128 | setPTO( value ); |
Yo_Robot | 26:dad0b2031173 | 129 | fq_actual = value; |
Yo_Robot | 32:4483d6c225e5 | 130 | bandera_inicio = 0; //En caso que haya estado yendo a HOME, cancelar |
Yo_Robot | 26:dad0b2031173 | 131 | |
Yo_Robot | 26:dad0b2031173 | 132 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 133 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 134 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 135 | else |
Yo_Robot | 26:dad0b2031173 | 136 | RS_232.printf("OK\r\n"); |
Yo_Robot | 23:2126e38bb48c | 137 | break; |
Yo_Robot | 24:a1d16835201c | 138 | } |
Yo_Robot | 27:b8254b76ec57 | 139 | |
Yo_Robot | 23:2126e38bb48c | 140 | case 'K': |
Yo_Robot | 24:a1d16835201c | 141 | { |
Yo_Robot | 23:2126e38bb48c | 142 | setPTO( value * 1000 ); |
Yo_Robot | 26:dad0b2031173 | 143 | fq_actual = value; |
Yo_Robot | 32:4483d6c225e5 | 144 | bandera_inicio = 0; //En caso que haya estado yendo a HOME, cancelar |
Yo_Robot | 32:4483d6c225e5 | 145 | |
Yo_Robot | 26:dad0b2031173 | 146 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 147 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 148 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 149 | else |
Yo_Robot | 26:dad0b2031173 | 150 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 151 | |
Yo_Robot | 23:2126e38bb48c | 152 | break; |
Yo_Robot | 24:a1d16835201c | 153 | } |
Yo_Robot | 23:2126e38bb48c | 154 | case 'A': // Cambiar voltaje de salida |
Yo_Robot | 24:a1d16835201c | 155 | { |
Yo_Robot | 23:2126e38bb48c | 156 | aout = (float)( value + 10000.0 ) / 20000.0; |
Yo_Robot | 26:dad0b2031173 | 157 | |
Yo_Robot | 26:dad0b2031173 | 158 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 159 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 160 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 161 | else |
Yo_Robot | 26:dad0b2031173 | 162 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 163 | |
Yo_Robot | 23:2126e38bb48c | 164 | break; |
Yo_Robot | 24:a1d16835201c | 165 | } |
Yo_Robot | 23:2126e38bb48c | 166 | case 'D': // Cambiar la direccion |
Yo_Robot | 24:a1d16835201c | 167 | { |
Yo_Robot | 26:dad0b2031173 | 168 | |
Yo_Robot | 23:2126e38bb48c | 169 | stopTimer2(); |
Yo_Robot | 23:2126e38bb48c | 170 | pin_dir = value; |
Yo_Robot | 23:2126e38bb48c | 171 | wait_us( 2 ); |
Yo_Robot | 32:4483d6c225e5 | 172 | bandera_inicio = 0; //En caso que haya estado yendo a HOME, cancelar |
Yo_Robot | 26:dad0b2031173 | 173 | |
Yo_Robot | 26:dad0b2031173 | 174 | if ( fq_actual != 0 ) |
Yo_Robot | 26:dad0b2031173 | 175 | { |
Yo_Robot | 26:dad0b2031173 | 176 | startTimer2(); |
Yo_Robot | 26:dad0b2031173 | 177 | } |
Yo_Robot | 26:dad0b2031173 | 178 | |
Yo_Robot | 26:dad0b2031173 | 179 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 180 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 181 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 182 | else |
Yo_Robot | 26:dad0b2031173 | 183 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 184 | |
Yo_Robot | 23:2126e38bb48c | 185 | break; |
Yo_Robot | 24:a1d16835201c | 186 | } |
Yo_Robot | 23:2126e38bb48c | 187 | case 'V': //Setear la velocidad de Posicionamiento |
Yo_Robot | 24:a1d16835201c | 188 | { |
Yo_Robot | 23:2126e38bb48c | 189 | fq_posicion = value; |
Yo_Robot | 26:dad0b2031173 | 190 | |
Yo_Robot | 26:dad0b2031173 | 191 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 192 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 193 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 194 | else |
Yo_Robot | 26:dad0b2031173 | 195 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 196 | |
Yo_Robot | 23:2126e38bb48c | 197 | break; |
Yo_Robot | 24:a1d16835201c | 198 | } |
Yo_Robot | 27:b8254b76ec57 | 199 | |
Yo_Robot | 27:b8254b76ec57 | 200 | // Generar un numero definido de pulsos a la velocidad de posicionamiento |
Yo_Robot | 29:52932326c45a | 201 | case 'P': |
Yo_Robot | 24:a1d16835201c | 202 | { |
Yo_Robot | 28:b7ded82ee7da | 203 | float pulsos = value; //Numero de pulsos a generar |
Yo_Robot | 28:b7ded82ee7da | 204 | t_alto = (float)(pulsos / fq_posicion); //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 27:b8254b76ec57 | 205 | |
Yo_Robot | 32:4483d6c225e5 | 206 | bandera_inicio = 0; //En caso que haya estado yendo a HOME, cancelar |
Yo_Robot | 32:4483d6c225e5 | 207 | |
Yo_Robot | 27:b8254b76ec57 | 208 | //DEBUG |
Yo_Robot | 32:4483d6c225e5 | 209 | //pc.printf("Tiempo en timer en seg = %f", t_alto); |
Yo_Robot | 27:b8254b76ec57 | 210 | |
Yo_Robot | 23:2126e38bb48c | 211 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 23:2126e38bb48c | 212 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 23:2126e38bb48c | 213 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 36:a99dcd4d8f65 | 214 | temporizador.attach( &fin_posicion, t_alto); |
Yo_Robot | 26:dad0b2031173 | 215 | |
Yo_Robot | 36:a99dcd4d8f65 | 216 | |
Yo_Robot | 26:dad0b2031173 | 217 | |
Yo_Robot | 23:2126e38bb48c | 218 | break; |
Yo_Robot | 24:a1d16835201c | 219 | } |
Yo_Robot | 27:b8254b76ec57 | 220 | |
Yo_Robot | 27:b8254b76ec57 | 221 | // Generar un numero definido de MILES de pulsos a la velocidad de posicionamiento |
Yo_Robot | 29:52932326c45a | 222 | case 'M': |
Yo_Robot | 27:b8254b76ec57 | 223 | { |
Yo_Robot | 28:b7ded82ee7da | 224 | float pulsos = value * 1000; //Numero de pulsos a generar |
Yo_Robot | 28:b7ded82ee7da | 225 | t_alto = (float)(pulsos / fq_posicion); //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 32:4483d6c225e5 | 226 | bandera_inicio = 0; //En caso que haya estado yendo a HOME, cancelar |
Yo_Robot | 32:4483d6c225e5 | 227 | |
Yo_Robot | 27:b8254b76ec57 | 228 | //DEBUG |
Yo_Robot | 32:4483d6c225e5 | 229 | //pc.printf("Tiempo en timer en seg = %f", t_alto); |
Yo_Robot | 27:b8254b76ec57 | 230 | |
Yo_Robot | 27:b8254b76ec57 | 231 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 232 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 27:b8254b76ec57 | 233 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 234 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 27:b8254b76ec57 | 235 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 27:b8254b76ec57 | 236 | |
Yo_Robot | 27:b8254b76ec57 | 237 | |
Yo_Robot | 27:b8254b76ec57 | 238 | // Envia un OK de comando recibido |
Yo_Robot | 27:b8254b76ec57 | 239 | if( isPC ) |
Yo_Robot | 27:b8254b76ec57 | 240 | pc.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 241 | else |
Yo_Robot | 27:b8254b76ec57 | 242 | RS_232.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 243 | |
Yo_Robot | 27:b8254b76ec57 | 244 | |
Yo_Robot | 27:b8254b76ec57 | 245 | break; |
Yo_Robot | 27:b8254b76ec57 | 246 | } |
Yo_Robot | 27:b8254b76ec57 | 247 | |
Yo_Robot | 27:b8254b76ec57 | 248 | // Generar un numero definido de MILLONES pulsos a la velocidad de posicionamiento |
Yo_Robot | 29:52932326c45a | 249 | case 'N': |
Yo_Robot | 27:b8254b76ec57 | 250 | { |
Yo_Robot | 28:b7ded82ee7da | 251 | float pulsos = value * 1000000; //Numero de pulsos a generar |
Yo_Robot | 28:b7ded82ee7da | 252 | t_alto = (float)(pulsos / fq_posicion); //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 32:4483d6c225e5 | 253 | bandera_inicio = 0; //En caso que haya estado yendo a HOME, cancelar |
Yo_Robot | 32:4483d6c225e5 | 254 | |
Yo_Robot | 27:b8254b76ec57 | 255 | //DEBUG |
Yo_Robot | 32:4483d6c225e5 | 256 | //pc.printf("Tiempo en timer en seg = %f", t_alto); |
Yo_Robot | 27:b8254b76ec57 | 257 | |
Yo_Robot | 27:b8254b76ec57 | 258 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 259 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 27:b8254b76ec57 | 260 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 27:b8254b76ec57 | 261 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 27:b8254b76ec57 | 262 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 27:b8254b76ec57 | 263 | |
Yo_Robot | 27:b8254b76ec57 | 264 | |
Yo_Robot | 27:b8254b76ec57 | 265 | // Envia un OK de comando recibido |
Yo_Robot | 27:b8254b76ec57 | 266 | if( isPC ) |
Yo_Robot | 27:b8254b76ec57 | 267 | pc.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 268 | else |
Yo_Robot | 27:b8254b76ec57 | 269 | RS_232.printf("OK\r\n"); |
Yo_Robot | 27:b8254b76ec57 | 270 | |
Yo_Robot | 27:b8254b76ec57 | 271 | |
Yo_Robot | 27:b8254b76ec57 | 272 | break; |
Yo_Robot | 27:b8254b76ec57 | 273 | } |
Yo_Robot | 27:b8254b76ec57 | 274 | |
Yo_Robot | 27:b8254b76ec57 | 275 | |
Yo_Robot | 29:52932326c45a | 276 | case 'R': // Leer la velocidd del encoder en RPM's |
Yo_Robot | 24:a1d16835201c | 277 | { |
Yo_Robot | 23:2126e38bb48c | 278 | if( isPC ) |
Yo_Robot | 33:e6ff02c3e0f5 | 279 | { |
Yo_Robot | 33:e6ff02c3e0f5 | 280 | pc.printf( "%u", encoder.CalculateRPM( encoder.GetVelocityCap(), 360) ); // ultima velocidad leida desde el encoder |
Yo_Robot | 33:e6ff02c3e0f5 | 281 | }else |
Yo_Robot | 33:e6ff02c3e0f5 | 282 | RS_232.printf( "%u",encoder.CalculateRPM( encoder.GetVelocityCap(), 360) ); |
Yo_Robot | 23:2126e38bb48c | 283 | break; |
Yo_Robot | 24:a1d16835201c | 284 | } |
Yo_Robot | 26:dad0b2031173 | 285 | |
Yo_Robot | 23:2126e38bb48c | 286 | case 'Z': //Limpiar contador encoder |
Yo_Robot | 29:52932326c45a | 287 | encoder.Reset(QEI_RESET_POS); |
Yo_Robot | 29:52932326c45a | 288 | encoder.Reset(QEI_RESET_VEL); |
Yo_Robot | 26:dad0b2031173 | 289 | |
Yo_Robot | 26:dad0b2031173 | 290 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 291 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 292 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 293 | else |
Yo_Robot | 26:dad0b2031173 | 294 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 295 | |
Yo_Robot | 23:2126e38bb48c | 296 | break; |
Yo_Robot | 23:2126e38bb48c | 297 | |
Yo_Robot | 23:2126e38bb48c | 298 | case 'S': //Encender el Servo |
Yo_Robot | 26:dad0b2031173 | 299 | |
Yo_Robot | 23:2126e38bb48c | 300 | pin_son = value; |
Yo_Robot | 32:4483d6c225e5 | 301 | bandera_inicio = 0; //En caso que haya estado yendo a HOME, cancelar |
Yo_Robot | 32:4483d6c225e5 | 302 | |
Yo_Robot | 32:4483d6c225e5 | 303 | // Envia un OK de comando recibido |
Yo_Robot | 32:4483d6c225e5 | 304 | if( isPC ) |
Yo_Robot | 32:4483d6c225e5 | 305 | pc.printf("OK\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 306 | else |
Yo_Robot | 32:4483d6c225e5 | 307 | RS_232.printf("OK\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 308 | |
Yo_Robot | 32:4483d6c225e5 | 309 | break; |
Yo_Robot | 32:4483d6c225e5 | 310 | |
Yo_Robot | 32:4483d6c225e5 | 311 | case 'I': //Ir al inicio del recorrido |
Yo_Robot | 32:4483d6c225e5 | 312 | |
Yo_Robot | 32:4483d6c225e5 | 313 | pin_dir = 1; //Mover hacia el motor |
Yo_Robot | 32:4483d6c225e5 | 314 | bandera_inicio = 1; // Informar a ISR_Adv_motor() acerca del Homing |
Yo_Robot | 32:4483d6c225e5 | 315 | setPTO( value ); // a la velocidad seteada |
Yo_Robot | 26:dad0b2031173 | 316 | |
Yo_Robot | 26:dad0b2031173 | 317 | // Envia un OK de comando recibido |
Yo_Robot | 26:dad0b2031173 | 318 | if( isPC ) |
Yo_Robot | 26:dad0b2031173 | 319 | pc.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 320 | else |
Yo_Robot | 26:dad0b2031173 | 321 | RS_232.printf("OK\r\n"); |
Yo_Robot | 26:dad0b2031173 | 322 | |
Yo_Robot | 23:2126e38bb48c | 323 | break; |
Yo_Robot | 28:b7ded82ee7da | 324 | |
Yo_Robot | 28:b7ded82ee7da | 325 | default: |
Yo_Robot | 28:b7ded82ee7da | 326 | |
Yo_Robot | 28:b7ded82ee7da | 327 | // EL COMANDO NO SE RECONOCE: No Aplica |
Yo_Robot | 28:b7ded82ee7da | 328 | if( isPC ) |
Yo_Robot | 28:b7ded82ee7da | 329 | pc.printf("NA\r\n"); |
Yo_Robot | 28:b7ded82ee7da | 330 | else |
Yo_Robot | 28:b7ded82ee7da | 331 | RS_232.printf("NA\r\n"); |
Yo_Robot | 28:b7ded82ee7da | 332 | |
Yo_Robot | 28:b7ded82ee7da | 333 | break; |
Yo_Robot | 32:4483d6c225e5 | 334 | |
Yo_Robot | 18:cf1e07d82630 | 335 | } |
Yo_Robot | 23:2126e38bb48c | 336 | |
Yo_Robot | 12:c02b08dacc45 | 337 | } |
Yo_Robot | 12:c02b08dacc45 | 338 | |
Yo_Robot | 12:c02b08dacc45 | 339 | |
Yo_Robot | 12:c02b08dacc45 | 340 | void setPTO( int freq ) |
Yo_Robot | 12:c02b08dacc45 | 341 | { |
Yo_Robot | 12:c02b08dacc45 | 342 | if( freq != 0 ) |
Yo_Robot | 12:c02b08dacc45 | 343 | { |
Yo_Robot | 12:c02b08dacc45 | 344 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 345 | setMR2( getMRvalue( freq ) ); |
Yo_Robot | 12:c02b08dacc45 | 346 | startTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 347 | |
Yo_Robot | 12:c02b08dacc45 | 348 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 349 | |
Yo_Robot | 12:c02b08dacc45 | 350 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 351 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 352 | } |
Yo_Robot | 12:c02b08dacc45 | 353 | } |
Yo_Robot | 12:c02b08dacc45 | 354 | |
Yo_Robot | 22:d5431fff164b | 355 | |
Yo_Robot | 22:d5431fff164b | 356 | |
Yo_Robot | 22:d5431fff164b | 357 | void ISR_Alarm() |
Yo_Robot | 22:d5431fff164b | 358 | { |
Yo_Robot | 22:d5431fff164b | 359 | pin_son = 0 ; |
Yo_Robot | 22:d5431fff164b | 360 | stopTimer2(); |
Yo_Robot | 22:d5431fff164b | 361 | aout = 0.5 ; |
Yo_Robot | 22:d5431fff164b | 362 | |
Yo_Robot | 22:d5431fff164b | 363 | if(isPC) |
Yo_Robot | 31:7e2cdd547cb2 | 364 | pc.printf( "AL\r\n" ); //ALARMA! solo es AL para que |
Yo_Robot | 31:7e2cdd547cb2 | 365 | //sea conciso con el modo ETH y funcione |
Yo_Robot | 31:7e2cdd547cb2 | 366 | //bien en LabVIEW. |
Yo_Robot | 22:d5431fff164b | 367 | else |
Yo_Robot | 26:dad0b2031173 | 368 | RS_232.printf( "AL\r\n" ); |
Yo_Robot | 22:d5431fff164b | 369 | } |
Yo_Robot | 22:d5431fff164b | 370 | |
Yo_Robot | 30:413d1a6648b5 | 371 | |
Yo_Robot | 30:413d1a6648b5 | 372 | |
Yo_Robot | 30:413d1a6648b5 | 373 | |
Yo_Robot | 30:413d1a6648b5 | 374 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 375 | * del sensor óptico, al recibir una ALARMA de proximidad al encoder |
Yo_Robot | 30:413d1a6648b5 | 376 | */ |
Yo_Robot | 31:7e2cdd547cb2 | 377 | void ISR_Alm_encoder() |
Yo_Robot | 31:7e2cdd547cb2 | 378 | { |
Yo_Robot | 31:7e2cdd547cb2 | 379 | if( isPC ) |
Yo_Robot | 32:4483d6c225e5 | 380 | pc.printf("A3\r\n"); |
Yo_Robot | 31:7e2cdd547cb2 | 381 | else |
Yo_Robot | 32:4483d6c225e5 | 382 | RS_232.printf("A3\r\n"); |
Yo_Robot | 31:7e2cdd547cb2 | 383 | } |
Yo_Robot | 30:413d1a6648b5 | 384 | |
Yo_Robot | 30:413d1a6648b5 | 385 | |
Yo_Robot | 30:413d1a6648b5 | 386 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 387 | * del sensor óptico, al recibir una ALARMA de proximidad al motor |
Yo_Robot | 30:413d1a6648b5 | 388 | */ |
Yo_Robot | 31:7e2cdd547cb2 | 389 | void ISR_Alm_motor() |
Yo_Robot | 31:7e2cdd547cb2 | 390 | { |
Yo_Robot | 32:4483d6c225e5 | 391 | wait_us( 50 ); |
Yo_Robot | 32:4483d6c225e5 | 392 | if ( limite_4 == 1) |
Yo_Robot | 32:4483d6c225e5 | 393 | { |
Yo_Robot | 32:4483d6c225e5 | 394 | if( isPC ) |
Yo_Robot | 32:4483d6c225e5 | 395 | pc.printf("A0\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 396 | else |
Yo_Robot | 32:4483d6c225e5 | 397 | RS_232.printf("A0\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 398 | } |
Yo_Robot | 31:7e2cdd547cb2 | 399 | } |
Yo_Robot | 30:413d1a6648b5 | 400 | |
Yo_Robot | 30:413d1a6648b5 | 401 | |
Yo_Robot | 30:413d1a6648b5 | 402 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 403 | * del sensor óptico, al recibir una advertencia de proximidad al encoder |
Yo_Robot | 30:413d1a6648b5 | 404 | */ |
Yo_Robot | 31:7e2cdd547cb2 | 405 | void ISR_Adv_encoder() |
Yo_Robot | 31:7e2cdd547cb2 | 406 | { |
Yo_Robot | 32:4483d6c225e5 | 407 | wait_ms( 50 ); |
Yo_Robot | 32:4483d6c225e5 | 408 | if ( limite_2 == 1) |
Yo_Robot | 32:4483d6c225e5 | 409 | { |
Yo_Robot | 31:7e2cdd547cb2 | 410 | |
Yo_Robot | 32:4483d6c225e5 | 411 | if( isPC ) |
Yo_Robot | 32:4483d6c225e5 | 412 | pc.printf("A2\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 413 | else |
Yo_Robot | 32:4483d6c225e5 | 414 | RS_232.printf("A2\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 415 | } |
Yo_Robot | 31:7e2cdd547cb2 | 416 | |
Yo_Robot | 31:7e2cdd547cb2 | 417 | } |
Yo_Robot | 30:413d1a6648b5 | 418 | |
Yo_Robot | 30:413d1a6648b5 | 419 | |
Yo_Robot | 30:413d1a6648b5 | 420 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 30:413d1a6648b5 | 421 | * del sensor óptico, al recibir una advertencia de proximidad al motor |
Yo_Robot | 30:413d1a6648b5 | 422 | */ |
Yo_Robot | 31:7e2cdd547cb2 | 423 | void ISR_Adv_motor() |
Yo_Robot | 31:7e2cdd547cb2 | 424 | { |
Yo_Robot | 32:4483d6c225e5 | 425 | if ( bandera_inicio == 1 ) |
Yo_Robot | 32:4483d6c225e5 | 426 | { |
Yo_Robot | 32:4483d6c225e5 | 427 | setPTO( 0 ); //detener el carro |
Yo_Robot | 32:4483d6c225e5 | 428 | pin_dir = 0; //hacer que se aleje del motor |
Yo_Robot | 32:4483d6c225e5 | 429 | |
Yo_Robot | 32:4483d6c225e5 | 430 | |
Yo_Robot | 32:4483d6c225e5 | 431 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 32:4483d6c225e5 | 432 | setPTO( 20000 ); //Nueva frecuencia de salida |
Yo_Robot | 32:4483d6c225e5 | 433 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 32:4483d6c225e5 | 434 | wait_ms( 100 ); //Espera hasta llegar a la posicion |
Yo_Robot | 32:4483d6c225e5 | 435 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 32:4483d6c225e5 | 436 | |
Yo_Robot | 32:4483d6c225e5 | 437 | setPTO (0); // Detener el carro cuando este en posición valida |
Yo_Robot | 32:4483d6c225e5 | 438 | wait_ms(100); |
Yo_Robot | 32:4483d6c225e5 | 439 | //Encerar el contador de encoder y de velocidad |
Yo_Robot | 32:4483d6c225e5 | 440 | encoder.Reset(QEI_RESET_POS); |
Yo_Robot | 32:4483d6c225e5 | 441 | encoder.Reset(QEI_RESET_VEL); |
Yo_Robot | 32:4483d6c225e5 | 442 | |
Yo_Robot | 32:4483d6c225e5 | 443 | bandera_inicio = 0; //Limpiar la bandera |
Yo_Robot | 32:4483d6c225e5 | 444 | |
Yo_Robot | 32:4483d6c225e5 | 445 | // Envia un IN de proceso terminado |
Yo_Robot | 32:4483d6c225e5 | 446 | if( isPC ) |
Yo_Robot | 32:4483d6c225e5 | 447 | pc.printf("IN\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 448 | else |
Yo_Robot | 32:4483d6c225e5 | 449 | RS_232.printf("IN\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 450 | } |
Yo_Robot | 32:4483d6c225e5 | 451 | |
Yo_Robot | 32:4483d6c225e5 | 452 | else{ |
Yo_Robot | 32:4483d6c225e5 | 453 | |
Yo_Robot | 32:4483d6c225e5 | 454 | if( isPC ) |
Yo_Robot | 32:4483d6c225e5 | 455 | pc.printf("A1\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 456 | else |
Yo_Robot | 32:4483d6c225e5 | 457 | RS_232.printf("A1\r\n"); |
Yo_Robot | 32:4483d6c225e5 | 458 | } |
Yo_Robot | 32:4483d6c225e5 | 459 | |
Yo_Robot | 31:7e2cdd547cb2 | 460 | } |
Yo_Robot | 30:413d1a6648b5 | 461 | |
Yo_Robot | 30:413d1a6648b5 | 462 | |
Yo_Robot | 30:413d1a6648b5 | 463 | |
Yo_Robot | 22:d5431fff164b | 464 | int getMRvalue( int fout ) |
Yo_Robot | 22:d5431fff164b | 465 | { |
Yo_Robot | 22:d5431fff164b | 466 | int toRegister; |
Yo_Robot | 22:d5431fff164b | 467 | |
Yo_Robot | 22:d5431fff164b | 468 | toRegister = (24000000 /(fout*2.0) ) -1; |
Yo_Robot | 22:d5431fff164b | 469 | return toRegister; |
Yo_Robot | 22:d5431fff164b | 470 | } |
Yo_Robot | 22:d5431fff164b | 471 | |
Yo_Robot | 22:d5431fff164b | 472 | |
Yo_Robot | 22:d5431fff164b | 473 | void setMR2( int newValue ) |
Yo_Robot | 22:d5431fff164b | 474 | { |
Yo_Robot | 22:d5431fff164b | 475 | LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas |
Yo_Robot | 22:d5431fff164b | 476 | LPC_TIM2->MR3 = newValue; // Para testear el programa. |
Yo_Robot | 22:d5431fff164b | 477 | } |
Yo_Robot | 22:d5431fff164b | 478 | |
Yo_Robot | 22:d5431fff164b | 479 | |
Yo_Robot | 22:d5431fff164b | 480 | |
Yo_Robot | 22:d5431fff164b | 481 | void startTimer2() |
Yo_Robot | 22:d5431fff164b | 482 | { |
Yo_Robot | 22:d5431fff164b | 483 | // Arrancer el Timer 2 |
Yo_Robot | 22:d5431fff164b | 484 | LPC_TIM2->TCR = 1; |
Yo_Robot | 22:d5431fff164b | 485 | } |
Yo_Robot | 22:d5431fff164b | 486 | |
Yo_Robot | 22:d5431fff164b | 487 | void stopTimer2() |
Yo_Robot | 22:d5431fff164b | 488 | { |
Yo_Robot | 22:d5431fff164b | 489 | // Detener el Timer 2 |
Yo_Robot | 22:d5431fff164b | 490 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 22:d5431fff164b | 491 | } |
Yo_Robot | 22:d5431fff164b | 492 | |
Yo_Robot | 22:d5431fff164b | 493 | int getBaud() |
Yo_Robot | 22:d5431fff164b | 494 | { |
Yo_Robot | 22:d5431fff164b | 495 | int baudios = 115200; //Valor por defecto |
Yo_Robot | 22:d5431fff164b | 496 | |
Yo_Robot | 22:d5431fff164b | 497 | FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 22:d5431fff164b | 498 | |
Yo_Robot | 22:d5431fff164b | 499 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 22:d5431fff164b | 500 | { |
Yo_Robot | 22:d5431fff164b | 501 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 22:d5431fff164b | 502 | printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n"); |
Yo_Robot | 22:d5431fff164b | 503 | printf("que contenga las lineas:\n\n"); |
Yo_Robot | 22:d5431fff164b | 504 | |
Yo_Robot | 22:d5431fff164b | 505 | printf(" 1\n"); |
Yo_Robot | 22:d5431fff164b | 506 | printf(" 2\n"); |
Yo_Robot | 22:d5431fff164b | 507 | printf(" 3\n"); |
Yo_Robot | 22:d5431fff164b | 508 | printf(" 4\n"); |
Yo_Robot | 22:d5431fff164b | 509 | printf(" baudios: 115200\n"); |
Yo_Robot | 22:d5431fff164b | 510 | |
Yo_Robot | 22:d5431fff164b | 511 | printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n"); |
Yo_Robot | 22:d5431fff164b | 512 | printf("luego reinicie el microcontrolador\n"); |
Yo_Robot | 22:d5431fff164b | 513 | exit(1); |
Yo_Robot | 22:d5431fff164b | 514 | |
Yo_Robot | 22:d5431fff164b | 515 | } |
Yo_Robot | 22:d5431fff164b | 516 | else |
Yo_Robot | 22:d5431fff164b | 517 | { |
Yo_Robot | 22:d5431fff164b | 518 | // Cadenas de caracteres desde el Archivo config.mbd |
Yo_Robot | 22:d5431fff164b | 519 | char notstr [04]; // linea vacia |
Yo_Robot | 22:d5431fff164b | 520 | char baud [40]; // valor en baudios |
Yo_Robot | 22:d5431fff164b | 521 | |
Yo_Robot | 22:d5431fff164b | 522 | // Leer linea a linea el archivo |
Yo_Robot | 22:d5431fff164b | 523 | // cuatro primeras lineas no sirven |
Yo_Robot | 22:d5431fff164b | 524 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 525 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 526 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 527 | fgets( notstr, 4, fp ); |
Yo_Robot | 22:d5431fff164b | 528 | fgets( baud, 40, fp ); |
Yo_Robot | 22:d5431fff164b | 529 | fclose(fp); |
Yo_Robot | 23:2126e38bb48c | 530 | |
Yo_Robot | 22:d5431fff164b | 531 | // Extraer los valores numericos |
Yo_Robot | 22:d5431fff164b | 532 | sscanf( baud,"%*s %d",&baudios ); |
Yo_Robot | 22:d5431fff164b | 533 | |
Yo_Robot | 22:d5431fff164b | 534 | |
Yo_Robot | 22:d5431fff164b | 535 | } |
Yo_Robot | 22:d5431fff164b | 536 | |
Yo_Robot | 22:d5431fff164b | 537 | return baudios; |
Yo_Robot | 22:d5431fff164b | 538 | } |
Yo_Robot | 22:d5431fff164b | 539 | |
Yo_Robot | 22:d5431fff164b | 540 | // **** Funciones Liberia Ethernet ***** // |
Yo_Robot | 12:c02b08dacc45 | 541 | void setPTO_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 542 | { |
Yo_Robot | 12:c02b08dacc45 | 543 | int freq = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 544 | |
Yo_Robot | 12:c02b08dacc45 | 545 | if( freq != 0 ){ |
Yo_Robot | 12:c02b08dacc45 | 546 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 547 | setMR2( getMRvalue( freq ) ); // Cambiar frefuencia |
Yo_Robot | 12:c02b08dacc45 | 548 | startTimer2(); // Iniciar Timer |
Yo_Robot | 14:039d070732d5 | 549 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 550 | { |
Yo_Robot | 31:7e2cdd547cb2 | 551 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 552 | { |
Yo_Robot | 31:7e2cdd547cb2 | 553 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 554 | } |
Yo_Robot | 31:7e2cdd547cb2 | 555 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 556 | { |
Yo_Robot | 31:7e2cdd547cb2 | 557 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 558 | } |
Yo_Robot | 31:7e2cdd547cb2 | 559 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 560 | { |
Yo_Robot | 31:7e2cdd547cb2 | 561 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 562 | } |
Yo_Robot | 31:7e2cdd547cb2 | 563 | |
Yo_Robot | 31:7e2cdd547cb2 | 564 | else |
Yo_Robot | 31:7e2cdd547cb2 | 565 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 566 | } |
Yo_Robot | 14:039d070732d5 | 567 | else |
Yo_Robot | 14:039d070732d5 | 568 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 569 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 570 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 571 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 14:039d070732d5 | 572 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 573 | { |
Yo_Robot | 31:7e2cdd547cb2 | 574 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 575 | { |
Yo_Robot | 31:7e2cdd547cb2 | 576 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 577 | } |
Yo_Robot | 31:7e2cdd547cb2 | 578 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 579 | { |
Yo_Robot | 31:7e2cdd547cb2 | 580 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 581 | } |
Yo_Robot | 31:7e2cdd547cb2 | 582 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 583 | { |
Yo_Robot | 31:7e2cdd547cb2 | 584 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 585 | } |
Yo_Robot | 31:7e2cdd547cb2 | 586 | |
Yo_Robot | 31:7e2cdd547cb2 | 587 | else |
Yo_Robot | 31:7e2cdd547cb2 | 588 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 589 | } |
Yo_Robot | 14:039d070732d5 | 590 | else |
Yo_Robot | 14:039d070732d5 | 591 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 592 | } |
Yo_Robot | 12:c02b08dacc45 | 593 | } |
Yo_Robot | 12:c02b08dacc45 | 594 | |
Yo_Robot | 15:a1ffa32ce9d1 | 595 | void setANG_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 596 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 597 | long int pulsos = atol( input ); //Numero de pulsos a generar |
Yo_Robot | 29:52932326c45a | 598 | float pulsos_f = (float) pulsos; |
Yo_Robot | 29:52932326c45a | 599 | t_alto = pulsos_f / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 15:a1ffa32ce9d1 | 600 | |
Yo_Robot | 15:a1ffa32ce9d1 | 601 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 602 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 15:a1ffa32ce9d1 | 603 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 604 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 18:cf1e07d82630 | 605 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 15:a1ffa32ce9d1 | 606 | |
Yo_Robot | 15:a1ffa32ce9d1 | 607 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 608 | { |
Yo_Robot | 31:7e2cdd547cb2 | 609 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 610 | { |
Yo_Robot | 31:7e2cdd547cb2 | 611 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 612 | } |
Yo_Robot | 31:7e2cdd547cb2 | 613 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 614 | { |
Yo_Robot | 31:7e2cdd547cb2 | 615 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 616 | } |
Yo_Robot | 31:7e2cdd547cb2 | 617 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 618 | { |
Yo_Robot | 31:7e2cdd547cb2 | 619 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 620 | } |
Yo_Robot | 31:7e2cdd547cb2 | 621 | |
Yo_Robot | 31:7e2cdd547cb2 | 622 | else |
Yo_Robot | 31:7e2cdd547cb2 | 623 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 624 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 625 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 626 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 627 | |
Yo_Robot | 15:a1ffa32ce9d1 | 628 | } |
Yo_Robot | 21:353b0fe8fc54 | 629 | |
Yo_Robot | 21:353b0fe8fc54 | 630 | |
Yo_Robot | 15:a1ffa32ce9d1 | 631 | void setSPD_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 632 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 633 | fq_posicion = atoi( input ); |
Yo_Robot | 15:a1ffa32ce9d1 | 634 | // Esta funcion cambia la velocidad con la que se |
Yo_Robot | 15:a1ffa32ce9d1 | 635 | // posicionara el eje del motor en un angulo determinado |
Yo_Robot | 15:a1ffa32ce9d1 | 636 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 637 | { |
Yo_Robot | 31:7e2cdd547cb2 | 638 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 639 | { |
Yo_Robot | 31:7e2cdd547cb2 | 640 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 641 | } |
Yo_Robot | 31:7e2cdd547cb2 | 642 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 643 | { |
Yo_Robot | 31:7e2cdd547cb2 | 644 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 645 | } |
Yo_Robot | 31:7e2cdd547cb2 | 646 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 647 | { |
Yo_Robot | 31:7e2cdd547cb2 | 648 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 649 | } |
Yo_Robot | 31:7e2cdd547cb2 | 650 | |
Yo_Robot | 31:7e2cdd547cb2 | 651 | else |
Yo_Robot | 31:7e2cdd547cb2 | 652 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 653 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 654 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 655 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 656 | |
Yo_Robot | 15:a1ffa32ce9d1 | 657 | } |
Yo_Robot | 21:353b0fe8fc54 | 658 | |
Yo_Robot | 12:c02b08dacc45 | 659 | void setAout_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 660 | { |
Yo_Robot | 12:c02b08dacc45 | 661 | int vout = atoi( input ); |
Yo_Robot | 14:039d070732d5 | 662 | aout = (float)( vout + 10000 ) / 20000; |
Yo_Robot | 14:039d070732d5 | 663 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 664 | { |
Yo_Robot | 31:7e2cdd547cb2 | 665 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 666 | { |
Yo_Robot | 31:7e2cdd547cb2 | 667 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 668 | } |
Yo_Robot | 31:7e2cdd547cb2 | 669 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 670 | { |
Yo_Robot | 31:7e2cdd547cb2 | 671 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 672 | } |
Yo_Robot | 31:7e2cdd547cb2 | 673 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 674 | { |
Yo_Robot | 31:7e2cdd547cb2 | 675 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 676 | } |
Yo_Robot | 31:7e2cdd547cb2 | 677 | |
Yo_Robot | 31:7e2cdd547cb2 | 678 | else |
Yo_Robot | 31:7e2cdd547cb2 | 679 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 680 | } |
Yo_Robot | 14:039d070732d5 | 681 | else |
Yo_Robot | 14:039d070732d5 | 682 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 683 | } |
Yo_Robot | 12:c02b08dacc45 | 684 | |
Yo_Robot | 12:c02b08dacc45 | 685 | |
Yo_Robot | 12:c02b08dacc45 | 686 | |
Yo_Robot | 12:c02b08dacc45 | 687 | void setDir_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 688 | { |
Yo_Robot | 12:c02b08dacc45 | 689 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 690 | |
Yo_Robot | 12:c02b08dacc45 | 691 | pin_dir = value; |
Yo_Robot | 12:c02b08dacc45 | 692 | |
Yo_Robot | 14:039d070732d5 | 693 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 694 | { |
Yo_Robot | 31:7e2cdd547cb2 | 695 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 696 | { |
Yo_Robot | 31:7e2cdd547cb2 | 697 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 698 | } |
Yo_Robot | 31:7e2cdd547cb2 | 699 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 700 | { |
Yo_Robot | 31:7e2cdd547cb2 | 701 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 702 | } |
Yo_Robot | 31:7e2cdd547cb2 | 703 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 704 | { |
Yo_Robot | 31:7e2cdd547cb2 | 705 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 706 | } |
Yo_Robot | 31:7e2cdd547cb2 | 707 | |
Yo_Robot | 31:7e2cdd547cb2 | 708 | else |
Yo_Robot | 31:7e2cdd547cb2 | 709 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 710 | } |
Yo_Robot | 12:c02b08dacc45 | 711 | else |
Yo_Robot | 14:039d070732d5 | 712 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 713 | } |
Yo_Robot | 12:c02b08dacc45 | 714 | |
Yo_Robot | 12:c02b08dacc45 | 715 | |
Yo_Robot | 12:c02b08dacc45 | 716 | |
Yo_Robot | 12:c02b08dacc45 | 717 | void setSON_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 718 | { |
Yo_Robot | 12:c02b08dacc45 | 719 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 720 | |
Yo_Robot | 12:c02b08dacc45 | 721 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 722 | |
Yo_Robot | 14:039d070732d5 | 723 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 724 | { |
Yo_Robot | 31:7e2cdd547cb2 | 725 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 726 | { |
Yo_Robot | 31:7e2cdd547cb2 | 727 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 728 | } |
Yo_Robot | 31:7e2cdd547cb2 | 729 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 730 | { |
Yo_Robot | 31:7e2cdd547cb2 | 731 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 732 | } |
Yo_Robot | 31:7e2cdd547cb2 | 733 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 734 | { |
Yo_Robot | 31:7e2cdd547cb2 | 735 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 736 | } |
Yo_Robot | 31:7e2cdd547cb2 | 737 | |
Yo_Robot | 31:7e2cdd547cb2 | 738 | else |
Yo_Robot | 31:7e2cdd547cb2 | 739 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 740 | } |
Yo_Robot | 12:c02b08dacc45 | 741 | else |
Yo_Robot | 14:039d070732d5 | 742 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 743 | |
Yo_Robot | 12:c02b08dacc45 | 744 | } |
Yo_Robot | 12:c02b08dacc45 | 745 | |
Yo_Robot | 12:c02b08dacc45 | 746 | |
Yo_Robot | 22:d5431fff164b | 747 | void getENC_eth( char * input, char * output ) |
Yo_Robot | 20:4b154134ab20 | 748 | { |
Yo_Robot | 22:d5431fff164b | 749 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 750 | { |
Yo_Robot | 31:7e2cdd547cb2 | 751 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 752 | { |
Yo_Robot | 31:7e2cdd547cb2 | 753 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 754 | } |
Yo_Robot | 31:7e2cdd547cb2 | 755 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 756 | { |
Yo_Robot | 31:7e2cdd547cb2 | 757 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 758 | } |
Yo_Robot | 31:7e2cdd547cb2 | 759 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 760 | { |
Yo_Robot | 31:7e2cdd547cb2 | 761 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 762 | } |
Yo_Robot | 31:7e2cdd547cb2 | 763 | |
Yo_Robot | 31:7e2cdd547cb2 | 764 | else |
Yo_Robot | 31:7e2cdd547cb2 | 765 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 766 | } |
Yo_Robot | 22:d5431fff164b | 767 | else |
Yo_Robot | 22:d5431fff164b | 768 | sprintf( output,"AL" ); |
Yo_Robot | 22:d5431fff164b | 769 | } |
Yo_Robot | 21:353b0fe8fc54 | 770 | |
Yo_Robot | 21:353b0fe8fc54 | 771 | |
Yo_Robot | 22:d5431fff164b | 772 | void setENC_eth( char * input, char * output ) |
Yo_Robot | 22:d5431fff164b | 773 | { |
Yo_Robot | 29:52932326c45a | 774 | encoder.Reset(QEI_RESET_POS); |
Yo_Robot | 29:52932326c45a | 775 | encoder.Reset(QEI_RESET_VEL); |
Yo_Robot | 22:d5431fff164b | 776 | |
Yo_Robot | 22:d5431fff164b | 777 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 778 | { |
Yo_Robot | 31:7e2cdd547cb2 | 779 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 780 | { |
Yo_Robot | 31:7e2cdd547cb2 | 781 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 782 | } |
Yo_Robot | 31:7e2cdd547cb2 | 783 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 784 | { |
Yo_Robot | 31:7e2cdd547cb2 | 785 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 786 | } |
Yo_Robot | 31:7e2cdd547cb2 | 787 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 788 | { |
Yo_Robot | 31:7e2cdd547cb2 | 789 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 790 | } |
Yo_Robot | 31:7e2cdd547cb2 | 791 | |
Yo_Robot | 31:7e2cdd547cb2 | 792 | else |
Yo_Robot | 31:7e2cdd547cb2 | 793 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 794 | } |
Yo_Robot | 22:d5431fff164b | 795 | else |
Yo_Robot | 22:d5431fff164b | 796 | sprintf( output,"AL" ); |
Yo_Robot | 22:d5431fff164b | 797 | |
Yo_Robot | 20:4b154134ab20 | 798 | } |
Yo_Robot | 12:c02b08dacc45 | 799 | |
Yo_Robot | 23:2126e38bb48c | 800 | void getRPM_eth( char * input, char * output ) |
Yo_Robot | 23:2126e38bb48c | 801 | { |
Yo_Robot | 29:52932326c45a | 802 | int rpm; |
Yo_Robot | 23:2126e38bb48c | 803 | |
Yo_Robot | 29:52932326c45a | 804 | rpm = encoder.CalculateRPM( encoder.GetVelocityCap(), 360); // ultima velocidad leida desde el encoder |
Yo_Robot | 29:52932326c45a | 805 | // numero de revoluciones por vuelta del encoder |
Yo_Robot | 29:52932326c45a | 806 | |
Yo_Robot | 23:2126e38bb48c | 807 | |
Yo_Robot | 23:2126e38bb48c | 808 | if( pin_alm == 0 ) |
Yo_Robot | 31:7e2cdd547cb2 | 809 | { |
Yo_Robot | 31:7e2cdd547cb2 | 810 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 811 | { |
Yo_Robot | 31:7e2cdd547cb2 | 812 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 813 | } |
Yo_Robot | 31:7e2cdd547cb2 | 814 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 31:7e2cdd547cb2 | 815 | { |
Yo_Robot | 31:7e2cdd547cb2 | 816 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 817 | } |
Yo_Robot | 31:7e2cdd547cb2 | 818 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 31:7e2cdd547cb2 | 819 | { |
Yo_Robot | 31:7e2cdd547cb2 | 820 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 821 | } |
Yo_Robot | 31:7e2cdd547cb2 | 822 | |
Yo_Robot | 31:7e2cdd547cb2 | 823 | else |
Yo_Robot | 31:7e2cdd547cb2 | 824 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 31:7e2cdd547cb2 | 825 | } |
Yo_Robot | 23:2126e38bb48c | 826 | else |
Yo_Robot | 29:52932326c45a | 827 | sprintf( output,"AL" ); |
Yo_Robot | 23:2126e38bb48c | 828 | |
Yo_Robot | 23:2126e38bb48c | 829 | |
Yo_Robot | 23:2126e38bb48c | 830 | } |
Yo_Robot | 23:2126e38bb48c | 831 | |
Yo_Robot | 21:353b0fe8fc54 | 832 | |
Yo_Robot | 32:4483d6c225e5 | 833 | void setHOME_eth( char * input, char * output ) |
Yo_Robot | 32:4483d6c225e5 | 834 | { |
Yo_Robot | 32:4483d6c225e5 | 835 | int value = atoi( input ); |
Yo_Robot | 32:4483d6c225e5 | 836 | pin_dir = 1; //Mover hacia el motor |
Yo_Robot | 32:4483d6c225e5 | 837 | bandera_inicio = 1; // Informar a ISR_Adv_motor() acerca del Homing |
Yo_Robot | 32:4483d6c225e5 | 838 | setPTO( value * 1000 ); // a la velocidad seteada en KiloHertzios |
Yo_Robot | 32:4483d6c225e5 | 839 | |
Yo_Robot | 32:4483d6c225e5 | 840 | if( pin_alm == 0 ) |
Yo_Robot | 32:4483d6c225e5 | 841 | { |
Yo_Robot | 32:4483d6c225e5 | 842 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 32:4483d6c225e5 | 843 | { |
Yo_Robot | 32:4483d6c225e5 | 844 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 32:4483d6c225e5 | 845 | } |
Yo_Robot | 32:4483d6c225e5 | 846 | |
Yo_Robot | 32:4483d6c225e5 | 847 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 32:4483d6c225e5 | 848 | { |
Yo_Robot | 32:4483d6c225e5 | 849 | if (bandera_inicio == 1) |
Yo_Robot | 32:4483d6c225e5 | 850 | { |
Yo_Robot | 32:4483d6c225e5 | 851 | sprintf( output,"IN\r\n" ); |
Yo_Robot | 32:4483d6c225e5 | 852 | } |
Yo_Robot | 32:4483d6c225e5 | 853 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 32:4483d6c225e5 | 854 | } |
Yo_Robot | 32:4483d6c225e5 | 855 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 32:4483d6c225e5 | 856 | { |
Yo_Robot | 32:4483d6c225e5 | 857 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 32:4483d6c225e5 | 858 | } |
Yo_Robot | 32:4483d6c225e5 | 859 | |
Yo_Robot | 32:4483d6c225e5 | 860 | else |
Yo_Robot | 32:4483d6c225e5 | 861 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 32:4483d6c225e5 | 862 | } |
Yo_Robot | 32:4483d6c225e5 | 863 | else |
Yo_Robot | 32:4483d6c225e5 | 864 | sprintf( output,"AL" ); |
Yo_Robot | 32:4483d6c225e5 | 865 | |
Yo_Robot | 32:4483d6c225e5 | 866 | |
Yo_Robot | 32:4483d6c225e5 | 867 | } |
Yo_Robot | 32:4483d6c225e5 | 868 | |
Yo_Robot | 34:bdf918bc9b59 | 869 | void getALM_eth ( char * input, char * output ) |
Yo_Robot | 34:bdf918bc9b59 | 870 | { |
Yo_Robot | 34:bdf918bc9b59 | 871 | |
Yo_Robot | 34:bdf918bc9b59 | 872 | if( pin_alm == 0 ) |
Yo_Robot | 34:bdf918bc9b59 | 873 | { |
Yo_Robot | 34:bdf918bc9b59 | 874 | if ( limite_2 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 34:bdf918bc9b59 | 875 | { |
Yo_Robot | 34:bdf918bc9b59 | 876 | sprintf( output,"A2\r\n" ); |
Yo_Robot | 34:bdf918bc9b59 | 877 | } |
Yo_Robot | 34:bdf918bc9b59 | 878 | if ( limite_3 == 1 ) // Alarma muy cerca al encoder |
Yo_Robot | 34:bdf918bc9b59 | 879 | { |
Yo_Robot | 34:bdf918bc9b59 | 880 | sprintf( output,"A1\r\n" ); |
Yo_Robot | 34:bdf918bc9b59 | 881 | } |
Yo_Robot | 34:bdf918bc9b59 | 882 | if ( limite_1 == 1 || limite_4 == 1 ) // ERROR DE POSICION |
Yo_Robot | 34:bdf918bc9b59 | 883 | { |
Yo_Robot | 34:bdf918bc9b59 | 884 | sprintf( output,"A0\r\n" ); |
Yo_Robot | 34:bdf918bc9b59 | 885 | } |
Yo_Robot | 34:bdf918bc9b59 | 886 | |
Yo_Robot | 34:bdf918bc9b59 | 887 | else |
Yo_Robot | 34:bdf918bc9b59 | 888 | sprintf( output,"OK\r\n" ); |
Yo_Robot | 34:bdf918bc9b59 | 889 | } |
Yo_Robot | 34:bdf918bc9b59 | 890 | else |
Yo_Robot | 34:bdf918bc9b59 | 891 | sprintf( output,"AL" ); |
Yo_Robot | 34:bdf918bc9b59 | 892 | |
Yo_Robot | 34:bdf918bc9b59 | 893 | } |
Yo_Robot | 22:d5431fff164b | 894 | |
Yo_Robot | 22:d5431fff164b | 895 | |
Yo_Robot | 4:552beeda4722 | 896 | /* LEGACY FUNCTIONS |
Yo_Robot | 4:552beeda4722 | 897 | * |
Yo_Robot | 20:4b154134ab20 | 898 | * El siguiente codigo no es utilizado por el |
Yo_Robot | 20:4b154134ab20 | 899 | * programa. Sin embargo pueden servir como |
Yo_Robot | 20:4b154134ab20 | 900 | * futuras referencias. |
Yo_Robot | 4:552beeda4722 | 901 | */ |
Yo_Robot | 4:552beeda4722 | 902 | void setMR3( int newValue ) |
Yo_Robot | 4:552beeda4722 | 903 | { |
Yo_Robot | 4:552beeda4722 | 904 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 4:552beeda4722 | 905 | } |
Yo_Robot | 4:552beeda4722 | 906 | |
Yo_Robot | 4:552beeda4722 | 907 | |
Yo_Robot | 4:552beeda4722 | 908 | void setPrescaler( int newValue) |
Yo_Robot | 4:552beeda4722 | 909 | { |
Yo_Robot | 4:552beeda4722 | 910 | LPC_TIM2->PR = newValue; |
Yo_Robot | 4:552beeda4722 | 911 | } |
Yo_Robot | 20:4b154134ab20 | 912 | |
Yo_Robot | 20:4b154134ab20 | 913 | |
Yo_Robot | 20:4b154134ab20 | 914 | EthernetNetIf configurarEthernet() |
Yo_Robot | 20:4b154134ab20 | 915 | { |
Yo_Robot | 20:4b154134ab20 | 916 | //____________ *** ARCHIVO DE CONFIGURACION ***_______________________ // |
Yo_Robot | 20:4b154134ab20 | 917 | |
Yo_Robot | 20:4b154134ab20 | 918 | printf("\n *** CONFIGURACION ETHERNET DE MBED ***\n"); |
Yo_Robot | 20:4b154134ab20 | 919 | printf("Leyendo archivo de configuracion...\n\n"); |
Yo_Robot | 20:4b154134ab20 | 920 | |
Yo_Robot | 20:4b154134ab20 | 921 | FILE *fp = fopen("/local/config.txt", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 20:4b154134ab20 | 922 | |
Yo_Robot | 20:4b154134ab20 | 923 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 20:4b154134ab20 | 924 | { |
Yo_Robot | 20:4b154134ab20 | 925 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 20:4b154134ab20 | 926 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 927 | |
Yo_Robot | 20:4b154134ab20 | 928 | } |
Yo_Robot | 20:4b154134ab20 | 929 | else |
Yo_Robot | 20:4b154134ab20 | 930 | { |
Yo_Robot | 20:4b154134ab20 | 931 | // Cadenas de caracteres desde el Archivo config.txt |
Yo_Robot | 20:4b154134ab20 | 932 | char isDHCP [15]; //Modo Automatico o Manual |
Yo_Robot | 20:4b154134ab20 | 933 | char empty [2]; // Linea vacia |
Yo_Robot | 20:4b154134ab20 | 934 | char ip [40]; // Direccion IP |
Yo_Robot | 20:4b154134ab20 | 935 | char mask [40]; // Mascara de Subred |
Yo_Robot | 20:4b154134ab20 | 936 | char gate [40]; // Puerta de enlace |
Yo_Robot | 20:4b154134ab20 | 937 | char dns [40]; // Direccion DNS |
Yo_Robot | 20:4b154134ab20 | 938 | |
Yo_Robot | 20:4b154134ab20 | 939 | // Valores 'int' para agregar a la configuracion manual |
Yo_Robot | 20:4b154134ab20 | 940 | int DHCP; |
Yo_Robot | 20:4b154134ab20 | 941 | int n_ip[4]; |
Yo_Robot | 20:4b154134ab20 | 942 | int n_mask[4]; |
Yo_Robot | 20:4b154134ab20 | 943 | int n_gate[4]; |
Yo_Robot | 20:4b154134ab20 | 944 | int n_dns[4]; |
Yo_Robot | 20:4b154134ab20 | 945 | |
Yo_Robot | 20:4b154134ab20 | 946 | |
Yo_Robot | 20:4b154134ab20 | 947 | // Leer linea a linea el archivo |
Yo_Robot | 20:4b154134ab20 | 948 | fgets( isDHCP, 15, fp ); |
Yo_Robot | 20:4b154134ab20 | 949 | fgets( empty, 2, fp ); |
Yo_Robot | 20:4b154134ab20 | 950 | fgets( ip, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 951 | fgets( mask, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 952 | fgets( gate, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 953 | fgets( dns, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 954 | |
Yo_Robot | 20:4b154134ab20 | 955 | printf("Cerrando archivo...\n"); |
Yo_Robot | 20:4b154134ab20 | 956 | fclose(fp); |
Yo_Robot | 20:4b154134ab20 | 957 | |
Yo_Robot | 20:4b154134ab20 | 958 | // Extraer los valores numericos |
Yo_Robot | 20:4b154134ab20 | 959 | sscanf( isDHCP,"%*s %d",&DHCP ); |
Yo_Robot | 20:4b154134ab20 | 960 | |
Yo_Robot | 20:4b154134ab20 | 961 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0],&n_ip[1],&n_ip[2],&n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 962 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 963 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 964 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0],&n_dns[1],&n_dns[2],&n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 965 | |
Yo_Robot | 20:4b154134ab20 | 966 | |
Yo_Robot | 20:4b154134ab20 | 967 | if(DHCP) //En caso de modo DHCP |
Yo_Robot | 20:4b154134ab20 | 968 | { |
Yo_Robot | 20:4b154134ab20 | 969 | printf("\n Configurar red de manera automatica\n"); |
Yo_Robot | 20:4b154134ab20 | 970 | EthernetNetIf eth; //Crea la interfaz |
Yo_Robot | 20:4b154134ab20 | 971 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 972 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 973 | { |
Yo_Robot | 20:4b154134ab20 | 974 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 975 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 976 | } |
Yo_Robot | 20:4b154134ab20 | 977 | |
Yo_Robot | 20:4b154134ab20 | 978 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 979 | return eth; |
Yo_Robot | 20:4b154134ab20 | 980 | } |
Yo_Robot | 20:4b154134ab20 | 981 | else |
Yo_Robot | 20:4b154134ab20 | 982 | { |
Yo_Robot | 20:4b154134ab20 | 983 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0], &n_ip[1], &n_ip[2], &n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 984 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 985 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 986 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0], &n_dns[1], &n_dns[2], &n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 987 | printf(" %s %s %s %s %s\n " , isDHCP, ip, mask, gate, dns ); |
Yo_Robot | 20:4b154134ab20 | 988 | |
Yo_Robot | 20:4b154134ab20 | 989 | printf("\n Configurar red de manera manual\n"); |
Yo_Robot | 20:4b154134ab20 | 990 | EthernetNetIf eth( |
Yo_Robot | 20:4b154134ab20 | 991 | IpAddr( n_ip[0], n_ip[1], n_ip[2], n_ip[3]), //IP Address |
Yo_Robot | 20:4b154134ab20 | 992 | IpAddr( n_mask[0],n_mask[1],n_mask[2],n_mask[3]), //Network Mask |
Yo_Robot | 20:4b154134ab20 | 993 | IpAddr( n_gate[0],n_gate[1],n_gate[2],n_gate[3]), //Gateway |
Yo_Robot | 20:4b154134ab20 | 994 | IpAddr( n_dns[0], n_dns[1], n_dns[2], n_dns[3] ) //DNS |
Yo_Robot | 20:4b154134ab20 | 995 | ); |
Yo_Robot | 20:4b154134ab20 | 996 | |
Yo_Robot | 20:4b154134ab20 | 997 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 998 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 999 | { |
Yo_Robot | 20:4b154134ab20 | 1000 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 1001 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 1002 | } |
Yo_Robot | 20:4b154134ab20 | 1003 | |
Yo_Robot | 20:4b154134ab20 | 1004 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 1005 | return eth; |
Yo_Robot | 20:4b154134ab20 | 1006 | } |
Yo_Robot | 20:4b154134ab20 | 1007 | |
Yo_Robot | 20:4b154134ab20 | 1008 | } |
Yo_Robot | 20:4b154134ab20 | 1009 | |
Yo_Robot | 20:4b154134ab20 | 1010 | EthernetNetIf eth; |
Yo_Robot | 20:4b154134ab20 | 1011 | return eth; |
Yo_Robot | 20:4b154134ab20 | 1012 | } |