From b90947c35eb90fd77cc05089dfa1147602677cc7 Mon Sep 17 00:00:00 2001 From: Sayeth Date: Wed, 30 Sep 2020 18:05:13 -0500 Subject: [PATCH] DAQ funtion commented. --- daq.c | 151 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 74 insertions(+), 77 deletions(-) diff --git a/daq.c b/daq.c index 49dc1ef..e6db976 100644 --- a/daq.c +++ b/daq.c @@ -140,84 +140,81 @@ int str2int(unsigned char *ptr){ int DAQ(int t_hrs, int tm_s) { - FILE* dfp; // create a file pointer dfp - //int co2, co, o2, temp; - unsigned char co2_uf[10]="", co2_f[10]="", - co_uf[10]="", co_f[10]="", - o2_ppm[10]="", o2_xcent[10]="", - co2_temp[10]="", o2_temp[10]="", - co2_press[10]="", o2_press[10]="", - co2_relH[10]="", DATA[100]=""; - - clock_t start_t, end_t; - time_t new_time, prev_time, t0; - double tm_ms = tm_s*1e6 - 0.20e6; - double dif = tm_ms; - - //registro datos de inicio - dfp = fopen(data_file_path, "w"); // open file for writing - time_t curtime; - time(&curtime); - fprintf(dfp, "%sStarting DAQ\ntime(s)\tCO2 unfil(ppm)\tCO2 fil(ppm)\tCO unfil(ppm)\tCO fil(ppm)"\ - "\tO2(ppm)\tO2(%c)\tTemperaure C02(ºC*10)\tTemperaure 02(ºC)"\ - "\tPressure C02(.mBar)\tPresure 02(mBar)\tRelative Humidity(.)\n", - ctime(&curtime), '%'); // send the value to the file - fclose(dfp); // close the file using the file pointer - - //ciclo - t0 = time(NULL) + tm_s; - new_time = t0; - for(int i = 0; i < (t_hrs*3600); i=i+tm_s){ - - if (dif <= 0) printf("T=%d Communication time exceded.\n", (int)(new_time-t0)); - else usleep((int)dif); - - if (time(NULL) < new_time) while(new_time != time(NULL)); - else printf("T=%d time error.\n", (int)(new_time-t0)); - - start_t = clock(); - new_time = time(NULL); - //command transmitions - uartTransmit(CM31911, get_readigns); - uartTransmit(OX0052, Readings_OX); - uartTransmit(GC0017, get_readigns); - - //uart receptions - uartReceive(OX0052); - uartReceive(CM31911); - uartReceive(GC0017); - - memcpy(co2_uf, gmeasures(receive[GC0017], 'z', 5), 5); - memcpy(co2_f, gmeasures(receive[GC0017], 'Z', 5), 5); - memcpy(co_uf, gmeasures(receive[CM31911], 'z', 5), 5); - memcpy(co_f, gmeasures(receive[CM31911], 'Z', 5), 5); - memcpy(o2_ppm, gmeasures(receive[OX0052], 'O', 6), 6); - memcpy(o2_xcent, gmeasures(receive[OX0052], '%', 6), 6); - memcpy(co2_temp, gmeasures(receive[CM31911], 'T', 5), 5); - memcpy(o2_temp, gmeasures(receive[OX0052], 'T', 5), 5); - memcpy(co2_press,gmeasures(receive[CM31911], 'B', 5), 5); - memcpy(o2_press, gmeasures(receive[OX0052], 'P', 4), 4); - memcpy(co2_relH, gmeasures(receive[CM31911], 'H', 5), 5); - - sprintf(DATA, "%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s", - (int)(new_time-t0), co2_uf, co2_f, co_uf, co_f, o2_ppm, o2_xcent, - co2_temp, o2_temp, co2_press, o2_press, co2_relH); - - printf("time(s)\tCO2 unfil(ppm)\tCO2 fil(ppm)\tCO unfil(ppm)\tCO fil(ppm)"\ - "\tO2(ppm)\tO2(%c)\tTemperaure C02(ºC*10)\tTemperaure 02(ºC)"\ - "\tPressure C02(.mBar)\tPresure 02(mBar)\tRelative Humidity(.)\n", '%'); - printf("%s\n", DATA); - - dfp = fopen(data_file_path, "a"); // open file for writing - fprintf(dfp, "%s\n", DATA); - fclose(dfp); // close the file using the file pointer - new_time += tm_s; - end_t = clock(); - - dif = tm_ms - ((double)(end_t - start_t)*1e6 / CLOCKS_PER_SEC); - } + FILE* dfp; // create a file pointer + unsigned char co2_uf[10]="", co2_f[10]="", + co_uf[10]="", co_f[10]="", + o2_ppm[10]="", o2_xcent[10]="", + co2_temp[10]="", o2_temp[10]="", + co2_press[10]="", o2_press[10]="", + co2_relH[10]="", DATA[100]=""; //measurements varirables + clock_t start_t, end_t; //processing time measurements variables + time_t new_time, prev_time, t0; //time control variables + double tm_ms = tm_s*1e6 - 0.20e6; //tiempo de muestreo en milisegundos (-) margen + double dif = tm_ms; //diferencia de tiempo(time to sleep) + + //registro datos de inicio + dfp = fopen(data_file_path, "w"); // open file for writing + time_t curtime; //current time (date) + time(&curtime); //saving date in curtime + fprintf(dfp, "%sStarting DAQ\ntime(s)\tCO2 unfil(ppm)\tCO2 fil(ppm)\tCO unfil(ppm)\tCO fil(ppm)"\ + "\tO2(ppm)\tO2(%c)\tTemperaure C02(ºC*10)\tTemperaure 02(ºC)"\ + "\tPressure C02(.mBar)\tPresure 02(mBar)\tRelative Humidity(.)\n", + ctime(&curtime), '%'); // send the value to the file + fclose(dfp); // close the file using the file pointer + + //ciclo + t0 = time(NULL) + tm_s; //intitial time = current time + tiempo de muestreo + new_time = t0; //saving initial time in time control variable + for(int i = 0; i < (t_hrs*3600); i=i+tm_s){ //cycle from 0 to adquisition time (seconds), incrementing sampling period + + if (dif <= 0) printf("T=%d Communication time exceded.\n", (int)(new_time-t0)); + else usleep((int)dif); //checking if there's time to sleep + //waiting new second (time.h system) to start measurements + if (time(NULL) < new_time) while(new_time != time(NULL)); + else printf("T=%d time error.\n", (int)(new_time-t0)); + + start_t = clock(); //saving start time + new_time = time(NULL); //refresh new_time (next iteration) + //transmiting commands to sensors + uartTransmit(CM31911, get_readigns); + uartTransmit(OX0052, Readings_OX); + uartTransmit(GC0017, get_readigns + //receiving replys from sensors + uartReceive(OX0052); + uartReceive(CM31911); + uartReceive(GC0017); + //saving gas measurements in measurements variables + memcpy(co2_uf, gmeasures(receive[GC0017], 'z', 5), 5); + memcpy(co2_f, gmeasures(receive[GC0017], 'Z', 5), 5); + memcpy(co_uf, gmeasures(receive[CM31911], 'z', 5), 5); + memcpy(co_f, gmeasures(receive[CM31911], 'Z', 5), 5); + memcpy(o2_ppm, gmeasures(receive[OX0052], 'O', 6), 6); + memcpy(o2_xcent, gmeasures(receive[OX0052], '%', 6), 6); + memcpy(co2_temp, gmeasures(receive[CM31911], 'T', 5), 5); + memcpy(o2_temp, gmeasures(receive[OX0052], 'T', 5), 5); + memcpy(co2_press,gmeasures(receive[CM31911], 'B', 5), 5); + memcpy(o2_press, gmeasures(receive[OX0052], 'P', 4), 4); + memcpy(co2_relH, gmeasures(receive[CM31911], 'H', 5), 5); + //saving formated measurements in string DATA + sprintf(DATA, "%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s", + (int)(new_time-t0), co2_uf, co2_f, co_uf, co_f, o2_ppm, o2_xcent, + co2_temp, o2_temp, co2_press, o2_press, co2_relH); + //showing measurements on display + printf("time(s)\tCO2 unfil(ppm)\tCO2 fil(ppm)\tCO unfil(ppm)\tCO fil(ppm)"\ + "\tO2(ppm)\tO2(%c)\tTemperaure C02(ºC*10)\tTemperaure 02(ºC)"\ + "\tPressure C02(.mBar)\tPresure 02(mBar)\tRelative Humidity(.)\n", '%'); + printf("%s\n", DATA); + + dfp = fopen(data_file_path, "a"); // open file for writing + fprintf(dfp, "%s\n", DATA); // saving measurements string to data file + fclose(dfp); // close the file using the file pointer + new_time += tm_s; //adding sampling period to new_time + end_t = clock(); //saving end time + //calculate time to sleep + dif = tm_ms - ((double)(end_t - start_t)*1e6 / CLOCKS_PER_SEC); + } - return 0; + return 0; } char *gmeasures(char src[], char fval, int nchar){