You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
7.7 KiB
C
213 lines
7.7 KiB
C
/*
|
|
*/
|
|
|
|
#include<stdio.h>
|
|
#include<fcntl.h>
|
|
#include<unistd.h>
|
|
#include<string.h>
|
|
#include<stdlib.h>
|
|
#include<time.h>
|
|
#include"uart.c"
|
|
|
|
#define data_file_path "/var/lib/cloud9/daq-serial-bbb/data.dat"
|
|
#define SprintIR 4 //co2 sensor
|
|
#define COAF 1 //co sensor
|
|
#define LuminOX 2 //o2
|
|
|
|
#define Rep_Dev_ID "Y\r\n"
|
|
#define Pulling_Mode "K 2\r\n"
|
|
#define OX_P_Mode "M 1\r\n"
|
|
#define FILnUNFIL "M 6\r\n"
|
|
#define unf_gas_con "z\r\n"
|
|
#define fil_gas_con "Z\r\n"
|
|
#define Temperature "T\r\n"
|
|
#define get_readigns "Q\r\n"
|
|
#define percent_oxigen "%\r\n"
|
|
#define ppm_oxigen "O\r\n"
|
|
#define M_zZTHBD "M 14406\r\n"
|
|
#define Readings_OX "A\r\n"
|
|
|
|
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
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
int T_acq, t_samp;
|
|
|
|
if(argc!=3){
|
|
printf("Invalid number of arguments,\n"\
|
|
"Usage: daq <time(h)> <sampling period(s)>\nexiting!\n");
|
|
return -2;
|
|
}
|
|
|
|
//configuración de sensores
|
|
printf("Configuring CO2 sensor\n");
|
|
sensConf(SprintIR, B9600, Pulling_Mode, " K 00002\r\n", 5);
|
|
sensConf(SprintIR, B9600, FILnUNFIL, " M 00006\r\n", 5);
|
|
printf("Configuring CO sensor\n");
|
|
sensConf(COAF, B9600, Pulling_Mode, "K 00002\r\n", 5);
|
|
sensConf(COAF, B9600, M_zZTHBD, "M 14406\r\n", 5);
|
|
printf("Configuring O2 sensor\n");
|
|
sensConf(LuminOX, B9600, OX_P_Mode, "M 01\r\n", 5);
|
|
|
|
//adquisición
|
|
T_acq = atoi(argv[1]);//str to int
|
|
t_samp = atoi(argv[2]);
|
|
printf("Starting data acquisition with duration of %dh every %ds\n", T_acq, t_samp);
|
|
DAQ(T, t);
|
|
|
|
//deinit uart
|
|
uartClose(SprintIR);
|
|
uartClose(COAF);
|
|
uartClose(LuminOX);
|
|
//finalización
|
|
printf("Exiting of the program...\n");
|
|
return 0;
|
|
}
|
|
|
|
|
|
int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[], int tries)
|
|
{
|
|
int count;
|
|
|
|
uartConf(uartNumber, baudRate);
|
|
while(tries){
|
|
uartTransmit(uartNumber, mode);
|
|
tcdrain(uartFile[uartNumber]); //wait all data has been sent
|
|
printf("Command sended.\n");
|
|
//usleep(100000); //give the sensor a chance to respond
|
|
count = uartReceive(uartNumber);
|
|
if (count == 0) printf("There was no data available to read!\n");
|
|
else if (strcmp(receive[uartNumber], response) == 0) {
|
|
printf("Sensor configurated.\n");
|
|
return 0;
|
|
} else {
|
|
printf("The following was read in [%d]: %s\n",count,receive[uartNumber]);
|
|
//char *c = receive[uartNumber];
|
|
//while (*c != '\0'){
|
|
// printf("%d = '%c'\n",*c, *c);
|
|
// c++;
|
|
//}
|
|
}
|
|
tries --;
|
|
}
|
|
printf("Sensor configuration failed.\n");
|
|
return -1;
|
|
}
|
|
|
|
int measure(unsigned char uartNumber, unsigned char command[], int multiplier, int excess){
|
|
int measure;
|
|
uartTransmit(uartNumber, command);
|
|
uartReceive(uartNumber);
|
|
measure = (str2int(&receive[uartNumber]) - excess)*multiplier ;
|
|
return measure;
|
|
}
|
|
|
|
int read_measure(unsigned char uartNumber, int multiplier, int excess){
|
|
int measure;
|
|
uartReceive(uartNumber);
|
|
measure = (str2int(&receive[uartNumber]) - excess)*multiplier ;
|
|
return measure;
|
|
}
|
|
|
|
int str2int(unsigned char *ptr){
|
|
|
|
int number = 0;
|
|
|
|
while (*ptr != '\0') {
|
|
if ((*ptr >= '0') && (*ptr <= '9')) {
|
|
int dig = (*ptr) - '0';
|
|
number = number*10 + dig;
|
|
}
|
|
ptr++;
|
|
}
|
|
return number;
|
|
}
|
|
|
|
int DAQ(int t_hrs, int tm_s)
|
|
{
|
|
FILE* dfp; // create a file pointer
|
|
const char colums[200] = "t(s)\tCO2 uf(ppm)\tCO2(ppm)\tCO uf(ppm)\t"\
|
|
"CO(ppm)\tO2(ppm)\tO2(%%)\tT C02(ºC*10)\t"\
|
|
"T 02(ºC)\tP C02(.mBar)\tP 02(mBar)\tRH(.)\n";
|
|
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
|
|
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(&curtime); //saving date in curtime
|
|
//registro datos de inicio
|
|
dfp = fopen(data_file_path, "w"); // open file for writing
|
|
// send the value to the file
|
|
fprintf(dfp, "Starting DAQ at %s\n", ctime(&curtime);
|
|
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));
|
|
|
|
start_t = clock(); //saving start time
|
|
//transmiting commands to sensors
|
|
uartTransmit(COAF, get_readigns);
|
|
uartTransmit(LuminOX, Readings_OX);
|
|
uartTransmit(SprintIR, get_readigns
|
|
//receiving replys from
|
|
uartReceive(COAF);
|
|
uartReceive(LuminOX);
|
|
uartReceive(SprintIR);
|
|
//interpreting and spliting measurements in variables
|
|
memcpy(co2_uf, gmeasures(receive[SprintIR],'z', 5), 5);
|
|
memcpy(co2_f, gmeasures(receive[SprintIR],'Z', 5), 5);
|
|
memcpy(co_uf, gmeasures(receive[COAF], 'z', 5), 5);
|
|
memcpy(co_f, gmeasures(receive[COAF], 'Z', 5), 5);
|
|
memcpy(o2_ppm, gmeasures(receive[LuminOX], 'O', 6), 6);
|
|
memcpy(o2_xcent, gmeasures(receive[LuminOX], '%', 6), 6);
|
|
memcpy(co2_temp, gmeasures(receive[COAF], 'T', 5), 5);
|
|
memcpy(o2_temp, gmeasures(receive[LuminOX], 'T', 5), 5);
|
|
memcpy(co2_press,gmeasures(receive[COAF], 'B', 5), 5);
|
|
memcpy(o2_press, gmeasures(receive[LuminOX], 'P', 4), 4);
|
|
memcpy(co2_relH, gmeasures(receive[COAF], '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",
|
|
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
|
|
fclose(dfp); // close the file using the file pointer
|
|
next_samp_time += tm_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);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
char *gmeasures(char src[], char fval, int nchar){
|
|
char * ptr = &src[0];
|
|
static char s[10]="";
|
|
|
|
while(*ptr != fval) ptr++;
|
|
ptr += 2;
|
|
memcpy(s, ptr, nchar);
|
|
return s;
|
|
}
|