Code covered by the BSD License  

Highlights from
PID Basics

image thumbnail

PID Basics

by

 

04 Sep 2012 (Updated )

PID Basics is a nice way to obtain a quick intuition and understanding of PID control.

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

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

% Last Modified by GUIDE v2.5 03-Aug-2012 13:31:00

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @tfProcess_OpeningFcn, ...
                   'gui_OutputFcn',  @tfProcess_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 tfProcess is made visible.
function tfProcess_OpeningFcn(hObject, eventdata, handles, varargin)
% 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 tfProcess (see VARARGIN)

% Choose default command line output for tfProcess
handles.output = hObject;

set(handles.customProcess, 'SelectionChangeFcn', ...
    {@customProcess_Change, handles})

% Update handles structure
guidata(handles.tfProcess, handles);

% UIWAIT makes tfProcess wait for user response (see UIRESUME)
uiwait(handles.tfProcess);


% --- Outputs from this function are returned to the command line.
function varargout = tfProcess_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)

% Get default command line output from handles structure
varargout{1} = handles.G;

delete(handles.tfProcess)


% --- Executes when user attempts to close tfProcess.
function tfProcess_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to tfProcess (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.G = [];
guidata(handles.tfProcess, handles);
uiresume(handles.tfProcess)


% --- Executes when Uibuttongroup is activated
function customProcess_Change(hObject, eventdata, handles)

selectedOption = get(get(hObject,'SelectedObject'),'Tag');

switch selectedOption
    case{'laplace'}
        enableDisableControls({'on','off','off'}, handles)              
    case{'zpk'}
        enableDisableControls({'off','on','off'}, handles)              
    case{'tf'}
        enableDisableControls({'off','off','on'}, handles)              
end


% --- Auxiliary function for enabling/disabling controls in the GUI
function enableDisableControls(enableDisable, handles)
set(handles.edit_laplace,'Enable',enableDisable{1})
set(handles.edit_z      ,'Enable',enableDisable{2})
set(handles.edit_p      ,'Enable',enableDisable{2})
set(handles.edit_k      ,'Enable',enableDisable{2})
set(handles.text_z      ,'Enable',enableDisable{2})
set(handles.text_p      ,'Enable',enableDisable{2})
set(handles.text_k      ,'Enable',enableDisable{2})
set(handles.edit_num    ,'Enable',enableDisable{3})
set(handles.edit_den    ,'Enable',enableDisable{3})
set(handles.text_num    ,'Enable',enableDisable{3})
set(handles.text_den    ,'Enable',enableDisable{3})


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


% --- Executes during object creation, after setting all properties.
function edit_laplace_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_laplace (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 edit_z_Callback(hObject, eventdata, handles)
% hObject    handle to edit_z (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function edit_z_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_z (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 edit_p_Callback(hObject, eventdata, handles)
% hObject    handle to edit_p (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function edit_p_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_p (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 edit_k_Callback(hObject, eventdata, handles)
% hObject    handle to edit_k (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function edit_k_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_k (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 edit_num_Callback(hObject, eventdata, handles)
% hObject    handle to edit_num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function edit_num_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_num (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 edit_den_Callback(hObject, eventdata, handles)
% hObject    handle to edit_den (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function edit_den_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_den (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 pushbutton_ok.
function pushbutton_ok_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_ok (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

selectedOption = get(get(handles.customProcess,'SelectedObject'),'Tag');

try    
    switch selectedOption
        case{'laplace'}
            s = tf('s');
            equation = get(handles.edit_laplace,'String');
            f = str2func(['@(s)', equation]);
            G = f(s);
            G = zpk(G);

        case{'zpk'}
            z = str2num(get(handles.edit_z,'String')); %#ok<ST2NM>
            p = str2num(get(handles.edit_p,'String')); %#ok<ST2NM>
            k = str2num(get(handles.edit_k,'String')); %#ok<ST2NM>
            G = zpk(z,p,k);
            
        case{'tf'}
            num = str2num(get(handles.edit_num,'String')); %#ok<ST2NM>
            den = str2num(get(handles.edit_den,'String')); %#ok<ST2NM>
            [z,p,k] = tf2zpk(num,den);
            G = zpk(z,p,k);
    end
    
    set(G,'DisplayFormat','Time');

    handles.G = G;
    guidata(handles.tfProcess, handles);

    uiresume(handles.tfProcess)

catch me
    msgbox('Please check you have typed your inputs correctly',...
        'Inputs not valid', 'error');
end
    

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

tfProcess_CloseRequestFcn(handles.tfProcess, eventdata, handles);

Contact us