@@ -1,1172 +1,1172 | |||
|
1 | 1 | /* No newline at end of file |
|
2 | 2 | * Servidor.c No newline at end of file |
|
3 | 3 | * No newline at end of file |
|
4 | 4 | * Created on: Nov 3, 2009 No newline at end of file |
|
5 | 5 | * Author: Jose Francisco Quenta No newline at end of file |
|
6 | 6 | * No newline at end of file |
|
7 | 7 | * Se implementa: No newline at end of file |
|
8 | 8 | * -Carga en memoria los apuntes contenidos en un archivo de experimentos: apunte0 -> GPIO No newline at end of file |
|
9 | 9 | * -Cambio de apunte. No newline at end of file |
|
10 | 10 | * -Lectura del estado actual del apunte y grabado del mismo en un archivo No newline at end of file |
|
11 | 11 | * No newline at end of file |
|
12 | 12 | * Modified by Iván Manay since Nov 2012 No newline at end of file |
|
13 | 13 | * -From UDP to TCP. No newline at end of file |
|
14 | 14 | * -Use of a frame for TCP communications with the central control module. No newline at end of file |
|
15 | 15 | */ No newline at end of file |
|
16 | 16 | No newline at end of file |
|
17 | 17 | #include <stdio.h> No newline at end of file |
|
18 | 18 | #include <stdlib.h> No newline at end of file |
|
19 | 19 | #include <string.h> No newline at end of file |
|
20 | 20 | #include <unistd.h> No newline at end of file |
|
21 | 21 | #include <errno.h> No newline at end of file |
|
22 | 22 | No newline at end of file |
|
23 | 23 | #include <sys/types.h> No newline at end of file |
|
24 | 24 | #include <sys/socket.h> No newline at end of file |
|
25 | 25 | #include <netinet/in.h> No newline at end of file |
|
26 | 26 | #include <arpa/inet.h> No newline at end of file |
|
27 | 27 | #include <netdb.h> No newline at end of file |
|
28 | 28 | #include <time.h> No newline at end of file |
|
29 | 29 | #include <math.h> No newline at end of file |
|
30 | 30 | No newline at end of file |
|
31 | 31 | #include "./Librerias/at91gpio.h" No newline at end of file |
|
32 | 32 | //#include "./Librerias/Mensajes.h" No newline at end of file |
|
33 | 33 | #include "./Librerias/at91adc.h" No newline at end of file |
|
34 | 34 | //clock No newline at end of file |
|
35 | 35 | #include "./Librerias/at91sysclock.h" No newline at end of file |
|
36 | 36 | No newline at end of file |
|
37 | 37 | #define PUERTO_SERVIDOR 5500 No newline at end of file |
|
38 | 38 | #define TAM_BUFFER 1024 No newline at end of file |
|
39 | 39 | No newline at end of file |
|
40 | 40 | #define maskc_out_beam PC30+PC28+PC26+PC24+PC22+PC20 //MSB-UP-LSB MSB-DOWN-LSB //APUNTE No newline at end of file |
|
41 | 41 | #define maskc_out_cntl PC4+PC5+PC6 //MONITORING CONTROL No newline at end of file |
|
42 | 42 | No newline at end of file |
|
43 | 43 | #define mask_sel_canal PC4 //MONITORING CONTROL No newline at end of file |
|
44 | 44 | #define mask_sel_atenuacion PC5 No newline at end of file |
|
45 | 45 | #define mask_sel_calibracion PC6 No newline at end of file |
|
46 | 46 | No newline at end of file |
|
47 | 47 | #define maskb_in PB16+PB18+PB20+PB30+PB24+PB22 //MSB-UP-LSB MSB-DOWN-LSB //VERIFICACION No newline at end of file |
|
48 | 48 | No newline at end of file |
|
49 | 49 | #define maskc_in_adc PC0+PC1 No newline at end of file |
|
50 | 50 | No newline at end of file |
|
51 | 51 | #define bit_up_2 0x00010000 //Mascara de cada bit a revisar: bit_up_2 es MSB No newline at end of file |
|
52 | 52 | #define bit_up_1 0x00040000 No newline at end of file |
|
53 | 53 | #define bit_up_0 0x00100000 No newline at end of file |
|
54 | 54 | #define bit_dow_2 0x40000000 No newline at end of file |
|
55 | 55 | #define bit_dow_1 0x01000000 No newline at end of file |
|
56 | 56 | #define bit_dow_0 0x00400000 No newline at end of file |
|
57 | 57 | No newline at end of file |
|
58 | 58 | #define MyID 11 No newline at end of file |
|
59 | 59 | #define MAXPENDING 5 /* Maximum outstanding connection requests */ No newline at end of file |
|
60 | 60 | No newline at end of file |
|
61 | 61 | //parameters for the name of the output file No newline at end of file |
|
62 | 62 | #define FPRE "AD" //prefix for the output file name No newline at end of file |
|
63 | 63 | #define FEXT ".out" //file extension for the output file No newline at end of file |
|
64 | 64 | #define FNAMELEN 40 No newline at end of file |
|
65 | 65 | No newline at end of file |
|
66 | 66 | //ADC parameters No newline at end of file |
|
67 | 67 | #define REP 1 //defines how many times the data acquisation loop is repeated No newline at end of file |
|
68 | 68 | #define NSAMPLES 10 //defines how many samples are taken in one data acqu- No newline at end of file |
|
69 | 69 | // isation loop No newline at end of file |
|
70 | 70 | No newline at end of file |
|
71 | 71 | #define NLOOPS 200 // Number of loops for the transmisor checking No newline at end of file |
|
72 | 72 | #define CNVTIME 14.3 //defines how long it takes to get one sample. Value No newline at end of file |
|
73 | 73 | // is only needed for the output file, doesn't change No newline at end of file |
|
74 | 74 | // any ADC configurations No newline at end of file |
|
75 | 75 | #define UREF 3.3 //Reference Voltage of ADC (max. ADC Voltage) No newline at end of file |
|
76 | 76 | #define ADCRES 1023 //Resolution of ADC (10bit=1023) No newline at end of file |
|
77 | 77 | No newline at end of file |
|
78 | 78 | No newline at end of file |
|
79 | 79 | char *buff_experimento= NULL; No newline at end of file |
|
80 | 80 | No newline at end of file |
|
81 | 81 | AT91S_PIO *pioc; No newline at end of file |
|
82 | 82 | AT91S_PIO *piob; No newline at end of file |
|
83 | 83 | AT91S_PIO *pio_adc; No newline at end of file |
|
84 | 84 | AT91S_ADC *padc; No newline at end of file |
|
85 | 85 | AT91S_ADC *padd; No newline at end of file |
|
86 | 86 | No newline at end of file |
|
87 | 87 | struct control_module_parameters { No newline at end of file |
|
88 | 88 | char ID[20]; No newline at end of file |
|
89 | 89 | char param2[20]; No newline at end of file |
|
90 | 90 | char param3[20]; No newline at end of file |
|
91 | 91 | char param4[20]; No newline at end of file |
|
92 | 92 | }; No newline at end of file |
|
93 | 93 | No newline at end of file |
|
94 | 94 | typedef struct control_module_parameters cmp; No newline at end of file |
|
95 | 95 | No newline at end of file |
|
96 | 96 | char *header = NULL; No newline at end of file |
|
97 | 97 | char *TypeOfInstrument = NULL; No newline at end of file |
|
98 | 98 | char *iDSource = NULL; No newline at end of file |
|
99 | 99 | char *iDDestino = NULL; No newline at end of file |
|
100 | 100 | char *rx_len = NULL; No newline at end of file |
|
101 | 101 | char *cmd = NULL; No newline at end of file |
|
102 | 102 | char *rx_data = NULL; No newline at end of file |
|
103 | 103 | char *crc = NULL; No newline at end of file |
|
104 | 104 | No newline at end of file |
|
105 | 105 | cmp parameters; No newline at end of file |
|
106 | 106 | /* No newline at end of file |
|
107 | 107 | * Zona de declaracion de cabeceras. No newline at end of file |
|
108 | 108 | */ No newline at end of file |
|
109 | 109 | cmp inicializa_modulo(cmp p, char *filename); No newline at end of file |
|
110 | 110 | int inicializa_ethernet(); No newline at end of file |
|
111 | 111 | int rxData(int, char*); No newline at end of file |
|
112 | 112 | void txData(int, char*); No newline at end of file |
|
113 | 113 | void inicializa_gpio(); No newline at end of file |
|
114 | 114 | void inicializa_adc(); No newline at end of file |
|
115 | 115 | void procesa_peticion(char *rx_buffer, char *tx_buffer); No newline at end of file |
|
116 | 116 | int cambia_apuntamiento(char *puntero_char); No newline at end of file |
|
117 | 117 | int carga_experimento(char *nombre_archivo); No newline at end of file |
|
118 | 118 | char *chequeo_sistema(char *filename, char *numero_muestras); No newline at end of file |
|
119 | 119 | void escribe_experimento(char *data, char filename[]); No newline at end of file |
|
120 | 120 | char *Lee_experimento(char *filename); No newline at end of file |
|
121 | 121 | void consigue_nombre_experimento(char *rx_data, char filename[]); No newline at end of file |
|
122 | 122 | void SplitFrame(char *frame); No newline at end of file |
|
123 | 123 | void intToStr( int number, char* str ); No newline at end of file |
|
124 | 124 | No newline at end of file |
|
125 | 125 | //ABS monitoring No newline at end of file |
|
126 | 126 | //int ABS_monitoreo(int sel_atenuador, int sel_calibracion, float umbral, int pulsewidth); No newline at end of file |
|
127 | 127 | char* ABS_monitoreo(int sel_atenuador, int sel_calibracion, float umbral, int pulsewidth); No newline at end of file |
|
128 | 128 | AT91S_ADC * configADC1(void); No newline at end of file |
|
129 | 129 | AT91S_ADC * configADC2(void); No newline at end of file |
|
130 | 130 | No newline at end of file |
|
131 | 131 | FILE * create_Output(char*, time_t); No newline at end of file |
|
132 | 132 | No newline at end of file |
|
133 | 133 | void writeOutput(float resultado, FILE * output); No newline at end of file |
|
134 | 134 | No newline at end of file |
|
135 | 135 | int checkTx(long int results1[],long int results2[], float umbral, int pulsewidth); No newline at end of file |
|
136 | 136 | No newline at end of file |
|
137 | 137 | double mediana(long int *results, unsigned int cuenta); No newline at end of file |
|
138 | 138 | float getPhase(long int results1[], long int results2[]); No newline at end of file |
|
139 | 139 | No newline at end of file |
|
140 | 140 | int fExists(char *); No newline at end of file |
|
141 | 141 | int configCLK(); No newline at end of file |
|
142 | 142 | // No newline at end of file |
|
143 | 143 | No newline at end of file |
|
144 | 144 | /* No newline at end of file |
|
145 | 145 | * No newline at end of file |
|
146 | 146 | */ No newline at end of file |
|
147 | 147 | int main(int argc, char *argv[]){ No newline at end of file |
|
148 | 148 | // arg[1] : Archivo de configuracion del modulo de control No newline at end of file |
|
149 | 149 | int servSocket; No newline at end of file |
|
150 | 150 | int clntSocket; No newline at end of file |
|
151 | 151 | No newline at end of file |
|
152 | 152 | char *rx_buffer = (char *) malloc(TAM_BUFFER); No newline at end of file |
|
153 | 153 | char *tx_buffer = (char *) malloc(TAM_BUFFER); No newline at end of file |
|
154 | 154 | /* Inicializa parametros del modulo*/ No newline at end of file |
|
155 | 155 | parameters = inicializa_modulo(parameters, argv[1]); No newline at end of file |
|
156 | 156 | printf("%s\n%s\n%s\n%s\n",parameters.ID, parameters.param2, parameters.param3, parameters.param4); No newline at end of file |
|
157 | 157 | /* Inicializa red*/ No newline at end of file |
|
158 | 158 | servSocket = inicializa_ethernet(); No newline at end of file |
|
159 | 159 | /* Inicializamos el puerto GPIO del sistema embebido GSBC-9260S */ No newline at end of file |
|
160 | 160 | inicializa_gpio(); No newline at end of file |
|
161 | 161 | inicializa_adc(); No newline at end of file |
|
162 | 162 | No newline at end of file |
|
163 | 163 | while(1){ No newline at end of file |
|
164 | 164 | // Recepción TCP de petición No newline at end of file |
|
165 | 165 | clntSocket = rxData(servSocket, rx_buffer); No newline at end of file |
|
166 | 166 | //testpoint No newline at end of file |
|
167 | 167 | printf("rx:%s\n",rx_buffer); No newline at end of file |
|
168 | 168 | // Procesamiento de la petición No newline at end of file |
|
169 | 169 | procesa_peticion(rx_buffer, tx_buffer); No newline at end of file |
|
170 | 170 | //testpoint No newline at end of file |
|
171 | 171 | printf("tx:%s\n",tx_buffer); No newline at end of file |
|
172 | 172 | // Respuesta del modulo de control No newline at end of file |
|
173 | 173 | txData(clntSocket, tx_buffer); No newline at end of file |
|
174 | 174 | No newline at end of file |
|
175 | 175 | } No newline at end of file |
|
176 | 176 | } No newline at end of file |
|
177 | 177 | No newline at end of file |
|
178 | 178 | No newline at end of file |
|
179 | 179 | int inicializa_ethernet(){ No newline at end of file |
|
180 | 180 | No newline at end of file |
|
181 | 181 | struct sockaddr_in inf_servidor; No newline at end of file |
|
182 | 182 | No newline at end of file |
|
183 | 183 | int servSocket; No newline at end of file |
|
184 | 184 | No newline at end of file |
|
185 | 185 | int resultado; No newline at end of file |
|
186 | 186 | No newline at end of file |
|
187 | 187 | /* Haciendo la estructura local*/ No newline at end of file |
|
188 | 188 | memset(&inf_servidor, 0, sizeof(inf_servidor)); No newline at end of file |
|
189 | 189 | inf_servidor.sin_family= AF_INET; No newline at end of file |
|
190 | 190 | inf_servidor.sin_port= htons(PUERTO_SERVIDOR); No newline at end of file |
|
191 | 191 | inf_servidor.sin_addr.s_addr= INADDR_ANY; No newline at end of file |
|
192 | 192 | No newline at end of file |
|
193 | 193 | /* Se establece el socket */ No newline at end of file |
|
194 | 194 | servSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP); No newline at end of file |
|
195 | 195 | if (servSocket == -1){ No newline at end of file |
|
196 | 196 | printf("No se establecio correctamente el socket: socket()\n"); No newline at end of file |
|
197 | 197 | //ERROR_FATAL("No se establecio correctamente el socket: socket()\n"); No newline at end of file |
|
198 | 198 | exit(-1); No newline at end of file |
|
199 | 199 | } No newline at end of file |
|
200 | 200 | No newline at end of file |
|
201 | 201 | /* Se asocia el socket a un puerto y una IP */ No newline at end of file |
|
202 | 202 | resultado = bind(servSocket,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor)); No newline at end of file |
|
203 | 203 | if (resultado== -1){ No newline at end of file |
|
204 | 204 | printf("No se establecio correctamente el socket: bind()\n"); No newline at end of file |
|
205 | 205 | //ERROR_FATAL("No se establecio correctamente el socket: bind()\n"); No newline at end of file |
|
206 | 206 | exit(-1); No newline at end of file |
|
207 | 207 | } No newline at end of file |
|
208 | 208 | No newline at end of file |
|
209 | 209 | if (listen(servSocket, MAXPENDING) < 0){ No newline at end of file |
|
210 | 210 | printf("listen() failed\n"); No newline at end of file |
|
211 | 211 | exit(-1); No newline at end of file |
|
212 | 212 | } No newline at end of file |
|
213 | 213 | No newline at end of file |
|
214 | 214 | return servSocket; No newline at end of file |
|
215 | 215 | No newline at end of file |
|
216 | 216 | } No newline at end of file |
|
217 | 217 | No newline at end of file |
|
218 | 218 | int rxData(int servSocket, char* rx_buffer){ No newline at end of file |
|
219 | 219 | No newline at end of file |
|
220 | 220 | int clntSocket; No newline at end of file |
|
221 | 221 | struct sockaddr_in inf_cliente; No newline at end of file |
|
222 | 222 | int numbytes_recibidos; No newline at end of file |
|
223 | 223 | unsigned int inf_client_Len; No newline at end of file |
|
224 | 224 | No newline at end of file |
|
225 | 225 | printf("\nEsperando solicitud de cliente...\n"); No newline at end of file |
|
226 | 226 | No newline at end of file |
|
227 | 227 | /* Set the size of the in-out parameter */ No newline at end of file |
|
228 | 228 | inf_client_Len = sizeof(inf_cliente); No newline at end of file |
|
229 | 229 | /* Se espera hasta que un cliente se conecte */ No newline at end of file |
|
230 | 230 | if ((clntSocket = accept(servSocket, (struct sockaddr *) &inf_cliente, No newline at end of file |
|
231 | 231 | &inf_client_Len)) < 0) No newline at end of file |
|
232 | 232 | printf("accept() failed\n"); No newline at end of file |
|
233 | 233 | No newline at end of file |
|
234 | 234 | if ((numbytes_recibidos = recv(clntSocket, rx_buffer, TAM_BUFFER, 0)) < 0) No newline at end of file |
|
235 | 235 | printf("recv() failed\n"); No newline at end of file |
|
236 | 236 | No newline at end of file |
|
237 | 237 | /* Se procede a procesar los datos recibidos */ No newline at end of file |
|
238 | 238 | rx_buffer[numbytes_recibidos]= '\0'; No newline at end of file |
|
239 | 239 | No newline at end of file |
|
240 | 240 | return clntSocket; No newline at end of file |
|
241 | 241 | } No newline at end of file |
|
242 | 242 | No newline at end of file |
|
243 | 243 | void txData(int clntSocket, char* data){ No newline at end of file |
|
244 | 244 | No newline at end of file |
|
245 | 245 | /* Echo message back to client */ No newline at end of file |
|
246 | 246 | if (send(clntSocket, data, strlen(data), 0) != strlen(data)) No newline at end of file |
|
247 | 247 | printf("send() failed\n"); No newline at end of file |
|
248 | 248 | No newline at end of file |
|
249 | 249 | close(clntSocket); /* Close client socket */ No newline at end of file |
|
250 | 250 | } No newline at end of file |
|
251 | 251 | No newline at end of file |
|
252 | 252 | /* No newline at end of file |
|
253 | 253 | * Esta funcion incializa el puerto GPIO No newline at end of file |
|
254 | 254 | */ No newline at end of file |
|
255 | 255 | void inicializa_gpio(){ No newline at end of file |
|
256 | 256 | No newline at end of file |
|
257 | 257 | int desplazamiento_beam[6]={30,28,26,24,22,20}; //Desplazamientos para los pines de apunte No newline at end of file |
|
258 | 258 | int desplazamiento_cntl[3]={4,5,6}; //Desplazamientos para los pines de control de monitoreo No newline at end of file |
|
259 | 259 | unsigned long acumulado_ceros=0; No newline at end of file |
|
260 | 260 | int i; No newline at end of file |
|
261 | 261 | No newline at end of file |
|
262 | 262 | // Configuracion de los pines de APUNTE y control de monitoreo No newline at end of file |
|
263 | 263 | pioc = pio_map(PIOC_BASE); No newline at end of file |
|
264 | 264 | pio_enable(pioc, maskc_out_beam + maskc_out_cntl); No newline at end of file |
|
265 | 265 | pio_disable_irq(pioc, maskc_out_beam + maskc_out_cntl); No newline at end of file |
|
266 | 266 | pio_disable_multiple_driver(pioc, maskc_out_beam + maskc_out_cntl); No newline at end of file |
|
267 | 267 | pio_disable_pull_ups(pioc, maskc_out_beam + maskc_out_cntl); No newline at end of file |
|
268 | 268 | pio_synchronous_data_output(pioc, maskc_out_beam + maskc_out_cntl); No newline at end of file |
|
269 | 269 | pio_output_enable(pioc, maskc_out_beam + maskc_out_cntl); No newline at end of file |
|
270 | 270 | No newline at end of file |
|
271 | 271 | // Configuracion de los pines de VERIFICACION No newline at end of file |
|
272 | 272 | piob = pio_map(PIOB_BASE); No newline at end of file |
|
273 | 273 | pio_enable(piob, maskb_in); No newline at end of file |
|
274 | 274 | pio_disable_irq(piob, maskb_in); No newline at end of file |
|
275 | 275 | pio_disable_multiple_driver(piob, maskb_in); No newline at end of file |
|
276 | 276 | pio_disable_pull_ups(piob, maskb_in); No newline at end of file |
|
277 | 277 | pio_input_enable(piob, maskb_in); No newline at end of file |
|
278 | 278 | No newline at end of file |
|
279 | 279 | //Pînes de ADC No newline at end of file |
|
280 | 280 | pio_adc = pio_map(PIOC_BASE); No newline at end of file |
|
281 | 281 | pin_adc_enable(pio_adc,maskc_in_adc); //Habilitamos PC0 para usar con ADC0 y 1 No newline at end of file |
|
282 | 282 | pio_disable_irq(pio_adc, maskc_in_adc); No newline at end of file |
|
283 | 283 | pio_disable_multiple_driver(pio_adc, maskc_in_adc); No newline at end of file |
|
284 | 284 | pio_disable_pull_ups(pio_adc, maskc_in_adc); No newline at end of file |
|
285 | 285 | pio_input_enable(pio_adc, maskc_in_adc); No newline at end of file |
|
286 | 286 | No newline at end of file |
|
287 | 287 | //Inicializando a cero los pines de apunte No newline at end of file |
|
288 | 288 | for(i=0;i<6;i++) No newline at end of file |
|
289 | 289 | acumulado_ceros= acumulado_ceros + (1 << desplazamiento_beam[i]); No newline at end of file |
|
290 | 290 | pio_out(pioc, maskc_out_beam, acumulado_ceros, 0); No newline at end of file |
|
291 | 291 | No newline at end of file |
|
292 | 292 | //Inicializando a cero los pines de control de monitoreo: atenuacion, calibracion y canal No newline at end of file |
|
293 | 293 | for(i=0;i<3;i++) No newline at end of file |
|
294 | 294 | acumulado_ceros= acumulado_ceros + (1 << desplazamiento_cntl[i]); No newline at end of file |
|
295 | 295 | pio_out(pioc, maskc_out_cntl, acumulado_ceros, 0); No newline at end of file |
|
296 | 296 | No newline at end of file |
|
297 | 297 | } No newline at end of file |
|
298 | 298 | No newline at end of file |
|
299 | 299 | void inicializa_adc(){ No newline at end of file |
|
300 | 300 | No newline at end of file |
|
301 | 301 | if (configCLK() == 1) No newline at end of file |
|
302 | 302 | printf("clock ADC enable.\n"); No newline at end of file |
|
303 | 303 | //configure ADC Settings No newline at end of file |
|
304 | 304 | padc=configADC1(); No newline at end of file |
|
305 | 305 | padd=configADC2(); No newline at end of file |
|
306 | 306 | } No newline at end of file |
|
307 | 307 | No newline at end of file |
|
308 | 308 | No newline at end of file |
|
309 | 309 | /* No newline at end of file |
|
310 | 310 | * Divide rx frame into the frame components No newline at end of file |
|
311 | 311 | */ No newline at end of file |
|
312 | 312 | void SplitFrame(char *frame){ No newline at end of file |
|
313 | 313 | header = malloc(4); No newline at end of file |
|
314 | 314 | *header = *frame; No newline at end of file |
|
315 | 315 | *(header + 1) = *(frame + 1); No newline at end of file |
|
316 | 316 | *(header + 2) = *(frame + 2); No newline at end of file |
|
317 | 317 | *(header + 3) = '\0'; No newline at end of file |
|
318 | 318 | TypeOfInstrument = malloc(4); No newline at end of file |
|
319 | 319 | *TypeOfInstrument = *(frame + 3); No newline at end of file |
|
320 | 320 | *(TypeOfInstrument + 1) = *(frame + 4); No newline at end of file |
|
321 | 321 | *(TypeOfInstrument + 2) = *(frame + 5); No newline at end of file |
|
322 | 322 | *(TypeOfInstrument + 3) = '\0'; No newline at end of file |
|
323 | 323 | iDSource = malloc(8); No newline at end of file |
|
324 | 324 | *iDSource = *(frame + 6); No newline at end of file |
|
325 | 325 | *(iDSource + 1) = *(frame + 7); No newline at end of file |
|
326 | 326 | *(iDSource + 2) = *(frame + 8); No newline at end of file |
|
327 | 327 | *(iDSource + 3) = *(frame + 9); No newline at end of file |
|
328 | 328 | *(iDSource + 4) = *(frame + 10); No newline at end of file |
|
329 | 329 | *(iDSource + 5) = *(frame + 11); No newline at end of file |
|
330 | 330 | *(iDSource + 6) = *(frame + 12); No newline at end of file |
|
331 | 331 | *(iDSource + 7) = '\0'; No newline at end of file |
|
332 | 332 | iDDestino = malloc(8); No newline at end of file |
|
333 | 333 | *iDDestino = *(frame + 13); No newline at end of file |
|
334 | 334 | *(iDDestino + 1) = *(frame + 14); No newline at end of file |
|
335 | 335 | *(iDDestino + 2) = *(frame + 15); No newline at end of file |
|
336 | 336 | *(iDDestino + 3) = *(frame + 16); No newline at end of file |
|
337 | 337 | *(iDDestino + 4) = *(frame + 17); No newline at end of file |
|
338 | 338 | *(iDDestino + 5) = *(frame + 18); No newline at end of file |
|
339 | 339 | *(iDDestino + 6) = *(frame + 19); No newline at end of file |
|
340 | 340 | *(iDDestino + 7) = '\0'; No newline at end of file |
|
341 | 341 | rx_len = malloc(7); No newline at end of file |
|
342 | 342 | *rx_len = *(frame + 20); No newline at end of file |
|
343 | 343 | *(rx_len + 1) = *(frame + 21); No newline at end of file |
|
344 | 344 | *(rx_len + 2) = *(frame + 22); No newline at end of file |
|
345 | 345 | *(rx_len + 3) = *(frame + 23); No newline at end of file |
|
346 | 346 | *(rx_len + 4) = *(frame + 24); No newline at end of file |
|
347 | 347 | *(rx_len + 5) = *(frame + 25); No newline at end of file |
|
348 | 348 | *(rx_len + 6) = '\0'; No newline at end of file |
|
349 | 349 | cmd = malloc(5); No newline at end of file |
|
350 | 350 | *cmd = *(frame + 26); No newline at end of file |
|
351 | 351 | *(cmd + 1) = *(frame + 27); No newline at end of file |
|
352 | 352 | *(cmd + 2) = *(frame + 28); No newline at end of file |
|
353 | 353 | *(cmd + 3) = *(frame + 29); No newline at end of file |
|
354 | 354 | *(cmd + 4) = '\0'; No newline at end of file |
|
355 | 355 | No newline at end of file |
|
356 | 356 | int l = atoi(rx_len); No newline at end of file |
|
357 | 357 | rx_data = malloc(l + 1); No newline at end of file |
|
358 | 358 | int i; No newline at end of file |
|
359 | 359 | for (i = 30; i < 30 + l; i++) No newline at end of file |
|
360 | 360 | *(rx_data + (i-30)) = *(frame + i); No newline at end of file |
|
361 | 361 | *(rx_data + l) = '\0'; No newline at end of file |
|
362 | 362 | crc = malloc(2); No newline at end of file |
|
363 | 363 | *crc = *(frame + 30 + l); No newline at end of file |
|
364 | 364 | *(crc + 1) = '\0'; No newline at end of file |
|
365 | 365 | } No newline at end of file |
|
366 | 366 | No newline at end of file |
|
367 | 367 | /* No newline at end of file |
|
368 | 368 | * Esta funcion procesa el mensaje de peticion y genera respuesta No newline at end of file |
|
369 | 369 | */ No newline at end of file |
|
370 | 370 | void procesa_peticion(char *rx_buffer, char *tx_buffer){ No newline at end of file |
|
371 | 371 | // int n = 0; No newline at end of file |
|
372 | 372 | char filename1[50]; No newline at end of file |
|
373 | 373 | char filename2[] = "verificacion.txt"; No newline at end of file |
|
374 | 374 | char *tx_data = NULL; No newline at end of file |
|
375 | 375 | char *tx_len = NULL; No newline at end of file |
|
376 | 376 | SplitFrame(rx_buffer); No newline at end of file |
|
377 | 377 | No newline at end of file |
|
378 | 378 | if ((cmd == NULL) || (rx_data == NULL)){ No newline at end of file |
|
379 | 379 | printf("procesarPeticion: formato de mensaje incorrecto"); No newline at end of file |
|
380 | 380 | //ERROR("procesarPeticion: formato de mensaje incorrecto"); No newline at end of file |
|
381 | 381 | No newline at end of file |
|
382 | 382 | } No newline at end of file |
|
383 | 383 | else{ No newline at end of file |
|
384 | 384 | if(strcmp(cmd,"SNDF") == 0){ No newline at end of file |
|
385 | 385 | escribe_experimento(rx_data,filename1); No newline at end of file |
|
386 | 386 | carga_experimento(filename1); |
|
387 | No newline at end of file | |
|
387 | //cambia_apuntamiento("0"); No newline at end of file | |
|
388 | 388 | tx_data = (char*)malloc(3); No newline at end of file |
|
389 | 389 | tx_data = "OK"; No newline at end of file |
|
390 | 390 | } No newline at end of file |
|
391 | 391 | else if(strcmp(cmd,"GETF") == 0){ No newline at end of file |
|
392 | 392 | consigue_nombre_experimento(rx_data,filename1); //get a filename from frame No newline at end of file |
|
393 | 393 | tx_data = Lee_experimento(filename1); //return a pointer to the content of the filename No newline at end of file |
|
394 | 394 | printf("%s\n",tx_data); No newline at end of file |
|
395 | 395 | } No newline at end of file |
|
396 | 396 | else if(strcmp(cmd,"CHGB") == 0){ No newline at end of file |
|
397 | 397 | cambia_apuntamiento(rx_data); No newline at end of file |
|
398 | 398 | tx_data = (char*)malloc(3); No newline at end of file |
|
399 | 399 | tx_data = "OK"; No newline at end of file |
|
400 | 400 | } No newline at end of file |
|
401 | 401 | else if(strcmp(cmd,"ANST") == 0){ No newline at end of file |
|
402 | 402 | tx_data = chequeo_sistema(filename2,rx_data); No newline at end of file |
|
403 | 403 | printf("%s\n",tx_data); No newline at end of file |
|
404 | 404 | } No newline at end of file |
|
405 | 405 | else if(strcmp(cmd,"BGPH") == 0){ No newline at end of file |
|
406 | 406 | tx_data = ABS_monitoreo(1, 0, 50, 10); No newline at end of file |
|
407 | 407 | // tx_data = "Not implemented\n"; No newline at end of file |
|
408 | 408 | printf("%s\n",tx_data); No newline at end of file |
|
409 | 409 | } No newline at end of file |
|
410 | 410 | else if(strcmp(cmd,"LWPH") == 0){ No newline at end of file |
|
411 | 411 | tx_data = ABS_monitoreo(0, 0, 50, 10); No newline at end of file |
|
412 | 412 | // tx_data = "Not implemented\n"; No newline at end of file |
|
413 | 413 | printf("%s\n",tx_data); No newline at end of file |
|
414 | 414 | } No newline at end of file |
|
415 | 415 | else if(strcmp(cmd,"NTST") == 0){ No newline at end of file |
|
416 | 416 | tx_data = malloc(strlen(parameters.ID) + 1); No newline at end of file |
|
417 | 417 | strcpy(tx_data,parameters.ID); No newline at end of file |
|
418 | 418 | printf("%s\n",tx_data); No newline at end of file |
|
419 | 419 | } No newline at end of file |
|
420 | 420 | else{ No newline at end of file |
|
421 | 421 | tx_data = (char*)malloc(6); No newline at end of file |
|
422 | 422 | tx_data = "Error"; No newline at end of file |
|
423 | 423 | printf("procesa_peticion: comando no reconocido"); No newline at end of file |
|
424 | 424 | //ERROR("procesa_peticion: comando no reconocido"); No newline at end of file |
|
425 | 425 | } No newline at end of file |
|
426 | 426 | No newline at end of file |
|
427 | 427 | tx_len = malloc(7); No newline at end of file |
|
428 | 428 | int number = strlen(tx_data); No newline at end of file |
|
429 | 429 | intToStr(number, tx_len ); No newline at end of file |
|
430 | 430 | No newline at end of file |
|
431 | 431 | strcpy(tx_buffer,header); //3 No newline at end of file |
|
432 | 432 | strcat(tx_buffer,TypeOfInstrument); //3 No newline at end of file |
|
433 | 433 | strcat(tx_buffer,parameters.ID); //7 No newline at end of file |
|
434 | 434 | strcat(tx_buffer,iDSource); //7 No newline at end of file |
|
435 | 435 | strcat(tx_buffer,tx_len); //6 No newline at end of file |
|
436 | 436 | strcat(tx_buffer,cmd); //4 No newline at end of file |
|
437 | 437 | strcat(tx_buffer,tx_data); //? No newline at end of file |
|
438 | 438 | strcat(tx_buffer,crc); //1 No newline at end of file |
|
439 | 439 | No newline at end of file |
|
440 | 440 | } No newline at end of file |
|
441 | 441 | No newline at end of file |
|
442 | 442 | } No newline at end of file |
|
443 | 443 | No newline at end of file |
|
444 | 444 | No newline at end of file |
|
445 | 445 | char* Lee_experimento(char filename[]){ No newline at end of file |
|
446 | 446 | char *buffer = NULL; No newline at end of file |
|
447 | 447 | FILE * fd = fopen(filename,"r"); No newline at end of file |
|
448 | 448 | fseek(fd, 0, SEEK_END); No newline at end of file |
|
449 | 449 | size_t sz = ftell(fd); No newline at end of file |
|
450 | 450 | fseek(fd, 0, SEEK_SET); No newline at end of file |
|
451 | 451 | buffer = malloc(sz); No newline at end of file |
|
452 | 452 | fread(buffer,sizeof(char),sz,fd); No newline at end of file |
|
453 | 453 | fclose(fd); No newline at end of file |
|
454 | 454 | return buffer; No newline at end of file |
|
455 | 455 | } No newline at end of file |
|
456 | 456 | /* No newline at end of file |
|
457 | 457 | * Esta función genera el archivo de experimento a partir de la trama TCP recibida No newline at end of file |
|
458 | 458 | */ No newline at end of file |
|
459 | 459 | void escribe_experimento(char *p_data, char filename[]){ No newline at end of file |
|
460 | 460 | FILE *fd; No newline at end of file |
|
461 | 461 | int i = 0; No newline at end of file |
|
462 | 462 | No newline at end of file |
|
463 | 463 | while (*p_data != '\n'){ No newline at end of file |
|
464 | 464 | filename[i] = *p_data; No newline at end of file |
|
465 | 465 | i++; No newline at end of file |
|
466 | 466 | p_data++; No newline at end of file |
|
467 | 467 | } No newline at end of file |
|
468 | 468 | filename[i] = '\0'; No newline at end of file |
|
469 | 469 | p_data = p_data - i; No newline at end of file |
|
470 | 470 | fd = fopen(filename,"w"); No newline at end of file |
|
471 | 471 | fprintf(fd, p_data); No newline at end of file |
|
472 | 472 | fclose(fd); No newline at end of file |
|
473 | 473 | } No newline at end of file |
|
474 | 474 | No newline at end of file |
|
475 | 475 | void consigue_nombre_experimento(char *p_data, char filename[]){ No newline at end of file |
|
476 | 476 | No newline at end of file |
|
477 | 477 | int i = 0; No newline at end of file |
|
478 | 478 | No newline at end of file |
|
479 | 479 | while (*p_data != '\n'){ No newline at end of file |
|
480 | 480 | filename[i] = *p_data; No newline at end of file |
|
481 | 481 | i++; No newline at end of file |
|
482 | 482 | p_data++; No newline at end of file |
|
483 | 483 | } No newline at end of file |
|
484 | 484 | filename[i] = '\0'; No newline at end of file |
|
485 | 485 | } No newline at end of file |
|
486 | 486 | No newline at end of file |
|
487 | 487 | /* No newline at end of file |
|
488 | 488 | * Esta funcion carga un archivo en un buffer que esta ubicado en memoria, luego No newline at end of file |
|
489 | 489 | * este buffer es usado en la funcion "cambia_apuntamiento" para obtener el dato No newline at end of file |
|
490 | 490 | * que sera usado en el cambio de apuntamiento. No newline at end of file |
|
491 | 491 | */ No newline at end of file |
|
492 | 492 | int carga_experimento(char *nombre_archivo){ No newline at end of file |
|
493 | 493 | No newline at end of file |
|
494 | 494 | FILE *Archivo_Fd; No newline at end of file |
|
495 | 495 | No newline at end of file |
|
496 | 496 | char *cadena = (char *) malloc(25); No newline at end of file |
|
497 | 497 | No newline at end of file |
|
498 | 498 | int longitud_cadena; No newline at end of file |
|
499 | 499 | int num_bytes= 0; No newline at end of file |
|
500 | 500 | int num_filas= 0; No newline at end of file |
|
501 | 501 | No newline at end of file |
|
502 | 502 | Archivo_Fd = fopen(nombre_archivo,"r"); // Se procede a abrir el archivo, segun la ruta especificada No newline at end of file |
|
503 | 503 | if(!Archivo_Fd){ No newline at end of file |
|
504 | 504 | printf("carga_archivo: No se pudo abrir el archivo!!! --> fopen()\n"); No newline at end of file |
|
505 | 505 | //ERROR("carga_archivo: No se pudo abrir el archivo!!! --> fopen()\n"); No newline at end of file |
|
506 | 506 | return -1; No newline at end of file |
|
507 | 507 | }else{ No newline at end of file |
|
508 | 508 | No newline at end of file |
|
509 | 509 | while(!feof(Archivo_Fd)){ // Se procede a calcular la longitud del archivo para separar memoria No newline at end of file |
|
510 | 510 | fgets(cadena,20,Archivo_Fd); No newline at end of file |
|
511 | 511 | longitud_cadena= strlen(cadena); No newline at end of file |
|
512 | 512 | cadena[longitud_cadena-1] = '\0'; No newline at end of file |
|
513 | 513 | num_bytes = num_bytes + longitud_cadena; No newline at end of file |
|
514 | 514 | num_filas++; No newline at end of file |
|
515 | 515 | } No newline at end of file |
|
516 | 516 | No newline at end of file |
|
517 | 517 | rewind(Archivo_Fd); // Se reinicia el puntero del archivo No newline at end of file |
|
518 | 518 | No newline at end of file |
|
519 | 519 | char *buffer_temporal = (char *) malloc(num_bytes+1); // Se separa espacio de memoria segun No newline at end of file |
|
520 | 520 | // la longitud del archivo No newline at end of file |
|
521 | 521 | fread(buffer_temporal, sizeof(char), num_bytes, Archivo_Fd); No newline at end of file |
|
522 | 522 | No newline at end of file |
|
523 | 523 | char *puntero= strstr(buffer_temporal,".ab1"); // Se procede a eliminar la cabecera del archivo No newline at end of file |
|
524 | 524 | puntero= puntero + 12; No newline at end of file |
|
525 | 525 | No newline at end of file |
|
526 | 526 | buff_experimento = (char *) malloc(7*(num_filas-3)); // num_bytes_fila*(num_filas-3); No newline at end of file |
|
527 | 527 | strncpy(buff_experimento,puntero,7*(num_filas-3)); // Se carga en memoria la informacion del archivo No newline at end of file |
|
528 | 528 | No newline at end of file |
|
529 | 529 | fclose(Archivo_Fd); No newline at end of file |
|
530 | 530 | No newline at end of file |
|
531 | 531 | return 1; No newline at end of file |
|
532 | 532 | } No newline at end of file |
|
533 | 533 | } No newline at end of file |
|
534 | 534 | No newline at end of file |
|
535 | 535 | /* No newline at end of file |
|
536 | 536 | * Esta funcion recibe un numero en formato char, el dato se transforma a su equivalente en No newline at end of file |
|
537 | 537 | * un numero entero, que sera usado para sacar un dato del buffer "buff_experimento", esta No newline at end of file |
|
538 | 538 | * dato es el valor que se enviara al sistema de conmutacion RF para el cambio de apunte a No newline at end of file |
|
539 | 539 | * traves del puerto GPIO. No newline at end of file |
|
540 | 540 | */ No newline at end of file |
|
541 | 541 | int cambia_apuntamiento(char *puntero_char){ No newline at end of file |
|
542 | 542 | No newline at end of file |
|
543 | 543 | /*MSB-UP-LSB MSB-DOWN-LSB*/ No newline at end of file |
|
544 | 544 | int desplazamiento[6]={30,28,26,24,22,20}; // Defino los dezplazamientos que se aplicara No newline at end of file |
|
545 | 545 | // al dato que ingresa para formar el número No newline at end of file |
|
546 | 546 | // entero que se le pasara al puerto GPIO No newline at end of file |
|
547 | 547 | // Estos números son los pines del puerto GPIO No newline at end of file |
|
548 | 548 | // que se estan usando para el control No newline at end of file |
|
549 | 549 | No newline at end of file |
|
550 | 550 | int puntero= atoi(puntero_char); // Se convierte a entero la direccion del puntero No newline at end of file |
|
551 | 551 | No newline at end of file |
|
552 | 552 | int base= 7*puntero; // base= cantidad_bytes del dato x puntero No newline at end of file |
|
553 | 553 | // cantidad de bytes es el numero de bytes que No newline at end of file |
|
554 | 554 | printf("%s\n",puntero_char); // contiene cada dato, para este caso es 7 No newline at end of file |
|
555 | 555 | // porque es 6 bits de datos + 1 bit del cambio No newline at end of file |
|
556 | 556 | // de linea. No newline at end of file |
|
557 | 557 | char valor_char; No newline at end of file |
|
558 | 558 | unsigned long valor; No newline at end of file |
|
559 | 559 | unsigned long acumulado_ceros=0; No newline at end of file |
|
560 | 560 | unsigned long acumulado_unos=0; No newline at end of file |
|
561 | 561 | No newline at end of file |
|
562 | 562 | int offset; // Defino offset para el desplazamiento a traves No newline at end of file |
|
563 | 563 | for(offset=0;offset<6;offset++){ // de cada dato que se obtiene del "buff_experimento" No newline at end of file |
|
564 | 564 | No newline at end of file |
|
565 | 565 | valor_char= buff_experimento[base+offset]; // Obtengo el dato No newline at end of file |
|
566 | 566 | No newline at end of file |
|
567 | 567 | if (valor_char == '0'){ // Obtengo el número acumulado segun sea un cero o un uno No newline at end of file |
|
568 | 568 | valor= 0; No newline at end of file |
|
569 | 569 | acumulado_ceros= acumulado_ceros + (1 << desplazamiento[offset]); No newline at end of file |
|
570 | 570 | }else{ No newline at end of file |
|
571 | 571 | valor= 1; No newline at end of file |
|
572 | 572 | acumulado_unos= acumulado_unos + (1 << desplazamiento[offset]); No newline at end of file |
|
573 | 573 | } No newline at end of file |
|
574 | 574 | } No newline at end of file |
|
575 | 575 | pio_out(pioc, maskc_out_beam, acumulado_unos, 1); No newline at end of file |
|
576 | 576 | pio_out(pioc, maskc_out_beam, acumulado_ceros, 0); No newline at end of file |
|
577 | 577 | No newline at end of file |
|
578 | 578 | return 1; No newline at end of file |
|
579 | 579 | No newline at end of file |
|
580 | 580 | } No newline at end of file |
|
581 | 581 | No newline at end of file |
|
582 | 582 | /* No newline at end of file |
|
583 | 583 | * Esta funcion lee "n" veces el estado del APUNTE actual y reporta No newline at end of file |
|
584 | 584 | * una cadena de Verificacion. No newline at end of file |
|
585 | 585 | */ No newline at end of file |
|
586 | 586 | char* chequeo_sistema(char *filename, char *numero_muestras){ No newline at end of file |
|
587 | 587 | No newline at end of file |
|
588 | 588 | int i; No newline at end of file |
|
589 | 589 | int cnt = 0; No newline at end of file |
|
590 | 590 | unsigned int entradac= 0; No newline at end of file |
|
591 | 591 | No newline at end of file |
|
592 | 592 | char page0[250]; No newline at end of file |
|
593 | 593 | No newline at end of file |
|
594 | 594 | /*strcpy(page0,"Verificacion\n"); No newline at end of file |
|
595 | 595 | strcat(page0,parameters.ID);*/ No newline at end of file |
|
596 | 596 | strcpy(page0,parameters.ID); No newline at end of file |
|
597 | 597 | strcat(page0,"\n-------\n"); No newline at end of file |
|
598 | 598 | No newline at end of file |
|
599 | 599 | char page1[8]; No newline at end of file |
|
600 | 600 | No newline at end of file |
|
601 | 601 | do{ No newline at end of file |
|
602 | 602 | //Inicializando arreglo No newline at end of file |
|
603 | 603 | for(i=0;i<6;i++) No newline at end of file |
|
604 | 604 | page1[i]='0'; No newline at end of file |
|
605 | 605 | page1[6] = '\n'; No newline at end of file |
|
606 | 606 | page1[7] = '\0'; No newline at end of file |
|
607 | 607 | //Lectura de puerto No newline at end of file |
|
608 | 608 | entradac= pio_in(piob,maskb_in); No newline at end of file |
|
609 | 609 | //Dandole formato al dato No newline at end of file |
|
610 | 610 | if (!(entradac & bit_up_2)) No newline at end of file |
|
611 | 611 | page1[0] = '1'; No newline at end of file |
|
612 | 612 | if (!(entradac & bit_up_1)) No newline at end of file |
|
613 | 613 | page1[1] = '1'; No newline at end of file |
|
614 | 614 | if (!(entradac & bit_up_0)) No newline at end of file |
|
615 | 615 | page1[2] = '1'; No newline at end of file |
|
616 | 616 | if (!(entradac & bit_dow_2)) No newline at end of file |
|
617 | 617 | page1[3] = '1'; No newline at end of file |
|
618 | 618 | if (!(entradac & bit_dow_1)) No newline at end of file |
|
619 | 619 | page1[4] = '1'; No newline at end of file |
|
620 | 620 | if (!(entradac & bit_dow_0)) No newline at end of file |
|
621 | 621 | page1[5] = '1'; No newline at end of file |
|
622 | 622 | No newline at end of file |
|
623 | 623 | strcat(page0, page1); No newline at end of file |
|
624 | 624 | cnt=cnt+1; No newline at end of file |
|
625 | 625 | usleep(1*1000*1000); No newline at end of file |
|
626 | 626 | No newline at end of file |
|
627 | 627 | }while(cnt < atoi(numero_muestras)); No newline at end of file |
|
628 | 628 | No newline at end of file |
|
629 | 629 | page0[strlen(page0)] = '\0'; No newline at end of file |
|
630 | 630 | No newline at end of file |
|
631 | 631 | char *all_pages = malloc(strlen(page0)+1); No newline at end of file |
|
632 | 632 | strcpy(all_pages, page0); No newline at end of file |
|
633 | 633 | return all_pages; No newline at end of file |
|
634 | 634 | } No newline at end of file |
|
635 | 635 | No newline at end of file |
|
636 | 636 | /* No newline at end of file |
|
637 | 637 | * No newline at end of file |
|
638 | 638 | */ No newline at end of file |
|
639 | 639 | cmp inicializa_modulo(cmp p, char *filename){ No newline at end of file |
|
640 | 640 | //FILE *fd = fopen("configuration.txt","r"); No newline at end of file |
|
641 | 641 | FILE *fd = fopen(filename,"r"); No newline at end of file |
|
642 | 642 | fgets(p.ID,20,fd); No newline at end of file |
|
643 | 643 | p.ID[7]='\0'; No newline at end of file |
|
644 | 644 | fgets(p.param2,20,fd); No newline at end of file |
|
645 | 645 | p.param2[10]='\0'; No newline at end of file |
|
646 | 646 | fgets(p.param3,20,fd); No newline at end of file |
|
647 | 647 | p.param3[10]='\0'; No newline at end of file |
|
648 | 648 | fgets(p.param4,20,fd); No newline at end of file |
|
649 | 649 | p.param4[10]='\0'; No newline at end of file |
|
650 | 650 | fclose(fd); No newline at end of file |
|
651 | 651 | return p; No newline at end of file |
|
652 | 652 | } No newline at end of file |
|
653 | 653 | No newline at end of file |
|
654 | 654 | /* No newline at end of file |
|
655 | 655 | * No newline at end of file |
|
656 | 656 | */ No newline at end of file |
|
657 | 657 | void intToStr( int number, char* str ) No newline at end of file |
|
658 | 658 | No newline at end of file |
|
659 | 659 | { No newline at end of file |
|
660 | 660 | int index = 0; No newline at end of file |
|
661 | 661 | No newline at end of file |
|
662 | 662 | while( number > 0 ) No newline at end of file |
|
663 | 663 | { No newline at end of file |
|
664 | 664 | int digit = number%10; No newline at end of file |
|
665 | 665 | str[index++] = digit + '0'; No newline at end of file |
|
666 | 666 | number /= 10; No newline at end of file |
|
667 | 667 | } No newline at end of file |
|
668 | 668 | str[index] = '\0'; No newline at end of file |
|
669 | 669 | //Adding zero to the left No newline at end of file |
|
670 | 670 | int n= strlen(str); No newline at end of file |
|
671 | 671 | if (n == 1) { No newline at end of file |
|
672 | 672 | strcat(str,"00000"); No newline at end of file |
|
673 | 673 | index = index + 5; No newline at end of file |
|
674 | 674 | }else if(n == 2){ No newline at end of file |
|
675 | 675 | strcat(str,"0000"); No newline at end of file |
|
676 | 676 | index = index + 4; No newline at end of file |
|
677 | 677 | }else if(n == 3){ No newline at end of file |
|
678 | 678 | strcat(str,"000"); No newline at end of file |
|
679 | 679 | index = index + 3; No newline at end of file |
|
680 | 680 | }else if(n == 4){ No newline at end of file |
|
681 | 681 | strcat(str,"00"); No newline at end of file |
|
682 | 682 | index = index + 2; No newline at end of file |
|
683 | 683 | }else if(n == 5){ No newline at end of file |
|
684 | 684 | strcat(str,"0"); No newline at end of file |
|
685 | 685 | index = index + 1; No newline at end of file |
|
686 | 686 | } No newline at end of file |
|
687 | 687 | //Now reverse the numbers in the string. No newline at end of file |
|
688 | 688 | int position; No newline at end of file |
|
689 | 689 | for( position = 0; position <= (index-1)/2; ++position ) No newline at end of file |
|
690 | 690 | { No newline at end of file |
|
691 | 691 | char tmp = str[position]; No newline at end of file |
|
692 | 692 | str[position] = str[(index-1)-position]; No newline at end of file |
|
693 | 693 | str[(index-1)-position] = tmp; No newline at end of file |
|
694 | 694 | } No newline at end of file |
|
695 | 695 | } No newline at end of file |
|
696 | 696 | No newline at end of file |
|
697 | 697 | No newline at end of file |
|
698 | 698 | //***************************************************************** No newline at end of file |
|
699 | 699 | //ABS_monitoreo es la funci�n principal del proyecto ABS_Monitoreo. No newline at end of file |
|
700 | 700 | //Esta funci�n es la que se debe agregar en otros c�digos. No newline at end of file |
|
701 | 701 | //***************************************************************** No newline at end of file |
|
702 | 702 | char* ABS_monitoreo(int sel_atenuador, int sel_calibracion, float umbral, int pulsewidth){ No newline at end of file |
|
703 | 703 | No newline at end of file |
|
704 | 704 | //local variables No newline at end of file |
|
705 | 705 | /* AT91S_PIO *pioc; No newline at end of file |
|
706 | 706 | pioc = pio_map(PIOC_BASE); No newline at end of file |
|
707 | 707 | unsigned int mask_sel_canal =PC4; //Aqu� se indican los pines que se desean usar como salidas. Las constantes PCx est�n defiidas en el header at91gpio.h No newline at end of file |
|
708 | 708 | unsigned int mask_sel_atenuacion =PC5; No newline at end of file |
|
709 | 709 | unsigned int mask_sel_calibracion =PC6;*/ No newline at end of file |
|
710 | 710 | No newline at end of file |
|
711 | 711 | unsigned long acumulado_ceros=0; No newline at end of file |
|
712 | 712 | unsigned long acumulado_unos=0; No newline at end of file |
|
713 | 713 | /* AT91S_ADC *padc; No newline at end of file |
|
714 | 714 | AT91S_ADC *padd;*/ No newline at end of file |
|
715 | 715 | FILE *fp; No newline at end of file |
|
716 | 716 | long int results1[NSAMPLES], results2[NSAMPLES], results3[NSAMPLES], results4[NSAMPLES]; No newline at end of file |
|
717 | 717 | unsigned int i=0; No newline at end of file |
|
718 | 718 | char fname[FNAMELEN]; No newline at end of file |
|
719 | 719 | int j=0; No newline at end of file |
|
720 | 720 | time_t now; No newline at end of file |
|
721 | 721 | FILE *archivo; No newline at end of file |
|
722 | 722 | float phase1; No newline at end of file |
|
723 | 723 | float phase2; No newline at end of file |
|
724 | 724 | char page0[30]; No newline at end of file |
|
725 | 725 | char page1[20]; No newline at end of file |
|
726 | 726 | No newline at end of file |
|
727 | 727 | int cnt = 0; No newline at end of file |
|
728 | 728 | //system("./map_clock"); No newline at end of file |
|
729 | 729 | /* No newline at end of file |
|
730 | 730 | if (configCLK() == 1) No newline at end of file |
|
731 | 731 | printf("clock ADC enable.\n"); No newline at end of file |
|
732 | 732 | */ No newline at end of file |
|
733 | 733 | /* No newline at end of file |
|
734 | 734 | //configurar tres pines como salida usando als m�scaras mask_sel_canal, mask_sel_atenuacion y mask_sel_calibracion. En este caso corresponden a los pines pc4, pc5 y pc6. No newline at end of file |
|
735 | 735 | pio_enable(pioc, mask_sel_canal); No newline at end of file |
|
736 | 736 | pio_enable(pioc, mask_sel_atenuacion); No newline at end of file |
|
737 | 737 | pio_enable(pioc, mask_sel_calibracion); No newline at end of file |
|
738 | 738 | pio_output_enable(pioc, mask_sel_canal); //configurar pc4 como salida No newline at end of file |
|
739 | 739 | pio_output_enable(pioc, mask_sel_atenuacion); //configurar pc5 como salida No newline at end of file |
|
740 | 740 | pio_output_enable(pioc, mask_sel_calibracion); //configurar pc6 como salida No newline at end of file |
|
741 | 741 | */ No newline at end of file |
|
742 | 742 | No newline at end of file |
|
743 | 743 | //Se modifican las salidas correspondientes a la selecci�n del atenuador y calibraci�n, de acuerdo a los par�metros ingresados en la funci�n ABS_monitoreo. No newline at end of file |
|
744 | 744 | /*if ( sel_atenuador == 1) No newline at end of file |
|
745 | 745 | pio_out(pioc, mask_sel_atenuacion, sel_atenuador,1); No newline at end of file |
|
746 | 746 | else No newline at end of file |
|
747 | 747 | pio_out(pioc, mask_sel_atenuacion, sel_atenuador,0); No newline at end of file |
|
748 | 748 | if ( sel_calibracion == 1) No newline at end of file |
|
749 | 749 | pio_out(pioc, mask_sel_calibracion, sel_calibracion,1); No newline at end of file |
|
750 | 750 | else No newline at end of file |
|
751 | 751 | pio_out(pioc, mask_sel_calibracion, sel_calibracion,0);*/ No newline at end of file |
|
752 | 752 | No newline at end of file |
|
753 | 753 | if ( sel_atenuador == 1) No newline at end of file |
|
754 | 754 | acumulado_unos = acumulado_unos + (1 << 5); No newline at end of file |
|
755 | 755 | else No newline at end of file |
|
756 | 756 | acumulado_ceros = acumulado_ceros + (1 << 5); No newline at end of file |
|
757 | 757 | if ( sel_calibracion == 1) No newline at end of file |
|
758 | 758 | acumulado_unos = acumulado_unos + (1 << 6); No newline at end of file |
|
759 | 759 | else No newline at end of file |
|
760 | 760 | acumulado_ceros = acumulado_ceros + (1 << 6); No newline at end of file |
|
761 | 761 | No newline at end of file |
|
762 | 762 | No newline at end of file |
|
763 | 763 | strcpy (fname, "/mnt/sd/absmonitoreo.txt"); //Direcci�n y nombre del archivo donde se desea guardar los datos. No newline at end of file |
|
764 | 764 | No newline at end of file |
|
765 | 765 | if (fExists(fname)==0){ //si el archivo no existe, crea uno y le asigna el titulo No newline at end of file |
|
766 | 766 | archivo = fopen(fname,"a+"); No newline at end of file |
|
767 | 767 | fprintf(archivo,"%s"," Registro de datos del ABS Control \n"); No newline at end of file |
|
768 | 768 | fprintf(archivo,"%s"," Fecha y hora Fase UP Fase DOWN\n"); No newline at end of file |
|
769 | 769 | fclose(archivo); No newline at end of file |
|
770 | 770 | } No newline at end of file |
|
771 | 771 | No newline at end of file |
|
772 | 772 | /* No newline at end of file |
|
773 | 773 | //configure ADC Settings No newline at end of file |
|
774 | 774 | padc=configADC1(); No newline at end of file |
|
775 | 775 | padd=configADC2(); No newline at end of file |
|
776 | 776 | */ No newline at end of file |
|
777 | 777 | //while (1){ No newline at end of file |
|
778 | 778 | No newline at end of file |
|
779 | 779 | ENABLE_CHANNEL(padc, ADC_CH0+ADC_CH1); No newline at end of file |
|
780 | 780 | printf("\nAdquiriendo datos...\n"); //Indica en el terminal que se est�n adquiriendo datos (muestreando la se�al). No newline at end of file |
|
781 | 781 | No newline at end of file |
|
782 | 782 | No newline at end of file |
|
783 | 783 | now = time(0); //Get current Time for File Name No newline at end of file |
|
784 | 784 | No newline at end of file |
|
785 | 785 | //Se pone la salida de selecci�n de canal para seleccionar el canal 1 del detector de fase No newline at end of file |
|
786 | 786 | acumulado_ceros = acumulado_ceros + (1 << 4); No newline at end of file |
|
787 | 787 | pio_out(pioc, maskc_out_cntl, acumulado_ceros, 0); No newline at end of file |
|
788 | 788 | pio_out(pioc, maskc_out_cntl, acumulado_unos, 1); No newline at end of file |
|
789 | 789 | sleep(1); No newline at end of file |
|
790 | 790 | No newline at end of file |
|
791 | 791 | //Se toman muestras para el canal 1 del detector de fase No newline at end of file |
|
792 | 792 | /*while(1){ No newline at end of file |
|
793 | 793 | for(i=0; i < NSAMPLES; i++){ No newline at end of file |
|
794 | 794 | No newline at end of file |
|
795 | 795 | ADC_INIT(padc); No newline at end of file |
|
796 | 796 | results1[i] = GET_ADC0(padc); No newline at end of file |
|
797 | 797 | results2[i] = GET_ADC1(padd); No newline at end of file |
|
798 | 798 | } No newline at end of file |
|
799 | 799 | No newline at end of file |
|
800 | 800 | if (checkTx(results1, results2, umbral, pulsewidth)==1){ //Se verifica que las muestras tomadas del canal 1 del datector de fase //correspondan a un pulso. No newline at end of file |
|
801 | 801 | break; No newline at end of file |
|
802 | 802 | } No newline at end of file |
|
803 | 803 | No newline at end of file |
|
804 | 804 | }*/ No newline at end of file |
|
805 | 805 | No newline at end of file |
|
806 | 806 | do{ No newline at end of file |
|
807 | 807 | for(i=0; i < NSAMPLES; i++){ No newline at end of file |
|
808 | 808 | No newline at end of file |
|
809 | 809 | ADC_INIT(padc); No newline at end of file |
|
810 | 810 | results1[i] = GET_ADC0(padc); No newline at end of file |
|
811 | 811 | results2[i] = GET_ADC1(padd); No newline at end of file |
|
812 | 812 | } No newline at end of file |
|
813 | 813 | No newline at end of file |
|
814 | 814 | if (checkTx(results1, results2, umbral, pulsewidth)==1){ //Se verifica que las muestras tomadas del canal 1 del datector de fase //correspondan a un pulso. No newline at end of file |
|
815 | 815 | break; No newline at end of file |
|
816 | 816 | } No newline at end of file |
|
817 | 817 | cnt += 1; No newline at end of file |
|
818 | 818 | }while (cnt < NLOOPS); No newline at end of file |
|
819 | 819 | No newline at end of file |
|
820 | 820 | //Se pone la salida de selecci�n de canal para seleccionar el canal 2 del detector de fase No newline at end of file |
|
821 | 821 | acumulado_ceros = acumulado_ceros - (1 << 4); No newline at end of file |
|
822 | 822 | acumulado_unos = acumulado_unos + (1 << 4); No newline at end of file |
|
823 | 823 | pio_out(pioc, maskc_out_cntl, acumulado_ceros, 0); No newline at end of file |
|
824 | 824 | pio_out(pioc, maskc_out_cntl, acumulado_unos, 1); No newline at end of file |
|
825 | 825 | sleep(1); No newline at end of file |
|
826 | 826 | No newline at end of file |
|
827 | 827 | No newline at end of file |
|
828 | 828 | //Setoman muestras para el canal 2 del detector de fase No newline at end of file |
|
829 | 829 | /* while(1){ No newline at end of file |
|
830 | 830 | for(i=0; i < NSAMPLES; i++){ No newline at end of file |
|
831 | 831 | No newline at end of file |
|
832 | 832 | ADC_INIT(padc); No newline at end of file |
|
833 | 833 | results3[i] = GET_ADC0(padc); No newline at end of file |
|
834 | 834 | results4[i] = GET_ADC1(padd); No newline at end of file |
|
835 | 835 | } No newline at end of file |
|
836 | 836 | No newline at end of file |
|
837 | 837 | if (checkTx(results3, results4, umbral, pulsewidth)==1){ //Se verifica que las muestras tomadas del canal 2 del detector de fase //correspondan a un pulso. No newline at end of file |
|
838 | 838 | break; No newline at end of file |
|
839 | 839 | } No newline at end of file |
|
840 | 840 | }*/ No newline at end of file |
|
841 | 841 | cnt = 0; No newline at end of file |
|
842 | 842 | do{ No newline at end of file |
|
843 | 843 | for(i=0; i < NSAMPLES; i++){ No newline at end of file |
|
844 | 844 | No newline at end of file |
|
845 | 845 | ADC_INIT(padc); No newline at end of file |
|
846 | 846 | results3[i] = GET_ADC0(padc); No newline at end of file |
|
847 | 847 | results4[i] = GET_ADC1(padd); No newline at end of file |
|
848 | 848 | } No newline at end of file |
|
849 | 849 | No newline at end of file |
|
850 | 850 | if (checkTx(results3, results4, umbral, pulsewidth)==1){ //Se verifica que las muestras tomadas del canal 2 del detector de fase //correspondan a un pulso. No newline at end of file |
|
851 | 851 | break; No newline at end of file |
|
852 | 852 | } No newline at end of file |
|
853 | 853 | cnt += 1; No newline at end of file |
|
854 | 854 | }while(cnt < NLOOPS); No newline at end of file |
|
855 | 855 | No newline at end of file |
|
856 | 856 | //Una vez que se ha encontrado un pulso en cada canal, se calcula la fase de ambos. No newline at end of file |
|
857 | 857 | No newline at end of file |
|
858 | 858 | phase1 = getPhase(results1, results2); //Calcular la fase del canal 1 del detector de fase. No newline at end of file |
|
859 | 859 | phase2 = getPhase(results3, results4); //Calcular la fase del canal 2 del detector de fase. No newline at end of file |
|
860 | 860 | //create Output File No newline at end of file |
|
861 | 861 | No newline at end of file |
|
862 | 862 | strcpy (fname, "/mnt/sd/absmonitoreo.txt"); No newline at end of file |
|
863 | 863 | printf("\nTerminada la prueba # %d \n", j++); No newline at end of file |
|
864 | 864 | fp=create_Output(fname, now); //Coloca la fecha y la hora en el archivo de texto No newline at end of file |
|
865 | 865 | printf("mediana ch1 = %1.2f\n", phase1); //muestra resultado en terminal No newline at end of file |
|
866 | 866 | printf("mediana ch2 = %1.2f\n", phase2); //muestra resultado en terminal No newline at end of file |
|
867 | 867 | writeOutput(phase1, fp); //graba el resultado en el archivo de texto No newline at end of file |
|
868 | 868 | writeOutput(phase2, fp); //graba el resultado en el archivo de texto No newline at end of file |
|
869 | 869 | fprintf(fp, "\n"); //Pasa a la siguiente l�nea del archivo de texto No newline at end of file |
|
870 | 870 | fclose(fp); No newline at end of file |
|
871 | 871 | printf("Resultado guardado en %s \n", fname); No newline at end of file |
|
872 | 872 | No newline at end of file |
|
873 | 873 | sleep(1); No newline at end of file |
|
874 | 874 | No newline at end of file |
|
875 | 875 | strcpy(page0,parameters.ID); No newline at end of file |
|
876 | 876 | strcat(page0,"\n-------\n"); No newline at end of file |
|
877 | 877 | No newline at end of file |
|
878 | 878 | //sprintf(page1,"UP:%1.2f DW:%1.2f\n",phase1, phase2); No newline at end of file |
|
879 | 879 | sprintf(page1,"%1.2f %1.2f\n",phase1, phase2); No newline at end of file |
|
880 | 880 | strcat(page0,page1); No newline at end of file |
|
881 | 881 | char *all_pages = malloc(strlen(page0)+1); No newline at end of file |
|
882 | 882 | strcpy(all_pages, page0); No newline at end of file |
|
883 | 883 | return all_pages; No newline at end of file |
|
884 | 884 | No newline at end of file |
|
885 | 885 | // } No newline at end of file |
|
886 | 886 | // return 0; No newline at end of file |
|
887 | 887 | } No newline at end of file |
|
888 | 888 | /*============================================================================= No newline at end of file |
|
889 | 889 | Function definitions No newline at end of file |
|
890 | 890 | =============================================================================*/ No newline at end of file |
|
891 | 891 | No newline at end of file |
|
892 | 892 | // Configures ADC registers in order to get a sample every 10us No newline at end of file |
|
893 | 893 | AT91S_ADC * configADC1(void){ No newline at end of file |
|
894 | 894 | //Variables a usar: No newline at end of file |
|
895 | 895 | /* No newline at end of file |
|
896 | 896 | unsigned int maskc_adc =PC0; //Usamos ADC0 y ADC1 No newline at end of file |
|
897 | 897 | No newline at end of file |
|
898 | 898 | //configuro pin: No newline at end of file |
|
899 | 899 | AT91S_PIO *pioc; No newline at end of file |
|
900 | 900 | pioc = pio_map(PIOC_BASE); No newline at end of file |
|
901 | 901 | pin_adc_enable(pioc,maskc_adc); //Habilitamos PC0 para usar con ADC0 y 1 No newline at end of file |
|
902 | 902 | pio_disable_irq(pioc, maskc_adc); No newline at end of file |
|
903 | 903 | pio_disable_multiple_driver(pioc, maskc_adc); No newline at end of file |
|
904 | 904 | pio_disable_pull_ups(pioc, maskc_adc); No newline at end of file |
|
905 | 905 | pio_input_enable(pioc, maskc_adc); No newline at end of file |
|
906 | 906 | */ No newline at end of file |
|
907 | 907 | No newline at end of file |
|
908 | 908 | //Configuro el ADC: No newline at end of file |
|
909 | 909 | //AT91S_ADC *padc; No newline at end of file |
|
910 | 910 | No newline at end of file |
|
911 | 911 | padc = adc_map1(ADC_BASE); No newline at end of file |
|
912 | 912 | No newline at end of file |
|
913 | 913 | //clock ADC = 1MHz No newline at end of file |
|
914 | 914 | //time startup = 8us No newline at end of file |
|
915 | 915 | //time sample and hold = 2us No newline at end of file |
|
916 | 916 | // hold No newline at end of file |
|
917 | 917 | // ___________ No newline at end of file |
|
918 | 918 | // start ___________| |___________ No newline at end of file |
|
919 | 919 | // No newline at end of file |
|
920 | 920 | // | --1.2us-- | --0.15us-- | No newline at end of file |
|
921 | 921 | //ADC_RESET(padc); No newline at end of file |
|
922 | 922 | CONFIG_ADC(padc,ADC_TRGEN_DIS | ADC_RES_10BIT | ADC_SLEEP_NORMAL_MODE | ADC_PRESCAL | ADC_STARTUP | ADC_SHTIM); No newline at end of file |
|
923 | 923 | ENABLE_CHANNEL(padc,ADC_CH0); //habilito canal 0 No newline at end of file |
|
924 | 924 | return padc; No newline at end of file |
|
925 | 925 | } No newline at end of file |
|
926 | 926 | No newline at end of file |
|
927 | 927 | AT91S_ADC * configADC2(void){ No newline at end of file |
|
928 | 928 | //Variables a usar: No newline at end of file |
|
929 | 929 | /* No newline at end of file |
|
930 | 930 | unsigned int maskc_adc =PC1; //Usamos ADC0 y ADC1 No newline at end of file |
|
931 | 931 | No newline at end of file |
|
932 | 932 | //configuro pin: No newline at end of file |
|
933 | 933 | AT91S_PIO *piod; No newline at end of file |
|
934 | 934 | piod = pio_map(PIOC_BASE); No newline at end of file |
|
935 | 935 | pin_adc_enable(piod,maskc_adc); //Habilitamos PC1 para usar con ADC0 y 1 No newline at end of file |
|
936 | 936 | pio_disable_irq(piod, maskc_adc); No newline at end of file |
|
937 | 937 | pio_disable_multiple_driver(piod, maskc_adc); No newline at end of file |
|
938 | 938 | pio_disable_pull_ups(piod, maskc_adc); No newline at end of file |
|
939 | 939 | pio_input_enable(piod, maskc_adc); No newline at end of file |
|
940 | 940 | */ No newline at end of file |
|
941 | 941 | //Configuro el ADC: No newline at end of file |
|
942 | 942 | //AT91S_ADC *padd; No newline at end of file |
|
943 | 943 | No newline at end of file |
|
944 | 944 | padd = adc_map1(ADC_BASE); No newline at end of file |
|
945 | 945 | No newline at end of file |
|
946 | 946 | //clock ADC = 1MHz No newline at end of file |
|
947 | 947 | //time startup = 8us No newline at end of file |
|
948 | 948 | //time sample and hold = 2us No newline at end of file |
|
949 | 949 | // hold No newline at end of file |
|
950 | 950 | // ___________ No newline at end of file |
|
951 | 951 | // start ___________| |___________ No newline at end of file |
|
952 | 952 | // No newline at end of file |
|
953 | 953 | // | --1.2us-- | --0.15us-- | No newline at end of file |
|
954 | 954 | //ADC_RESET(padc); No newline at end of file |
|
955 | 955 | CONFIG_ADC(padd,ADC_TRGEN_DIS | ADC_RES_10BIT | ADC_SLEEP_NORMAL_MODE | ADC_PRESCAL | ADC_STARTUP | ADC_SHTIM); No newline at end of file |
|
956 | 956 | ENABLE_CHANNEL(padd,ADC_CH1); //habilito canal 1 No newline at end of file |
|
957 | 957 | return padd; No newline at end of file |
|
958 | 958 | } No newline at end of file |
|
959 | 959 | No newline at end of file |
|
960 | 960 | No newline at end of file |
|
961 | 961 | //++++++++++++++++++++ No newline at end of file |
|
962 | 962 | No newline at end of file |
|
963 | 963 | //creats the output file with a timestamp in the name No newline at end of file |
|
964 | 964 | FILE * create_Output(char *fname, time_t rawtime){ No newline at end of file |
|
965 | 965 | FILE *file; No newline at end of file |
|
966 | 966 | char timestamp[80];//, counter[5]="dcv"; No newline at end of file |
|
967 | 967 | //char str[4]; No newline at end of file |
|
968 | 968 | struct tm * timeinfo; No newline at end of file |
|
969 | 969 | No newline at end of file |
|
970 | 970 | //format time No newline at end of file |
|
971 | 971 | timeinfo = localtime ( &rawtime ); No newline at end of file |
|
972 | 972 | strftime (timestamp,sizeof(timestamp),"%a %y-%m-%d %H:%M:%S %Z",timeinfo); No newline at end of file |
|
973 | 973 | No newline at end of file |
|
974 | 974 | No newline at end of file |
|
975 | 975 | //Creates the file name out of the #define parameters No newline at end of file |
|
976 | 976 | No newline at end of file |
|
977 | 977 | strcpy (fname, "/mnt/sd/absmonitoreo.txt"); No newline at end of file |
|
978 | 978 | file = fopen(fname,"a+"); No newline at end of file |
|
979 | 979 | fprintf(file,"%s", timestamp); No newline at end of file |
|
980 | 980 | //printf("\nTerminada la prueba # %d. Guardando resultado en %s\n",r, fname); No newline at end of file |
|
981 | 981 | //printf("\nTerminada la prueba # %d/%d. Writing data to the file %s\n",r+1 , REP, fname); No newline at end of file |
|
982 | 982 | //printf("\nAAAAAAAAAA %d...%s\n", counter[1], fname); No newline at end of file |
|
983 | 983 | // return file pointer No newline at end of file |
|
984 | 984 | return file; No newline at end of file |
|
985 | 985 | } No newline at end of file |
|
986 | 986 | No newline at end of file |
|
987 | 987 | //++++++++++++++++++++ No newline at end of file |
|
988 | 988 | No newline at end of file |
|
989 | 989 | //tests if a file already exists. returns 1 if it exists and 0 if it doesn't No newline at end of file |
|
990 | 990 | No newline at end of file |
|
991 | 991 | No newline at end of file |
|
992 | 992 | No newline at end of file |
|
993 | 993 | //Funci�n checkTx verifica que la se�al muestreada corresponda a un pulso. No newline at end of file |
|
994 | 994 | //results1 y results2 son los arreglos que contienen los datos muestreados por ambos canales del ADC del embebido. No newline at end of file |
|
995 | 995 | //umbral indica qu� valor debe superar una muestra para considerarla un posible pulso o pico. No newline at end of file |
|
996 | 996 | //pulsewidth indica cu�ntas muestras consecutivas deben superar el umbral para que se considere que se ha detectado un pulso. No newline at end of file |
|
997 | 997 | int checkTx(long int results1[],long int results2[], float umbral, int pulsewidth){ No newline at end of file |
|
998 | 998 | No newline at end of file |
|
999 | 999 | int i, cont; No newline at end of file |
|
1000 | 1000 | float z[NSAMPLES], sum, avg; No newline at end of file |
|
1001 | 1001 | int isSignal, pulse; No newline at end of file |
|
1002 | 1002 | No newline at end of file |
|
1003 | 1003 | for(i=0;i<NSAMPLES;i++){ No newline at end of file |
|
1004 | 1004 | No newline at end of file |
|
1005 | 1005 | z[i] =sqrt(1.0*results1[i]*results1[i]+1.0*results2[i]*results2[i]); No newline at end of file |
|
1006 | 1006 | } No newline at end of file |
|
1007 | 1007 | No newline at end of file |
|
1008 | 1008 | pulse = 0; No newline at end of file |
|
1009 | 1009 | isSignal = 0; No newline at end of file |
|
1010 | 1010 | cont =0; No newline at end of file |
|
1011 | 1011 | No newline at end of file |
|
1012 | 1012 | sum = 0; No newline at end of file |
|
1013 | 1013 | for(i=0;i<NSAMPLES;i++){ No newline at end of file |
|
1014 | 1014 | No newline at end of file |
|
1015 | 1015 | sum += z[i]; No newline at end of file |
|
1016 | 1016 | avg = sum/(i+1); No newline at end of file |
|
1017 | 1017 | if ((z[i] - avg) > umbral){ No newline at end of file |
|
1018 | 1018 | if (isSignal == 1){ No newline at end of file |
|
1019 | 1019 | cont += 1; No newline at end of file |
|
1020 | 1020 | } No newline at end of file |
|
1021 | 1021 | if (cont == pulsewidth){ No newline at end of file |
|
1022 | 1022 | pulse = 1; No newline at end of file |
|
1023 | 1023 | break; No newline at end of file |
|
1024 | 1024 | } No newline at end of file |
|
1025 | 1025 | isSignal = 1; No newline at end of file |
|
1026 | 1026 | continue; No newline at end of file |
|
1027 | 1027 | isSignal = 0; No newline at end of file |
|
1028 | 1028 | cont = 0; No newline at end of file |
|
1029 | 1029 | } No newline at end of file |
|
1030 | 1030 | } No newline at end of file |
|
1031 | 1031 | No newline at end of file |
|
1032 | 1032 | return pulse; //devuelve un entero: 1 si se ha detectado pulso, de lo contrario, 0. No newline at end of file |
|
1033 | 1033 | } No newline at end of file |
|
1034 | 1034 | No newline at end of file |
|
1035 | 1035 | No newline at end of file |
|
1036 | 1036 | int fExists(char * fname){ No newline at end of file |
|
1037 | 1037 | FILE * file; No newline at end of file |
|
1038 | 1038 | No newline at end of file |
|
1039 | 1039 | file = fopen (fname, "r"); No newline at end of file |
|
1040 | 1040 | if (file == NULL) No newline at end of file |
|
1041 | 1041 | { No newline at end of file |
|
1042 | 1042 | return 0; No newline at end of file |
|
1043 | 1043 | } No newline at end of file |
|
1044 | 1044 | fclose(file); No newline at end of file |
|
1045 | 1045 | return 1; No newline at end of file |
|
1046 | 1046 | } No newline at end of file |
|
1047 | 1047 | No newline at end of file |
|
1048 | 1048 | No newline at end of file |
|
1049 | 1049 | //Funci�n que calcula la mediana de un conjunto de muestras No newline at end of file |
|
1050 | 1050 | double mediana(long int *results,unsigned int cuenta){ No newline at end of file |
|
1051 | 1051 | unsigned int i=0,j=0,aux=0; No newline at end of file |
|
1052 | 1052 | No newline at end of file |
|
1053 | 1053 | double median=0; No newline at end of file |
|
1054 | 1054 | /*Calculo mediana */ No newline at end of file |
|
1055 | 1055 | No newline at end of file |
|
1056 | 1056 | for(i=0;i<cuenta-1;i++){ No newline at end of file |
|
1057 | 1057 | for (j=i+1;j<cuenta;j++){ No newline at end of file |
|
1058 | 1058 | if(results[i]>results[j] ){ No newline at end of file |
|
1059 | 1059 | No newline at end of file |
|
1060 | 1060 | aux=results[i]; No newline at end of file |
|
1061 | 1061 | results[i]=results[j]; No newline at end of file |
|
1062 | 1062 | results[j]=aux; No newline at end of file |
|
1063 | 1063 | No newline at end of file |
|
1064 | 1064 | } No newline at end of file |
|
1065 | 1065 | } No newline at end of file |
|
1066 | 1066 | No newline at end of file |
|
1067 | 1067 | } No newline at end of file |
|
1068 | 1068 | median=results[cuenta/2]; No newline at end of file |
|
1069 | 1069 | return median; No newline at end of file |
|
1070 | 1070 | } No newline at end of file |
|
1071 | 1071 | No newline at end of file |
|
1072 | 1072 | No newline at end of file |
|
1073 | 1073 | No newline at end of file |
|
1074 | 1074 | //Funci�n que halla la fase de la se�al. No newline at end of file |
|
1075 | 1075 | //Tiene como entradas las muestras correspondientes a la parte real e imaginaria de la se�al. No newline at end of file |
|
1076 | 1076 | float getPhase(long int results1[],long int results2[]){ No newline at end of file |
|
1077 | 1077 | No newline at end of file |
|
1078 | 1078 | unsigned int count=0, i=0,umbral=1000; No newline at end of file |
|
1079 | 1079 | //long int results1[]; No newline at end of file |
|
1080 | 1080 | //long int results2[]; No newline at end of file |
|
1081 | 1081 | long int power[NSAMPLES]; No newline at end of file |
|
1082 | 1082 | long int sumI=0,sumQ=0,I[NSAMPLES], Q[NSAMPLES],II[NSAMPLES], QQ[NSAMPLES]; No newline at end of file |
|
1083 | 1083 | double median1=0,median2=0; No newline at end of file |
|
1084 | 1084 | long int promedioI=0,promedioQ=0;/*Calculo mediana 1*/ No newline at end of file |
|
1085 | 1085 | float resultado=0; No newline at end of file |
|
1086 | 1086 | No newline at end of file |
|
1087 | 1087 | for(i=0;i<NSAMPLES;i++){ No newline at end of file |
|
1088 | 1088 | No newline at end of file |
|
1089 | 1089 | I[i] =results1[i]; No newline at end of file |
|
1090 | 1090 | Q[i] =results2[i]; No newline at end of file |
|
1091 | 1091 | } No newline at end of file |
|
1092 | 1092 | No newline at end of file |
|
1093 | 1093 | /*Calculo mediana 1*/ No newline at end of file |
|
1094 | 1094 | median1=mediana(I,NSAMPLES); No newline at end of file |
|
1095 | 1095 | No newline at end of file |
|
1096 | 1096 | /*Calculo mediana 2*/ No newline at end of file |
|
1097 | 1097 | median2=mediana(Q,NSAMPLES); No newline at end of file |
|
1098 | 1098 | No newline at end of file |
|
1099 | 1099 | No newline at end of file |
|
1100 | 1100 | No newline at end of file |
|
1101 | 1101 | No newline at end of file |
|
1102 | 1102 | No newline at end of file |
|
1103 | 1103 | No newline at end of file |
|
1104 | 1104 | for(i=0;i<NSAMPLES;i++){ No newline at end of file |
|
1105 | 1105 | No newline at end of file |
|
1106 | 1106 | I[i] =results1[i]; No newline at end of file |
|
1107 | 1107 | Q[i] =results2[i]; No newline at end of file |
|
1108 | 1108 | No newline at end of file |
|
1109 | 1109 | } No newline at end of file |
|
1110 | 1110 | No newline at end of file |
|
1111 | 1111 | No newline at end of file |
|
1112 | 1112 | No newline at end of file |
|
1113 | 1113 | for(i = 0; i < NSAMPLES ; i++){ No newline at end of file |
|
1114 | 1114 | No newline at end of file |
|
1115 | 1115 | I[i]=(I[i]-median1); No newline at end of file |
|
1116 | 1116 | Q[i]=(Q[i]-median2); No newline at end of file |
|
1117 | 1117 | No newline at end of file |
|
1118 | 1118 | } No newline at end of file |
|
1119 | 1119 | No newline at end of file |
|
1120 | 1120 | for(i = 0; i < NSAMPLES ; i++){ No newline at end of file |
|
1121 | 1121 | No newline at end of file |
|
1122 | 1122 | power[i]=I[i]*I[i]+Q[i]*Q[i]; No newline at end of file |
|
1123 | 1123 | No newline at end of file |
|
1124 | 1124 | if(power[i] > umbral) No newline at end of file |
|
1125 | 1125 | { No newline at end of file |
|
1126 | 1126 | No newline at end of file |
|
1127 | 1127 | II[count]=I[i]; No newline at end of file |
|
1128 | 1128 | QQ[count]=Q[i]; No newline at end of file |
|
1129 | 1129 | count=count+1; No newline at end of file |
|
1130 | 1130 | No newline at end of file |
|
1131 | 1131 | } No newline at end of file |
|
1132 | 1132 | No newline at end of file |
|
1133 | 1133 | } No newline at end of file |
|
1134 | 1134 | No newline at end of file |
|
1135 | 1135 | for(i = 0; i < count ; i++){ No newline at end of file |
|
1136 | 1136 | No newline at end of file |
|
1137 | 1137 | sumI=sumI+II[i]; No newline at end of file |
|
1138 | 1138 | sumQ=sumQ+QQ[i]; No newline at end of file |
|
1139 | 1139 | No newline at end of file |
|
1140 | 1140 | } No newline at end of file |
|
1141 | 1141 | No newline at end of file |
|
1142 | 1142 | promedioI=sumI; No newline at end of file |
|
1143 | 1143 | promedioQ=sumQ; No newline at end of file |
|
1144 | 1144 | No newline at end of file |
|
1145 | 1145 | resultado = atan2(1.0*promedioI,1.0*promedioQ)*180/3.1416+62-44; No newline at end of file |
|
1146 | 1146 | No newline at end of file |
|
1147 | 1147 | No newline at end of file |
|
1148 | 1148 | return resultado; No newline at end of file |
|
1149 | 1149 | No newline at end of file |
|
1150 | 1150 | } No newline at end of file |
|
1151 | 1151 | No newline at end of file |
|
1152 | 1152 | No newline at end of file |
|
1153 | 1153 | No newline at end of file |
|
1154 | 1154 | //Funci�n que muestra la fase detectada en el terminal y tambi�n la graba en el archivo de texto. No newline at end of file |
|
1155 | 1155 | void writeOutput(float resultado, FILE * output){ No newline at end of file |
|
1156 | 1156 | No newline at end of file |
|
1157 | 1157 | No newline at end of file |
|
1158 | 1158 | // No newline at end of file |
|
1159 | 1159 | No newline at end of file |
|
1160 | 1160 | fprintf(output," %1.2f ",resultado); //graba resultado en archivo .txt No newline at end of file |
|
1161 | 1161 | // No newline at end of file |
|
1162 | 1162 | No newline at end of file |
|
1163 | 1163 | } No newline at end of file |
|
1164 | 1164 | No newline at end of file |
|
1165 | 1165 | int configCLK(){ No newline at end of file |
|
1166 | 1166 | //configuro pin: No newline at end of file |
|
1167 | 1167 | AT91S_PMC *sys_clock; No newline at end of file |
|
1168 | 1168 | sys_clock = clock_map(CLOCK_BASE); No newline at end of file |
|
1169 | 1169 | enable_clock_adc(sys_clock); No newline at end of file |
|
1170 | 1170 | //printf("clock ADC enable.\n"); No newline at end of file |
|
1171 | 1171 | return 1; No newline at end of file |
|
1172 | 1172 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now