private void Cerrar()
 {
     arduinoController.EnviarAngulosFromAngulosServos(new AngulosServos(ArduinoController.BRAZO_GB));
     //videoController.FinGrabacion = DateTime.Now.Ticks;
     arduinoController.CerrarPuerto();
     if (null != this.sensor)
     {
         this.sensor.Stop();
         this.sensor.Dispose();
     }
 }
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            //Asigno lo delegates
            consumoHombroArribaAbajoDelegate    = new ConsumoHombroArribaAbajoDelegate(SetConsumoHombroArribaAbajo);
            consumoHombroAdelanteAtrasDelegate  = new ConsumoHombroAdelanteAtras(SetConsumoHombroAdelanteAtras);
            consumoCodoArribaAbajoDelegate      = new ConsumoCodoArribaAbajo(SetConsumoCodoArribaAbajo);
            consumoCodoDerechaIzquierdaDelegate = new ConsumoCodoDerechaIzquierda(SetConsumoCodoDerechaIzquierda);

            // Create the drawing group we'll use for drawing
            this.drawingGroup = new DrawingGroup();

            // Create an image source that we can use in our image control
            this.imageSource = new DrawingImage(this.drawingGroup);

            // Display the drawing using our image control
            Image.Source = this.imageSource;

            foreach (var potentialSensor in KinectSensor.KinectSensors)
            {
                if (potentialSensor.Status == KinectStatus.Connected)
                {
                    this.sensor = potentialSensor;
                    break;
                }
            }

            if (null != this.sensor)
            {
                // Turn on the skeleton stream to receive skeleton frames
                this.sensor.SkeletonStream.Enable();
                this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                this.colorPixels      = new byte[this.sensor.ColorStream.FramePixelDataLength];
                this.colorBitmap      = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
                this.colorCoordinates = new ColorImagePoint[this.sensor.DepthStream.FramePixelDataLength];
                this.depthWidth       = this.sensor.DepthStream.FrameWidth;
                this.depthHeight      = this.sensor.DepthStream.FrameHeight;
                int colorWidth  = this.sensor.ColorStream.FrameWidth;
                int colorHeight = this.sensor.ColorStream.FrameHeight;
                this.colorToDepthDivisor    = colorWidth / this.depthWidth;
                this.sensor.AllFramesReady += this.SensorAllFramesReady;
                this.colorToDepthDivisor    = colorWidth / this.depthWidth;
                depthPixels = new DepthImagePixel[sensor.DepthStream.FramePixelDataLength];

                // Start the sensor!
                try
                {
                    this.sensor.Start();
                }
                catch (IOException)
                {
                    this.sensor = null;
                }
            }

            if (null == this.sensor)
            {
                //Image.Source = new BitmapImage(
                //    new Uri("pack://application:,,,/AtaxiaVision;component/Imagenes/KinectAV.png"));
                //EstadoSnackBar("Kinect no lista.");
                if (null != this.sensor)
                {
                    this.sensor.Stop();
                    this.sensor.Dispose();
                }
                arduinoController.CerrarPuerto();
                var win = new Inicio("Kinect no detectada, para el ejercicio de reach se requiere una. Conéctela e intente nuevamente");
                win.Show();
                Close();
            }
            else
            {
                if (!arduinoController.Inicializar(ArduinoController.BRAZO_GB))
                {
                    if (null != this.sensor)
                    {
                        this.sensor.Stop();
                        this.sensor.Dispose();
                    }
                    arduinoController.CerrarPuerto();
                    var win = new Inicio("Exoesqueleto no detectado. No se puede ejecutar el ejecicio sin el mismo.");
                    win.Show();
                    Close();
                }
            }

            TensionesServosBG();
            //ReviarFaltaDeKinectOExoesqueleto();
            existenciaKinectExoesqueletoDelegate = new ExistenciaKinectExoesqueletoDelegate(ReviarFaltaDeKinectOExoesqueleto);
            ExistenciaKinectExoesqueletoBackgroundWorker.DoWork += ReviarFaltaDeKinectOExoesqueletoBG;
            ExistenciaKinectExoesqueletoBackgroundWorker.RunWorkerAsync();
        }