-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAQ.cc
91 lines (77 loc) · 2.43 KB
/
DAQ.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <thread>
#include <time.h>
#include "dask.h"
#include "conio.h"
#include "DAQ.h"
#include "TransferFunctions.h"
#include "database.h"
extern int testNumber;
extern long long sampleCount;
long long realSampleCount = 0;
I16 cardAI;
TransferFunctions func[MAX_CHAN];
struct timespec sampTime[MAX_CHAN], deadline;
uint64_t sensorUpdateThrottleNS[MAX_CHAN];
double localSensorVals[MAX_CHAN] = {0};
void releaseAI() {
Release_Card(cardAI);
}
void initAI() {
if ((cardAI = Register_Card(PCI_9113, 0)) < 0) {
printf("Register_Card error=%d\n", cardAI);
exit(1);
}
for(int i=0; i < MAX_CHAN; i++) {
std::string transfunc = getSensorTransferFunction(i);
if(transfunc.compare("")!=0) {
func[i].setFunction(transfunc);
} else {
func[i].setFunction("x");
}
}
for(int i=0; i < MAX_CHAN; i++) {
sensorUpdateThrottleNS[i] = 1000 * getSensorUpdateThrottle(i);
clock_gettime(CLOCK_MONOTONIC, &sampTime[i]);
}
// set sample tick time
deadline.tv_sec = 0;
deadline.tv_nsec = 1;
}
void tickAI() {
struct timespec currentTime;
U16 chan_data[MAX_CHAN];
F64 chan_voltage[MAX_CHAN];
for(int i = 0 ; i < MAX_CHAN; i++ ) {
clock_gettime(CLOCK_MONOTONIC, ¤tTime);
uint64_t diff = BILLION * (currentTime.tv_sec - sampTime[i].tv_sec) + currentTime.tv_nsec - sampTime[i].tv_nsec;
if(diff >= sensorUpdateThrottleNS[i]) {
I16 err;
if((err = AI_ReadChannel(cardAI, i, range, &chan_data[i])) == NoError) {
//printf("sampled %lld\n", realSampleCount);
realSampleCount++;
clock_gettime(CLOCK_MONOTONIC, &sampTime[i]);
AI_VoltScale(cardAI, range, chan_data[i], &chan_voltage[i]);
localSensorVals[i] = func[i].callFunction(chan_voltage[i]);
bufferSensorData(testNumber,&sampTime[i],i,chan_data[i],localSensorVals[i]);
} else {
printf("AI_ReadChannel Ch#%d error : error_code: %d \n", i, err );
}
} else {
// not time to sample
}
}
if(realSampleCount%4 == 0 && realSampleCount > 0) {
//printf("writing to database... %lld\n", realSampleCount);
std::thread databaseWriterThread(executeDatabaseWrite);
databaseWriterThread.detach();
}
sampleCount++;
};