|
Tracking IR LEDs with Matlab
by W. Thielicke
Programmed for my tricopter (micro air vehicle), http://shrediquette.blogspot.com/
My tricopter tracking process uses several functions: There is one function (StartCam.m) that sets up everything, starts a GUI and another function (independentTrack_timer.m). The latter is the main function, which grabs the images from the camera and executes a tracking algorithm. A GUI is used to enable "on the fly" parameter editing. I will not include the code of the GUI because it is quite a lot of code and in essence, it doesn't do anything important. It simply makes PID parameter optimization more comfortable. Another function (figureclosingTrack.m) is used as the close request function of the GUI. It will be executed when the user wants to stop tracking. This function closes all connections (camera, serialport, timer function).
I tried to comment everything in the source code. Comments are always on top of the commands that they describe.
--------------------------------------------------------------------------------
This is the code of the .m file "StartCam.m". This function connects to a USB webcam, starts a timer and opens a serial port connection.
function StartCam (s,events)
%This block deletes all variables that were stored as application data
%(via "setappdata") in previous sessions. Restarting Matlab would have the
%same effect. The variables have to be deleted in order to enable a "clean" start.
try
app=getappdata(0);
appdatas = fieldnames(app);
for kA = 1:length(appdatas)
rmappdata(0,appdatas{kA});
end
catch ME
disp('rmappdata unsuccessful')
disp(ME.message)
end
%Clear the variables in the workspace
clear all
clc
%Find available video devices
info = imaqhwinfo('winvideo');
%Create a video object (camera device nr.2, resolution setting 5 = 640x480)
vid = videoinput('winvideo', 2, info.DeviceInfo(1,2).SupportedFormats{5});
%If we send a trigger signal, we want to receive 1 frame from the camera
set(vid,'FramesPerTrigger',1);
%Don't stop automatically after one frame was triggered
set(vid,'TriggerRepeat',inf);
%Manual means: The camera only records a frame if we send a trigger signal
triggerconfig(vid, 'Manual');
%Specific setting for my USB Camera
srcObj1 = get(vid, 'Source');
set(srcObj1(1), 'FrameRate', '30');
set(srcObj1(1), 'sharpness', 4);
set(srcObj1(1), 'BacklightCompensation', 'off');
set(srcObj1(1), 'ColorEnable', 'off');
set(srcObj1(1), 'ExposureMode', 'manual');
set(srcObj1(1), 'Exposure', 5);
%Connect to the camera...
start(vid);
%...and trigger a frame
trigger(vid)
%get the frame from the camera (the frame will not be used, just for "fun")
A = (getdata(vid,1,'uint8')); %get a frame from cam
%specific settings for my GUI application
setappdata(0,'timecount',0);
setappdata(0,'creep',0);
%I am calling "tic" here, because I will measure the speed of the analysis
%in a different function. "toc" without "tic" won't work of course...
tic
%start the GUI where you can set all parameters for the PID control loop
sensGUI;
%Get the "address" of the GUI
HsensGUI=getappdata(0,'sensGUI');
%Get a list of all elements in the GUI
handles=guihandles(HsensGUI);
%Some changes of the appearance of the GUI
set(HsensGUI, 'DockControls', 'off', 'menubar', 'none', 'name', 'Tritrack');
%When the user closes the GUI, some specfic actions have to be performed
%(e.g. closing the serialport, shutting down camera etc.) These actions will
%be found in the function "figureclosingTrack.m"
set(HsensGUI,'CloseRequestFcn',@figureclosingTrack)
%save the video object as appdata, so that other functions know which
%camera was selected.
setappdata(0,'vid', vid);
%save the list of elements of the GUI
setappdata(0,'handles',handles);
%Create a serialport with specific settings
s = serial('COM5','BaudRate',38400);
%After each command that was send via the serial port,
%a "carriage return" is added
set(s, 'Terminator', 'CR');
%open the serial port
fopen(s);
%save the serialport object
setappdata(0,'s',s);
%Create a timer object that will execute the function in
%"independentTrack_timer.m" as fast as possible with a pause of 0.01
%seconds between each execution. This little pause is necessary for matlab
%to perform other tasks. If we don't pause, Matlab will not accept any
%commands from the user anymore (that means you can't end the execution of
%the function independentTrack_timer anymore, nor can you exit Matlab.
t = timer('TimerFcn',@independentTrack_timer, 'Period', 0.01,'ExecutionMode', 'fixedSpacing');
%Start the timer
start(t)
%Open a window that shows a live preview of the camera image. First I
%wanted to have this preview inside my GUI. But it is much faster if you
%use the preview(vid) function. I don't know why.
preview(vid);
|
|