Educational materials

View   r1
ExWiiLabSkeletonCodeObjectMove 1 - 21 Jul 2008 - Main.JordanBrindza
Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WiiLabMatlabGettingStartedTutorial"

WiiLAB Example Code - Using the SkeletonCode

This tutorial shows you how to use create a program that uses the Wiimote's acceleration to move a graphics around the screen. The program is built off of the SkeletonCode file.


Tutorial

Using the SkeletonCode

  1. The first thing to notice is that this program is created without changing any of the provided code from the given SkeletonCode file. The only code that is added is contained within the two sections that are blocked off for the users custom code. This will be true for almost all programs that can be made with WiiLAB.
  2. We need to create the graphics object that will be controlled by the Wiimote. I have chosen a circle.
    • % Create the object that will be controlled by the Wiimote's Acceleration
      circle = drawCircle(150, 150, 5, 'black');
      
    • This code will go in the first section reserved for the user's code in the SkeletonCode. This section will contain most code used to set up the program.
  3. The rest of the code will go inside the while loop.
  4. Now we need to get the acceleration values from the Wiimote.
    • % Get the Wiimote's acceleration, They will be values between -9.8
      % and 9.8
      [x y z] = getWiimoteAccel();
      
  5. Using the acceleration values we will move the circle.
    • % Since we are working in only two dimensions, we are going to
      % ignore the z acceleration and use the x and y accelerations to
      % move the circle
      % We are going to treat the Wiimote's acceration as the circle's
      % velocity to keep the program simple.
      yMove(circle, y);
      xMove(circle, x);
      
  6. We are done. In four lines of code we were able to accomplish our goal.

Below is the completed program and the .m file is 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 an program that will use the Wiimote 
% to move a circle around the screen, you should create the circle here.

% Create the object that will be controlled by the Wiimote's Acceleration
circle = drawCircle(150, 150, 5, 'black');

% -------------------------------------------------------------


% 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.
        
        % First
        % Get the Wiimote's acceleration, They will be values between -9.8
        % and 9.8
        [x y z] = getWiimoteAccel();
        
        
        % Since we are working in only two dimensions, we are going to
        % ignore the z acceleration and use the x and y accelerations to
        % move the circle
        % We are going to treat the Wiimote's acceration as the circle's
        % velocity to keep the program simple.
        yMove(circle, y);
        xMove(circle, x);
        
            
        % -------------------------------------------------------------    
    
    
        % 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

META FILEATTACHMENT attachment="AccelMove.m" attr="" comment="" date="1216656953" name="AccelMove.m" path="AccelMove.m" size="3551" stream="AccelMove.m" tmpFilename="/usr/tmp/CGItemp24882" user="JordanBrindza" version="1"

Revision 1r1 - 21 Jul 2008 - 16:15:55 - JordanBrindza
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Syndicate this site RSSATOM