Notice: MediaWiki has been updated. Report any rough edges to marcan@marcan.st
CSharp Wrapper: Difference between revisions
LostInCake (talk | contribs) m (→Sample Code) |
LostInCake (talk | contribs) m (Added note about copying over libusb to bin directory on windows.) |
||
Line 10: | Line 10: | ||
== Installation/Configuration == | == Installation/Configuration == | ||
NOTE: Windows | 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=== | ===General Overview=== | ||
# Build the base library. Please refer to [http://openkinect.org/wiki/Getting_Started Getting Started] for this. | # 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 | # 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. | # Build C# wrapper (freenect.dll) and demo application (KinectDemo) using the VS2008 or VS2010 solutions. Detailed directions are below. | ||
# Run. Enjoy | # Run. Enjoy |
Latest revision as of 03: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
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
- Build the base library. Please refer to 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
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 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 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
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
}
}
}