function edit6_CreateFcnhObject, eventdata, handles % hObject handle to edit6 see GCBO % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not cr
Trang 1Bà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
Trang 23.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
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
Trang 3Bướ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
Trang 4(2) Thiết lập bảng điều khiển trên giao diện
- Chọn công cụ Panel
Trang 5- 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”
-
Trang 6- Chọn công cụ Push Button để khai báo nút khảo sát hệ thống
Trang 7Đổi tên PushButton trong String ở Inspector của nút sang tên mới là khảo sát hệ thống
Trang 8+ Kết quả thu được
Trang 9+ Tương tự như vậy ta lấy nút có tên gọi là “Thoát khỏi hệ thống”
Trang 10- Chọn công cụ Panel để khai báo bảng nhập giá trị
Trang 11- Chọn công cụ Static Text để định nghĩa các ô nhập giá trị từ bàn phím
Trang 12+ Kích đúp vào Static Text để thay đổi tên Static Text trong String sang tên “Nhap gia tri do cung K =”
Trang 13+Tương tự như vậy ta khai báo nhập giá trị cho m, C, T
Trang 14- 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
Trang 15+ 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
Trang 16Bướ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
Trang 17+ Ta lập trình như sau:
hoi=questdlg('Ban muon thoat khoi chuong trinh?',
'THUC SU MUON THOAT?','Yes','No','No');
Trang 19(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
Trang 20+ 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
Trang 21% - 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
% 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 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?
Trang 22C = 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
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
Trang 23Num = K; % Tu so cua ham truyen W(s)
Den = [m,C,K]; % Mau so cua ham truyen
% - 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');
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)
Trang 24% 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)
Trang 25% 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
Trang 26% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% - Executes during object creation, after setting all properties
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% - Executes during object creation, after setting all properties
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
Trang 27% 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
% - 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
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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
end
function edit11_Callback(hObject, eventdata, handles)
Trang 28% hObject handle to edit11 (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 edit11 as text
% str2double(get(hObject,'String')) returns contents of edit11 as a double
% - Executes during object creation, after setting all properties
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit11 (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 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)
%
-function Untitled_2_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)