-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniworker.cpp
143 lines (119 loc) · 5.02 KB
/
niworker.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
#include "niworker.h"
#include <NIDAQmx.h>
#include <QDebug>
#include <atomic>
static const float64 SAMPLE_FACTOR=1.2;//1
static const uInt64 MAX_COUNT=900000*SAMPLE_FACTOR;
NIWorker::NIWorker():m_rate(2000000.0*SAMPLE_FACTOR){
m_htaskCtr=NULL;m_htaskAO=NULL;m_htaskDO=NULL;
m_sampleCount=20006*SAMPLE_FACTOR;m_bChanged=false;m_bUsetrigger=true;
}
NIWorker::~NIWorker(){
stop();
}
void NIWorker::closeTask(TaskHandle &t){
if(NULL!=t){
TaskHandle _t=t;t=NULL;
DAQmxStopTask(_t);DAQmxClearTask(_t);
}
}
bool NIWorker::stopClock(){
return !m_htaskCtr||!failed(DAQmxStopTask(m_htaskCtr));
}
bool NIWorker::startClock(){
return m_htaskCtr&&!failed(DAQmxStartTask(m_htaskCtr));
}
bool NIWorker::failed(int32 e){
bool bOK=!DAQmxFailed(e);
if(!bOK){
char errBuff[2048]={'\0'};
DAQmxGetExtendedErrorInfo(errBuff,2048);
qDebug()<<"DAQmx Error:"<<errBuff;
}
return !bOK;
}
bool NIWorker::init(double amplitude, double offset,int larserTriggerPort){
m_bChanged=false;qDebug()<<"init NI"<<amplitude<<offset;
if(failed(DAQmxCreateTask("",&m_htaskCtr))||failed(DAQmxCreateTask("",&m_htaskAO)
||failed(DAQmxCreateTask("",&m_htaskDO)))){
qWarning()<<"failed to create task";return false;
}
uInt64 i=0;int32 written;//const uInt64 sampleCount=2006;
uInt64 sampleCount=m_sampleCount;
float64 riseCount=sampleCount/2;
static float64 *dataAO=(float64*)malloc(MAX_COUNT*2);//float64 dataAO[MAX_COUNT];
for(i=0;i<sampleCount;i++){
float64 v=0;
if(i<riseCount){
v=(i+0.0)*2/riseCount-1;
}else{
v=-1*(i+0.0-riseCount)*2/(sampleCount-1-riseCount)+1;
}
v*=amplitude;v+=offset;
dataAO[i]=v;
}
if(failed(DAQmxCreateAOVoltageChan(m_htaskAO,"Dev1/ao1","",-10.0,10.0,DAQmx_Val_Volts,NULL))
||failed(DAQmxCfgSampClkTiming(m_htaskAO,"/Dev1/PFI12",m_rate,DAQmx_Val_Rising,
DAQmx_Val_ContSamps,sampleCount))
//||failed(DAQmxCfgDigEdgeStartTrig(m_htaskAO,"/Dev1/PFI12",DAQmx_Val_Rising))
||failed(DAQmxWriteAnalogF64(m_htaskAO,sampleCount,0,-1,DAQmx_Val_GroupByChannel,
dataAO,&written,NULL))
||failed(DAQmxStartTask(m_htaskAO))){
qWarning()<<"failed to create AO channel";return false;
}
//const uInt64 sampleDOCount=sampleCount*5;//const uInt64 delayCount=8;
uInt64 laserCount=riseCount,cameraCount=2000*SAMPLE_FACTOR;
uInt64 laserStart=300*SAMPLE_FACTOR,cameraStart=100*SAMPLE_FACTOR;
uInt64 sampleCountDO=sampleCount;
static uInt8 *dataDO=(uInt8*)malloc(MAX_COUNT*5*2);//uInt8 dataDO[MAX_COUNT*5*2];
const int port=larserTriggerPort-4;
for(i=0;i<sampleCount;i++){
for(int j=0;j<4;j++){
uInt8 v=((i>laserStart&&i<laserCount)?1:0);
if(port>=0&&port!=j){v=0;}
dataDO[5*i+j]=v;
}
dataDO[5*i+4]=((i>cameraStart&&i<cameraCount)?1:0);
}
if(failed(DAQmxCreateDOChan(m_htaskDO,"Dev1/port0/line4:7,Dev1/port0/line0",
"",DAQmx_Val_ChanForAllLines))
||failed(DAQmxCfgSampClkTiming(m_htaskDO,"/Dev1/PFI12",m_rate,DAQmx_Val_Rising,
DAQmx_Val_ContSamps,sampleCountDO))
||failed(DAQmxWriteDigitalLines(m_htaskDO,sampleCountDO,0,-1,DAQmx_Val_GroupByChannel,
dataDO,&written,NULL))
||failed(DAQmxStartTask(m_htaskDO))){
qWarning()<<"failed to create DO channel";return false;
}
static const float64 highTime=0.0000003/SAMPLE_FACTOR;
static const float64 lowTime=0.0000002/SAMPLE_FACTOR;
if(failed(DAQmxCreateCOPulseChanTime(m_htaskCtr,"Dev1/ctr0","",DAQmx_Val_Seconds,DAQmx_Val_Low,0,highTime,lowTime))
||failed(DAQmxCfgImplicitTiming(m_htaskCtr,DAQmx_Val_ContSamps,100000))
||failed(DAQmxConnectTerms("/Dev1/Ctr0InternalOutput","/Dev1/PFI12",DAQmx_Val_DoNotInvertPolarity))
||(m_bUsetrigger&&failed(DAQmxCfgDigEdgeStartTrig(m_htaskCtr,"/Dev1/PFI0",DAQmx_Val_Rising)))
||failed(DAQmxStartTask(m_htaskCtr))
){
qWarning()<<"failed to create counter";return false;
}
return true;
}
void NIWorker::stop(){qDebug()<<"NI stop all";
closeTask(m_htaskCtr);closeTask(m_htaskAO);closeTask(m_htaskDO);
}
void NIWorker::setSampleCount(uInt64 sampleCount){
static const uInt64 MIN_COUNT=3000*SAMPLE_FACTOR;
qDebug()<<sampleCount<<MAX_COUNT<<MIN_COUNT;
if(sampleCount>MAX_COUNT){sampleCount=MAX_COUNT;}
if(sampleCount<MIN_COUNT){sampleCount=MIN_COUNT;}
m_sampleCount=sampleCount;
m_bChanged=true;
}
void NIWorker::setInterval(float time){
uInt64 t=time*20000.0*SAMPLE_FACTOR/10.0;
setSampleCount(t);
}
bool NIWorker::changed(){return m_bChanged;}
void NIWorker::useTrigger(bool bUse){
if(bUse==m_bUsetrigger){return;}
qDebug()<<(bUse?"":"do not")<<"use ni trigger";
m_bUsetrigger=bUse;m_bChanged=true;
}