function varargout = single_loop(varargin)
% SINGLE_LOOP MATLAB code for single_loop.fig
% SINGLE_LOOP, by itself, creates a new SINGLE_LOOP or raises the existing
% singleton*.
%
% H = SINGLE_LOOP returns the handle to a new SINGLE_LOOP or the handle to
% the existing singleton*.
%
% SINGLE_LOOP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SINGLE_LOOP.M with the given input arguments.
%
% SINGLE_LOOP('Property','Value',...) creates a new SINGLE_LOOP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before single_loop_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to single_loop_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 single_loop
% Last Modified by GUIDE v2.5 29-May-2019 19:23:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @single_loop_OpeningFcn, ...
'gui_OutputFcn', @single_loop_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 single_loop is made visible.
function single_loop_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for single_loop
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% 定义一个结构体,用来存储计算过程中的变量
global data;
% --- Outputs from this function are returned to the command line.
function varargout = single_loop_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function numInput_CreateFcn(hObject, eventdata, handles)
% 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 during object creation, after setting all properties.
function denInput_CreateFcn(hObject, eventdata, handles)
% 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 during object creation, after setting all properties.
function edit15_CreateFcn(hObject, eventdata, handles)
% 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 during object creation, after setting all properties.
function kpInput_CreateFcn(hObject, eventdata, handles)
% 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 during object creation, after setting all properties.
function kdInput_CreateFcn(hObject, eventdata, handles)
% 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 during object creation, after setting all properties.
function kiInput_CreateFcn(hObject, eventdata, handles)
% 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 during object creation, after setting all properties.
function rInput_CreateFcn(hObject, eventdata, handles)
% 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 pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
try
delete(allchild(handles.axes));
end
set(handles.axes, 'XLim', [0 1]);
function increaseR_Callback(hObject, eventdata, handles)
r = str2double(get(handles.rInput,'String'));
r = r + 50;
set(handles.rInput, 'String', num2str(r));
setR_Callback(handles.rInput, eventdata, handles);
function decreaseR_Callback(hObject, eventdata, handles)
r = str2double(get(handles.rInput,'String'));
r = r - 50;
set(handles.rInput, 'String', num2str(r));
setR_Callback(handles.rInput, eventdata, handles);
function setR_Callback(hObject, eventdata, handles)
global data
r = str2num(get(handles.rInput,'String'));
data.r = r;
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function delayInput_CreateFcn(hObject, eventdata, handles)
% 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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
function startEmulation_Callback(hObject, eventdata, handles)
global data
% 读入pid控制参数
data.kp = str2num(get(handles.kpInput,'String'));
data.kd = str2num(get(handles.kdInput,'String'));
data.ki = str2num(get(handles.kiInput,'String'));
% 读入给定值和采样时间
data.r = str2num(get(handles.rInput,'String'));
data.ts = str2num(get(handles.tsInput,'String'));
% 设置开始模拟
data.stop = false;
% 更新结构体数据
guidata(hObject, handles);
% 运行增量式pid算法
incremental_pid(hObject, eventdata, handles);
% 增量式pid算法
function incremental_pid(hObject, eventdata, handles)
global data
curTime = 0; % 当前时间
u_k1 = 0; u_k = 0; % 输出,初始输出为0
r = data.r; % 初始化给定值
e_k = 0; e_k1 = 0; e_k2 = 0; % 偏差,初始偏差为给定值
while data.stop ~= true
curTime = curTime + data.ts; % 计算当前时间
% 更改时间轴范围
curLim = get(handles.axes, 'XLim');
if (curTime > curLim(2))
set(handles.axes, 'XLim', [curTime-0.5, curTime+0.5]);
end
% 计算阀门开度
% delta1 = data.kp * (e_k - e_k1)
% delta2 = data.ki * data.ts * e_k
% delta3 = data.kd *(e_k - 2*e_k1 + e_k2)
delta_u = data.kp * (e_k - e_k1) + data.ki * e_k + data.kd *(e_k - 2*e_k1 + e_k2);
u_k = u_k1 + delta_u;
% 绘图
plot([curTime-data.ts, curTime], [u_k1, u_k], 'r'); hold on;
plot([curTime-data.ts, curTime], [r, data.r]); hold on;
% 控制坐标轴范围
%XLim = get(handles.axes)
%if XLim(2) > 10
% set(handles.axes, 'Xlim', [curTime-10, curTime]);
%end
% 更新各个量
r = data.r; % 更新给定值输入
u_k1 = u_k;
e_k2 = e_k1; e_k1 = e_k; e_k = r - u_k;
pause(0.1);
end
function stopEmulation_Callback(hObject, eventdata, handles)
global data
% 设�
评论0