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@16:d50665706f21, 2012-04-26 (annotated)
- Committer:
- Yo_Robot
- Date:
- Thu Apr 26 04:35:21 2012 +0000
- Revision:
- 16:d50665706f21
- Parent:
- 15:a1ffa32ce9d1
- Child:
- 17:a4f380bc2516
Version 1.3 Se cambio los pines de ALM, HTTP, baudios
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 4:552beeda4722 | 1 | /** |
Yo_Robot | 4:552beeda4722 | 2 | * @brief Tren de impulsos con Timer2 |
Yo_Robot | 4:552beeda4722 | 3 | * @file setup.cpp |
Yo_Robot | 4:552beeda4722 | 4 | * @author Ernesto Palacios |
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 | 3:8d5a9e3cd680 | 14 | #include "mbed.h" |
Yo_Robot | 3:8d5a9e3cd680 | 15 | |
Yo_Robot | 3:8d5a9e3cd680 | 16 | // Salida Serial de mbed |
Yo_Robot | 3:8d5a9e3cd680 | 17 | extern Serial pc; |
Yo_Robot | 12:c02b08dacc45 | 18 | extern DigitalOut pin_son; // SON |
Yo_Robot | 12:c02b08dacc45 | 19 | extern DigitalOut pin_dir; // SIGN+ |
Yo_Robot | 12:c02b08dacc45 | 20 | extern InterruptIn pin_alm; // ALM |
Yo_Robot | 12:c02b08dacc45 | 21 | extern AnalogOut aout; // +-10V |
Yo_Robot | 3:8d5a9e3cd680 | 22 | |
Yo_Robot | 15:a1ffa32ce9d1 | 23 | int fq_posicion = 10000; // Variable global donde se almacenara |
Yo_Robot | 15:a1ffa32ce9d1 | 24 | // la velocidad de posicionamiento en Hz |
Yo_Robot | 15:a1ffa32ce9d1 | 25 | |
Yo_Robot | 3:8d5a9e3cd680 | 26 | |
Yo_Robot | 4:552beeda4722 | 27 | void setTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 28 | { |
Yo_Robot | 3:8d5a9e3cd680 | 29 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 3:8d5a9e3cd680 | 30 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 3:8d5a9e3cd680 | 31 | |
Yo_Robot | 3:8d5a9e3cd680 | 32 | // Resetear y parar el Timer |
Yo_Robot | 3:8d5a9e3cd680 | 33 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 3:8d5a9e3cd680 | 34 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 3:8d5a9e3cd680 | 35 | |
Yo_Robot | 4:552beeda4722 | 36 | // Establecer el Preescaler en cero |
Yo_Robot | 4:552beeda4722 | 37 | // SIn Preesclaer |
Yo_Robot | 4:552beeda4722 | 38 | LPC_TIM2->PR = 0; |
Yo_Robot | 3:8d5a9e3cd680 | 39 | |
Yo_Robot | 4:552beeda4722 | 40 | // Calcular el periodo Inicial |
Yo_Robot | 4:552beeda4722 | 41 | uint32_t periodo = ( SystemCoreClock / 400 ); |
Yo_Robot | 3:8d5a9e3cd680 | 42 | |
Yo_Robot | 3:8d5a9e3cd680 | 43 | // Establecer los Registros de Coincidencia |
Yo_Robot | 3:8d5a9e3cd680 | 44 | // ( Match Register ) |
Yo_Robot | 3:8d5a9e3cd680 | 45 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 4:552beeda4722 | 46 | LPC_TIM2->MR3 = periodo; // Legacy, salidas identicas |
Yo_Robot | 3:8d5a9e3cd680 | 47 | |
Yo_Robot | 4:552beeda4722 | 48 | LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 |
Yo_Robot | 3:8d5a9e3cd680 | 49 | |
Yo_Robot | 3:8d5a9e3cd680 | 50 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 3:8d5a9e3cd680 | 51 | // y MAT2.3 |
Yo_Robot | 3:8d5a9e3cd680 | 52 | |
Yo_Robot | 3:8d5a9e3cd680 | 53 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 8:958dfe5052b9 | 54 | // y MAT2.3 como salidas |
Yo_Robot | 8:958dfe5052b9 | 55 | |
Yo_Robot | 6:b4dae934e1ea | 56 | } |
Yo_Robot | 6:b4dae934e1ea | 57 | |
Yo_Robot | 12:c02b08dacc45 | 58 | void ISR_Serial() |
Yo_Robot | 12:c02b08dacc45 | 59 | { |
Yo_Robot | 12:c02b08dacc45 | 60 | int value; // Nuevo Valor |
Yo_Robot | 12:c02b08dacc45 | 61 | char command; // Comando al que aplicar el nuevo valor |
Yo_Robot | 12:c02b08dacc45 | 62 | |
Yo_Robot | 12:c02b08dacc45 | 63 | pc.scanf( "%d-%c", &value, &command ) ; |
Yo_Robot | 14:039d070732d5 | 64 | //pc.printf("\n %d-%c \n", value, command ); |
Yo_Robot | 12:c02b08dacc45 | 65 | |
Yo_Robot | 12:c02b08dacc45 | 66 | // Establecer nueva frecuencia |
Yo_Robot | 12:c02b08dacc45 | 67 | if( command == 'H') |
Yo_Robot | 12:c02b08dacc45 | 68 | setPTO( value ); |
Yo_Robot | 12:c02b08dacc45 | 69 | |
Yo_Robot | 12:c02b08dacc45 | 70 | else if( command == 'K' ) |
Yo_Robot | 12:c02b08dacc45 | 71 | setPTO( value * 1000 ); |
Yo_Robot | 12:c02b08dacc45 | 72 | |
Yo_Robot | 12:c02b08dacc45 | 73 | // Nuevo voltaje de salida |
Yo_Robot | 12:c02b08dacc45 | 74 | // Alguna formula para calcular el Vout necesario |
Yo_Robot | 12:c02b08dacc45 | 75 | // -100% a +100% |
Yo_Robot | 12:c02b08dacc45 | 76 | else if( command == 'A') |
Yo_Robot | 14:039d070732d5 | 77 | aout = (float)( value + 10000.0 ) / 20000.0; |
Yo_Robot | 12:c02b08dacc45 | 78 | |
Yo_Robot | 12:c02b08dacc45 | 79 | |
Yo_Robot | 12:c02b08dacc45 | 80 | // Cambiar la direccion |
Yo_Robot | 12:c02b08dacc45 | 81 | else if( command == 'D') |
Yo_Robot | 14:039d070732d5 | 82 | { |
Yo_Robot | 14:039d070732d5 | 83 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 84 | pin_dir = value; |
Yo_Robot | 14:039d070732d5 | 85 | wait_us( 2 ); |
Yo_Robot | 14:039d070732d5 | 86 | startTimer2(); |
Yo_Robot | 14:039d070732d5 | 87 | } |
Yo_Robot | 14:039d070732d5 | 88 | |
Yo_Robot | 12:c02b08dacc45 | 89 | |
Yo_Robot | 12:c02b08dacc45 | 90 | //Encender el Servo |
Yo_Robot | 12:c02b08dacc45 | 91 | else if( command == 'S') |
Yo_Robot | 12:c02b08dacc45 | 92 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 93 | |
Yo_Robot | 12:c02b08dacc45 | 94 | //else if( command == 'E') |
Yo_Robot | 12:c02b08dacc45 | 95 | // setDir( value ); |
Yo_Robot | 12:c02b08dacc45 | 96 | |
Yo_Robot | 12:c02b08dacc45 | 97 | } |
Yo_Robot | 12:c02b08dacc45 | 98 | |
Yo_Robot | 12:c02b08dacc45 | 99 | |
Yo_Robot | 12:c02b08dacc45 | 100 | void setPTO( int freq ) |
Yo_Robot | 12:c02b08dacc45 | 101 | { |
Yo_Robot | 12:c02b08dacc45 | 102 | if( freq != 0 ) |
Yo_Robot | 12:c02b08dacc45 | 103 | { |
Yo_Robot | 12:c02b08dacc45 | 104 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 105 | setMR2( getMRvalue( freq ) ); |
Yo_Robot | 12:c02b08dacc45 | 106 | startTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 107 | |
Yo_Robot | 12:c02b08dacc45 | 108 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 109 | |
Yo_Robot | 12:c02b08dacc45 | 110 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 111 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 112 | } |
Yo_Robot | 12:c02b08dacc45 | 113 | } |
Yo_Robot | 12:c02b08dacc45 | 114 | |
Yo_Robot | 12:c02b08dacc45 | 115 | void setPTO_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 116 | { |
Yo_Robot | 12:c02b08dacc45 | 117 | int freq = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 118 | |
Yo_Robot | 12:c02b08dacc45 | 119 | if( freq != 0 ){ |
Yo_Robot | 12:c02b08dacc45 | 120 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 121 | setMR2( getMRvalue( freq ) ); // Cambiar frefuencia |
Yo_Robot | 12:c02b08dacc45 | 122 | startTimer2(); // Iniciar Timer |
Yo_Robot | 14:039d070732d5 | 123 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 124 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 125 | else |
Yo_Robot | 14:039d070732d5 | 126 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 127 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 128 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 129 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 14:039d070732d5 | 130 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 131 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 132 | else |
Yo_Robot | 14:039d070732d5 | 133 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 134 | } |
Yo_Robot | 12:c02b08dacc45 | 135 | } |
Yo_Robot | 12:c02b08dacc45 | 136 | |
Yo_Robot | 15:a1ffa32ce9d1 | 137 | void setANG_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 138 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 139 | long int pulsos = atol( input ); //Numero de pulsos a generar |
Yo_Robot | 15:a1ffa32ce9d1 | 140 | float t_alto = pulsos / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 15:a1ffa32ce9d1 | 141 | |
Yo_Robot | 15:a1ffa32ce9d1 | 142 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 143 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 15:a1ffa32ce9d1 | 144 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 145 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 15:a1ffa32ce9d1 | 146 | stopTimer2(); //Posición alcanzada ALTO. |
Yo_Robot | 15:a1ffa32ce9d1 | 147 | |
Yo_Robot | 15:a1ffa32ce9d1 | 148 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 149 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 150 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 151 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 152 | |
Yo_Robot | 15:a1ffa32ce9d1 | 153 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 154 | void setSPD_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 155 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 156 | fq_posicion = atoi( input ); |
Yo_Robot | 15:a1ffa32ce9d1 | 157 | // Esta funcion cambia la velocidad con la que se |
Yo_Robot | 15:a1ffa32ce9d1 | 158 | // posicionara el eje del motor en un angulo determinado |
Yo_Robot | 15:a1ffa32ce9d1 | 159 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 160 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 161 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 162 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 163 | |
Yo_Robot | 15:a1ffa32ce9d1 | 164 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 165 | void getENC_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 166 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 167 | //Leer el Encoder ******* F A L T A |
Yo_Robot | 15:a1ffa32ce9d1 | 168 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 169 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 170 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 171 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 172 | |
Yo_Robot | 15:a1ffa32ce9d1 | 173 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 174 | |
Yo_Robot | 12:c02b08dacc45 | 175 | |
Yo_Robot | 12:c02b08dacc45 | 176 | void ISR_Alarm() |
Yo_Robot | 12:c02b08dacc45 | 177 | { |
Yo_Robot | 12:c02b08dacc45 | 178 | pin_son = 0 ; |
Yo_Robot | 12:c02b08dacc45 | 179 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 180 | aout = 0.5 ; |
Yo_Robot | 12:c02b08dacc45 | 181 | |
Yo_Robot | 16:d50665706f21 | 182 | pc.printf( "AL" ); //ALARMA! solo es AL para que |
Yo_Robot | 16:d50665706f21 | 183 | //sea conciso con el modo ETH y funcione |
Yo_Robot | 16:d50665706f21 | 184 | //bien en LabVIEW. |
Yo_Robot | 12:c02b08dacc45 | 185 | } |
Yo_Robot | 6:b4dae934e1ea | 186 | |
Yo_Robot | 4:552beeda4722 | 187 | int getMRvalue( int fout ) |
Yo_Robot | 3:8d5a9e3cd680 | 188 | { |
Yo_Robot | 4:552beeda4722 | 189 | int toRegister; |
Yo_Robot | 4:552beeda4722 | 190 | |
Yo_Robot | 12:c02b08dacc45 | 191 | toRegister = (24000000 /(fout*2.0) ) -1; |
Yo_Robot | 4:552beeda4722 | 192 | return toRegister; |
Yo_Robot | 3:8d5a9e3cd680 | 193 | } |
Yo_Robot | 3:8d5a9e3cd680 | 194 | |
Yo_Robot | 3:8d5a9e3cd680 | 195 | |
Yo_Robot | 3:8d5a9e3cd680 | 196 | void setMR2( int newValue ) |
Yo_Robot | 3:8d5a9e3cd680 | 197 | { |
Yo_Robot | 4:552beeda4722 | 198 | LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas |
Yo_Robot | 4:552beeda4722 | 199 | LPC_TIM2->MR3 = newValue; // Para testear el programa. |
Yo_Robot | 3:8d5a9e3cd680 | 200 | } |
Yo_Robot | 3:8d5a9e3cd680 | 201 | |
Yo_Robot | 3:8d5a9e3cd680 | 202 | |
Yo_Robot | 3:8d5a9e3cd680 | 203 | |
Yo_Robot | 3:8d5a9e3cd680 | 204 | void startTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 205 | { |
Yo_Robot | 8:958dfe5052b9 | 206 | // Arrancer el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 207 | LPC_TIM2->TCR = 1; |
Yo_Robot | 3:8d5a9e3cd680 | 208 | } |
Yo_Robot | 3:8d5a9e3cd680 | 209 | |
Yo_Robot | 3:8d5a9e3cd680 | 210 | void stopTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 211 | { |
Yo_Robot | 4:552beeda4722 | 212 | // Detener el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 213 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 4:552beeda4722 | 214 | } |
Yo_Robot | 4:552beeda4722 | 215 | |
Yo_Robot | 12:c02b08dacc45 | 216 | |
Yo_Robot | 12:c02b08dacc45 | 217 | // **** Funciones Liberia Ethernet ***** // |
Yo_Robot | 12:c02b08dacc45 | 218 | |
Yo_Robot | 12:c02b08dacc45 | 219 | void setAout_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 220 | { |
Yo_Robot | 12:c02b08dacc45 | 221 | int vout = atoi( input ); |
Yo_Robot | 14:039d070732d5 | 222 | aout = (float)( vout + 10000 ) / 20000; |
Yo_Robot | 14:039d070732d5 | 223 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 224 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 225 | else |
Yo_Robot | 14:039d070732d5 | 226 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 227 | } |
Yo_Robot | 12:c02b08dacc45 | 228 | |
Yo_Robot | 12:c02b08dacc45 | 229 | |
Yo_Robot | 12:c02b08dacc45 | 230 | |
Yo_Robot | 12:c02b08dacc45 | 231 | void setDir_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 232 | { |
Yo_Robot | 12:c02b08dacc45 | 233 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 234 | |
Yo_Robot | 12:c02b08dacc45 | 235 | pin_dir = value; |
Yo_Robot | 12:c02b08dacc45 | 236 | |
Yo_Robot | 14:039d070732d5 | 237 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 238 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 239 | else |
Yo_Robot | 14:039d070732d5 | 240 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 241 | } |
Yo_Robot | 12:c02b08dacc45 | 242 | |
Yo_Robot | 12:c02b08dacc45 | 243 | |
Yo_Robot | 12:c02b08dacc45 | 244 | |
Yo_Robot | 12:c02b08dacc45 | 245 | void setSON_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 246 | { |
Yo_Robot | 12:c02b08dacc45 | 247 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 248 | |
Yo_Robot | 12:c02b08dacc45 | 249 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 250 | |
Yo_Robot | 14:039d070732d5 | 251 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 252 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 253 | else |
Yo_Robot | 14:039d070732d5 | 254 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 255 | |
Yo_Robot | 12:c02b08dacc45 | 256 | } |
Yo_Robot | 12:c02b08dacc45 | 257 | |
Yo_Robot | 12:c02b08dacc45 | 258 | |
Yo_Robot | 12:c02b08dacc45 | 259 | |
Yo_Robot | 4:552beeda4722 | 260 | /* LEGACY FUNCTIONS |
Yo_Robot | 4:552beeda4722 | 261 | * |
Yo_Robot | 5:c5aea1eb10bb | 262 | * El codigo actual no hace referencia a estas funciones |
Yo_Robot | 5:c5aea1eb10bb | 263 | * sin embargo no hay problema en definirlas. |
Yo_Robot | 4:552beeda4722 | 264 | */ |
Yo_Robot | 4:552beeda4722 | 265 | void setMR3( int newValue ) |
Yo_Robot | 4:552beeda4722 | 266 | { |
Yo_Robot | 4:552beeda4722 | 267 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 4:552beeda4722 | 268 | } |
Yo_Robot | 4:552beeda4722 | 269 | |
Yo_Robot | 4:552beeda4722 | 270 | |
Yo_Robot | 4:552beeda4722 | 271 | void setPrescaler( int newValue) |
Yo_Robot | 4:552beeda4722 | 272 | { |
Yo_Robot | 4:552beeda4722 | 273 | LPC_TIM2->PR = newValue; |
Yo_Robot | 4:552beeda4722 | 274 | } |