Thursday 2 February 2012

Tutorial 2: Moving The Kinect Sensor

Right time to take control of Kinect.  In this tutorial we are going to initialise the Kinect Sensor and then increase and decrease its elevation angle before adding some nice simple debugging tools that will be useful in future tutorials.

Load the project we started last time and the first thing we are going to do is include the Kinect namespace.  At the top of the code add the following line
using Microsoft.Kinect;
So you should have something like
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Microsoft.Kinect;
Now we have the Kinect namespace we can add a KinectSensor to our project and Initialise the sensor.

Adding the Kinect Sensor
Just below our class declaration there are two objects, we are going to add our Kinect Sensor here too:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

KinectSensor kinect;
Our KinectSensor will provide us with access to most of the Kinect API but first we need to initialise the sensor.

Initialise the Kinect Sensor
In the last tutorial we created a new project based on the XNA 4.0 Game template and this added some procedure for us.  We are now going to visit the first of these procedures "Initialize".
In here we need to add two lines, one to get the Kenct Sensor and the other to Start it.
The first line we are going to add returns the first Kinect Sensor from the collection of Kinect Sensor, more on this can be found in the documentation.
kinect = KinectSensor.KinectSensors[0];
The second line starts the Kinect Sensor:
kinect.Start();
The modified Initialize method should look like this:
protected override void Initialize()
{
    //Initialise Kinect
    kinect = KinectSensor.KinectSensors[0];
    kinect.Start();
    base.Initialize();
}
Now press F5 to run the project and you will see nothing different to before but providing everything is written correctly you should not receive any errors.


Moving The Sensor
Now to actually move the sensor!  Kinect has a Maximum and Minimum elevation angle ranging from +27 to -27, luckily these are available in the API.  

Having set the angle too far and had my Kinect making horrible noises (thankfully it did not break) I can assure you that +25 and -25 are plenty enough, and to be honest I doubt for most gaming purposes you will need more than +/-10.
But to demonstrate these angles lets add two simple lines of code below our initialisation of the Kinect Sensor:
kinect.ElevationAngle = 25;
kinect.ElevationAngle = -25;
kinect.ElevationAngle = 0;
This will set the elevation angle to +25, then to -25 then back to 0;  Note: use the = operator NOT +=, if you tell Kinect to go above 27 it will try.  Although in  future tutorials we will be adjusting the sensor angle on the fly based on player clipping but we will get to that.
Congratulations you have successfully controlled the Kinect Sensor.

Debugging
It is inevitable that we will at some point need to find out what went wrong, so before that happens lets set up some debugging tools.
First add another using statement to the top of our file:
using System.Diagnostics;

Right click on the KinectForXna project, select Add > New Item.  In the new window select the Visual C# templates and find "Application Configuration File", leave the name as App.config.


The configuration file we just added will allow us to use the Diagnostics features of C#, mostly the Debug feature.
Now we need to change the contents of this file to:

  
    
      
        
        
      
    
  


Now something I find useful is being able to turn debugging on and off so I have added a Boolean variable to my code and for now set it to true.
Boolean debugging = true;
I have also added a try/catch around the initialisation of our Kinect Sensor:
try
{
    kinect = KinectSensor.KinectSensors[0];
    kinect.Start();
    Debug.WriteLineIf(debugging, kinect.Status);
}
catch (Exception e)
{
    Debug.WriteLine(e.ToString());
}
This will allow us to catch any errors such as forgetting to plug the Kinect Sensor into your machine, or as you can see to simply see the status of Kinect in our debug.
Press F5 to run the project and once it has loaded simply close the window.  Now head to your Project Directory in windows, usually My Documents > Visual Studio > Projects.
Now go to the KinectForXna > KinectForXna >KinectForXna > bin > x86 > Debug folder.

You should see something similar to this:

Open the KinectForXna text file and it should simply say "Connected".  Showing how we are now debugging to a file.

Wrapping Up
In this tutorial we covered adding the Kinect Sensor to our game, moving the Sensor and setting up some basic debugging that will prove useful later.
Next we will get the Color Image Stream from the Kinect Sensor.

8 comments:

  1. I really enjoy the tutorial, waiting for the updates!

    ReplyDelete
    Replies
    1. I am glad you enjoyed it. Aiming to get a few more up later this week.

      Delete
  2. Hi! You're tutorials are one of the easiest to follow I've seen on the web so far. I have a question though. In appconfig file, "initializedata" is said to be "not allowed" according to Visual C# Express's IDE. What does this mean? Also, as expected, I did not find the "KinectForXna.txt" file inside Debug/x86

    ReplyDelete
  3. I had the same exact problems ad anonymous above, so I disabled the debugging procedure.

    ReplyDelete
  4. Great, thank you served me well.
    Although not because Debug was "Try" and not "Catch"

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi, I'm using VC#2010 here and I found out that the correct config part is actually initializeData.

    Here's the code: http://pastebin.com/mwveVcN9

    ReplyDelete