-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDAQReader.cpp
343 lines (263 loc) · 10.1 KB
/
DAQReader.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <QtGui>
#include <cmath>
#include "DAQReader.h"
#if defined(USE_NIDAQMXBASE)
#include <NIDAQmxBase.h>
#elif defined(USE_COMEDI)
#include <unistd.h>
#include <comedilib.h>
#else
#error No DAQ library was defined!
#endif
#define DAQCheck(x) DAQCheckHandler(#x,x)
DAQReader::DAQReader() :
shouldStop(false),
numChannels(1),
dt(0.01),
mutex(QMutex::Recursive)
{
Vmins[0] = -10.0;
Vmaxes[0] = 10.0;
}
void DAQReader::updateDAQSettings(const DAQSettings& settings)
{
numChannels = settings.numChannels;
dt = 1.0/settings.samplingRate;
for (int chan = 0; chan < numChannels; ++chan) {
Vmins[chan] = settings.minVoltage[chan];
Vmaxes[chan] = settings.maxVoltage[chan];
}
}
int DAQReader::appendData(QMap<int, QVector<QPointF> >* curveMap)
{
QMutexLocker lock(&mutex);
int numScans = newDataBuffer[0].count();
double tStart = ((*curveMap)[0].isEmpty()
? 0.0 : ((*curveMap)[0].last().x() + dt));
for (int chan = 0; chan < numChannels; ++chan) {
QVector<QPointF>& points = (*curveMap)[chan];
points.reserve(points.count() + newDataBuffer[chan].count());
for (int scan = 0; scan < newDataBuffer[chan].count(); ++scan) {
points.push_back(QPointF(
qreal(tStart + scan*dt),
newDataBuffer[chan][scan]
));
}
newDataBuffer[chan].clear();
}
return numScans;
}
void DAQReader::stop()
{
shouldStop=true;
}
#ifdef USE_NIDAQMXBASE
void DAQReader::run()
{
TaskHandle recordingTask = NULL;
char channelNames[256];
sprintf(channelNames, "Dev1/ai0:%d", numChannels-1);
if (
DAQCheck( DAQmxBaseCreateTask("",&recordingTask) )
&& DAQCheck( DAQmxBaseCreateAIVoltageChan(
recordingTask, channelNames, NULL,
DAQmx_Val_Diff, Vmins[0], Vmaxes[0], DAQmx_Val_Volts, NULL))
&& DAQCheck( DAQmxBaseCfgSampClkTiming(
recordingTask, "OnboardClock", int(1/dt),
DAQmx_Val_Rising, DAQmx_Val_ContSamps, 0))
&& DAQCheck(DAQmxBaseStartTask(recordingTask))
) {
const int updateInterval = 10; // ms
float64 buffer[maxChannels*maxScansPerSecond*updateInterval/1000];
const int scansPerRead = int(1/dt)*updateInterval/1000;
const float64 timeout = 0.1; // seconds
emit startedRecording();
while (!shouldStop) {
int32 numScansRead;
// HACK: because we need to run this on the GUI thread on the mac,
// it may take longer than our update interval to run the emit call
// and thus we may need to read more than one update's worth of
// data per emit to avoid having large amounts of data queue up.
// The only way to find out if there's queued data is to do a read
// with a timeout of zero and see if you get a timeout error, so
// that's what we do.
float64 nextTimeout = timeout;
bool emitNewData = false;
int daqReadError;
while (0 == (daqReadError = DAQmxBaseReadAnalogF64(
recordingTask, scansPerRead, nextTimeout,
DAQmx_Val_GroupByScanNumber, buffer,
scansPerRead*numChannels, &numScansRead, NULL
))) {
QMutexLocker lock(&mutex);
nextTimeout = 0;
emitNewData = true;
for (int chan = 0; chan < numChannels; ++chan) {
newDataBuffer[chan].reserve(
newDataBuffer[chan].count() + numScansRead
);
}
for (int scan = 0; scan < numScansRead; ++scan) {
for (int chan = 0; chan < numChannels; ++chan) {
newDataBuffer[chan].push_back(
buffer[scan*numChannels + chan]
);
}
}
}
if (emitNewData)
emit newData();
const int32 timeoutError = -200284;
if (daqReadError != timeoutError)
DAQCheck(daqReadError);
}
shouldStop = false;
emit stoppedRecording();
}
if (recordingTask != NULL) {
DAQCheck( DAQmxBaseStopTask(recordingTask) );
DAQCheck( DAQmxBaseClearTask(recordingTask) );
}
}
bool DAQReader::DAQCheckHandler(const char* cmd, int error)
{
if( DAQmxFailed(error) ) {
shouldStop = true;
int messageBufLength = DAQmxBaseGetExtendedErrorInfo(NULL,0);
char* messageBuf = new char[messageBufLength];
DAQmxBaseGetExtendedErrorInfo(messageBuf, messageBufLength);
emit daqError(
QString::number(error) + tr(": \"") + QString(messageBuf)
+ tr("\"\nWhile processing command:\n\"") + QString(cmd)
);
delete messageBuf;
return false;
}
else
{
return true;
}
}
#endif
#ifdef USE_COMEDI
void DAQReader::run()
{
comedi_t *dev;
comedi_cmd c,*cmd=&c;
unsigned int chanlist[maxChannels];
int aref = AREF_DIFF;
int subdevice = 0;
const int updateInterval = 100; // ms
const int targetSamplingRate = 250000/numChannels;
int overSampling = std::max(1, int(targetSamplingRate*dt));
dev = comedi_open("/dev/comedi0");
if(!dev){
DAQCheckHandler("comedi_open",-1);
}
else {
int range[maxChannels];
comedi_range* crange[maxChannels];
int maxdata[maxChannels];
/* Set up channel list */
for(int chan=0; chan<numChannels; ++chan){
range[chan] = comedi_find_range(dev, subdevice, chan,
UNIT_volt, Vmins[chan], Vmaxes[chan]);
crange[chan] = comedi_get_range(dev, subdevice, chan, range[chan]);
maxdata[chan] = comedi_get_maxdata(dev, subdevice, chan);
chanlist[chan]=CR_PACK(chan,range[chan],aref);
}
memset(cmd,0,sizeof(*cmd));
if (DAQCheck(
comedi_get_cmd_generic_timed(dev,subdevice,cmd,
numChannels,
(unsigned int)(1e9*dt/overSampling)))
) {
/* Modify parts of the command */
cmd->chanlist = chanlist;
cmd->chanlist_len = numChannels;
cmd->convert_arg /= numChannels;
cmd->scan_end_arg = numChannels;
cmd->stop_src=TRIG_NONE;
cmd->stop_arg=0;
int secondComediTestResult;
if (DAQCheck(comedi_command_test(dev,cmd))
&& DAQCheck( secondComediTestResult =
comedi_command_test(dev, cmd) )
&& DAQCheck( (secondComediTestResult == 0) ? 0 : -1 )
&& DAQCheck( comedi_command(dev, cmd) )) {
// make sure we have an accurate sampling rate
dt = cmd->scan_begin_arg*1.0e-9*overSampling;
emit startedRecording();
const int bufferSize =
maxChannels*maxScansPerSecond/1000*updateInterval;
sampl_t buffer[bufferSize];
int bytesRead;
int nextChan = 0;
bool stopping = false;
double overSampleSum[maxChannels];
int overSampleCount = 0;
for (int chan = 0; chan < numChannels; ++chan) {
overSampleSum[chan] = 0.0;
}
while (DAQCheck(bytesRead =
read(comedi_fileno(dev),buffer, bufferSize))
&& bytesRead > 0) {
if (shouldStop) {
shouldStop = false;
stopping = true;
comedi_cancel(dev, subdevice);
}
{
QMutexLocker lock(&mutex);
DAQCheck((bytesRead & 1) ? -1 : 0); // no partial samples!
int numScansRead = bytesRead/sizeof(sampl_t)/numChannels;
for (int chan = 0; chan < numChannels; ++chan) {
newDataBuffer[chan].reserve(
newDataBuffer[chan].count()
+ numScansRead/overSampling + 1
);
}
for (unsigned i = 0; i < bytesRead/sizeof(sampl_t); ++i,
nextChan = (nextChan+1)%numChannels) {
overSampleSum[nextChan] += comedi_to_phys(
buffer[i], crange[nextChan], maxdata[nextChan]
);
if (nextChan == numChannels-1) {
++overSampleCount;
if (overSampleCount == overSampling) {
overSampleCount = 0;
for (int chan = 0; chan < numChannels; ++chan) {
newDataBuffer[chan].push_back(
overSampleSum[chan]/overSampling);
overSampleSum[chan] = 0.0;
}
}
}
}
}
emit newData();
}
emit stoppedRecording();
}
}
comedi_close(dev);
}
}
bool DAQReader::DAQCheckHandler(const char* cmd, int error)
{
if( error < 0 ) {
shouldStop = true;
int errorNumber = comedi_errno();
QString message = QString(comedi_strerror(errorNumber));
emit daqError(
QString::number(errorNumber) + tr(": \"") + message
+ tr("\"\nWhile processing command:\n\"") + QString(cmd)
);
return false;
}
else
{
return true;
}
}
#endif