[3D] Draw in air with Wii controller in Matlab
This code in matlab enables you to write with your wiimote in free-air to a pointcloud. So you can make a 3d drawing! You can draw in the air, either connected lines or just points. The code is actually fairly easy, but this is what you need to do:
Put the wiimote somewhere pointing to you (we will use its infrared cam). Then, you can draw in the air using either infrared leds connected to batterys, or better: buy a infrared bar from ebay! I did and tested both just fine. This code i wrote 32 years a go so its a bit old. But so are the wiimotes. But, they’re cheap too! Buy them via ebay for like $15.
Requirements
You need a infrared LED (just connect it straight to a AAA 1.5v battery =)). And otherwise you need a Infrared wii bar (buy it on ebay for like $2).
You need to install the WiiLAB files. Also, you have to connect the Wiimote via bluetooth with your computer (easy). I recommend blue-soleil as bluetooth driver, that worked very well for me. Also this code is tested in Windows 7 and Windows XP.
Code
addpath 'C:Documents and SettingsXXXMijn documentenMATLABWiiLabWiiLab_MatlabWiimoteFunctions'
addpath 'C:Documents and SettingsXXXMijn documentenMATLABWiiLabWiiLab_MatlabEG111-H'
clc
clear all
close all
% Connect to the Wiimote
initializeWiimote();
% Create the graphics Window (optional but recommended)
%initializeWindow(0);
% -------------------------------------------------------------
% YOUR CODE HERE
% Any graphics objects that will be used throughout your program should go
% here.
% For example, if you are creating an program that will use the Wiimote
% to move a circle around the screen, you should create the circle here.
Im=imread('test.jpeg');
figure
xLB=200;
yLB=200;
hoekdiff=0;
afstscale=0;
xM=0; yM=0; zM=0;t=1;
BG=uint8(ones(800,800));
BGIm=uint8(BG);
BGIm(xLB:xLB-1+size(Im,1),yLB:yLB-1+size(Im,2),1:3)=Im;
%imshow(BGIm)
x=0;y=0;z=0;
x1=0;x2=0;y1=0;y2=0; eerste=1;
getWiimoteBattery()
% -------------------------------------------------------------
% Check to make sure the Wiimote is connected
if( isWiimoteConnected() > 0 )
% Main program loop
% Used for polling the Wiimote data
% This loop will run until 'HOME' on the Wiimote is Pressed
while(~isButtonPressed('HOME'))
% -------------------------------------------------------------
% YOUR CODE HERE
% The main portion of your program will go here.
% Functies:
Twee=0;
[xp1 yp1 xp2 yp2] = getWiimoteIR;
if xp1>0 && yp1<1 && yp1>0 && yp1<1
x1=1-xp1;
y1=yp1;
Twee=1;
end
if xp2>0 && yp2<1 && yp2>0 && yp2<1
x2=1-xp2;
y2=yp2;
Twee=Twee+1;
end
if Twee == 2
t=t+1;
clear Imr BGIm
BGIm=BG;
if eerste == 1
hoekeerste=atand((y2-y1)/(x2-x1));
afst=sqrt((y2-y1).^2+(x2-x1).^2);
end
eerste =0;
hoeklive=atand((y2-y1)/(x2-x1));
afstlive=sqrt((y2-y1).^2+(x2-x1).^2);
hoekdiff=hoekeerste-hoeklive;
%afstscale=afstlive/afst;
xM(t)=(x2+(x1-x2)/2)*200;
yM(t)=(y2+(y1-y2)/2)*200;
zM(t)=afstlive*500;
beep
plot3(xM(2:end),zM(2:end),yM(2:end))
axis([0 200 0 200 0 200])
%Imr=imrotate(Im,-hoekdiff,'nearest');
%Imr=imresize(Imr,abs(afstscale),'nearest');
%BGIm(xLB:xLB-1+size(Imr,1),yLB:yLB-1+size(Imr,2),1:3)=Imr;
%imshow(BGIm)
else
eerste=1;
%Im=Imr;
end
%plot([x1,x2,],[y1,y2],'o','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)
%hold on
%plot([xM],[yM],'o','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','r','MarkerSize',10)
%axis([0 1 0 1])
%hold off
%text(0.35,0.5,['hoek ' num2str(hoekdiff)],'HorizontalAlignment','left')
%text(0.35,0.6,['afstandschaal ' num2str(afstscale)],'HorizontalAlignment','left')
%text(0.35,0.7,['Locatie Mid ' num2str(afstscale)],'HorizontalAlignment','left')
if(isButtonPressed('ONE'))
disp('Calibration in 2 sec..')
end
if(isButtonPressed('TWO'))
x=0;y=0;z=0;
close all
figure
end
if(isButtonPressed('B'))
setWiimoteRumble(true)
pause(.3)
setWiimoteRumble(false)
end
if(isButtonPressed('A'))
setWiimoteLEDs(1, 0, 0, 0);
pause(.1)
setWiimoteLEDs(0, 1, 0, 0);
pause(.1)
setWiimoteLEDs(0, 0, 1, 0);
pause(.1)
setWiimoteLEDs(0, 0, 0, 1);
pause(.1)
end
% Pause - This is a required step. If it is not included you will
% experience unexpected behavior.
pause(.001);
% -------------------------------------------------------------
% Redraw - This is for the graphics functions to make sure they are
% displaying the most recent changes
redraw();
% Pause - This is a required step. If it is not included you will
% experience unexpected behavior.
pause(.001);
end % while
else
% The Wiimote was not successfully connected
message = [ 'There was a problem with connecting to the Wiimote. ' ...
'Please make sure the Wiimote is properly connected ' ...
'to the computer and rerun the program.' ];
% This will write the message to the command line
disp(message);
% This will create a message box with the message
uiwait(msgbox(message, 'Connection Problem', 'error'));
end % if
close all
% Disconnect the Wiimote
disconnectWiimote();