In: Electrical Engineering
Write a MATLAB program to demonstrate the Ziegler-Nichols method of PID loop tuning. Also in no more than one A4 page justify how your MATLAB code demonstrates the PID loop tuning procedure.
function sys = CLS( Wp,Wc ) %CLS Closed loop system function %% Parameters % Wp : Plant transfer function % Wc : Controller transfer function % sys : Closed Loop transfer function with assuming unity feedback. %% Function implementation sys=feedback(series(Wp,Wc),1); end
function [ Wp ] = CreatePlant( num,den ) %CreatePlant Creates plant transfer function. % The returned value is the system in numerator/denomerator format %% Parameters % num : Numerator vector (starting from highest order of coefficients) % den : Denomerator vector (starting from highest order of coefficients) % plant : Plant transfer function %% EXAMPLE % num=[1]; % den=[1 0 1]; % sys=CreatePlant(num,den) %% Result is % 1 % sys= --------------- % S^2+1 %% Function implementation syms s; Wp=tf(num,den); end
function varargout = example(varargin) % EXAMPLE MATLAB code for example.fig % EXAMPLE, by itself, creates a new EXAMPLE or raises the existing % singleton*. % % H = EXAMPLE returns the handle to a new EXAMPLE or the handle to % the existing singleton*. % % EXAMPLE('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in EXAMPLE.M with the given input arguments. % % EXAMPLE('Property','Value',...) creates a new EXAMPLE or raises % the existing singleton*. Starting from the left, property value pairs are % applied to the GUI before example_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to example_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 example % Last Modified by GUIDE v2.5 01-Sep-2014 10:35:27 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @example_OpeningFcn, ... 'gui_OutputFcn', @example_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 example is made visible. function example_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 example (see VARARGIN) % Choose default command line output for example handles.output = hObject; % Update handles structure guidata(hObject, handles); initialize_gui(hObject, handles, false); % UIWAIT makes example wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = example_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.output; % --- Executes during object creation, after setting all properties. function density_CreateFcn(hObject, eventdata, handles) % hObject handle to density (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu 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 density_Callback(hObject, eventdata, handles) % hObject handle to density (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 density as text % str2double(get(hObject,'String')) returns contents of density as a double density = str2double(get(hObject, 'String')); if isnan(density) set(hObject, 'String', 0); errordlg('Input must be a number','Error'); end % Save the new density value handles.metricdata.density = density; guidata(hObject,handles) % --- Executes during object creation, after setting all properties. function volume_CreateFcn(hObject, eventdata, handles) % hObject handle to volume (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu 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 volume_Callback(hObject, eventdata, handles) % hObject handle to volume (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 volume as text % str2double(get(hObject,'String')) returns contents of volume as a double volume = str2double(get(hObject, 'String')); if isnan(volume) set(hObject, 'String', 0); errordlg('Input must be a number','Error'); end % Save the new volume value handles.metricdata.volume = volume; guidata(hObject,handles) % --- Executes on button press in calculate. function calculate_Callback(hObject, eventdata, handles) % hObject handle to calculate (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) mass = handles.metricdata.density * handles.metricdata.volume; set(handles.mass, 'String', mass); % --- Executes on button press in reset. function reset_Callback(hObject, eventdata, handles) % hObject handle to reset (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) initialize_gui(gcbf, handles, true); % --- Executes when selected object changed in unitgroup. function unitgroup_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to the selected object in unitgroup % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if (hObject == handles.english) set(handles.text4, 'String', 'lb/cu.in'); set(handles.text5, 'String', 'cu.in'); set(handles.text6, 'String', 'lb'); else set(handles.text4, 'String', 'kg/cu.m'); set(handles.text5, 'String', 'cu.m'); set(handles.text6, 'String', 'kg'); end % -------------------------------------------------------------------- function initialize_gui(fig_handle, handles, isreset) % If the metricdata field is present and the reset flag is false, it means % we are we are just re-initializing a GUI by calling it from the cmd line % while it is up. So, bail out as we dont want to reset the data. if isfield(handles, 'metricdata') && ~isreset return; end handles.metricdata.density = 0; handles.metricdata.volume = 0; set(handles.density, 'String', handles.metricdata.density); set(handles.volume, 'String', handles.metricdata.volume); set(handles.mass, 'String', 0); set(handles.unitgroup, 'SelectedObject', handles.english); set(handles.text4, 'String', 'lb/cu.in'); set(handles.text5, 'String', 'cu.in'); set(handles.text6, 'String', 'lb'); % Update handles structure guidata(handles.figure1, handles);
function varargout = PIDExperiment(varargin) gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @PIDExperiment_OpeningFcn, ... 'gui_OutputFcn', @PIDExperiment_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 function PIDExperiment_OpeningFcn(hObject, eventdata, handles, varargin) % Choose default command line output for PIDExperiment handles.output = hObject; handles.tfdata.num=1; handles.tfdata.den=[1000 300 30 1]; handles.tfdata.t='0:0.01:300'; % Update handles structure guidata(hObject, handles); % --- Outputs from this function are returned to the command line. function varargout = PIDExperiment_OutputFcn(hObject, eventdata, handles) % Get default command line output from handles structure varargout{1} = handles.output; function num_Callback(hObject, eventdata, handles) num=str2num(get(handles.num,'string')); handles.tfdata.num=num; guidata(hObject,handles); % --- Executes during object creation, after setting all properties. function num_CreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function den_Callback(hObject, eventdata, handles) den=str2num(get(handles.den,'string')); handles.tfdata.den=den; guidata(hObject,handles); % --- Executes during object creation, after setting all properties. function den_CreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in gettf. function gettf_Callback(hObject, eventdata, handles) Plant=CreatePlant(handles.tfdata.num,handles.tfdata.den); handles.tfdata.plant=Plant; guidata(hObject,handles); sys=evalc('Plant'); set(handles.tf,'string',sys); % --- Executes on button press in plotnyquist. function plotnyquist_Callback(hObject, eventdata, handles) axes(handles.nyquistaxis); nyquist(handles.tfdata.plant); % --- Executes on button press in findkc. function findkc_Callback(hObject, eventdata, handles) [Kc,Pm,Wc,Wm]=margin(handles.tfdata.plant); pu=2*pi/Wc; set(handles.kc,'string',num2str(Kc)); set(handles.pu,'string',num2str(pu)); handles.tfdata.kc=Kc; handles.tfdata.pu=pu; guidata(hObject,handles); % --- Executes on button press in plotziegler. function plotziegler_Callback(hObject, eventdata, handles) %P-Controller Kpp=handles.tfdata.kc*0.5; Tip=100000; Tdp=0; %PI-Controller Kppi=handles.tfdata.kc*0.45; Tipi=handles.tfdata.pu*0.83; Tdpi=0; %PID-Controller Kppid=handles.tfdata.kc*0.59; Tipid=handles.tfdata.pu*0.5; Tdpid=handles.tfdata.pu*0.12; %Closed Loop transfer function Wcp=ZieglerNicholasPID(Kpp,Tip,Tdp); Wcpi=ZieglerNicholasPID(Kppi,Tipi,Tdpi); Wcpid=ZieglerNicholasPID(Kppid,Tipid,Tdpid); sysp=CLS(handles.tfdata.plant,Wcp); syspi=CLS(handles.tfdata.plant,Wcpi); syspid=CLS(handles.tfdata.plant,Wcpid); zntable=[Kpp Tip Tdp;Kppi Tipi Tdpi;Kppid Tipid Tdpid ]; set(handles.table,'data',zntable); %plotting axes(handles.responseaxis); t=str2num(handles.tfdata.t); step(sysp,syspi,syspid,t), legend('P','PI','PID'); function t_Callback(hObject, eventdata, handles) t=num2str(get(handles.t,'string')); handles.tfdata.t=t; guidata(hObject,handles); % --- Executes during object creation, after setting all properties. function t_CreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function Wc = ZieglerNicholasPID( Kc,Ti,Td ) % ZieglerNicholasPID function to generate the PID controller transfer %% Parameters % Kc : Critical gain % Ti : Reset time (minutes) % Td : Derivative time (minutes) % Wc : The laplace representation of Z-N %% EXAMPLE % Kc=10; % Ti=0.83; % Td=2.5 % Wc=ZieglerNicholasPID( Kc,Ti,Td ) %% Result is % 1 % Wc= 10*(1+ -------- + 2.5*s % 0.83*s %% Function implementation s=tf('s'); Wc=Kc*(1+(1/(Ti*s))+Td*s); end