This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
5462f59
commit bd8dfa0
Showing
11 changed files
with
2,708 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
#include "NIDAQmxInterface.h" | ||
#include "C:\Program Files (x86)\National Instruments\NI-DAQ\DAQmx ANSI C Dev\include\NIDAQmx.h" | ||
|
||
/* initialize tasks in DAQmx interface */ | ||
NIDAQmxInterface::NIDAQmxInterface() | ||
{ | ||
|
||
} | ||
|
||
void NIDAQmxInterface::DAQmxInitializeInterface() | ||
{ | ||
/* initialize daq pointer */ | ||
DAQmxSetDefaultEngine(); | ||
|
||
/* configure digital read error task */ | ||
DAQmxCreateTask("DigError", &daq->di_error); | ||
//DAQmxErrChk(DAQmxCreateTask("DigError", &daq->di_error)); | ||
// DAQmxErrChk(DAQmxCreateDIChan(daq->di_error, "Dev1/port2", "", DAQmx_Val_ChanForAllLines)); | ||
// DAQmxReadErrorPort(); | ||
|
||
// /* configure digital read task */ | ||
// DAQmxErrChk(DAQmxCreateTask("DigIn", &daq->di_port)); | ||
// DAQmxErrChk(DAQmxCreateDIChan(daq->di_port, "Dev1/port0", "", DAQmx_Val_ChanForAllLines)); | ||
// DAQmxErrChk(DAQmxCfgChangeDetectionTiming(daq->di_port, "Dev1/port0/line2,Dev1/port0/line6", "Dev1/port0/line2,Dev1/port0/line6", DAQmx_Val_ContSamps, 1)); | ||
// //DAQmxErrChk(DAQmxRegisterSignalEvent(daq->di_port, DAQmx_Val_ChangeDetectionEvent, 0, FcnCbckDetectDI, daq)); | ||
|
||
// /* configure digital write task */ | ||
// DAQmxErrChk(DAQmxCreateTask("DigOut", &daq->do_port)); | ||
// DAQmxErrChk(DAQmxCreateDOChan(daq->do_port, "Dev1/port1", "", DAQmx_Val_ChanForAllLines)); | ||
|
||
// /* configure digital internal clock task */ | ||
// DAQmxErrChk(DAQmxCreateTask("DigClk", &daq->do_clk)); | ||
// DAQmxErrChk(DAQmxCreateCOPulseChanTime(daq->do_clk, "Dev1/ctr0", "", DAQmx_Val_Seconds, DAQmx_Val_Low, 0, 0.05, DO_PULSE_WIDTH)); | ||
// DAQmxErrChk(DAQmxCfgImplicitTiming(daq->do_clk, DAQmx_Val_FiniteSamps, 1)); | ||
// //DAQmxErrChk(DAQmxRegisterDoneEvent(daq->do_clk, 0, FcnCbckTriggerStopDO, daq)); | ||
|
||
// /* initialize query performance timer QPC */ | ||
// //QPCInitialize(); | ||
|
||
// /* start custom task */ | ||
// DAQmxStartCustomTask(daq->di_port); | ||
|
||
return; | ||
} | ||
|
||
/* allocate engine pointer */ | ||
void NIDAQmxInterface::DAQmxSetDefaultEngine() | ||
{ | ||
char file_out[256]; | ||
char date_str[32]; | ||
time_t now = time(NULL); | ||
struct tm *t = localtime(&now); | ||
strftime(date_str, sizeof(date_str), "%Y%m%d_%H%M", t); | ||
sprintf(file_out, "..\\Logs\\%s_TriggerTasks_logFile.txt", date_str); | ||
|
||
|
||
/* ports TaskHandle */ | ||
daq->di_port = 0; | ||
daq->di_error = 0; | ||
daq->do_port = 0; | ||
|
||
/* ports states uInt8 */ | ||
daq->di_bit_now = DAQ_PORT_NULL; | ||
daq->di_bit_prev = DAQ_PORT_NULL; | ||
daq->do_bit_wrt = DAQ_PORT_NULL; | ||
daq->do_bit_qry = DAQ_PORT_NULL; | ||
daq->flag_scanner = DAQ_PORT_NULL; | ||
|
||
/* event counters uInt32 */ | ||
daq->di_cntr_lick = 0; | ||
daq->di_cntr_frame = 0; | ||
daq->di_cntr_imaq = 0; | ||
|
||
/* log file pointer */ | ||
// daq->fp_log = fopen(file_out, "w"); | ||
// if (daq->fp_log == NULL) | ||
// { | ||
// fprintf(stderr, "DAQmxError:NIDAQmxInterface/DAQmxSetDefaultEngine:\n\tfailed to open %s to write!\n", file_out); | ||
// exit(EXIT_FAILURE); | ||
// } | ||
// fprintf(daq->fp_log, "#event\tbit_mask\tstate\ttick[usec]\n"); | ||
|
||
return; | ||
} | ||
|
||
/* read error port */ | ||
void NIDAQmxInterface::DAQmxReadErrorPort() | ||
{ | ||
// int32 read = 0; | ||
// uInt8 port_bit = DAQ_PORT_NULL; | ||
|
||
// /* start task */ | ||
// DAQmxStartCustomTask(daq->di_error); | ||
|
||
// /* read port */ | ||
// if (daq->di_error != 0) | ||
// DAQmxErrChk(DAQmxReadDigitalU8(daq->di_error, 1, 10.0, DAQmx_Val_GroupByChannel, &port_bit, 1, &read, NULL)); | ||
|
||
// /* stop task */ | ||
// DAQmxStopCustomTask(daq->di_error); | ||
|
||
// /* check for active error signals */ | ||
// if (read) | ||
// { | ||
// if ((port_bit & DAQ_PORT_DE_MONITOR) == DAQ_PORT_DE_MONITOR) | ||
// { | ||
// fprintf(stderr, "DAQmxError:NIDAQmxInterface/DAQmxReadErrorPort:\n\tMONITOR failed!\n"); | ||
// exit(EXIT_FAILURE); | ||
// } | ||
|
||
// if ((port_bit & DAQ_PORT_DE_AIR) == DAQ_PORT_DE_AIR) | ||
// { | ||
// fprintf(stderr, "DAQmxError:NIDAQmxInterface/DAQmxReadErrorPort:\n\tAIR pump failed!\n"); | ||
// exit(EXIT_FAILURE); | ||
// } | ||
|
||
// if ((port_bit & DAQ_PORT_DE_WATER) == DAQ_PORT_DE_WATER) | ||
// { | ||
// fprintf(stderr, "DAQmxError:NIDAQmxInterface/DAQmxReadErrorPort:\n\tWATER pump failed!\n"); | ||
// exit(EXIT_FAILURE); | ||
// } | ||
// } | ||
|
||
// return; | ||
} | ||
|
||
|
||
/* destroy tasks in DAQmx interface */ | ||
void NIDAQmxInterface::DAQmxDestroyInterface() | ||
{ | ||
// /* check if task is digital output task is done */ | ||
// DAQmxErrChk(DAQmxWaitUntilTaskDone(daq->do_clk, -1)); | ||
|
||
// /* stop tasks */ | ||
// DAQmxStopCustomTask(daq->di_port); | ||
// DAQmxStopCustomTask(daq->do_port); | ||
// DAQmxStopCustomTask(daq->do_clk); | ||
|
||
// /* clear tasks */ | ||
// DAQmxErrChk(DAQmxClearTask(daq->di_port)); | ||
// daq->di_port = 0; | ||
// DAQmxErrChk(DAQmxClearTask(daq->do_port)); | ||
// daq->do_port = 0; | ||
// DAQmxErrChk(DAQmxClearTask(daq->do_clk)); | ||
// daq->do_clk = 0; | ||
|
||
// /* close file */ | ||
// if (daq->fp_log) | ||
// fclose(daq->fp_log); | ||
|
||
// return; | ||
} | ||
|
||
|
||
/* Start custom tasks */ | ||
void NIDAQmxInterface::DAQmxStartCustomTask(TaskHandle taskHandle) | ||
{ | ||
// bool32 done = false; | ||
|
||
// if (taskHandle != 0) | ||
// { | ||
// /* check if task is already active */ | ||
// DAQmxErrChk(DAQmxIsTaskDone(taskHandle, &done)); | ||
// if (!done) | ||
// DAQmxErrChk(DAQmxStopTask(taskHandle)); | ||
|
||
// /* start / re-start task */ | ||
// DAQmxErrChk(DAQmxStartTask(taskHandle)); | ||
// } | ||
|
||
return; | ||
} | ||
|
||
/* Stop custom tasks */ | ||
void NIDAQmxInterface::DAQmxStopCustomTask(TaskHandle taskHandle) | ||
{ | ||
// bool32 done = false; | ||
|
||
// if (taskHandle != 0) | ||
// { | ||
// /* check if task is active */ | ||
// DAQmxErrChk(DAQmxIsTaskDone(taskHandle, &done)); | ||
// if (!done) | ||
// DAQmxErrChk(DAQmxStopTask(taskHandle)); | ||
// } | ||
|
||
// return; | ||
} | ||
|
||
/* Trigger digital output */ | ||
void NIDAQmxInterface::DAQmxTriggerDO(uInt8 qry_port_bit, bool arm_timer) | ||
{ | ||
// int32 written = 0; | ||
// bool32 done = TRUE; | ||
|
||
// /* flip query bit to switch on qry bit */ | ||
// if (daq->do_port != 0) | ||
// { | ||
// daq->do_bit_wrt ^= qry_port_bit; | ||
// DAQmxErrChk(DAQmxWriteDigitalU8(daq->do_port, 1, 1, 10.0, DAQmx_Val_GroupByChannel, &daq->do_bit_wrt, &written, NULL)); | ||
// } | ||
|
||
// /* check for error */ | ||
// if (written == 0) | ||
// { | ||
// fprintf(stderr, "Error::NIDAQmxInterface/DAQmxTriggerDO:\n\tfailed to write to DO port!\n"); | ||
// exit(EXIT_FAILURE); | ||
// } | ||
|
||
// /* arm time out if required */ | ||
// if ((daq->do_clk != 0) & (arm_timer == TRUE)) | ||
// { | ||
// DAQmxErrChk(DAQmxIsTaskDone(daq->do_clk, &done)); | ||
// daq->do_bit_qry = (done) ? qry_port_bit : (daq->do_bit_qry | qry_port_bit); | ||
// DAQmxErrChk(DAQmxStopTask(daq->do_clk)); | ||
// DAQmxErrChk(DAQmxStartTask(daq->do_clk)); | ||
// } | ||
|
||
// /* log trigger */ | ||
// DAQmxLogToFile(daq->fp_log, "TRIGGER", qry_port_bit, (qry_port_bit & daq->do_bit_wrt) == qry_port_bit); | ||
|
||
// return; | ||
} | ||
|
||
/* log NIDAQmx events to file */ | ||
void NIDAQmxInterface::DAQmxLogToFile(FILE *fp, const char *msg, uInt32 id, uInt32 counter) | ||
{ | ||
if (fp) | ||
//fprintf(fp, "%s\t%d\t%d\t%ld\n", msg, id, counter, QPCReadTick()); | ||
return; | ||
} | ||
|
||
/* Detect Input Callback */ | ||
int32 CVICALLBACK NIDAQmxInterface::FcnCbckDetectDI(TaskHandle taskHandle, int32 signalID, void *callback_data) | ||
{ | ||
// DAQmxEngine *daq = (DAQmxEngine *)callback_data; | ||
// int32 read = 0; | ||
|
||
// if (taskHandle != 0) | ||
// DAQmxErrChk(DAQmxReadDigitalU8(taskHandle, 1, 10.0, DAQmx_Val_GroupByChannel, &daq->di_bit_now, 1, &read, NULL)); | ||
|
||
// if (read) | ||
// { | ||
// /* SHUTTER ON condition, SHUTTER line changes from 0 to 1 */ | ||
// if (((daq->di_bit_prev & DAQ_PORT_DI_SHUTTER) == DAQ_PORT_NULL) & | ||
// ((daq->di_bit_now & DAQ_PORT_DI_SHUTTER) == DAQ_PORT_DI_SHUTTER) & | ||
// (daq->flag_scanner == DAQ_PORT_NULL)) | ||
// { | ||
// daq->flag_scanner = DAQ_PORT_TRUE; | ||
// daq->di_cntr_imaq++; | ||
// daq->di_cntr_frame = 0; | ||
|
||
// /* switch OFF box internal resonance generator */ | ||
// DAQmxTriggerDO(DAQ_PORT_DO_FREQGEN, FALSE); | ||
// } | ||
|
||
// /* SHUTTER OFF condition, SHUTTER line changes from 1 to 0 */ | ||
// if (((daq->di_bit_prev & DAQ_PORT_DI_SHUTTER) == DAQ_PORT_DI_SHUTTER) & | ||
// ((daq->di_bit_now & DAQ_PORT_DI_SHUTTER) == DAQ_PORT_NULL) & | ||
// (daq->flag_scanner == DAQ_PORT_TRUE)) | ||
// { | ||
// daq->flag_scanner = DAQ_PORT_NULL; | ||
|
||
// /* switch ON box internal resonance generator */ | ||
// DAQmxTriggerDO(DAQ_PORT_DO_FREQGEN, FALSE); | ||
// } | ||
|
||
// /* FRAME condition, line 2 changes from 1 to 0 */ | ||
// if (((daq->di_bit_prev & DAQ_PORT_DI_FRAME) == DAQ_PORT_DI_FRAME) & ((daq->di_bit_now & DAQ_PORT_DI_FRAME) == DAQ_PORT_NULL)) | ||
// { | ||
// daq->di_cntr_frame++; | ||
// DAQmxLogToFile(daq->fp_log, "FRAME", daq->di_cntr_imaq, daq->di_cntr_frame); | ||
// } | ||
|
||
// /* LICK condition, line 6 changes from 0 to 1 */ | ||
// if (((daq->di_bit_prev & DAQ_PORT_DI_LICK) == DAQ_PORT_NULL) & ((daq->di_bit_now & DAQ_PORT_DI_LICK) == DAQ_PORT_DI_LICK)) | ||
// { | ||
// daq->di_cntr_lick++; | ||
// DAQmxLogToFile(daq->fp_log, "LICK", DAQ_PORT_DI_LICK, daq->di_cntr_lick); | ||
|
||
// /* every N licks give a reward */ | ||
// if ((daq->di_cntr_lick % 16) == 0) | ||
// DAQmxTriggerDO(DAQ_PORT_DO_WATER, TRUE); | ||
|
||
// } | ||
|
||
// /* update bit history */ | ||
// daq->di_bit_prev = daq->di_bit_now; | ||
// } | ||
|
||
return 0; | ||
} | ||
|
||
/* Timer Digital Out is Done */ | ||
int32 CVICALLBACK NIDAQmxInterface::FcnCbckTriggerStopDO(TaskHandle taskHandle, int32 signalID, void *callback_data) | ||
{ | ||
// DAQmxEngine *daq = (DAQmxEngine *)callback_data; | ||
// int32 written = 0; | ||
|
||
// /* flip query bit, to switch off pulse */ | ||
// if (daq->do_port != 0) | ||
// { | ||
// daq->do_bit_wrt ^= daq->do_bit_qry; | ||
// DAQmxErrChk(DAQmxWriteDigitalU8(daq->do_port, 1, 1, 10.0, DAQmx_Val_GroupByChannel, &daq->do_bit_wrt, &written, NULL)); | ||
// } | ||
|
||
// /* check for error */ | ||
// if (written == 0) | ||
// { | ||
// fprintf(stderr, "Error::NIDAQmxInterface/FcnCbckTriggerStopDO: failed to write to DO port!\n"); | ||
// exit(EXIT_FAILURE); | ||
// } | ||
|
||
// /* log trigger */ | ||
// DAQmxLogToFile(daq->fp_log, "TRIGGER", daq->do_bit_qry, (daq->do_bit_qry & daq->do_bit_wrt) == daq->do_bit_qry); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.