Notice: MediaWiki has been updated. Report any rough edges to marcan@marcan.st

CSharp Wrapper

From OpenKinect
Revision as of 13:16, 2 June 2011 by LostInCake (talk | contribs)
Jump to: navigation, search

About

This effort will provide a wrapper to access the Kinect through C# or most other .NET languages such as VB.NET. Instead of being a light wrapper, this aims to bring the capabilities of the low level wrapper and let it be used in a way that conforms to .NET patterns. In the following page, it is assumed that you are using C#, but the library should work just as well with any other .NET language that has similar language constructs such as events, threading etc. VB.NET is a good example of an alternative .NET language.

Who is involved

Coordinator/Development: Aditya Gaddam (User:LostInCake)

Media

Screenshot of demo application.

Installation/Configuration

General Overview

  1. Build the base library. Please refer to Getting Started for this.
  2. Copy libfreenect.so (linux) or libfreenect.dll (windows) to {Libfreenect Directory}/wrappers/csharp/bin
  3. Build freenectlib and KinectDemo using the VS2008 or VS2010 solutions.
  4. Run. Enjoy

Building C# wrapper and demo application

The following instructions use MonoDevelop. If you have access to Visual Studio or Visual Studio Express, you can safely follow the same instructions and perform analogous steps inside the Visual Studio IDE.

  1. Download and install MonoDevelop. In Ubuntu, this can be found under the Software Center.
  2. Browse to {Libfreenect Directory}/wrappers/csharp/src/lib/ and then use the solutions in either VS2008 or VS2010 depending on your version of MonoDevelop.
    1. Once you build freenectlib, a freenect.dll should appear in {Libfreenect Directory}/wrappers/csharp/bin
  3. Download OpenTK binaries. Place OpenTK.dll and OpenTK.GLControl.dll in {Libfreenect Directory}/wrappers/csharp/support. On Linux, you can use fetch_opentk.sh in {Libfreenect Directory}/wrappers/csharp/support/ to do this for you.
  4. Browse to {Libfreenect Directory}/wrappers/csharp/src/test/KinectDemo and then use the solutions in either VS2008 or VS2010 depending on your version of MonoDevelop.
    1. Fix references in the solution for the location to OpenTK binaries.
    2. Once you build KinectDemo, a KinectDemo.exe should appear in {Libfreenect Directory}/wrappers/csharp/bin

Sample Code

The following sample code provides a basic look at how the C# wrapper is used. Full a full blown example, please look at the KinectDemo application in the repository.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using freenect;
using System.Drawing;
using System.Diagnostics;

namespace KinectNetSample
{
	public class Sample : Form
	{
		
		/// <summary>
		/// Constructor
		/// </summary>
		public Sample()
		{
			// Initialize UI stuff
			this.InitializeComponents();
			
			// Find device count
			Console.WriteLine("There are {0} Kinect Devices Connected", Kinect.DeviceCount);
			
			if(Kinect.DeviceCount > 0)
			{
				// Connect to first device
				Kinect kinect = new Kinect(0);
				kinect.Open();
				
				// Setup event handlers
				kinect.VideoCamera.DataReceived += HandleKinectVideoCameraDataReceived;
				kinect.DepthCamera.DataReceived += HandleKinectDepthCameraDataReceived;
				
				// Start cameras
				kinect.VideoCamera.Start();
				kinect.DepthCamera.Start();

				// Set LED to Yellow
				kinect.LED.Color = LEDColor.Yellow;

				// Set tilt to halfway up
				kinect.Motor.Tilt = 0.5;
				
				// Start update thread
				Thread t = new Thread(new ThreadStart(delegate()
				{
					while(true)
					{
						kinect.UpdateStatus();
					}
				}));
			}
		}

		/// <summary>
		/// Handle depth data
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="BaseCamera.DataReceivedEventArgs"/>
		/// </param>
		private void HandleKinectDepthCameraDataReceived (object sender, BaseCamera.DataReceivedEventArgs e)
		{
			Console.WriteLine("Depth data received at {0}", e.Timestamp);
			
			// Actual data is in e.Data
		}

		/// <summary>
		/// Handle video data
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="BaseCamera.DataReceivedEventArgs"/>
		/// </param>
		private void HandleKinectVideoCameraDataReceived (object sender, BaseCamera.DataReceivedEventArgs e)
		{
			Console.WriteLine("Video data received at {0}", e.Timestamp);
			
			// Actual data is in e.Data
		}
	}
}