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

📄 state_feedback_gui.m

📁 This software is developed to provide ease with controller design. For PID control, options are give
💻 M
📖 第 1 页 / 共 4 页
字号:
% hObject    handle to zeta_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 wn_edit_Callback(hObject, eventdata, handles)
% hObject    handle to wn_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 wn_edit as text
%        str2double(get(hObject,'String')) returns contents of wn_edit as a double


% --- Executes during object creation, after setting all properties.
function wn_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wn_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 PPC_pushb.
function PPC_pushb_Callback(hObject, eventdata, handles)
% hObject    handle to PPC_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 Controller popup menu =================

clc;
value = get(handles.PPC_popupmenu,'Value');

%====================== Values from Radio Button ==========================

ss_pushbutton = get (handles.ss_radiob,'Value');
tf_pushbutton = get (handles.tf_radiob,'Value');


%==================== Selecting Designed by Zeta and Wn  %=================

if value == 1
    value_zeta_str = get(handles.zeta_edit,'String');
    value_zeta_dou = str2num (value_zeta_str);
    
    value_wn_str = get(handles.wn_edit,'String');
    value_wn_dou = str2num (value_wn_str);
    
    
    if tf_pushbutton == get(handles.tf_radiob,'Max')
        
        global sys_tf
        [num,den] = tfdata(sys_tf);
        [A B C D] = tf2ss (num{1,1},den{1,1});
        sys_uncomp = ss(A,B,C,D);

        % =================================================================
        syms s
        Det = det(s*eye(2) - A);                    % Eigen Values
        
        % ================== Desired Characteristics Equation =============
        
        num = [1 (2*value_wn_dou*value_zeta_dou) value_wn_dou^2];
        sys_sfb = tf(num,1);
        
        % ================== Transformation Matrix ========================
        
        M = [B A*B];                                % Controllability Matrix
        
%         if rank(M) ~= order (sys_uncomp)
%             set(handles.error_text,'string','System is Uncotrollable, State feedback Controller is not possible');
%             return;
%         end

        W = [(-A(2:2,2:2)- A(1:1,1:1)) 1;1 0];
        T = M*W ;                                   % Transformation Matrix
        
        % % ================== Gain Vector  ===============================
        
        k1 = value_wn_dou^2-(A(1:1,1:1)* A(2:2,2:2) - A(1:1,2:2) * A(2:2,1:1));
        k2 = 2*value_zeta_dou*value_wn_dou - (-A(2:2,2:2)- A(1:1,1:1)); 
        K = [k1 k2];
        K_original = [k1 k2] * inv(T);
        
        % % ================== Checking Step Response =====================
        
        A_new = A - (B*K_original);
        sys_ss_sfb = ss(A_new,B,C,D);
        figure
        step(sys_ss_sfb), title ('Step Response with Controller'),grid on
        [num1,den1] = ss2tf(A_new,B,C,D);
        global sys_fd
        sys_fd = tf(num1,den1);
    end
        
        if ss_pushbutton == get(handles.ss_radiob,'Max')
            
            global a_dou b_dou c_dou d_dou
            global sys_ss
            
            % =============================================================
            
            syms s
%             H = [c_dou*(s*eye(2)-a_dou)*b_dou + d_dou]
            Det = det(s*eye(2) - a_dou);                    % Eigen Values
            
            % ================== Desired Characteristics Equation =========
            
            num = [1 (2*value_wn_dou*value_zeta_dou) value_wn_dou^2];
            sys_sfb = tf(num,1);
            
            % ================== Transformation Matrix ====================
            
            M = [b_dou (a_dou * b_dou)];                      % Controllability Matrix

%             if rank(M) ~= order (sys_ss)
%                 clc
%                 set(handles.error_text,'string','System is Uncotrollable, State feedback Controller is not possible');
%                 return;
%             end

            W = [(-a_dou(2:2,2:2)- a_dou(1:1,1:1)) 1;1 0];
            T = M*W ;                                       % Transformation Matrix
            
            % % ================== Gain Vector  ===========================
            
            k1 = value_wn_dou^2-(a_dou(1:1,1:1)* a_dou(2:2,2:2) - a_dou(1:1,2:2) * a_dou(2:2,1:1)); 
            k2 = 2*value_zeta_dou*value_wn_dou - (-a_dou(2:2,2:2)- a_dou(1:1,1:1)); 
            K = [k1 k2];
            K_original = [k1 k2] * inv(T);
            
            % % ================== Checking Step Response =================
            
            A_new = a_dou - (b_dou*K_original);
            sys_ss_sfb = ss(A_new,b_dou,c_dou,d_dou);
            figure
            step(sys_ss_sfb), title ('Step Response with Controller'),grid on
            [num1,den1] = ss2tf(A_new,b_dou,c_dou,d_dou);
            global sys_fd
            sys_fd = tf(num1,den1);
        end

        [num2,den2] = tfdata(sys_fd);
        [r c] = size(den2{1,1});
        
        if c ~= 3
            set(handles.MO_text,'String','Approximation Not Valid');
            set(handles.PT_text,'String','Approximation Not Valid');
            set(handles.ST_text,'String','Approximation Not Valid');
        end

        roots_sys = roots(den2{1,1});
        roots_sys_str = num2str(roots_sys);
        
        set(handles.roots_text,'String',roots_sys_str);
        den_sp = den2{1,1}(1,3);
        wn = sqrt(den_sp);
        den_sp2 = den2{1,1}(1,2);
        b = den_sp2/(2*wn);
        
        if b<=1 && num2{1,1}(1,end-1) == 0 && c==3
            MO = exp(-(b*pi)/(sqrt(1-b^2)))*100;
            MO_str = num2str(MO);
            MO_real = strcat(MO_str,' %');
            set(handles.MO_text,'String',MO_real);

            PT = pi/(wn*(sqrt(1-b^2)));
            PT_str = num2str(PT);
            PT_real = strcat(PT_str,' Sec');
            set(handles.PT_text,'String',PT_real);
            
            ST = 4/(b*wn);
            ST_str = num2str(ST);
            ST_real = strcat(ST_str,' Sec');
            set(handles.ST_text,'String',ST_real);
        else
            set(handles.MO_text,'String','Approximation Not Valid');
            set(handles.PT_text,'String','Approximation Not Valid');
            set(handles.ST_text,'String','Approximation Not Valid');
        end
end

    
    
% %============== Selecting design by desired Poles location ==============

if value == 2 
    value_str_rp = get(handles.rp_edit,'String');
    value_dou_rp = str2num (value_str_rp);
    
    if tf_pushbutton == get(handles.tf_radiob,'Max')
        global sys_tf
        [num,den] = tfdata(sys_tf);
        [A B C D] = tf2ss (num{1,1},den{1,1});
        sys_uncomp = ss(A,B,C,D);
       
        % =================================================================
        
        syms s
        Det = det(s*eye(2) - A);                    % Eigen Values
        
        % ================== Desired Characteristics Equation =============
        
        num1 = [1 (-value_dou_rp(1) - value_dou_rp(2)) (-value_dou_rp(1) * -value_dou_rp(2))];
        sys_sfb = tf(num1,1);
        
        % ================== Transformation Matrix ========================
        
        M = [B A*B];                                % Controllability Matrix
        
        if rank(M) ~= order (sys_uncomp)
            clc
            set(handles.error_text,'string','System is Uncotrollable, State feedback Controller is not possible');
            return;
        end

        W = [(-A(2:2,2:2)- A(1:1,1:1)) 1;1 0];
        T = M*W ;                                   % Transformation Matrix
        
        % % ================== Gain Vector  ===============================
        [num2,den2] = tfdata(sys_sfb);
        value_wn  = num2{1,1}(1,3);
        k1 = value_wn -(A(1:1,1:1)* A(2:2,2:2) - A(1:1,2:2) * A(2:2,1:1)); 
        den_sp2 = num2{1,1}(1,2);
        b = den_sp2/(2*sqrt(value_wn));
        k2 = 2*b*sqrt(value_wn) - (-A(2:2,2:2)- A(1:1,1:1)); 
        K = [k1 k2];
        K_original = [k1 k2] * inv(T);
        
        % % ================== Checking Step Response =====================
        
        A_new = A - (B*K_original);
        sys_tf_sfb = ss(A_new,B,C,D);
        figure
        step(sys_tf_sfb), title ('Step Response with Controller'),grid on
        [num1,den1] = ss2tf(A_new,B,C,D);
        global sys_fd_rp
        sys_fd_rp = tf(num1,den1);
    end

    if ss_pushbutton == get(handles.ss_radiob,'Max')
        global a_dou b_dou c_dou d_dou
        global sys_ss
        
        % =================================================================
        
        syms s
        Det = det(s*eye(2) - a_dou);                    % Eigen Values
        
        % ================== Desired Characteristics Equation =============
        
        num1 = [1 (-value_dou_rp(1) - value_dou_rp(2)) (-value_dou_rp(1) * -value_dou_rp(2))];
        sys_sfb = tf(num1,1);
        
        % ================== Transformation Matrix ========================
        
        M = [b_dou a_dou*b_dou];                                % Controllability Matrix
        
%         if rank(M) ~= order (sys_ss)
%             clc
%             set(handles.error_text,'string','System is Uncotrollable, State feedback Controller is not possible');
%             return;
%         end

        W = [(-a_dou(2:2,2:2)- a_dou(1:1,1:1)) 1;1 0];
        T = M*W ;                                   % Transformation Matrix
        
        % % ================== Gain Vector  ===============================
        [num2,den2] = tfdata(sys_sfb);
        de_sp = num2{1,1}(1,3);
        value_wn = sqrt(de_sp);
        k1 = value_wn -(a_dou(1:1,1:1)* a_dou(2:2,2:2) - a_dou(1:1,2:2) * a_dou(2:2,1:1)); 
        den_sp2 = num2{1,1}(1,2);
        b = den_sp2/(2*value_wn);
        k2 = 2*b*value_wn - (-a_dou(2:2,2:2)- a_dou(1:1,1:1)); 
        K = [k1 k2];
        K_original = [k1 k2] * inv(T);
        
        % % ================== Checking Step Response =====================
        
        A_new = a_dou - (b_dou*K_original);
        sys_ss_sfb = ss(A_new,b_dou,c_dou,d_dou);
        figure
        step(sys_ss_sfb), title ('Step Response with Controller'),grid on
        [num1,den1] = ss2tf(A_new,b_dou,c_dou,d_dou);
        global sys_fd_rp
        sys_fd_rp = tf(num1,den1);
    end
               
        [num2,den2] = tfdata(sys_fd_rp);
        [r c] = size(den2{1,1});
        
        if c ~= 3
            set(handles.MO_text,'String','Approximation Not Valid');
            set(handles.PT_text,'String','Approximation Not Valid');
            set(handles.ST_text,'String','Approximation Not Valid');
        end
        
        roots_sys = roots(den2{1,1});
        roots_sys_str = num2str(roots_sys);
        
        set(handles.roots_text,'String',roots_sys_str);
        den_sp = den2{1,1}(1,3);
        wn = sqrt(den_sp);
        den_sp2 = den2{1,1}(1,2);
        b = den_sp2/(2*wn);
        
        if b<=1 && num2{1,1}(1,end-1) == 0 && c==3
            MO = exp(-(b*pi)/(sqrt(1-b^2)))*100;
            MO_str = num2str(MO);
            MO_real = strcat(MO_str,' %');
            set(handles.MO_text,'String',MO_real);
            
            PT = pi/(wn*(sqrt(1-b^2)));
            PT_str = num2str(PT);
            PT_real = strcat(PT_str,' Sec');
            set(handles.PT_text,'String',PT_real);
            
            ST = 4/(b*wn);
            ST_str = num2str(ST);
            ST_real = strcat(ST_str,' Sec');
            set(handles.ST_text,'String',ST_real);
        else
            set(handles.MO_text,'String','Approximation Not Valid');
            set(handles.PT_text,'String','Approximation Not Valid');
            set(handles.ST_text,'String','Approximation Not Valid');
        
        end
end
% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (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 radiobutton3


% --- Executes on button press in radiobutton6.
function radiobutton6_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton6 (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 radiobutton6



⌨️ 快捷键说明

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