当前位置 : 主页 > 编程语言 > python >

【控制】基于PID实现水箱控制系统matlab代码

来源:互联网 收集:自由互联 发布时间:2022-09-29
1 内容介绍 "计算机控制技术"课程是电气信息类专业的主干课程之一,实验教学是该课程教学的重要组成部分.本实验项目以水箱液位为控制参量,设计了包括系统硬件,基于Matlab的控制界面

1 内容介绍

"计算机控制技术"课程是电气信息类专业的主干课程之一,实验教学是该课程教学的重要组成部分.本实验项目以水箱液位为控制参量,设计了包括系统硬件,基于Matlab的控制界面,PID控制程序等内容的"计算机控制技术"实验方案.通过教学实践,培养了学生的综合能力,达到了预期的教学目的.

2 部分代码

function varargout = pid_opt_pj(varargin)

% PID_OPT_PJ M-file for pid_opt_pj.fig

%      PID_OPT_PJ, by itself, creates a new PID_OPT_PJ or raises the existing

%      singleton*.

%

%      H = PID_OPT_PJ returns the handle to a new PID_OPT_PJ or the handle to

%      the existing singleton*.

%

%      PID_OPT_PJ('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in PID_OPT_PJ.M with the given input arguments.

%

%      PID_OPT_PJ('Property','Value',...) creates a new PID_OPT_PJ or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before pid_opt_pj_OpeningFunction gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to pid_opt_pj_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


% Copyright 2002-2003 The MathWorks, Inc.


% Edit the above text to modify the response to help pid_opt_pj


% Last Modified by GUIDE v2.5 30-Apr-2022 09:11:18


% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

                   'gui_Singleton',  gui_Singleton, ...

                   'gui_OpeningFcn', @pid_opt_pj_OpeningFcn, ...

                   'gui_OutputFcn',  @pid_opt_pj_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_opt_pj is made visible.

function pid_opt_pj_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_opt_pj (see VARARGIN)


% Choose default command line output for pid_opt_pj

handles.output = hObject;


% Update handles structure

guidata(hObject, handles);


% UIWAIT makes pid_opt_pj wait for user response (see UIRESUME)

% uiwait(handles.figure_pid_opt_pj);

setappdata(handles.axes1,'tout',0);

setappdata(handles.axes1,'yout',0);

set(handles.btn_no_pid,'Enable','on');

set(handles.btn_pid,'Enable','on');

set(handles.btn_reset,'Enable','on');


% --- Outputs from this function are returned to the command line.

function varargout = pid_opt_pj_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 edit_pid_Callback(hObject, eventdata, handles)

% hObject    handle to edit_pid (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 edit_pid as text

%        str2double(get(hObject,'String')) returns contents of edit_pid as a double



% --- Executes during object creation, after setting all properties.

function edit_pid_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit_pid (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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end




function edit_I_Callback(hObject, eventdata, handles)

% hObject    handle to edit_I (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 edit_I as text

%        str2double(get(hObject,'String')) returns contents of edit_I as a double



% --- Executes during object creation, after setting all properties.

function edit_I_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit_I (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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end




function edit_P_Callback(hObject, eventdata, handles)

% hObject    handle to edit_P (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 edit_P as text

%        str2double(get(hObject,'String')) returns contents of edit_P as a double



% --- Executes during object creation, after setting all properties.

function edit_P_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit_P (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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end




function edit_D_Callback(hObject, eventdata, handles)

% hObject    handle to edit_D (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 edit_D as text

%        str2double(get(hObject,'String')) returns contents of edit_D as a double



% --- Executes during object creation, after setting all properties.

function edit_D_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit_D (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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end



% --- Executes on button press in btn_pid.

function btn_pid_Callback(hObject, eventdata, handles)

[t,x,y]=sim('have_pid');

plot(t,y);

a=get_param('have_pid/Subsystem','Kp');

b=get_param('have_pid/Subsystem','Ki');

c=get_param('have_pid/Subsystem','Kd');

set(handles.edit_P,'string',a);

set(handles.edit_I,'string',b);

set(handles.edit_D,'string',c);


ym=max(y);

ys=y(length(t)) ;

n=1;

ys=y(length(t)) ;

n=1;

while y(n)<ys

         n=n+1;end

set(handles.edit5,'string',tr);    %上升时间


tp=spline(t,y,ym)

set(handles.edit6,'string',tp);   %峰值时间

L=length(t); 

while (y(L)>0.98*ys)&(y(L)<1.02*ys)

         L=L-1;end

ts=t(L)

set(handles.edit7,'string',ts)   %调节时间

ctl=(ym-ys)/ys*100;

set(handles.edit8,'string',ctl);  %超调量

es=(ys-20)/20*100;

set(handles.edit9,'string',es);   %稳态误差



% --- Executes on button press in btn_no_pid.

function btn_no_pid_Callback(hObject, eventdata, handles)

% hObject    handle to btn_no_pid (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

[t,x,y]=sim('no_pid');

plot(t,y);


% --- Executes on button press in btn_reset.

function btn_reset_Callback(hObject, eventdata, handles)

% hObject    handle to btn_reset (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 btn_reset

val=get(hObject,'value')

if val==1

x=get(handles.edit_P,'string');

y=get(handles.edit_I,'string');

z=get(handles.edit_D,'string');

set_param('have_pid/Subsystem','Kp',x);

set_param('have_pid/Subsystem','Ki',y);

set_param('have_pid/Subsystem','Kd',z);

[t,x,y]=sim('have_pid');

plot(t,y);

ym=max(y);

ys=y(length(t)) ;

n=1;

ys=y(length(t)) ;

n=1;

while y(n)<ys

         n=n+1;end

set(handles.edit5,'string',tr);    %上升时间


tp=spline(t,y,ym)

set(handles.edit6,'string',tp);   %峰值时间

L=length(t); 

while (y(L)>0.98*ys)&(y(L)<1.02*ys)

         L=L-1;end

ts=t(L)

set(handles.edit7,'string',ts)   %调节时间

ctl=(ym-ys)/ys*100;

set(handles.edit8,'string',ctl);  %超调量

es=(ys-20)/20*100;

set(handles.edit9,'string',es);   %稳态误差



n=1;

set(handles.btn_reset,'Enable','on');

val=0;

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



% --- 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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

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



% --- 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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end




function edit7_Callback(hObject, eventdata, handles)

% hObject    handle to edit7 (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 edit7 as text

%        str2double(get(hObject,'String')) returns contents of edit7 as a double



% --- Executes during object creation, after setting all properties.

function edit7_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit7 (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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end




function edit8_Callback(hObject, eventdata, handles)

% hObject    handle to edit8 (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 edit8 as text

%        str2double(get(hObject,'String')) returns contents of edit8 as a double



% --- Executes during object creation, after setting all properties.

function edit8_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit8 (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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end




function edit9_Callback(hObject, eventdata, handles)

% hObject    handle to edit9 (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 edit9 as text

%        str2double(get(hObject,'String')) returns contents of edit9 as a double



% --- Executes during object creation, after setting all properties.

function edit9_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit9 (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

    set(hObject,'BackgroundColor','white');

else

    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end





3 运行结果

【控制】基于PID实现水箱控制系统matlab代码_2d

【控制】基于PID实现水箱控制系统matlab代码_ide_02

4 参考文献

[1]范振瑞. 基于Matlab的PID温度控制系统设计[J]. 电子科技, 2013, 26(8):164-167.

[2]王佳庆, 肖忠, 王晓刚,等. 基于Matlab的水箱液位控制系统实验设计及教学实践[J]. 高教学刊, 2020(8):3.

部分理论引用网络文献,若有侵权联系博主删除。


网友评论