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

Difference between revisions of "CSharp Wrapper"

From OpenKinect
Jump to: navigation, search
(Installation/Configuration)
(Updated for new C# wrapper after rewrite for new API)
Line 1: Line 1:
This effort will provide a wrapper to access the Kinect through C#. The wrapper is pretty much done (see Image below).
 
 
The C# wrapper is currently feature complete. But the win32 version of libfreenect has a lot of issues. In order to keep support issues down and reduce confusion etc. the C# wrapper will not be put into the mainline until the win32 version of libfreenect is working. The version that does work in linux can be found at: https://github.com/lostincake/libfreenect/tree/csharp_wrapper Please understand however, that this is not an official release yet. But you are welcome to play with the code. If you have any questions, you can try contacting Aditya (LostInCake).
 
 
 
== About ==
 
== About ==
 +
This effort will provide a wrapper to access the Kinect through C#. 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 C# patterns.
  
 
== Who is involved ==
 
== Who is involved ==
Line 23: Line 20:
 
|-
 
|-
 
| Control motor
 
| Control motor
 +
| Yes
 +
|
 +
|-
 +
| Get motor status
 
| Yes
 
| Yes
 
|
 
|
Line 30: Line 31:
 
|  
 
|  
 
|-
 
|-
| RGB data callback
+
| Video streams
 
| Yes
 
| Yes
 
|
 
|
 
|-
 
|-
| Depth data callback
+
| Depth streams
 
| Yes
 
| Yes
 
|  
 
|  
Line 41: Line 42:
 
== Media ==
 
== Media ==
  
[[File:Kinect.Net.0001.png|none|thumb|400px|Screenshot of demo application showing some initial success.]]
+
[[File:Csharp_kinect_demo_screenshot.png|none|thumb|400px|Screenshot of demo application.]]
  
  
 
== Installation/Configuration ==
 
== Installation/Configuration ==
Quick getting started:
 
  
Open up the KinectDemo Solution in Visual Studio, then build - it will fail compaining about being unable to open libfreenect. Go to wherever your build files ended up from building libfreenect - mine are in D:\Kinect\build32\bin\debug, and copy all of the files to the csharp wrappers bin folder - mine is D:\Kinect\lostincake-libfreenect-3251748\wrappers\csharp\bin. After that mine ran, although I only got one video frame, and a complaint about a function call. It was nice to see the one frame though.
+
===General Overview===
 +
# Build the base library. Please refer to [http://openkinect.org/wiki/Getting_Started Getting Started] for this.
 +
# Copy libfreenect.so (linux) or libfreenect.dll (windows) to {Libfreenect Directory}/wrappers/csharp/bin
 +
# Build freenectlib and KinectDemo using the VS2008 or VS2010 solutions.
 +
# 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.
 +
 
 +
# Download and install [http://monodevelop.com/ MonoDevelop]. In Ubuntu, this can be found under the Software Center.
 +
# Browse to {Libfreenect Directory}/wrappers/csharp/src/lib/ and then use the solutions in either VS2008 or VS2010 depending on your version of MonoDevelop.  
 +
## Once you build freenectlib, a freenect.dll should appear in {Libfreenect Directory}/wrappers/csharp/bin
 +
# Download [http://www.opentk.com/ OpenTK] binaries. Place these in a directory of your choice.  
 +
# 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.  
 +
## Fix references in the solution for the location to OpenTK binaries.
 +
## Once you build KinectDemo, a KinectDemo.exe should appear in {Libfreenect Directory}/wrappers/csharp/bin
  
 
== Sample Code ==
 
== 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.
 +
 
<source lang="csharp" border="1">
 
<source lang="csharp" border="1">
using LibFreenect;
+
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();
  
// Getting device count
+
// Set LED to Yellow
Console.WriteLine("Number of devices = " + Kinect.DeviceCount);
+
kinect.LED.Color = LEDColor.Yellow;
  
if(Kinect.DeviceCount > 0)
+
// Set tilt to halfway up
{
+
kinect.Motor.Tilt = 0.5;
    // Connecting to a device
+
    Kinect k = new Kinect(0);
+
// Start update thread
    k.Open();
+
Thread t = new Thread(new ThreadStart(delegate()
 +
{
 +
while(true)
 +
{
 +
kinect.UpdateStatus();
 +
}
 +
}));
 +
}
 +
}
  
    // Setting LED color
+
/// <summary>
    k.LED.Color = KinectLED.ColorOption.Yellow;
+
/// 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
 +
}
  
    // Closing connection
+
/// <summary>
    k.Close();
+
/// 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
 +
}
 +
}
 
}
 
}
 
// Shutdown library and close any open devices
 
// Should probably do this at the end of every program.
 
Kinect.Shutdown();
 
 
</source>
 
</source>
  
  
 
[[Category:Wrappers]]
 
[[Category:Wrappers]]

Revision as of 07:57, 27 May 2011

About

This effort will provide a wrapper to access the Kinect through C#. 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 C# patterns.

Who is involved

Coordinator/Development: Aditya Gaddam (User:LostInCake)

Current Status

Feature Supported? Notes
Connect to Kinect (heh) Yes
Control LED Yes 0x04 and 0x05 for the LED status seem to do the same thing - Blink Green. Seems to be something on the native lib side though.
Control motor Yes
Get motor status Yes
Get accelerometer data Yes
Video streams Yes
Depth streams Yes

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 these in a directory of your choice.
  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
		}
	}
}