WiiLAB Example Code - Getting Started With WiiLAB and MATLAB
This tutorial shows you how to use WiiLAB and the Wiimote in a MATLAB program. You have to first install the WiiLAB libraries on your computer; a tutorial on how to do that is found
here. It would also be beneficial to go over the most common WiiLAB functions and their uses, which can be found
here.
This page goes over the SkeletonCode file and what is included in it. There is also a program that uses the SkeletonCode file to create simple program that shows how to use the Wiimote's acceleration to move a graphics object around the screen, found
here
SkeletonCode
Getting Started with WiiLAB
- The first thing you should do in every program is to add the paths that contain any custom functions you are using. In our case we need to add the path for the WiimoteFunctions and the graphics functions that we will be using (EG111-H for UND students).
- The next thing that you need to do in every program that uses WiiLAB and the Wiimote is to connect the Wiimote.
- This is done by simply calling the
initializeWiimote global function. This will connect to the Wiimote and initialize the MATLAB object that will communicate with the Wiimote.
- Almost every program that uses the Wiimote will also be using some sort of graphics functions. This is also a good point in the program to initialize the graphics window you are going to be using. For those using the EG111 graphics functions then the
initializeWindow(0) command will create the graphics window.
- After the
initializeWiimote method is called it is always a good idea to make sure that the Wiimote was connected successfully. The initializeWiimote function will write a command line message indicating whether or not it was able to connect to the Wiimote, but this does not allow our program to automatically check if the Wiimote was connected. The isWiimoteConnected function was added for this purpose. The function will return a number greater than zero if the Wiimote was successfully connected and a number less than or equal to zero if it was not.
- Now that the Wiimote is successfully connected, we start the actual program. Using the Wiimote with WiiLAB will require you to poll the Wiimote information.
- Finally, the very last thing that you should do is disconnect the Wiimote. To do this simply call the
disconnectWiimote function.
- This will disconnect the Wiimote from your program, allowing another program to connect to it. It is always good practice to disconnect the Wiimote when your program is finished with it.
Below is the complete version of the default program that you should start with. The .m file is also attached at the bottom of the page.
%------------------------------------------------------------------------
% Getting Started Program Demo
%
% This program is designed to give a new user the skeleton code to allow
% them to easily and quickly begin using WiiLAB. The program contains all
% the vital code that is needed to connect to and use the Wiimote along
% with reserved sections for the user to add their code.
%
% Jordan Brindza, University of Notre Dame
%------------------------------------------------------------------------
% Add the paths for the Wiimote and Graphics Functions
% These paths are assuming you are using the installer for WiiLAB that was
% provided. If you did not then change the paths to the location of the
% WiimoteFunctions and EG111-H folders on your computer
addpath C:\'Program Files'\Wiimote\WiiLAB_Matlab\WiimoteFunctions
addpath C:\'Program Files'\Wiimote\WiiLAB_Matlab\EG111-H
% 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 a program that will use the Wiimote
% to move a circle around the screen, you should create the circle here.
% -------------------------------------------------------------
% 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.
% -------------------------------------------------------------
% 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
% Disconnect the Wiimote
disconnectWiimote();
--
JordanBrindza - 21 Jul 2008