Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation



06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

rheodyne232objwriteperiod(serialObject, period)
function rheodyne232objwriteperiod(serialObject, period)
% RHEODYNE232OBJWRITEPERIOD writes the period to the Rheodyne timer object
% rheodyne232objwriteperiod(serialObject, period) changes the timer period of
% the Rheodyne timer.  serialObject is a serial object which contains the
% timer object in its user data. The period is required as a scalar in
% seconds.

% e.g. rheodyne232objwriteperiod(rheodyneobj, period) - changes the period of
% the rheodyne timer, e.g. to 5 (s).

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

% error handling
if ~isserial(serialObject) || ~isrunning(serialObject)
    % errors
    error('serialObject must be a valid open serial object.')
elseif ~isnumeric(period) || ~isscalar(period) || isnan(period) || ~isreal(period) || period < 2.5 || isinf(period)
    % errors
    error('Timer period must be a real number no smaller than 2.5 seconds.')

% see if it was running or not
running = isrunning(serialObject.UserData.timerObject);

% formats the period so MATLAB doesn't whinge about sub-millisecond
% precision - also doubles it, since the timer period covers TWO valve
% turns - necessary to avoid erratic switching behaviour
period = roundto(period, 3);

% if it was running
if running
    % briefly stops the timer
    % gets the user data
    userData = serialObject.UserData.timerObject;
    % changes the start delay to the new period, less the time since the
    % last execution of the timer function BUT no less than 0 (done using
    % the max([x, 0]) method)
    if isfield(userData, 'lastTimerFcn') && ~isnan(lastTimerFcn)
        % changes it
        startDelay = max([roundto(period - ((now - userData.lastTimerFcn) * 24 * 60 * 60), 3), 0]);
        % sets it to the period to avoid too rapid switching)
        startDelay = period;
    % applies it
    set(serialObject.UserData.timerObject, 'StartDelay', startDelay)
    % changes the start delay to 0 so it starts straight away when the
    % timer is restarted
    set(serialObject.UserData.timerObject, 'StartDelay', 0);

% changes the timer period
set(serialObject.UserData.timerObject, 'Period', period)

% restarts the timer only if it was running before
if running
    % restarts the timer

Contact us