Tải bản đầy đủ (.pdf) (29 trang)

Bai 2 thuc hanh thiet ke mo hinh hoa mo phong he thong giam soc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.19 MB, 29 trang )

Bài 2. XÂY DỰNG MÔ HÌNH HÓA MÔ PHỎNG HÊ THỐNG GIẢM SÓC
TRÊN CÔNG CỤ GUIDE CỦA PHẦN MỀM MATLAB
I. Mục đích
- Giúp sinh viên hiểu phương pháp xây dựng mô hình hóa mô phỏng một hệ thống vật lý.
- Thu thập và phân tích các bảng dữ liệu của hệ thống.
- Đánh giá được kết quả mô phỏng, so sánh độ tương đồng giữa kết quả mô phỏng của mô hình với các tính chất của đối
tượng thực.
- Đưa ra kết luận và các phương án điều chỉnh cho đối tượng thực dựa trên kết quả mô phỏng sau khi phân tích chúng.
II. Yêu cầu thực hành
- Máy vi tính cài đặt sẵn phần mềm Matlab với đầy đủ các ứng dụng của Math Work cung cấp.
- Yêu cầu sinh viên được trang bị phần lý thuyết môn Tin học chuyên ngành, hết chương 3 môn Mô hình hóa.
- Tuân thủ mọi nội quy, quy định khi thực hành tại phòng thí nghiệm.
III. Nội dung thực hành
3.1 Bài toán yêu cầu
Hãy dùng máy tính số để mô phỏng độ dịch chuyển xi(t) và xo(t) của hệ thống vật lý có sơ đồ sau: - Độ cứng lò
xo K = 10

- Hệ số giảm lắc C = 20; - Khối lượng m = 10(kg); - Chu kỳ lấy mẫu T =1s


3.2. Các bước tiến hành
Bước 1. Tìm phương trình sai phân của hệ
Từ đồ thị tổng lực tác động lên vật m ta chiếu lên trụctọa độ nằm ngang.
Theo định luật II Newton
ta có:

* Biến đổi Laplace: Thay số và biến đổi Laplace với điều kiện ban đầu triệt tiêu ta có
m.s2. X0(s) + C.s.X0(s) + KX0(s) = KXi(s)
* Phương trình đại số: W(s) = X0(s)/Xi(s) = K/(ms2 + Cs + K)
* Tìm hàm truyền đạt số:


Thay s = (2/T).((z-1)/(z+1)) ta có:

W(z) = X0(z)/Xi(z) = (K.T2.z2 + 2K.T2.z + K.T2)/[(K.T2+2CT+4m)z2+(2KT2-8m)z+(4m-2CT+KT2)]
Đặt A = K.T2+2CT+4m; B = 2KT2-8m; D= 4m-2CT+KT2
W(z) = X0(z)/Xi(z) = (K.T2.z2 + 2K.T2.z + K.T2)/[Az2+Bz+D]
* Tìm phương trình sai phân của hệ thống
Az2 .X0(z) + Bz.X0(z) + D.X0(z) = K.T2.z2 .Xi(z) + 2K.T2.z .Xi(z) + K.T2 .Xi(z)
A.x0(k+2) + B.x0(k+1) + D.x0(k) = K.T2xi (k+2) +2K.T2xi(k+1) + K.T2xi(k)
Nếu xét tín hiệu vào là hàm xi = 1(t) ta có Xi [k+2] = Xi [k+1] = Xi [k] =1
A.x0(k+2) + B.x0(k+1) + D.x0(k) = 4K.T2

x0(k+2) = (- B.x0(k+1) - D.x0(k) + 4K.T2)/A


Bước 2. Xây dựng giao diện mô phỏng hệ thống điều khiển tự động
(1) Khởi tạo guide trong phần mềm Matlab


(2) Thiết lập bảng điều khiển trên giao diện
- Chọn công cụ Panel


- Thay đổi tên cho Panel bằng cách kích đúp vào Panel và thay tên trong phần Title của nó từ tên
- Panel sang tên “Bang dieu
khien”

-


- Chọn công cụ Push Button để khai báo nút khảo sát hệ thống



Đổi tên PushButton trong String ở Inspector của nút sang tên mới là khảo sát hệ thống


+ Kết quả thu được


+ Tương tự như vậy ta lấy nút có tên gọi là “Thoát khỏi hệ thống”


- Chọn công cụ Panel để khai báo bảng nhập giá trị


- Chọn công cụ Static Text để định nghĩa các ô nhập giá trị từ bàn phím


+ Kích đúp vào Static Text để thay đổi tên Static Text trong String sang tên “Nhap gia tri do cung K =”


+Tương tự như vậy ta khai báo nhập giá trị cho m, C, T


- Chọn công cụ Edit Text để khai báo ô nhập và xuất giá trị trên màn hình
+ Kích đúp vào Edit1 để khai báo giá trị mặc định cho hệ số K = 10 trong phần String từ chuỗi ký tự “ Edit Text” sang
số 10. Làm tương tự chọn Edit2, Edit3, Edit4 cho các biến m, C, T, với mặc định m = 10; C = 20; và T = 1.


+ Tương tự ta lập bảng xuất giá trị tính toán ra:
> Giá trị cực đại xuất ra Edit6

> Thời gian đạt cực đại xuất ra Edit11
> Thời gian ổn định xuất ra Edit7
> Độ quá điều chỉnh xuất ra Edit8


Bước 3 Viết Code cho các nút điều khiển
(1) Nút thoát khỏi hệ thống
Nhấn chuột phải vào Nút “ Thoat khoi he thong” chọn View callBacks/Callback


+ Ta lập trình như sau:
hoi=questdlg('Ban muon thoat khoi chuong trinh?',...
'THUC SU MUON THOAT?','Yes','No','No');
if strcmp(hoi,'Yes')
close
if strcmp(hoi,'No')
return;
end
end



(2) Nút “Khao sat he thong”
Nhấn chuột phải vào Nút “Khao sat he thong” chọn View callBacks/Callback


+ Ta lập trình:
function varargout = Bai2(varargin)
% BAI2 MATLAB code for Bai2.fig
%

BAI2, by itself, creates a new BAI2 or raises the existing
%
singleton*.
%
%
H = BAI2 returns the handle to a new BAI2 or the handle to
%
the existing singleton*.
%
%
BAI2('CALLBACK',hObject,eventData,handles,...) calls the local
%
function named CALLBACK in BAI2.M with the given input arguments.
%
%
BAI2('Property','Value',...) creates a new BAI2 or raises the
%
existing singleton*. Starting from the left, property value pairs are
%
applied to the GUI before Bai2_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property application
%
stop. All inputs are passed to Bai2_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 Bai2
% Last Modified by GUIDE v2.5 05-Mar-2013 22:11:55
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Bai2_OpeningFcn, ...
'gui_OutputFcn', @Bai2_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 Bai2 is made visible.
function Bai2_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 Bai2 (see VARARGIN)
% Choose default command line output for Bai2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Bai2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

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

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(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)
% Ch??ng trình ví du 3.1 v? v? ?? th? c?a h? th?ng ?i?u khi?n t? ??ng

disp('MO PHONG HE THONG DIEU KHIEN TU DONG - THM')
clc;
% Khai bao bien
syms a b c d max k1 k2 t1 t2 tm tod xichma gd gm k km
y = ones(1,1000);%Khai bao y la mot ma tran 1 hang 1000 cot
K = get(handles.edit1,'String');% Nh?n chu?i ký t? ? edit1 gán cho k1
K = str2num(K);
%Chuy?n ??i t? chu?i ký t? sang d?ng s?
m = get(handles.edit2,'String');% Nh?n chu?i ký t? ? edit2 gán cho k2
m = str2num(m);
%Chuy?n ??i t? chu?i ký t? sang d?ng s?


C = get(handles.edit3,'String');% Nh?n chu?i ký t? ? edit3 gán cho t1
C = str2num(C);
%Chuy?n ??i t? chu?i ký t? sang d?ng s?
T = get(handles.edit4,'String');% Nh?n chu?i ký t? ? edit4 gán cho t2
T = str2num(T);
%Chuy?n ??i t? chu?i ký t? sang d?ng s?
A = K*T*T+2*C*T+4*m;
B = 2*K*T*T-8*m;
D=4*m-2*C*T+K*T*T;

y(1,1)=0; % Khai bao phan tu y(1) = 0
y(1,2)=0; % Khai bao phan tu y(2) = 0
y(1,3)=0; % Khai bao phan tu y(3) = 0
for k =1:998
y(1,k+2)=(-B*y(1,k+1)-D*y(1,k)+4*K*T*T)/A;
end


% Ch??ng trình ví du 3.1 v? v? ?? th? c?a h? th?ng ?i?u khi?n t? ??ng
% Tim gia tri lon nhat
max = y(1,1);
for k =1:998
if y(1,k)>max
max = y(1,k);
km = k;
end
end
set(handles.edit6,'String',max);
set(handles.edit11,'String',T*km);
%================================
%Tim khoang thoi gian on dinh
k =1000;
while abs((y(1,k)-1))<=0.05
k = k-1;
tod=k*T;
end
set(handles.edit7,'String',tod);
% Ch??ng trình ví du 3.1 v? v? ?? th? c?a h? th?ng ?i?u khi?n t? ??ng
%=================================
%Tim do qua dieu chinh
xichma = (max-1);
set(handles.edit8,'String',xichma);


%=================================
%Ve do thi
tx = 1:1000;
tx = T*tx;

subplot(2,2,2);
plot(tx,y)
title('DAC TINH QUA DO CUA VAT LY');
xlabel('t(Second)')
ylabel('y(t)')
hold on
grid on

Num = K; % Tu so cua ham truyen W(s)
Den = [m,C,K]; % Mau so cua ham truyen
W = tf(Num,Den)
step(Num,Den)
title('DAC TINH QUA DO CUA HE VAT LY');
xlabel('t')
ylabel('y(t)')

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% 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)
hoi=questdlg('Ban muon thoat khoi chuong trinh?',...
'THUC SU MUON THOAT?','Yes','No','No');
if strcmp(hoi,'Yes')
close
if strcmp(hoi,'No')
return;
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

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

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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit1_Callback(hObject, eventdata, handles)
% hObject
handle to edit1 (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 edit1 as text
%
str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)

% hObject
handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject
handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB


×