diff --git a/daq.c b/daq.c index 01e2d2a..94ac048 100644 --- a/daq.c +++ b/daq.c @@ -29,9 +29,9 @@ int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[], int tries); int measure(unsigned char uartNumber, unsigned char command[], int multiplier, int excess); -char *gmeasures(char src[], char fval, int nchar -int str2int(unsigned char *ptr -int DAQ(int t_hrs, int tm_s +char *gmeasures(char src[], char fval, int nchar); +int str2int(unsigned char *ptr); +int DAQ(int t_hrs, int sp_s); int main(int argc, char *argv[]){ @@ -127,7 +127,7 @@ int str2int(unsigned char *ptr){ return number; } -int DAQ(int t_hrs, int tm_s) +int DAQ(int t_hrs, int sp_s) { FILE* dfp; // create a file pointer const char colums[200] = "t(s)\tCO2 uf(ppm)\tCO2(ppm)\tCO uf(ppm)\t"\ @@ -141,9 +141,9 @@ int DAQ(int t_hrs, int tm_s) co2_relH[10]="", DATA[100]=""; //measurements varirables time_t curtime; //current time (date) clock_t start_t, end_t; //processing time measurements variables - time_t next_samp_time; //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) + time_t next_samp_time, t0; //time control variables + double iteration_time_ms = sp_s*1e6 - 0.2e6; //cicle iteration maximum time + double inactivity_time = 1; //time to sleep time(&curtime); //saving date in curtime //registro datos de inicio @@ -153,21 +153,25 @@ int DAQ(int t_hrs, int tm_s) fclose(dfp); // close the file using the file pointer printf("%s", colums); //display variables colums //ciclo - next_samp_time = time(NULL) + tm_s; //setting nex sampling time - for(int t = 0; t < (t_hrs*3600); t+=tm_s){ //cycle from 0 to adquisition time (seconds), incrementing sampling period - //checking if there's time to sleep - if (dif <= 0) printf("Ejecution time exceded.\n%s", colums); - else usleep((int)dif); //inactivity time - //waiting to start measurements - while(next_samp_time != time(NULL)); + next_samp_time = time(NULL)+1; //setting next sampling time + t0 = next_samp_time; //saving initial time + for(time_t t = 0; t < (t_hrs*3600); t+=sp_s){ //cycle from 0 to adquisition time (seconds), incrementing sampling period + //checking inactivity time + if (inactivity_time <= 1){ + usleep((int)inactivity_time); //inactivity + } + else{ + printf("Ejecution time exceded.\n%s", colums); + next_samp_time = time(NULL)+1; //updating time values + t = next_samp_time-t0; + } + while(next_samp_time != time(NULL)); //synchronizing/waiting to start measurements start_t = clock(); //saving start time - //transmiting commands to sensors - uartTransmit(COAF, get_readigns); + uartTransmit(COAF, get_readigns); //transmiting commands to sensors uartTransmit(LuminOX, Readings_OX); uartTransmit(SprintIR, get_readigns - //receiving replys from - uartReceive(COAF); + uartReceive(COAF); //receiving replys from uartReceive(LuminOX); uartReceive(SprintIR); //interpreting and spliting measurements in variables @@ -185,17 +189,17 @@ int DAQ(int t_hrs, int tm_s) //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", - t, co2_uf, co2_f, co_uf, co_f, o2_ppm, o2_xcent, + (int)t, co2_uf, co2_f, co_uf, co_f, o2_ppm, o2_xcent, co2_temp, o2_temp, co2_press, o2_press, co2_relH); printf("\r%s", DATA); //showing measurements on display dfp = fopen(data_file_path, "a"); // open file for writing - fprintf(dfp, "%s\n", DATA); // saving measurements string to data file + fprintf(dfp, "%s\n", DATA); // saving measurements string to data file fclose(dfp); // close the file using the file pointer - next_samp_time += tm_s; //adding sampling period to next_samp_time + next_samp_time += sp_s; //adding sampling period to next_samp_time end_t = clock(); //saving end time //calculate time to sleep - dif = tm_ms - ((double)(end_t - start_t)*1e6 / CLOCKS_PER_SEC); + inactivity_time = iteration_time_ms - ((double)(end_t - start_t)*1e6 / CLOCKS_PER_SEC); } return 0;