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.

tempobjgui(varargin)
function varargout = tempobjgui(varargin)
% TEMPOBJGUI M-file for tempobjgui.fig
%      TEMPOBJGUI, by itself, creates a new TEMPOBJGUI or raises the existing
%      singleton*.
%
%      H = TEMPOBJGUI returns the handle to a new TEMPOBJGUI or the handle to
%      the existing singleton*.
%
%      TEMPOBJGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEMPOBJGUI.M with the given input arguments.
%
%      TEMPOBJGUI('Property','Value',...) creates a new TEMPOBJGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before tempobjgui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to tempobjgui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help tempobjgui

% Last Modified by GUIDE v2.5 12-Oct-2009 12:06:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 0;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @tempobjgui_OpeningFcn, ...
                   'gui_OutputFcn',  @tempobjgui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before tempobjgui is made visible.
function tempobjgui_OpeningFcn(hObject, eventdata, handles, mainGuiHandle, serialObject)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to tempobjgui (see VARARGIN)

% checks the serial object
if nargin < 5
    % serial object must be supplied and must be valid
    error('Cannot open GUI - serial object absent.')
end

% brings up a waitbar
waitBarHandle = waitbar(0, 'Retrieving information...');

% sets the pointer to an hourglass
set(hObject, 'Pointer', 'watch')

% gets the number from the serial object
serialObjectUserData = serialObject.UserData;

% defines the collect data flag field
collectDataFlag = sprintf('collectDataFlag%d', serialObjectUserData.objectNumber);

% turns the collect data flag off temporarily - this is necessary to prevent the
% serial timer interrupting the collection of all the data
setappdata(mainGuiHandle, collectDataFlag, 0)

% stashes away the main gui handle
handles.mainGuiHandle = mainGuiHandle;

% stashes away the serial object for future use
handles.serialObject = serialObject;

% Update the handles structure
guidata(hObject, handles)

% try-catches everything in case something interrupts any of the commands
try
    % gets the auto/manual mode
    mode = tempobjreadmode(serialObject);

    % sets the correct value
    switch mode
        case 0
            % sets it to auto, and disables the output power field
            set(handles.autoRadio, 'Value', 1)
            set(handles.outputPower, 'Enable', 'off')

        case 1
            % sets it to manual, and enables the output power field
            set(handles.manualRadio, 'Value', 1)
            set(handles.outputPower, 'Enable', 'on')

        case 2
            % sets it to off, and disables the output power field
            set(handles.offRadio, 'Value', 1)
            set(handles.outputPower, 'Enable', 'off')

        otherwise
            % brings an an error dialog
            errordlg('Unknown mode type selected.')
    end

    % updates waitbar
    waitbar(1 / 5, waitBarHandle)

    % sets the output power
    set(handles.outputPower, 'String', num2str(tempobjreadoutputpower(serialObject)))

    % updates waitbar
    waitbar(2 / 5, waitBarHandle)

    % sets the minimum temperature
    set(handles.minTemp, 'String', num2str(tempobjreadmintemp(serialObject)))

    % updates waitbar
    waitbar(3 / 5, waitBarHandle)

    % sets the maximum temperature
    set(handles.maxTemp, 'String', num2str(tempobjreadmaxtemp(serialObject)))

    % updates waitbar
    waitbar(4 / 5, waitBarHandle)

    % gets the set point rate limit
    set(handles.setPointRateLimit, 'String', num2str(tempobjreadsetpointratelimit(serialObject)))

    % updates waitbar
    waitbar(1, waitBarHandle)
    
catch
    % brings up an question dialog to ask use what they want to do if the
    % data couldn't be fetched
    selection = questdlg(   'The Temperature controller configuration could not be retrieved.  Would you like to continue anyway?',...
                            'More Options Menu Confirmation',...
                            'Yes', 'No',...
                            'Yes');

    % close the GUI if 'No' was selected
    if strcmp(selection, 'No')
        % deletes the GUI
        delete(hObject)
    end
end

% sets the pointer back to an arrow
set(hObject, 'Pointer', 'arrow')

% closes waitbar
close(waitBarHandle)

% turns the collect data flag back on
setappdata(mainGuiHandle, collectDataFlag, 1)


% --- Outputs from this function are returned to the command line.
function tempobjgui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on button press in autoRadio.
function autoRadio_Callback(hObject, eventdata, handles)
% hObject    handle to autoRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of autoRadio

% always sets the value to 1 so that it doesn't get unchecked
set(hObject, 'Value', 1)

% runs the mode_Callback to send the correct command
mode_Callback(hObject, eventdata, handles)


% --- Executes on button press in manualRadio.
function manualRadio_Callback(hObject, eventdata, handles)
% hObject    handle to manualRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of manualRadio

% always sets the value to 1 so that it doesn't get unchecked
set(hObject, 'Value', 1)

% runs the mode_Callback to send the correct command
mode_Callback(hObject, eventdata, handles)


% --- Executes on button press in mode panel
function mode_Callback(callingObject, eventdata, handles)
% callingObject	handle to callingObject (see GCBO)
% eventdata     reserved - to be defined in a future version of MATLAB
% handles       structure with handles and user data (see GUIDATA)

% gets the mode
mode(1) = get(handles.autoRadio, 'Value');
mode(2) = get(handles.manualRadio, 'Value');
mode(3) = get(handles.offRadio, 'Value');

% try-catch for error handling
try
    % send the command to change it
    tempobjwritemode(handles.serialObject, find(mode) - 1)

    % enables the output power box only if its in manual mode, disables it
    % otherwise
    set(handles.outputPower, 'Enable', onoff(mode(2)))
    
    % fetches the current output power
    set(handles.outputPower, 'String', num2str(tempobjreadoutputpower(handles.serialObject)))
   
catch
    % displays an error dialog
    errordlg('Could not change the control mode.')
    
    % set the value back to the old one (copy and pasted from the opening
    % sequence
    
    % gets the auto/manual mode
    mode = tempobjreadmode(handles.serialObject);

    % sets the correct value
    switch mode
        case 0
            % sets it to auto, and disables the output power field
            set(handles.autoRadio, 'Value', 1)
            set(handles.outputPower, 'Enable', 'off')

        case 1
            % sets it to manual, and enables the output power field
            set(handles.manualRadio, 'Value', 1)
            set(handles.outputPower, 'Enable', 'on')

        case 2
            % sets it to off, and disables the output power field
            set(handles.offRadio, 'Value', 1)
            set(handles.outputPower, 'Enable', 'off')

        otherwise
            % brings an an error dialog
            errordlg('Unknown mode type selected.')
    end
end


% --- Executes on button press in readRadio.
function readRadio_Callback(hObject, eventdata, handles)
% hObject    handle to readRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of readRadio

% always sets the value to 1 so that it doesn't get unchecked
set(hObject, 'Value', 1)


% --- Executes on button press in writeRadio.
function writeRadio_Callback(hObject, eventdata, handles)
% hObject    handle to writeRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of writeRadio

% always sets the value to 1 so that it doesn't get unchecked
set(hObject, 'Value', 1)


function minTemp_Callback(hObject, eventdata, handles)
% hObject    handle to minTemp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of minTemp as text
%        str2double(get(hObject,'String')) returns contents of minTemp as a double

% tries to set the new minimum temperature
try
    % sends command
    tempobjwritemintemp(handles.serialObject, str2double(get(hObject, 'String')))
catch
    % displays an error dialog
    errordlg('Could not set minimum temperature')
    
    % tries to refill the box with the current value
    set(hObject, 'String', num2str(tempobjreadmintemp(handles.serialObject)))
end


% --- Executes during object creation, after setting all properties.
function minTemp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minTemp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function maxTemp_Callback(hObject, eventdata, handles)
% hObject    handle to maxTemp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxTemp as text
%        str2double(get(hObject,'String')) returns contents of maxTemp as a double

% tries to set the new maximum temperature
try
    % sends command
    tempobjwritemaxtemp(handles.serialObject, str2double(get(hObject, 'String')))
catch
    % displays an error dialog
    errordlg('Could not set maximum temperature')
    
    % tries to refill the box with the current value
    set(hObject, 'String', num2str(tempobjreadmaxtemp(handles.serialObject)))
end


% --- Executes during object creation, after setting all properties.
function maxTemp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxTemp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in readManual.
function readManual_Callback(hObject, eventdata, handles)
% hObject    handle to readManual (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% opens the manual
open('Eurotherm 2416 Temperature Controller Manual.pdf')


% --- Executes on button press in readCommsManual.
function readCommsManual_Callback(hObject, eventdata, handles)
% hObject    handle to readCommsManual (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% opens the comms manual
open('Eurotherm Series 2000 Modbus and EI-Bisynch Digital Communications Handbook.pdf')


function outputPower_Callback(hObject, eventdata, handles)
% hObject    handle to outputPower (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outputPower as text
%        str2double(get(hObject,'String')) returns contents of outputPower as a double

% tries to set the new output power
try
    % sends command
    tempobjwriteoutputpower(handles.serialObject, str2double(get(hObject, 'String')))
catch
    % displays an error dialog
    errordlg('Could not set output power.')
    
    % tries to refill the box with the current value
    set(hObject, 'String', num2str(tempobjreadoutputpower(handles.serialObject)))
end


% --- Executes during object creation, after setting all properties.
function outputPower_CreateFcn(hObject, eventdata, handles)
% hObject    handle to outputPower (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function setPointRateLimit_Callback(hObject, eventdata, handles)
% hObject    handle to setPointRateLimit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of setPointRateLimit as text
%        str2double(get(hObject,'String')) returns contents of setPointRateLimit as a double

% tries to set the set point rate limit
try
    % sends command
    tempobjwritesetpointratelimit(handles.serialObject, str2double(get(hObject, 'String')))
catch
    % displays an error dialog
    errordlg('Could not set new set point rate limit')
    
    % tries to refill the box with the current value
    set(hObject, 'String', num2str(tempobjreadsetpointratelimit(handles.serialObject)))
end


% --- Executes during object creation, after setting all properties.
function setPointRateLimit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to setPointRateLimit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function parameterAddress_Callback(hObject, eventdata, handles)
% hObject    handle to parameterAddress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of parameterAddress as text
%        str2double(get(hObject,'String')) returns contents of parameterAddress as a double

% sets the new value
newValue = str2double(get(hObject, 'String'));

% checks the value
if ~ispositiveinteger(newValue) || ~isequal(newValue, 0)
    % shows an error dialog
    errordlg('Parameter address must be a positive integer (0 allowed).')
end


% --- Executes during object creation, after setting all properties.
function parameterAddress_CreateFcn(hObject, eventdata, handles)
% hObject    handle to parameterAddress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function value_Callback(hObject, eventdata, handles)
% hObject    handle to value (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of value as text
%        str2double(get(hObject,'String')) returns contents of value as a double

% sets the new value
newValue = str2double(get(hObject, 'String'));

% checks the value
if ~ispositiveinteger(newValue)
    % shows an error dialog
    errordlg('Value/items to read must be a positive integer.')
end


% --- Executes during object creation, after setting all properties.
function value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to value (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function response_Callback(hObject, eventdata, handles)
% hObject    handle to response (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of response as text
%        str2double(get(hObject,'String')) returns contents of response as a double


% --- Executes during object creation, after setting all properties.
function response_CreateFcn(hObject, eventdata, handles)
% hObject    handle to response (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in command.
function command_Callback(hObject, eventdata, handles)
% hObject    handle to command (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% sets the values
parameterAddress = str2double(get(handles.parameterAddress, 'String'));
value = str2double(get(handles.value, 'String'));

% if there is no valid parameter or value selected... (they start empty,
% and if the input is changed, they are subject to parsing, so a NaN check
% is adequate
if isnan(value)
    % brings up an error dialog
    warntooltip(hObject, 'Must choose a valid parameter address')

elseif isnan(value)
    % brings up an error dialog
    warntooltip(hObject, 'Must choose a valid value/items to read')

else
    % decides on the command mode
    switch true
        case get(handles.writeRadio, 'Value')
            % command mode
            commandMode = 'write';

        case get(handles.writeFullResRadio, 'Value')
            % command mode
            commandMode = 'writefullres';

        case get(handles.readRadio, 'Value')
            % command mode
            commandMode = 'read';

        case get(handles.readFullResRadio, 'Value')
            % command mode
            commandMode = 'readfullres';

        otherwise
            % error
            error('Unknown command mode for Eurotherm.')
    end

    % for the command...
    try
        % if the command mode is writing...
        if any(strfind(commandMode, 'write'))
            % don't need to get the response
            tempobjcomm(commandMode, handles.serialObject, 1, parameterAddress, value)

        else
            % we do want to do something with the response
            response = tempobjcomm(commandMode, handles.serialObject, 1, parameterAddress, value);

            % formats the response
            responseString = num2str(response, '%.3f ');

            % fills the response box (ignores the last space)
            set(handles.response, 'String', responseString(1:end - 1))
        end
        
    catch
        % bring up an error response
        errortooltip(hObject, 'Could not send command.')
    end
end


% --- Executes on button press in readFullResRadio.
function readFullResRadio_Callback(hObject, eventdata, handles)
% hObject    handle to readFullResRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of readFullResRadio

% always sets the value to 1 so that it doesn't get unchecked
set(hObject, 'Value', 1)


% --- Executes on button press in writeFullResRadio.
function writeFullResRadio_Callback(hObject, eventdata, handles)
% hObject    handle to writeFullResRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of writeFullResRadio

% always sets the value to 1 so that it doesn't get unchecked
set(hObject, 'Value', 1)


% --- Executes on button press in offRadio.
function offRadio_Callback(hObject, eventdata, handles)
% hObject    handle to offRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of offRadio

% always sets the value to 1 so that it doesn't get unchecked
set(hObject, 'Value', 1)

% runs the mode_Callback to send the correct command
mode_Callback(hObject, eventdata, handles)

Contact us