DAQ funtion commented.

master
Sayeth 5 years ago
parent 77ce5114b3
commit b90947c35e

53
daq.c

@ -140,24 +140,22 @@ 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;
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]="";
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;
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;
time(&curtime);
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",
@ -165,28 +163,27 @@ int DAQ(int t_hrs, int tm_s)
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){
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);
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();
new_time = time(NULL);
//command transmitions
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);
//uart receptions
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);
@ -198,22 +195,22 @@ int DAQ(int t_hrs, int tm_s)
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);
fprintf(dfp, "%s\n", DATA); // saving measurements string to data file
fclose(dfp); // close the file using the file pointer
new_time += tm_s;
end_t = clock();
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);
}

Loading…
Cancel
Save