[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

  1. addpath 'C:Documents and SettingsXXXMijn documentenMATLABWiiLabWiiLab_MatlabWiimoteFunctions'
  2. addpath 'C:Documents and SettingsXXXMijn documentenMATLABWiiLabWiiLab_MatlabEG111-H'
  3.  
  4. clc
  5. clear all
  6. close all
  7. % Connect to the Wiimote
  8. initializeWiimote();
  9.  
  10. % Create the graphics Window (optional but recommended)
  11. %initializeWindow(0);
  12.  
  13.  
  14. % -------------------------------------------------------------
  15.  
  16.  
  17. % YOUR CODE HERE
  18. % Any graphics objects that will be used throughout your program should go
  19. % here.
  20. % For example, if you are creating an program that will use the Wiimote
  21. % to move a circle around the screen, you should create the circle here.
  22.  
  23.  
  24. Im=imread('test.jpeg');
  25. figure
  26.  
  27.  
  28. xLB=200;
  29. yLB=200;
  30. hoekdiff=0;
  31. afstscale=0;
  32. xM=0; yM=0; zM=0;t=1;
  33.  
  34. BG=uint8(ones(800,800));
  35. BGIm=uint8(BG);
  36. BGIm(xLB:xLB-1+size(Im,1),yLB:yLB-1+size(Im,2),1:3)=Im;
  37.  
  38. %imshow(BGIm)
  39.  
  40. x=0;y=0;z=0;
  41. x1=0;x2=0;y1=0;y2=0; eerste=1;
  42. getWiimoteBattery()
  43. % -------------------------------------------------------------
  44.  
  45.  
  46. % Check to make sure the Wiimote is connected
  47. if( isWiimoteConnected() > 0 )
  48.  
  49.  
  50.     % Main program loop
  51.     % Used for polling the Wiimote data
  52.     % This loop will run until 'HOME' on the Wiimote is Pressed
  53.     while(~isButtonPressed('HOME'))
  54.  
  55.  
  56.         % -------------------------------------------------------------
  57.  
  58.  
  59.  
  60.         % YOUR CODE HERE
  61.         % The main portion of your program will go here.
  62.         % Functies:
  63.  
  64.         Twee=0;
  65.  
  66.         [xp1 yp1 xp2 yp2] = getWiimoteIR;
  67.         if xp1>0 && yp1<1 && yp1>0 && yp1<1
  68.             x1=1-xp1;
  69.             y1=yp1;
  70.             Twee=1;
  71.         end
  72.         if xp2>0 && yp2<1 && yp2>0 && yp2<1
  73.             x2=1-xp2;
  74.             y2=yp2;
  75.             Twee=Twee+1;
  76.         end
  77.  
  78.         if Twee == 2
  79.             t=t+1;
  80.             clear Imr BGIm
  81.             BGIm=BG;
  82.             if eerste == 1
  83.                 hoekeerste=atand((y2-y1)/(x2-x1));
  84.                 afst=sqrt((y2-y1).^2+(x2-x1).^2);
  85.             end
  86.             eerste =0;
  87.             hoeklive=atand((y2-y1)/(x2-x1));
  88.             afstlive=sqrt((y2-y1).^2+(x2-x1).^2);
  89.  
  90.  
  91.             hoekdiff=hoekeerste-hoeklive;
  92.             %afstscale=afstlive/afst;
  93.  
  94.  
  95.             xM(t)=(x2+(x1-x2)/2)*200;
  96.             yM(t)=(y2+(y1-y2)/2)*200;
  97.             zM(t)=afstlive*500;
  98.  
  99.  
  100.  
  101.              beep
  102.  
  103.              plot3(xM(2:end),zM(2:end),yM(2:end))
  104.              axis([0 200 0 200 0 200])
  105.  
  106.             %Imr=imrotate(Im,-hoekdiff,'nearest');
  107.             %Imr=imresize(Imr,abs(afstscale),'nearest');
  108.  
  109.             %BGIm(xLB:xLB-1+size(Imr,1),yLB:yLB-1+size(Imr,2),1:3)=Imr;
  110.             %imshow(BGIm)
  111.  
  112.         else
  113.             eerste=1;
  114.             %Im=Imr;
  115.         end
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.         %plot([x1,x2,],[y1,y2],'o','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)
  123.         %hold on
  124.         %plot([xM],[yM],'o','LineWidth',2,'MarkerEdgeColor','b','MarkerFaceColor','r','MarkerSize',10)
  125.         %axis([0 1 0 1])
  126.         %hold off
  127.         %text(0.35,0.5,['hoek  ' num2str(hoekdiff)],'HorizontalAlignment','left')
  128.         %text(0.35,0.6,['afstandschaal  ' num2str(afstscale)],'HorizontalAlignment','left')
  129.         %text(0.35,0.7,['Locatie Mid  ' num2str(afstscale)],'HorizontalAlignment','left')
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.         if(isButtonPressed('ONE'))
  141.             disp('Calibration in 2 sec..')
  142.         end
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.     if(isButtonPressed('TWO'))
  150.         x=0;y=0;z=0;
  151.         close all
  152.         figure
  153.     end
  154.  
  155.     if(isButtonPressed('B'))
  156.         setWiimoteRumble(true)
  157.         pause(.3)
  158.         setWiimoteRumble(false)
  159.     end
  160.     if(isButtonPressed('A'))
  161.         setWiimoteLEDs(1, 0, 0, 0);
  162.         pause(.1)
  163.         setWiimoteLEDs(0, 1, 0, 0);
  164.         pause(.1)
  165.         setWiimoteLEDs(0, 0, 1, 0);
  166.         pause(.1)
  167.         setWiimoteLEDs(0, 0, 0, 1);
  168.         pause(.1)
  169.     end
  170.  
  171.  
  172.  
  173.  
  174.  
  175.     % Pause - This is a required step. If it is not included you will
  176.     % experience unexpected behavior.
  177.     pause(.001);
  178.  
  179.     % -------------------------------------------------------------
  180.  
  181.     % Redraw - This is for the graphics functions to make sure they are
  182.     % displaying the most recent changes
  183.     redraw();
  184.  
  185.     % Pause - This is a required step. If it is not included you will
  186.     % experience unexpected behavior.
  187.     pause(.001);
  188.  
  189. end % while
  190.  
  191. else
  192.  
  193.     % The Wiimote was not successfully connected
  194.     message = [ 'There was a problem with connecting to the Wiimote. ' ...
  195.         'Please make sure the Wiimote is properly connected ' ...
  196.         'to the computer and rerun the program.' ];
  197.  
  198.     % This will write the message to the command line
  199.     disp(message);
  200.  
  201.     % This will create a message box with the message
  202.     uiwait(msgbox(message, 'Connection Problem', 'error'));
  203.  
  204.     end % if
  205.     close all
  206.  
  207.     % Disconnect the Wiimote
  208.     disconnectWiimote();

Tim Zaman

MSc Biorobotics. Specialization in computer vision and deep learning. Works at NVIDIA.

You may also like...