WiiLAB Example Code - getWiimoteIR
Note: MATLAB R2007a or greater is required for this code to run correctly.
Annotations
The getWiimoteIR function allows the user to obtain the current readings from the Wiimote's IR camera. The camera is capable of tracking up to four IR sources.
Usage
IR = getWiimoteIR(source);
* where source is 0, 1, 2, 3, or 4 corresponding to one of the four IR sources the Wiimote is capable of tracking. 0 is the parameter to get the Midpoint value of the two most distinct sources, this is the recommended source to use for position tracking.
Return
getWiimoteIR will return an array of two elements, the X and Y values of the IR source respectively. Each element will be a normalized value (between 0->1) reflecting the actual position of the IR source.
Raw Source Code
The source code is also available as a .m file download at the bottom of the page.
function [X Y] = getWiimoteIR (source)
% usage: getWiimoteIR (source)
% purpose: Gets the IR values from the wiimote for a given source
% (0,1,2,3,4)
% note: initializeWiimote() must be called before calling this function
global wiimote;
% update the wiimote state values
wiimote.GetIRState();
% assign outputs
switch (source)
case 0
if(wiimote.IR.midpoint.found)
X = wiimote.IR.midpoint.X;
Y = wiimote.IR.midpoint.Y;
else
X = 0;
Y = 0;
end
case 1
if (wiimote.IR.source1.found)
X = wiimote.IR.source1.X;
Y = wiimote.IR.source1.Y;
else
X = 0;
Y = 0;
end
case 2
if (wiimote.IR.source1.found)
X = wiimote.IR.source2.X;
Y = wiimote.IR.source2.Y;
else
X = 0;
Y = 0;
end
case 3
if (wiimote.IR.source1.found)
X = wiimote.IR.source3.X;
Y = wiimote.IR.source3.Y;
else
X = 0;
Y = 0;
end
case 4
if (wiimote.IR.source1.found)
X = wiimote.IR.source4.X;
Y = wiimote.IR.source4.Y;
else
X = 0;
Y = 0;
end
otherwise
% Bad input
error('Bad input to getWiimoteIR, "source" must be 1, 2, 3, 4 or 0 (for midpoint).');
end
--
JordanBrindza - 30 Jun 2008