-
GUI
图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。 图形用户界面是一种人与计算机通信的界面显示格式,允许用户使用鼠标等输入设备操纵屏幕上的图标或菜单选项,以选择命令、调用文件、启动程序或执行其它一些日常任务。与通过键盘输入文本或字符命令来完成例行任务的字符界面相比,图形用户界面有许多优点。图形用户界面由窗口、下拉菜单、对话框及其相应的控制机制构成,在各种新式应用程序中都是标准化的,即相同的操作总是以同样的方式来完成,在图形用户界面,用户看到和操作的都是图形对象,应用的是计算机图形学的技术。 -
PID控制器
在过程控制中,按偏差的比例(P)、积分(I)和微分(D)进行控制的PID控制器(亦称PID调节器)是应用最为广泛的一种自动控制器。它具有原理简单,易于实现,适用面广,控制参数相互独立,参数的选定比较简单等优点;而且在理论上可以证明,对于过程控制的典型对象──“一阶滞后+纯滞后”与“二阶滞后+纯滞后”的控制对象,PID控制器是一种最优控制。PID调节规律是连续系统动态品质校正的一种有效方法,它的参数整定方式简便,结构改变灵活(PI、PD、…)。 -
为什么用MATLAB做GUI
四个字:简单直观。MATLAB从诞生到现在一直秉持的理念就是从顶层设计,避免繁琐的底层开发。这也是民间流传“MATLAB除了不会生孩子什么都会”的依据吧。 -
The first thing
MATLAB命令栏输入:
guide
选择新建,之后就有如下界面,你可以拖动你想要使用的控件,以及绘制图框。
-
在编写具体代码之前需要明确的最基本概念——Callback
MATLAB GUI的原理就是你对控件的操作都会触发回调函数,每个控件都有自己的回调函数,这样就能在触发后马上做出反应。 -
handles
这个handles你可以把它看做全局结构体,可以储存你想保存的数据,也保存了所有控件的信息,便于不同控件之间的沟通与联系,比如我下拉菜单选中某一项,绘图元素变化等等。 -
hObject
这个hObject相当于就是这个控件自己本身,当然你也可以通过handles.‘控件’来访问这个控件,效果一样。我的习惯还是用handles来管控。 -
界面设计
因为我要做一个PID响应的GUI,所以界面应该有选择输入信号种类的控件,在这里因为我有三种信号:阶跃、斜坡、抛物。因此选用下拉菜单比较合理。另外设置一个开始按钮开始绘制图像,一个清屏按钮、三个可编辑文本用来输入PID控制器的三个参数Kp、Ki、Kd、一个绘图的图框。界面如下:
下面开始编写回调函数。 -
.m文件编写
代码看起来很长,但是很多事guide自己生成的,函数名注意对应上你的控件,你只需要在对应的地方填写代码即可。其中有一个启动函数function untitled_OpeningFcn(hObject, eventdata, handles, varargin),在这可以写一些初始化的代码段。
另外强调一点,用handles.a=2这种保存完全局变量后一定要接一句guidata(hObject, handles);否则保存不成功。
还有我这里绘制PID的方法是一种最简单的增量式PID方法,也没有使用MATLAB的任何控制领域专门的函数。
function varargout = untitled(varargin)
%UNTITLED MATLAB code file for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED using the
% given property value pairs. Unrecognized properties are passed via
% varargin to untitled_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% UNTITLED('CALLBACK') and UNTITLED('CALLBACK',hObject,...) call the
% local function named CALLBACK in UNTITLED.M with the given input
% arguments.
%
% *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 untitled
% Last Modified by GUIDE v2.5 02-Mar-2020 18:13:15
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_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 unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
handles.value = 1;
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
grid on;
xlim([0,50]);
end
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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;
end
function edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (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 edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
Kp = get(handles.edit4,'String');
Kp = str2double(Kp);
handles.Kp = Kp;
guidata(hObject,handles);
end
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (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
end
function edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (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 edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
Ki = get(handles.edit5,'String');
Ki = str2double(Ki);
handles.Ki = Ki;
guidata(hObject,handles);
end
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (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
end
function edit6_Callback(hObject, eventdata, handles)
% hObject handle to edit6 (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 edit6 as text
% str2double(get(hObject,'String')) returns contents of edit6 as a double
Kd = get(handles.edit6,'String');
Kd = str2double(Kd);
handles.Kd = Kd;
guidata(hObject,handles);
end
% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit6 (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
end
% --- Executes on button press in pushbutton4.
function pushbutton4_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)
if(handles.value==1)
t = 1:50;
ideal = 5;
x=[];e=[];
x(1)=0;
Kp = handles.Kp; Ki = handles.Ki; Kd = handles.Kd;
for i = t
e(i) = ideal - x(i);
if(i>1)
x(i+1) = x(i) + Kp*e(i) + Ki*sum(e) + Kd*(e(i)-e(i-1));
end
if i==1
x(i+1) = x(i) + Kp*e(i) + Ki*sum(e);
end
end
G = plot(t,x(1:50));
hold on;
plot(1:50,5*ones(1,50),'r');
h = legend('输出信号','输入信号');
set(h,'box','on');
handles.h = h;
xlim([0,50]);
grid on;
end
if(handles.value==2)
t = 1:50;
ideal = 5;
x=[];e=[];
x(1)=0;
Kp = handles.Kp; Ki = handles.Ki; Kd = handles.Kd;
for i = t
e(i) = ideal*i - x(i);
if(i>1)
x(i+1) = x(i) + Kp*e(i) + Ki*sum(e) + Kd*(e(i)-e(i-1));
end
if i==1
x(i+1) = x(i) + Kp*e(i) + Ki*sum(e);
end
end
G = plot(t,x(1:50));
hold on;
plot(t,5.*t,'r');
h = legend('输出信号','输入信号');
set(h,'box','on');
handles.h = h;
xlim([0,50]);
grid on;
end
if(handles.value==3)
t = 1:50;
ideal = 5;
x=[];e=[];
x(1)=0;
Kp = handles.Kp; Ki = handles.Ki; Kd = handles.Kd;
for i = t
e(i) = ideal*i^2 - x(i);
if(i>1)
x(i+1) = x(i) + Kp*e(i) + Ki*sum(e) + Kd*(e(i)-e(i-1));
end
if i==1
x(i+1) = x(i) + Kp*e(i) + Ki*sum(e);
end
end
G = plot(t,x(1:50));
hold on;
plot(t,5.*t.^2,'r');
xlim([0,50]);
h = legend('输出信号','输入信号');
set(h,'box','on');
handles.h = h;
grid on;
end
guidata(hObject,handles);
end
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla;
set(handles.h,'box','off');
grid on;
end
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, 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 = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
handles.value = get(handles.popupmenu1,'Value');
guidata(hObject,handles);
end
% --- 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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
-
GUI转exe
具体怎么转很简单,这里不一一赘述,可自行谷歌。其实转exe也没必要,要在另外一台电脑也需要MATLAB环境,不如就用.m文件运行。 -
效果展示
可以看到当Ki=0时斜坡输入存在静差,这与控制中的理论相符合,因此这个GUI可以用来做教学演示。 -
总结
MATLAB GUI靠着简便的编写和强大的环境支持,特别适合新手上手,如果稍微专注点两个小时内应该可以入门,学海无涯,共勉!