gusucode.com > ​建立一个对倒立摆动画进行控制的用户界面源码程序 > code/Exp1.m

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

% Last Modified by GUIDE v2.5 07-Dec-2005 11:02:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Exp1_OpeningFcn, ...
                   'gui_OutputFcn',  @Exp1_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 Exp1 is made visible.
function Exp1_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 Exp1 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


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


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (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 popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in popupmenu1.
function varargout=popupmenu1_Callback(hObject, eventdata, handles,varargin)
val=get(hObject,'value');
switch val
    case 1
    case 2
        handles.data1=0
    case 3
         handles.data1=2
    case 4
         handles.data1=3
    case 5
          handles.data1=4
  end
  guidata(hObject,handles)
  
  
  
  
  function popupmenu2_Callback(hObject, eventdata, handles)
val=get(hObject,'value');
switch val
    case 1
    case 2
        handles.data2=0
    case 3
         handles.data2=1
    case 4
         handles.data2=3
    case 5
          handles.data2=5
  end
  guidata(hObject,handles)
        

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

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes on button press in pushbutton1.
function varargout=pushbutton1_Callback(hObject, eventdata, handles,varargin)
%参数
h=1.5;k=5;dt=0.01;
l=10;r=0.5;
M=2.0;m=0.1;g=9.81;
%系统初始值
zf=[0 0 handles.data1 0]';
z=[0 0 handles.data2 0]';
%创建系统
c1=M*l;c2=m*l;c3=m*g;c4=(M+m)*g;
A=[0 1 0 0;c4/c1 0 0 0;0 0 0 1;-c3/M 0 0 0];
B=[0 -1/c1 0 1/M]';
C=[1 0 0 0;0 0 1 0];
D=[0 0]';
%配置极点
cpl=[-1.5+3.0j -1.5-3.0j -5 -2];
Ks=place(A,B,cpl);
for i=1:500
    z1=(A-B*Ks)*(z-zf);
    z=z+z1*dt;
    y(:,i)=z;
%作动画
x1=z(3)-k/2+z(4)*dt*i;x2=z(3)+k/2+z(4)*dt*i;
y1=-4;y2=-2.5;
x4=(x1+x2)/2+l*sin(z(1));
y4=y2+l*cos(z(1));
t=0:pi/100:2*pi;
x3=x4+r*cos(t);
y3=y4+r*sin(t);
plot(x3,y3,[(x1+x2)/2,x4],[y2,y4],[-20,20],[-4,-4],[x1,x2],[y1,y1],[x2,x2],[y1,y2],[x1,x2],[y2,y2],[x1,x1],[y1,y2])
axis([-15 15 -10 15])
axis square
pause(0.001);
if (i==1)
    pause(0.01);
end
if (abs(z(1)-zf(1))<=0.001)&(abs(z(3)-zf(3))<=0.002)
    break;
end
end

% --- Executes on button press in pushbutton2.
function varargout=pushbutton2_Callback(hObject, eventdata, handles,varargin)
plot(0,0,'w');
axis([-15 15 -10 15])
axis square

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



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% 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 pushbutton4.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% 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 pushbutton3.
uiwait(msgbox('Thank you,sir!'))
close;

% hObject    handle to pushbutton3 (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 popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in popupmenu2.

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

% Hints: contents = get(hObject,'String') returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes on button press in pushbutton4.

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