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
(Note about C# waiting for win32 before hitting mainline)
m (Added note about copying over libusb to bin directory on windows.)
 
(10 intermediate revisions by 2 users not shown)
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.
 
 
 
== About ==
 
== 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 ==
 
== Who is involved ==
 
Coordinator/Development: Aditya Gaddam ([[User:LostInCake]])
 
Coordinator/Development: Aditya Gaddam ([[User:LostInCake]])
  
== Current Status ==
+
== Media ==
{| border="1" cellpadding="10"
+
[[File:Csharp_kinect_demo_screenshot.png|none|thumb|400px|Screenshot of demo application.]]
! 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 accelerometer data
 
| Yes
 
|
 
|-
 
| RGB data callback
 
| Yes
 
|
 
|-
 
| Depth data callback
 
| Yes
 
|
 
|}
 
  
== Media ==
+
== Installation/Configuration ==
 +
 
 +
NOTE: As of June 02, 2011, the Windows version is running fairly slow. I am not sure if it's just the drivers on windows that are to blame. But the demo DOES run.
  
[[File:Kinect.Net.0001.png|none|thumb|400px|Screenshot of demo application showing some initial success.]]
+
===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
 +
# If you are on windows, make sure libusb0.dll is in your PATH somewhere. Alternatively, you can copy over libusb0.dll from your libusb directory to {Libfreenect Directory}/wrappers/csharp/bin
 +
# Build C# wrapper (freenect.dll) and demo application (KinectDemo) using the VS2008 or VS2010 solutions. Detailed directions are below.
 +
# Run. Enjoy
  
 +
===Building C# wrapper and demo application===
  
== Installation/Configuration ==
+
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.
Coming sooooon!
 
  
 +
# 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 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.
 +
# 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.
 +
## If you put the OpenTK dlls in the right place as instructed in the above steps, the solutions should build as is. Otherwise, you will have to fix references to OpenTK in the projects manually.
 +
## 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();
  
...
+
// Set LED to Yellow
 +
kinect.LED.Color = LEDColor.Yellow;
  
// Getting device count
+
// Set tilt to halfway up
Console.WriteLine("Number of devices = " + Kinect.DeviceCount);
+
kinect.Motor.Tilt = 0.5;
 +
 +
// Start update thread
 +
Thread t = new Thread(new ThreadStart(delegate()
 +
{
 +
while(true)
 +
{
 +
// Update status of accelerometer/motor etc.
 +
kinect.UpdateStatus();
  
if(Kinect.DeviceCount > 0)
+
// Process any pending events.
{
+
Kinect.ProcessEvents();
    // Connecting to a device
+
}
    Kinect k = new Kinect(0);
+
}));
    k.Open();
+
}
 +
}
  
    // 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]]

Latest revision as of 04:00, 3 June 2011

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

NOTE: As of June 02, 2011, the Windows version is running fairly slow. I am not sure if it's just the drivers on windows that are to blame. But the demo DOES run.

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. If you are on windows, make sure libusb0.dll is in your PATH somewhere. Alternatively, you can copy over libusb0.dll from your libusb directory to {Libfreenect Directory}/wrappers/csharp/bin
  4. Build C# wrapper (freenect.dll) and demo application (KinectDemo) using the VS2008 or VS2010 solutions. Detailed directions are below.
  5. 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. If you put the OpenTK dlls in the right place as instructed in the above steps, the solutions should build as is. Otherwise, you will have to fix references to OpenTK in the projects manually.
    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)
					{
						// Update status of accelerometer/motor etc.
						kinect.UpdateStatus();

						// Process any pending events.
						Kinect.ProcessEvents();
					}
				}));
			}
		}

		/// <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
		}
	}
}