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