Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

rheodyne232objtimer(serialObject)
function timerObject = rheodyne232objtimer(serialObject)
% RHEODYNE232OBJTIMER returns the timer to be used in Rheodyne serial objs
% timerObject = rheodyne232objtimer(serialObject) returns the timer object
% to be used with the rheodyne serial object (stored in UserData).

% checks the number of arguments
error(nargchk(1, 1, nargin))

% checks the serial object
if ~isserial(serialObject)
    % error
    error('Must supply a valid serial object.')
end

% defines the default period (in seconds)
period = 5;

% generates the timer
timerObject = timer('ExecutionMode', 'fixedRate',...
                    'Period', period,...
                    'StartDelay', period,...
                    'TimerFcn', {@rheodyneObjTimerCallback, serialObject},...
                    'Tag', 'rheodyne',...
                    'ErrorFcn', @rheodyneObjErrorCallback,...
                    'ObjectVisibility', 'off',...
                    'UserData', struct('lastTimerFcn', NaN));


function rheodyneObjTimerCallback(timerObject, eventdata, serialObject)
% RHEODYNEOBJTIMERCALLBACK changes the valve time

% gets the user data
userData = timerObject.userData;

% stores the timer Fcn time (so when the timer period is changed mid-cycle,
% the start delay can be adjusted so that the timer period doesn't become
% longer than intended
userData.lastTimerFcn = now;

% try-catch for robustness
try
    % checks that the user data contains the last valve position (this is
    % necessary to know where to change the valve position next)
    if ~isfield(userData, 'currentValvePosition')
        % gets the valve position
        userData.currentValvePosition = rheodyne232objreadvalveposition(serialObject) - 1;
    end
    
    % defines the new position
    newValvePosition = ~userData.currentValvePosition;

catch
    % it guesses instead
    newValvePosition = 0;
end

% checks the fast mode is there first
if ~isfield(serialObject.UserData, 'fastMode')
    % set it
    serialObject.UserData.fastMode = true;
end

% sends the command to change the valve position
try
    % robustness
    if serialObject.UserData.fastMode
        % sets it
        rheodyne232objwritevalvepositionfast(serialObject, newValvePosition + 1)

    else
        % sets it more robustly
        rheodyne232objwritevalveposition(serialObject, newValvePosition + 1)
    end

    % saves the valve position back to user data
    userData.currentValvePosition = newValvePosition;

catch
    % warnings
    warning('rheodyne232obj:valveChangeError', 'Did not correctly change the Rheodyne valve position.')
end

% saves user data back
set(timerObject, 'UserData', userData)


function rheodyneObjErrorCallback(timerObject, eventdata)
% RHEODYNEOBJERRORCALLBACK errors appropriately

% brings up an error dialog
errordlg('Rheodyne timer (RS232) has errored and is now stopped.  Restart the timer only if you know what you''re doing.')

Contact us