Issue using a parfor loop to control two instruments at the same time
Show older comments
Hi all,
I am trying to control two instruments, 1) to tell a laser to sweep a range of wavelengths, 2) signal and read data from a NI data aquisition unit. As I am trying to record the signal as the laser sweeps I need both to run at the same time hence the parfor. I have tested the code individually and they are working my issue is getting them to work in a parfor loop. I am new to using libaries and setting up parpools so I might be doing something stupid (fingercrossed).
When I run the code below, i get the error ---> Library was not found
Below is the relevent code.
function output = main4
main1; %<------ Connects to laser and starts connection etc (WORKS FINE)
dq = MCTsetup %<----- Connects to data logger (WORKS FINE)
readout = 1;
delete(gcp('nocreate'));
parpool('AttachedFiles',{'MIRcatSDK'});
parfor i=1:2
if i == 1
pause(0.1);
runlasersweep; %<------ begins laser sweep scan (Code below)
end
if i == 2
readout = read(dq, seconds(5)); %<------ begins the data logger
end
end
main3; %<---- turn off laser emission (WORKS FINE)
output = readout;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%runlasersweep - code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function runlasersweep
load('MIRcatSDKconstants.mat');
fprintf('========================================================\n');
ret = calllib('MIRcatSDK','MIRcatSDK_StartSweepScan', ...
single(1200), single(1900), single(500), ...
MIRcatSDK_UNITS_CM1, uint16(1), true, uint8(1));
if MIRcatSDK_RET_SUCCESS == ret
fprintf(' Successful\n' );
else
% If the operation fails, unload the library and raise an error.
fprintf(' Failure\n' );
calllib('MIRcatSDK','MIRcatSDK_DeInitialize');
unloadlibrary MIRcatSDK;
error('Error! Code: %d', ret);
end
isScanInProgress = true;
isScanInProgressPtr = libpointer('bool', isScanInProgress);
isScanActive = false;
isScanActivePtr = libpointer('bool', isScanActive);
isScanPaused = false;
isScanPausedPtr = libpointer('bool', isScanPaused);
curScanNum = uint16(0);
curScanNumPtr = libpointer('uint16Ptr', curScanNum);
curScanPercent = uint16(0);
curScanPercentPtr = libpointer ('uint16Ptr', curScanPercent);
curWW = single(0);
curWWPtr = libpointer('singlePtr', curWW);
isTECinProgress = false;
isTECinProgressPtr = libpointer('bool', isTECinProgress);
isMotionInProgress = false;
isMotionInProgressPtr = libpointer('bool', isMotionInProgress);
units = uint8(0);
unitsPtr = libpointer('uint8Ptr', units);
fprintf('========================================================\n');
fprintf('Test: Get Scan Status\n');
while isScanInProgress
calllib('MIRcatSDK','MIRcatSDK_GetScanStatus', ...
isScanInProgressPtr, isScanActivePtr, isScanPausedPtr, ...
curScanNumPtr, curScanPercentPtr, curWWPtr, unitsPtr, ...
isTECinProgressPtr, isMotionInProgressPtr);
isScanInProgress = isScanInProgressPtr.value;
isScanActive = isScanActivePtr.value;
isScanPaused = isScanPausedPtr.value;
curScanNum = curScanNumPtr.value;
curScanPercent = curScanPercentPtr.value;
curWW = curWWPtr.value;
units = unitsPtr.value;
isTECinProgress = isTECinProgressPtr.value;
isMotionInProgress = isMotionInProgressPtr.value;
fprintf(['\tIsScanInProgress: %d \tIsScanActive: %d \tisScanPaused: %d', ...
'\tcurScanNum: %d \tcurWW: %.3f \tunits: %u \tcurScanPercent: %.2f', ...
'\tisTECinProgress: %d \tisMotionInProgress: %d\n'], ...
isScanInProgress, isScanActive, isScanPaused, curScanNum, curWW, ...
units, curScanPercent, isTECinProgress, isMotionInProgress);
pause(0.3);
end
end
1 Comment
Accepted Answer
More Answers (0)
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!