Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Detecting the Kinect sensor state change/disconnection example

Today's project is a simple example, but one that will help you add that professional and completeness to your Kinect for Windows SDK applications...

[# KINECTSDK] HowTo: Detect the sensor state change and sensor disconnection

If we go ahead we can see in KinectSensor we have a special event to capture the States of sensors: StatusChanged.This event also can be used to detect when you connect or disconnect a sensor to the USB port.

An example with this code in a console application:

...

When we launched the application we can see how the timestamp, shows a small difference between the connected sensor and active sensor (lines 2y3 vs 4.5 lines)

   1: 20:23:32.9637224 - Start sample application
   2: 20:23:40.0351074 - New Status: Initializing
   3: 20:23:40.0351074 - Sensor Status: Initializing
   4: 20:23:44.7783652 - New Status: Connected
   5: 20:23:44.7783652 - Sensor Status: Connected
   6: 20:23:51.2279666 - New Status: Disconnected
   7: 20:23:51.2279666 - Sensor Status: Disconnected
   8: 20:23:59.2548372 - New Status: Initializing
   9: 20:23:59.2558370 - Sensor Status: Initializing
  10: 20:24:03.5861851 - New Status: Connected
  11: 20:24:03.5861851 - Sensor Status: Connected
  12: 20:24:08.8158976 - New Status: Disconnected
  13: 20:24:08.8158976 - Sensor Status: Disconnected

If we also want to take advantage of this event to initialize the sensor, we must bear in mind that we can only do so when the State of the sensor is "connected".

The complete example below shows one way of doing it.

Project Information URL: http://elbruno.com/2012/03/06/kinectsdk-howto-detect-the-sensor-state-change-and-sensor-disconnection/

using System; 
using Microsoft.Kinect;

namespace KinectStatus01 
{ 
    class Program 
    { 
        private static KinectSensor _kinect; 
        static void Main(string[] args) 
         { 
             Console.WriteLine("{0} - Start sample application", DateTime.Now.TimeOfDay.ToString()); 
             KinectSensor.KinectSensors.StatusChanged += KinectSensorsStatusChanged; 
             Console.ReadLine(); 
             if(_kinect != null) 
             { 
                 _kinect.Stop(); 
             } 
         } 
  

         static void KinectSensorsStatusChanged(object sender, StatusChangedEventArgs e) 
         { 
             Console.WriteLine("{0} - New Status: {1}", DateTime.Now.TimeOfDay.ToString(), e.Status); 
             _kinect = e.Sensor; 
             if (e.Sensor != null) 
             { 
                 Console.WriteLine("{0} - Sensor Status: {1}", DateTime.Now.TimeOfDay.ToString(), _kinect.Status); 
                 if (_kinect.Status == KinectStatus.Connected) 
                 { 
                     _kinect.DepthStream.Enable(); 
                     _kinect.ColorStream.Enable(); 
                     _kinect.SkeletonStream.Enable(); 
                     _kinect.Start(); 
                     Console.WriteLine("{0} - Sensor Started", DateTime.Now.TimeOfDay.ToString()); 
                 } 
             } 
             else 
             { 
                 Console.WriteLine("{0} - No Sensor", DateTime.Now.TimeOfDay.ToString()); 
             } 
         } 
     } 
}

 

Contact Information:

Tags:

Follow the Discussion

  • Greg,

    The code seems to have a small error. I tried it, and while it does start, it does not report the status of my Kinect for Windows sensor. I think this *may* have something to do with line 27, where in the list of arguments for console.writeline there is "ect.Status". Did you mean e.Status, or _kinect.Status? Or is it something else entirely?

  • Greg Duncangduncan411 It's amazing what a professional photographer can do...

    @Nerdenator: Good catch, thank you.

    Yep, there was a bug on line 27 (now fixed). I switched it to e.Status and it seems to work for me.

    In looking at the original post though, looks like the author later updated it to use _kinect.Status. Either one should work.

    (Just a side note, I cannot take any credit for this project/code. The original author, http://elbruno.com/, deserves all the kudo's and credit, etc for it. I'm just the messenger...

     Wink

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.