How to Tilt the Kinect Sensor

Vuyiswamb
Posted by in Kinect category on for Beginner level | Points: 250 | Views : 24247 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article i demonstrate a simple way to tilt the Kinect Sensor.


 Download source code for How to Tilt the Kinect Sensor

Introduction

This is one of the most exciting projects I have ever done. In this article I will demonstrate to you, how you can move the Kinect sensor to look where you want it to look.

Objective

The Object is to move the Kinect Sensor to move in a click of a Button, Later you might want to point a sensor to look in a different directions based on some hand gestures. But for the purpose of simplicity, we will click the button and look at the sensor while it moves.

 

Start

 

In the previous article explained how you can setup your environment and I also left a link that has downloads to Kinect templates that will appear in your visual studio if you want to create a new project.

In this article I will not attempt to explain the setting up of environment. If you are reading this article it is assumed that you have already installed the latest Kinect sdk and also the development toolkit.

First Start Visual Studio 2010 or 2012, Kinect will not work with Visual studio 2008. Kinect applications are created using WPF, fire-up your visual studio as depicted in Step 1.


Step 1




Figure 1.1

 

If you have already installed the templates , you will notice that the Kinect templates appears in the list of available Visual studio templates, you can choose any or you can just select a normal wpf project.

 

The difference between choosing the wpf project and the template is that the template already add the namespaces that are commonly used when building Kinect application as depicted in figure 1.2


Figure 1.2

 

If you have created your project using the normal wpf, you need to add a reference to the “Microsoft.Kinect” that gets installed in the following path

 

C:\Program Files\Microsoft SDKs\Kinect\v1.6\Assemblies\Microsoft.Kinect.dll

If you have been using a template, you will notice that there will be a lot of generated code for you, but because of the simplicity we want to create on this example, we will remove all the code that will not affect the success of our demonstration.


Step 2

 

Clean your code, and make sure your Xaml looks like this

  

<Window x:Class="Tilt_Example.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="600" Width="800">

    <Grid>

        <Button Content="Set Tilt" HorizontalAlignment="Left" x:Name="btnsetTilt" Click="btnsetTilt_Click_1" Margin="453,233,0,0" VerticalAlignment="Top" Width="214" Height="61"/>

        <TextBox HorizontalAlignment="Left" Height="56" Margin="196,233,0,0" TextWrapping="Wrap" Text="" x:Name="txttilt" VerticalAlignment="Top" Width="235"/> 

    </Grid>

</Window>

 

Which will result into this

Figure 1.3

Figure 1.3 has a textbox and a button to set the tilt. To tilt we use ElevationAngle property of the Camera object to a value of between -27 to +27. The Tilt is not designed for frequent use, I thought I should just show you how easy it will be for you to tilt your Kinect Sensor. You should never change the tilt programmatically too often because it will results to a run-time error.

In our example if you enter the value, it will tilt according to the value you have entered in the textbox. Below is the code behind to do the tilt example

 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.Linq;
 
namespace Tilt_Example
{
    public partial class MainWindow : Window
    {
        //Instantiate the Kinect runtime. Required to initialize the device.
  //IMPORTANT NOTE: You can pass the device ID here, in case more than one Kinect      device is connected.
        KinectSensor sensor = KinectSensor.KinectSensors[0];
      
        public MainWindow()
        {
            InitializeComponent();
 
//Runtime initialization is handled when the window is opened. When the window
            //is closed, the runtime MUST be unitialized.
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            //We must stop the Sensor when we exit our project
            this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded);
 
        }
        void MainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
//Stop the Sensor, when exit the application
            sensor.Stop();
        }
 
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
//Start the Sensor, when exit the application
            sensor.Start();
        }
 
        private void btnsetTilt_Click_1(object sender, RoutedEventArgs e)
        {
            if (txttilt.Text != string.Empty)
            {
//change the Elevation based on what has been entered in the textbox
                sensor.ElevationAngle =Convert.ToInt32(txttilt.Text);
            }
        }    }}

Conclusion

Microsoft gave us flexibility to be in control of the sensor while writing our application. In this article I have successfully explained how to tilt your Kinect sensor.

Thank you for visiting Dotnetfunda

Page copy protected against web site content infringement by Copyscape

About the Author

Vuyiswamb
Full Name: Vuyiswa Maseko
Member Level: NotApplicable
Member Status: Member,MVP,Administrator
Member Since: 7/6/2008 11:50:44 PM
Country: South Africa
Thank you for posting at Dotnetfunda [Administrator]
http://www.Dotnetfunda.com
Vuyiswa Junius Maseko is a Founder of Vimalsoft (Pty) Ltd (http://www.vimalsoft.com/) and a forum moderator at www.DotnetFunda. Vuyiswa has been developing for 16 years now. his major strength are C# 1.1,2.0,3.0,3.5,4.0,4.5 and vb.net and sql and his interest were in asp.net, c#, Silverlight,wpf,wcf, wwf and now his interests are in Kinect for Windows,Unity 3D. He has been using .net since the beta version of it. Vuyiswa believes that Kinect and Hololen is the next generation of computing.Thanks to people like Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject), Sheo Narayan (.Netfunda),Rajesh Kumar(Microsoft) They have made vuyiswa what he is today.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)