Home > Motorized 4-Slide Stage with Integrated Controller > Relocation Repeatability > Stage Repeatability Script in MATLAB

Stage Repeatability Script in MATLAB

% Parameters:
imdir = 'C:\...\2014-11-07-Y4-Bay4';
pref = 'testY4-2014-11-07-Bay4-';
ext = 'jpg';
dist = 5000; % Move-out distance
ntest = 50; % Number of repetiotions from each side
settletime = 0.2; % Settling time in seconds
targetposX = 98000; % Target position for Bay1-15000, Bay2-45000, Bay3-75000, Bay4-98000
targetposY = 30000;
count = 0;

% Before use open serial port.
[s,erro] = EZStepperOpenSerial('COM3');

% Create XYStage object:
xy = XYStage('PortObj', s, 'ControllerNameX', 3, 'ControllerNameY', 4, ...
    'LeadUmX', 1000, 'LeadUmY', 1000, ...
    'MotorStepsX', 400, 'MotorStepsY', 200, ...
    'HoldCurrentX', 5, 'HoldCurrentY', 5, ...
    'RunCurrentX', 65, 'RunCurrentY', 65, ...
    'StepDivideX', 256, 'StepDivideY', 256, ...
    'SlewVelocityX', 15000, 'SlewVelocityY', 15000, ...
    'InitVelocityX', 0, 'InitVelocityY', 0, ...
    'RampX', 50, 'RampY', 50, ...
    'WithLimits', 1, 'Log', 1);

% Home
errh = XYHomeComplete(xy,5000,5000);

% Start Camera (GigE color camera AVT Manta G282C)
vid1 = videoinput('gige',1,'Mono8');
vid1.FramesPerTrigger = 1;
vid1.ROIPosition = [500 300 900 800]; % x1 y1 xext yext
triggerconfig(vid1, 'manual');
vid1.TriggerRepeat = 1000000000;
src1 = getselectedsource(vid1);
src1.PacketSize = 9014;
src1.PacketDelay = 100;
src1.ExposureAuto = 'Off';
src1.ExposureTimeAbs = 80;
src1.AcquisitionFrameRateAbs = 46;
src1.Gamma = 0.8;
start(vid1);


% Test Run from Top Left (TL)
for j = 1 : ntest
    % Move to TL
    err1 = XYAbsolute(xy,targetposX-dist,targetposY-dist); XYWait(xy);
    % Move to Target Position
    err2 = XYAbsolute(xy,targetposX,targetposY); XYWait(xy);
    % Wait to Settle
    pause(settletime);
    % Snap image
    frame = getsnapshot(vid1);
    % Show and Save image
    figure(1); imshow(frame); drawnow;
    count = count+1;
    imwrite(frame,[imdir '\' pref sprintf('%03d',count) '.' ext],ext,'Quality',95);
end

% Test Run from Bottom Right (BR)
    for j = 1 : ntest
    % Move to TL
    err3 = XYAbsolute(xy,targetposX+dist,targetposY+dist); XYWait(xy);
    % Move to Target Position
    err4 = XYAbsolute(xy,targetposX,targetposY); XYWait(xy);
    % Wait to Settle
    pause(settletime);
    % Snap image
    frame = getsnapshot(vid1);
    % Show and Save image
    figure(2); imshow(frame); drawnow;
    count = count+1;
    imwrite(frame,[imdir '\' pref sprintf('%03d',count) '.' ext],ext,'Quality',95);
end

% Close camera
stop(vid1); delete(vid1); clear vid1;

% Delete XYStage object
delete(xy); clear xy;

%After use close serial port
[errc] = EZStepperCloseSerial(s);