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

image

Hi,

back a little to the Kinect SDK after 5 days in which I slept very little to migrate a large enough Beta 2 application to the current version of Kinect SDK.

However, the new SDK has changed much and in some things is far more coherent than the previous. For example at the time of adding the reference to our project, in this case already has an official name:

Microsoft.Kinect

image

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:

   1: static void KinectSensorsStatusChanged(object sender, StatusChangedEventArgs e)

   2: {

   3:     Console.WriteLine("{0} - New Status: {1}", DateTime.Now.TimeOfDay.ToString(), e.Status);

   4:     _kinect = e.Sensor;

   5:     if (e.Sensor != null)

   6:     {

   7:         Console.WriteLine("{0} - Sensor Status: {1}", DateTime.Now.TimeOfDay.ToString(), _kinect.Status);

   8:     }

   9:     else

  10:     {

  11:         Console.WriteLine("{0} - No Sensor", DateTime.Now.TimeOfDay.ToString());

  12:     }

  13: }

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.

   1: using System;

   2: using Microsoft.Kinect;

   3:  

   4: namespace KinectStatus01

   5: {

   6:     class Program

   7:     {

   8:         private static KinectSensor _kinect;

   9:         static void Main(string[] args)

  10:         {

  11:             Console.WriteLine("{0} - Start sample application", DateTime.Now.TimeOfDay.ToString());

  12:             KinectSensor.KinectSensors.StatusChanged += KinectSensorsStatusChanged;

  13:             Console.ReadLine();

  14:             if(_kinect != null)

  15:             {

  16:                 _kinect.Stop();

  17:             }

  18:         }

  19:  

  20:         static void KinectSensorsStatusChanged(object sender, StatusChangedEventArgs e)

  21:         {

  22:             Console.WriteLine("{0} - New Status: {1}", DateTime.Now.TimeOfDay.ToString(), e.Status);

  23:             _kinect = e.Sensor;

  24:             if (e.Sensor != null)

  25:             {

  26:                 Console.WriteLine("{0} - Sensor Status: {1}", DateTime.Now.TimeOfDay.ToString(), _kinect.Status);

  27:                 if (_kinect.Status == KinectStatus.Connected)

  28:                 {

  29:                     _kinect.DepthStream.Enable();

  30:                     _kinect.ColorStream.Enable();

  31:                     _kinect.SkeletonStream.Enable();

  32:                     _kinect.Start();

  33:                     Console.WriteLine("{0} - Sensor Started", DateTime.Now.TimeOfDay.ToString());

  34:                 }

  35:             }

  36:             else

  37:             {

  38:                 Console.WriteLine("{0} - No Sensor", DateTime.Now.TimeOfDay.ToString());

  39:             }

  40:         }

  41:     }

  42: }

 

Saludos @ Home

El Bruno

image image image
About these ads

Deja un comentario

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s