Tải bản đầy đủ (.doc) (23 trang)

thiết kế module ghép nối vs pc qua giao diện lpt điều khiển góc quay cua hệ thống quan sát gồm 4 camera

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 (236.75 KB, 23 trang )

MỤC LỤC
MỤC LỤC 1
CHƯƠNG I. GIỚI THIỆU ĐỀ TÀI 2
1. Mục tiêu đề tài 2
2. Hoạt động 2
3. Thực hiện 2
CHƯƠNG II. PHẦN CỨNG 4
1. Động cơ bước 4
2. Webcam 4
3. Mạch điều khiển động cơ 5
CHƯƠNG III. PHẦN MỀM 7
1.Giao diện chương trình 7
2.Điều khiển động cơ bước 7
3.Code chương trình chính 9
4.Hướng phát triển 23
1
CHƯƠNG I. GIỚI THIỆU ĐỀ TÀI
1. Mục tiêu đề tài
 Thiết kế camera an ninh bám theo chuyển động trong vùng không gian
xác định-có giao tiếp, điều khiển bằng máy tính.
 Xây dựng giao diện phần mềm có hiệu quả giám sát cao, dễ sử dụng,
nhiều tính năng.
2. Hoạt động
Camera có tác dụng giám sát chuyển động trong một phạm vi giới hạn, ví dụ
một căn phòng kín.
 Khi có đối tượng xâm nhập vào phạm vi giám sát của Camera thì lập tức
sẽ có tín hiệu báo động cho con người (còi báo động và đèn báo động).
 Khi đối tượng chuyển động, Camera được điều khiển bởi động cơ bước
tiếp tục bám theo đối tượng, đưa tín hiệu báo động đồng thời liên tiếp
chụp lại ảnh của đối tượng.
3. Thực hiện


 Phần cứng
 Camera (webcam) thu tín hiệu hình ảnh và gửi về cho máy tính xử lý.
Webcam này kết nối với máy tính bằng cổng USB thông dụng. Trước khi hoạt
động, webcam cần được cài sẵn driver của nhà sản xuất.
 Để truyền động cho động cơ, ta sử dụng động cơ bước. Theo đó, đế của
Camera được gắn trực tiếp lên trục động cơ đặt thẳng đứng. Khi trục động cơ
quay, camera quay trong mặt phẳng ngang vuông góc với trục động cơ.
 Mạch điều khiển động cơ bước lấy xung từ cống LPT của máy tính qua
cách ly quang và IC đệm công suất để cấp xung cho các cuộn dây của động cơ.
 Phần mềm
 Lập trình điều khiển camera bằng ngôn ngữ visual studio 2010 với ưu
điểm là khả năng tính toán mạnh đồng thời hỗ trợ nhiều hàm thu thập và xử lý
hình ảnh từ các thiết bị số như camera.
2
 Chương trình viết trên nền form của studio 2010, tạo ra giao diện người
dùng-gần gũi với các ứng dụng windows, dễ vận hành, điều khiển, thao tác.
 Giao diện sử dụng tiếng Việt.
3
CHƯƠNG II. PHẦN CỨNG
1. Động cơ bước
Để điều khiển chuyển động của
camera, ta sử dụng động cơ bước
loại nhỏ do Trung Quốc sản xuất.
Một số đặc điểm kỹ thuật:
 Loại động cơ: động cơ đơn
cực 5 dây
 Điện áp trên mỗi cuộn dây:
24V DC
 Dòng điện trên mỗi cuộn:
0.5A

 Góc bước: 1.8
o
/bước. Với góc
bước rất nhỏ này ta có thể điều khiển camera bám đối tượng khá chính
xác.
2. Webcam
Để thu hình ảnh trong vùng giám
sát, ta sử dụng webcam kí hiệu
Tako01 của hãng Tako. Một số
đặc điểm kỹ thuật
 Độ phân giải ảnh : 300000
pixels
 Có 6 đèn hồng ngoại có thể
chụp ảnh vào ban đêm
 Kết nối máy tính qua cổng
USB.
4
 Tốc độ chụp ảnh còn hạn chế, tuy nhiên đáp ứng khá tốt mục tiêu đã đặt
ra của đề tài.
3. Mạch điều khiển động cơ
1 6
2
5
4
1 6
2
5
4
1 6
2

5
4
1 6
2
5
4
1 6
2
5
4
1 6
2
5
4
P 1
C O N N E C T O R D B 9
5
9
4
8
3
7
2
6
1
1 2 VL
S
D 1
L E D
L S 1

S P E A K E R
S
U 8
L M 7 8 0 5 / T O
V I N
1
G N D
2
V O U T
3
5 V
+
-
~
~
D 2
D I O D E B R I D G E _ 1 2 3 4
J 2
C O N 2
1
2
1 A
1 B
2 A
2 B
L E D
S P E A K E R
1 A
L E D
1 B

2 A
2 B
G _ P C
1 2 V
S P E A K E R
C 1
C
C 2
C
U 7
U L N 2 0 0 3 A
1 B
1
2 B
2
3 B
3
4 B
4
5 B
5
6 B
6
7 B
7
1 C
1 6
2 C
1 5
3 C

1 4
4 C
1 3
5 C
1 2
6 C
1 1
7 C
1 0
C O M
9
G N D
8
G _ P C
5 V
5 V
5 V
5 V
5 V
5 V
L
1 A _ M
1 B _ M
2 A _ M
2 B _ M
L E D _ M
S P E A K E R _ M
1 2 V
1 A _ M
1 B _ M

2 A _ M
2 B _ M
L E D _ M
S P E A K E R _ M
6 8
1 2 V
J 1
C O N 5
1
2
3
4
5
 Khối kết nối với cổng LPT máy tính
P 1
C O N N E C T O R D B 9
5
9
4
8
3
7
2
6
1
1 A
L E D
1 B
2 A
2 B

G _ P C
S P E A K E R
 Khối cách ly quang: cách ly mạch lực động cơ và mạch điều khiển từ cổng
LPT, đảm bảo an toàn cho cổng máy in.
5
1 6
2
5
4
1 6
2
5
4
1 6
2
5
4
1 6
2
5
4
1 6
2
5
4
1 6
2
5
4
1 A

1 B
2 A
2 B
L E D
S P E A K E R
G _ P C
5 V
5 V
5 V
5 V
5 V
5 V
1 A _ M
1 B _ M
2 A _ M
2 B _ M
L E D _ M
S P E A K E R _ M
 Khối tạo nguồn 5V cấp cho Cách ly quang
U 8
L M 7 8 0 5 / T O
V I N
1
G N D
2
V O U T
3
5 V
+
-

~
~
D 2
D I O D E B R I D G E _ 1 2 3 4
J 2
C O N 2
1
2
1 2 V
C 1
C
C 2
C
 Khối IC đệm: sử dụng IC ULN2003A
1 2 VL
S
U 7
U L N 2 0 0 3 A
1 B
1
2 B
2
3 B
3
4 B
4
5 B
5
6 B
6

7 B
7
1 C
1 6
2 C
1 5
3 C
1 4
4 C
1 3
5 C
1 2
6 C
1 1
7 C
1 0
C O M
9
G N D
8
1 A _ M
1 B _ M
2 A _ M
2 B _ M
L E D _ M
S P E A K E R _ M
6 8
1 2 V
J 1
C O N 5

1
2
3
4
5
 Khối báo động: còi báo động và đèn báo động
D 1
L E D
L S 1
S P E A K E R
S
L
1 2 V
6
CHƯƠNG III. PHẦN MỀM
1. Giao diện chương trình
2. Điều khiển động cơ bước
Để điều khiển động cơ bước, ta cần phát xung qua cổng LPT của máy tính.
Hỗ trợ việc này, matlab cần sử dụng 2 thư viện liên kết động là io32.dll và
inpout32.dll.
Đầu tiên, khai báo sử dụng thư viện này như sau:
obj_io=io32;
status=io32(obj_io);
address=hex2dec('378');%địa chỉ của cổng LPT
Để xuất giá trị 60 qua cổng LPT:
io32(obj_io,address,60)
Giả sử để xuất xung điều khiển động cơ quay theo chiều thuận, ta phát các
chùm xung tương ứng (cách điều khiển 2 pha cả bước):
7
0011 1100

0011 0110
0011 0011
0011 1001
Trong đó: 0011 để điều khiển còi và loa báo động (bit 1).
Hàm quay trái của động cơ:
function quay_trai(obj_io,address,x)
%obj_io=io32;
%status=io32(obj_io);
%if status =0 thi cong da san sang
%address=hex2dec('378');
for i=1:x
io32(obj_io,address,60)
pause(0.01);
io32(obj_io,address,57);
pause(0.01);
io32(obj_io,address,51);
pause(0.01);
io32(obj_io,address,54);
pause(0.01);
end
io32(obj_io,address,0);
Hàm quay phải của động cơ:
function quay_phai(obj_io,address,x)
%obj_io=io32;
%status=io32(obj_io);
8
%if status =0 thi cong da san sang
%address=hex2dec('378');
for i=1:x
io32(obj_io,address,54)

pause(0.01);
io32(obj_io,address,51);
pause(0.01);
io32(obj_io,address,57);
pause(0.01);
io32(obj_io,address,60);
pause(0.01);
end
io32(obj_io,address,0);
3. Code chương trình chính
function varargout = myCameraGUI(varargin)
% MYCAMERAGUI M-file for myCameraGUI.fig
% MYCAMERAGUI, by itself, creates a new MYCAMERAGUI or raises
the existing
% singleton*.
%
% H = MYCAMERAGUI returns the handle to a new MYCAMERAGUI or
the handle to
% the existing singleton*.
%
% MYCAMERAGUI('CALLBACK',hObject,eventData,handles, ) calls the
local
9
% function named CALLBACK in MYCAMERAGUI.M with the given
input arguments.
%
% MYCAMERAGUI('Property','Value', ) creates a new MYCAMERAGUI
or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before myCameraGUI_OpeningFunction gets called.

An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to myCameraGUI_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 myCameraGUI

% Last Modified by GUIDE v2.5 25-May-2009 03:24:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename,
'gui_Singleton', gui_Singleton,
'gui_OpeningFcn', @myCameraGUI_OpeningFcn,
'gui_OutputFcn', @myCameraGUI_OutputFcn,
10
'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 myCameraGUI is made visible.
function myCameraGUI_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 myCameraGUI (see VARARGIN)

%khai bao cong vao ra LPT
global obj_io address status temp tong_so_anh
tong_so_anh=str2double(get(handles.tong_so_anh,'String'));
temp=0;
obj_io=io32;
11
status=io32(obj_io);
address=hex2dec('378');
% Choose default command line output for myCameraGUI
handles.output = hObject;
imaqreset; %reset webcam

%khai bao cac doi tuong anh
handles.video=videoinput('winvideo',1,'RGB24_352x288');
set(handles.video,'TriggerRepeat',Inf);
set(handles.video, 'FramesPerTrigger', 1)
triggerconfig(handles.video, 'Manual');

start(handles.video);
% Update handles structure
guidata(hObject, handles);



% UIWAIT makes myCameraGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%========================================================
===
uiwait(handles.MyCameraGUI);
%========================================================
===

% Outputs from this function are returned to the command line.
function varargout = myCameraGUI_OutputFcn(hObject, eventdata, handles)
12
% 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)
%==================
handles.output = hObject;
%==================
% Get default command line output from handles structure
varargout{1} = handles.output;


% Executes on button press in startStopCamera.
%function startStopCamera_Callback(hObject, eventdata, handles)

% hObject handle to startStopCamera (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

%========================================================
==================
function startStopCamera_Callback(hObject, eventdata, handles)
% hObject handle to startStopCamera (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global obj_io address bien_trai bien_phai nguong n so_anh tong_so_anh
% Start/Stop Camera
tong_so_anh=str2double(get(handles.tong_so_anh,'String'));
if strcmp(get(hObject,'String'),'Take Photo')
13
so_anh=so_anh+1;
if so_anh==tong_so_anh+1
so_anh=1;
end
imwrite(getsnapshot(handles.video),strcat(num2str(so_anh),'.jpg'),'jpg');
end
if strcmp(get(hObject,'String'),'Start Camera')&&
(get(handles.automation,'Value')==1)
% Camera is off. Change button string and start camera.
set(hObject,'String','Stop Camera');
n=0;
so_anh=0;
bien_trai=176;
bien_phai=176;
nguong=50;

while strcmp(get(hObject,'String'),'Stop Camera')&&
strcmp(get(hObject,'Enable'),'on')
time=clock;
Image1=getsnapshot(handles.video);
%pause(0.05);
imshow(Image1);
Image2=getsnapshot(handles.video);
imshow(Image2);

if rem(round(time(6)),3)==0
so_anh=so_anh+1;
if so_anh==(tong_so_anh+1)
14
so_anh=1;
end
imwrite(Image2,strcat(num2str(so_anh),'.jpg'),'jpg');
end
Image=imsubtract(Image1,Image2);
%handles=guidata(gcf); de thi khong chay duoc
Image_grey= rgb2gray(Image);
for i=1:1:352
if max(Image_grey(:,i))>=nguong
bien_trai=i;
break
end
end

for i=352:-1:1
if max(Image_grey(:,i))>=nguong
bien_phai=i;

break
end
end
trung_tam=int16(((bien_trai)+(bien_phai))/2);
n=int8(abs((176-trung_tam))/70);
if (trung_tam<176)
quay_trai(obj_io,address,n);
elseif (trung_tam>176)
quay_phai(obj_io,address,n);
end
15

end
%=====================================================
%start(handles.video)
elseif strcmp(get(hObject,'String'),'Stop Camera')
% Camera is on. Stop camera and change button string.
set(handles.startStopCamera,'String','Start Camera');
%stop(handles.video);
%close all;
end
% Executes on button press in automation.
function automation_Callback(hObject, eventdata, handles)
% hObject handle to automation (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 automation
set(handles.automation,'Value',1);
set(handles.startStopCamera,'Enable','on');

set(handles.startStopCamera,'String','Stop Camera');
set(handles.direction,'Enable','off');


% Executes on button press in manual.
function manual_Callback(hObject, eventdata, handles)
% hObject handle to manual (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
16
% handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of manual
set(handles.manual,'Value',1);
%set(handles.startStopCamera,'Enable','off');
set(handles.startStopCamera,'String','Take Photo');
%set(handles.startStopCamera,'Enable','off');
set(handles.direction,'Enable','on');
set(handles.direction,'Value',0);
uicontrol(handles.direction);
stop(handles.video);
start(handles.video);
while (get(handles.automation,'Value')~=1)
imshow(getsnapshot(handles.video));
end

% Executes on slider movement.
function direction_Callback(hObject, eventdata, handles)
% hObject handle to direction (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global obj_io address temp
quay=get(handles.direction,'Value')*100-temp;
temp=get(handles.direction,'Value')*100;
if (quay<0)
17
quay_trai(obj_io,address,int16(abs(quay)));
elseif (quay>0)
quay_phai(obj_io,address,int16(quay));
end


% Executes on mouse motion over figure - except title and menu.
function MyCameraGUI_WindowButtonMotionFcn(hObject, eventdata,
handles)
% hObject handle to MyCameraGUI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


% Executes during object creation, after setting all properties.
function cameraAxes_CreateFcn(hObject, eventdata, handles)
% hObject handle to cameraAxes (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate cameraAxes



% Executes during object creation, after setting all properties.
function direction_CreateFcn(hObject, eventdata, handles)
% hObject handle to direction (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
18
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% Executes on mouse press over figure background, over a disabled or
% inactive control, or over an axes background.
function MyCameraGUI_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject handle to MyCameraGUI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Executes on scroll wheel click while the figure is in focus.
function MyCameraGUI_WindowScrollWheelFcn(hObject, eventdata, handles)
% hObject handle to MyCameraGUI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Executes when user attempts to close MyCameraGUI.
function MyCameraGUI_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to MyCameraGUI (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
19


% Hint: delete(hObject) closes the figure
% Get the current position of the GUI from the handles structure
% to pass to the modal dialog.
pos_size = get(handles.MyCameraGUI,'Position');
% Call modaldlg with the argument 'Position'.
user_response = modaldlg('Title','Confirm Close');
switch user_response
case ('No')
% take no action
case ('Yes')
% Prepare to close GUI application window
% .
% .
% .
delete(hObject);
close all;
end
% Executes during object deletion, before destroying properties.
function MyCameraGUI_DeleteFcn(hObject, eventdata, handles)
% hObject handle to MyCameraGUI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Executes on button press in Exit.
function Exit_Callback(hObject, eventdata, handles)

20
% hObject handle to Exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close all;


% Executes on button press in about.
function about_Callback(hObject, eventdata, handles)
% hObject handle to about (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
about;



% Executes on button press in Help.
function Help_Callback(hObject, eventdata, handles)
% hObject handle to Help (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
help;
function tong_so_anh_Callback(hObject, eventdata, handles)
% hObject handle to tong_so_anh (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

21
% Hints: get(hObject,'String') returns contents of tong_so_anh as text
% str2double(get(hObject,'String')) returns contents of tong_so_anh as a

double


% Executes during object creation, after setting all properties.
function tong_so_anh_CreateFcn(hObject, eventdata, handles)
% hObject handle to tong_so_anh (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

% Executes on button press in so_anh.
function so_anh_Callback(hObject, eventdata, handles)
% hObject handle to so_anh (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global tong_so_anh
tong_so_anh=str2double(get(handles.tong_so_anh,'String'));
22
4. Hướng phát triển
Từ đề tài này có thể phát triển lên, xây dựng nhiều ứng dụng giám sát an
ninh có hiệu quả trong thực tế. Chúng em đề xuất một số hướng phát triển sau:
• Phát triển thuật toán xử lý ảnh, sao cho nhận dạng được hình dạng đối
tượng, giúp cho việc bám đối tượng thêm hiệu quả.
• Tăng thêm bậc tự do cho camera giúp cho camera giám sát được linh

hoạt, theo nhiều chiều trong không gian.
23

×