⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pid_controller.m

📁 This software is developed to provide ease with controller design. For PID control, options are give
💻 M
📖 第 1 页 / 共 5 页
字号:
function varargout = PID_Controller(varargin)

global sys_tf sys_ss sys_fd_P sys_fd_PD sys_fd_PI sys_fd_PID
global a_dou b_dou c_dou d_dou
global sys_final_P sys_final_PI sys_final_PD sys_final_PID

% PID_CONTROLLER M-file for PID_Controller.fig
%      PID_CONTROLLER, by itself, creates a new PID_CONTROLLER or raises the existing
%      singleton*.
%
%      H = PID_CONTROLLER returns the handle to a new PID_CONTROLLER or the handle to
%      the existing singleton*.
%
%      PID_CONTROLLER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PID_CONTROLLER.M with the given input arguments.
%
%      PID_CONTROLLER('Property','Value',...) creates a new PID_CONTROLLER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before PID_Controller_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to PID_Controller_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 PID_Controller

% Last Modified by GUIDE v2.5 04-Sep-2008 09:14:05

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @PID_Controller_OpeningFcn, ...
                   'gui_OutputFcn',  @PID_Controller_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 PID_Controller is made visible.
function PID_Controller_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 PID_Controller (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = PID_Controller_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 on button press in ss_pushb.
function ss_pushb_Callback(hObject, eventdata, handles)
% hObject    handle to ss_pushb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%====================== Values from Radio Button ======================
ss_pushbutton = get (handles.ss_radiob,'Value');
tf_pushbutton = get (handles.tf_radiob,'Value');

%================= Implementing State Space Model ====================
if ss_pushbutton == get(handles.ss_radiob,'Max')

    a = get(handles.A_edit,'String');
    global a_dou
    a_dou = str2num(a);
    
    b= get(handles.B_edit,'String');
    global b_dou
    b_dou = str2num(b);
    
    c= get(handles.C_edit,'String');
    global c_dou
    c_dou = str2num(c);
    
    d= get(handles.D_edit,'String');
    global d_dou
    d_dou = str2num(d);
    
    %================= Error If Either A,B,C or D is empty ================
    
    if isempty(a_dou) || isempty(b_dou) || isempty(c_dou) || isempty(d_dou)
        errordlg('Please enter all A,B,C and D in correct format');
    end
    
    %======= Error If the State Space Model is not correct ================
    [r1 c1] = size(a_dou);
    [r2 c2] = size(c_dou);
    [r3 c3] = size(b_dou);
    [r4 c4] = size(d_dou);
        if c1 ~= c2
            errordlg('Please Enter Valid State Space Model');
        end
        if r1 ~= r3
            errordlg('Please Enter Valid State Space Model');
        end
        if c3 ~= c4
            errordlg('Please Enter Valid State Space Model');
        end
           
    global sys_ss
    sys_ss = ss(a_dou,b_dou,c_dou,d_dou);            % State Space Model
    
     [s t]= step(sys_ss);
     axes(handles.axes1)
     plot(t,s),grid on
    
    %================= Error If State Space Button is not selected ========
else
    errordlg('Select "State Space"');
end

    %======================= Time Response ================================
    
%     sys_s = tf(a_dou,b_dou,c_dou,d_dou)
%     sys_s = tf(n,d)
    sys_s= tf(sys_ss);
    [num,den] = tfdata(sys_s);
    [r c] = size(den{1,1});
    
    if c ~= 3
        set(handles.MOS_text,'String','Approximation Not Valid');
        set(handles.PTS_text,'String','Approximation Not Valid');
        set(handles.STS_text,'String','Approximation Not Valid');
    end
    
    roots_sys = roots(den{1,1});
    roots_sys_str = num2str(roots_sys);
    
    set(handles.ROS_text,'String',roots_sys_str);

    den_sp = den{1,1}(1,3);
    wn = sqrt(den_sp);
    den_sp2 = den{1,1}(1,2);
    b = den_sp2/(2*wn);
    [r c] = size(den{1,1});
    
    if b<=1 && num{1,1}(1,end-1) == 0 && c == 3
        MOS = exp(-(b*pi)/(sqrt(1-b^2)))*100;
        MOS_str = num2str(MOS);
        MOS_real = strcat(MOS_str,' %');
        set(handles.MOS_text,'String',MOS_real);
        
        PTS = pi/(wn*(sqrt(1-b^2)));
        PTS_str = num2str(PTS);
        PTS_real = strcat(PTS_str,' Sec');
        set(handles.PTS_text,'String',PTS_real);
        
        STS = 4/(b*wn);
        STS_str = num2str(STS);
        STS_real = strcat(STS_str,' Sec');
        set(handles.STS_text,'String',STS_real);
    else
        set(handles.MOS_text,'String','Approximation Not Valid');
        set(handles.PTS_text,'String','Approximation Not Valid');
        set(handles.STS_text,'String','Approximation Not Valid');
    end


%==========================================================================


function A_edit_Callback(hObject, eventdata, handles)
% hObject    handle to A_edit (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 A_edit as text
%        str2double(get(hObject,'String')) returns contents of A_edit as a double


% --- Executes during object creation, after setting all properties.
function A_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_edit (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 B_edit_Callback(hObject, eventdata, handles)
% hObject    handle to B_edit (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 B_edit as text
%        str2double(get(hObject,'String')) returns contents of B_edit as a double


% --- Executes during object creation, after setting all properties.
function B_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_edit (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 C_edit_Callback(hObject, eventdata, handles)
% hObject    handle to C_edit (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 C_edit as text
%        str2double(get(hObject,'String')) returns contents of C_edit as a double


% --- Executes during object creation, after setting all properties.
function C_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to C_edit (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 D_edit_Callback(hObject, eventdata, handles)
% hObject    handle to D_edit (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 D_edit as text
%        str2double(get(hObject,'String')) returns contents of D_edit as a double


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

%====================== Values from Radio Button ======================
ss_pushbutton = get (handles.ss_radiob,'Value');
tf_pushbutton = get (handles.tf_radiob,'Value');

%================= Implementing Transfer Function ====================

if tf_pushbutton == get(handles.tf_radiob,'Max')
     
    num= get (handles.num_edit,'string');
    num_dou = str2num(num);
    
    den= get (handles.den_edit,'string');
    den_dou = str2num(den);

    %================= Error If Either Num or Den is empty  ====================
    if isempty(num_dou) || isempty(den_dou)
        errordlg('Please enter in right format, For Example Num = [1 2] or Den = [1 2 3]');
    end
    % =====================================================================
    
    global sys_tf
    sys_tf = tf(num_dou,den_dou);            % Transfer Function
    
    [s t]= step(sys_tf);
    axes(handles.axes1)
    plot(t,s),grid on
    
    %================= Error If Transfer Function Button is not selected=======

else
    errordlg('Select "Transfer Function');
end

    %======================= Time Response ================================
    
    [num,den] = tfdata(sys_tf);
    [r c] = size(den{1,1});
    
    if c ~= 3
        set(handles.MOS_text,'String','Approximation Not Valid');
        set(handles.PTS_text,'String','Approximation Not Valid');
        set(handles.STS_text,'String','Approximation Not Valid');
    end
    
    roots_sys = roots(den{1,1});
    roots_sys_str = num2str(roots_sys);
    
    set(handles.ROS_text,'String',roots_sys_str);

    den_sp = den{1,1}(1,3);
    wn = sqrt(den_sp);
    den_sp2 = den{1,1}(1,2);
    b = den_sp2/(2*wn);
    [r c] = size(den{1,1});
    
    if b<=1 && num{1,1}(1,end-1) == 0 && c == 3
        MOS = exp(-(b*pi)/(sqrt(1-b^2)))*100;
        MOS_str = num2str(MOS);
        MOS_real = strcat(MOS_str,' %');
        set(handles.MOS_text,'String',MOS_real);
        
        PTS = pi/(wn*(sqrt(1-b^2)));
        PTS_str = num2str(PTS);
        PTS_real = strcat(PTS_str,' Sec');
        set(handles.PTS_text,'String',PTS_real);
        
        STS = 4/(b*wn);
        STS_str = num2str(STS);
        STS_real = strcat(STS_str,' Sec');
        set(handles.STS_text,'String',STS_real);
    else
        set(handles.MOS_text,'String','Approximation Not Valid');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -