Example #1
0
        void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            try
            {
                using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
                {
                    if (colorFrame != null)
                    {
                        byte[] colorPixel = new byte[colorFrame.PixelDataLength];
                        colorFrame.CopyPixelDataTo(colorPixel);
                        imageRgb.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                                                              PixelFormats.Bgr32, null, colorPixel, colorFrame.Width * colorFrame.BytesPerPixel);
                    }
                    FramesPerSecondElement.Text = string.Format("{0:0} fps", (this._TotalFrames++ / DateTime.Now.Subtract(this._StartFrameTime).TotalSeconds));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            Vector4 accelerometer = kinect.AccelerometerGetCurrentReading();

            textAccelerometerX.Text = accelerometer.X.ToString();
            textAccelerometerY.Text = accelerometer.Y.ToString();
            textAccelerometerZ.Text = accelerometer.Z.ToString();

            textAngle.Text = GetAccelerometerAngle().ToString();

            textTiltAngle.Text = kinect.ElevationAngle.ToString();
        }
Example #2
0
        /// <summary>
        /// Returns the current acceleration of the physical device.</summary>
        /// <returns>
        /// True if the physical device is moving, false otherwise.</returns>
        private double GetAccelerationDiff()
        {
            double diff = 0; // Difference between last accelerometer readings and actual readings

            diff += (KinectDevice.AccelerometerGetCurrentReading().W - _lastAcceleration.W);
            diff += (KinectDevice.AccelerometerGetCurrentReading().X - _lastAcceleration.X);
            diff += (KinectDevice.AccelerometerGetCurrentReading().Y - _lastAcceleration.Y);
            diff += (KinectDevice.AccelerometerGetCurrentReading().Z - _lastAcceleration.Z);
            _lastAcceleration = KinectDevice.AccelerometerGetCurrentReading();
            return(diff);
        }
Example #3
0
        /// <summary>
        /// 表示データを更新する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            Vector4 accelerometer = kinect.AccelerometerGetCurrentReading();

            textAccelerometerX.Text = accelerometer.X.ToString();
            textAccelerometerY.Text = accelerometer.Y.ToString();
            textAccelerometerZ.Text = accelerometer.Z.ToString();

            textAngle.Text = GetAccelerometerAngle().ToString();

            textTiltAngle.Text = kinect.ElevationAngle.ToString();
        }
Example #4
0
        //Updates the acceleration on the GUI and the server, 30 FPS may be a little fast for the GUI, but for VRPN, it probably needs to be at least that fast
        private void accelerationUpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            bool    dataValid      = false;
            Vector4 acceleration   = new Vector4();
            int     elevationAngle = 0;

            lock (kinect)
            {
                if (kinect.IsRunning)
                {
                    acceleration   = kinect.AccelerometerGetCurrentReading();
                    elevationAngle = kinect.ElevationAngle;
                    dataValid      = true;
                }
            }

            //Update the GUI
            if (dataValid)
            {
                if (isGUI && parent.kinectOptionGUIPages[kinectID].IsVisible)
                {
                    //Note: This method is on a different thread from the rest of the KinectCore because of the timer, thus the need for the invoke
                    parent.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        parent.kinectOptionGUIPages[kinectID].AccelXTextBlock.Text = acceleration.X.ToString("F2");
                        parent.kinectOptionGUIPages[kinectID].AccelYTextBlock.Text = acceleration.Y.ToString("F2");
                        parent.kinectOptionGUIPages[kinectID].AccelZTextBlock.Text = acceleration.Z.ToString("F2");
                        parent.kinectOptionGUIPages[kinectID].AngleTextBlock.Text = elevationAngle.ToString();
                    }), null
                                                  );
                }

                //Update the VRPN server
                if (server.isRunning && server.serverMasterOptions.kinectOptions[kinectID].sendAcceleration)
                {
                    for (int i = 0; i < server.analogServers.Count; i++)
                    {
                        if (server.serverMasterOptions.analogServers[i].serverName == server.serverMasterOptions.kinectOptions[kinectID].accelerationServerName)
                        {
                            lock (server.analogServers[i])
                            {
                                server.analogServers[i].AnalogChannels[server.serverMasterOptions.kinectOptions[kinectID].accelXChannel].Value = acceleration.X;
                                server.analogServers[i].AnalogChannels[server.serverMasterOptions.kinectOptions[kinectID].accelYChannel].Value = acceleration.Y;
                                server.analogServers[i].AnalogChannels[server.serverMasterOptions.kinectOptions[kinectID].accelZChannel].Value = acceleration.Z;
                                server.analogServers[i].Report();
                            }
                            break;
                        }
                    }
                }
            }
        }
Example #5
0
        void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (var colorFrame = e.OpenColorImageFrame()) {
                if (colorFrame != null)
                {
                    var pixel = new byte[colorFrame.PixelDataLength];
                    colorFrame.CopyPixelDataTo(pixel);

                    ImageRgb.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                                                          PixelFormats.Bgr32, null, pixel, colorFrame.Width * 4);
                }
            }

            using (var depthFrame = e.OpenDepthImageFrame()) {
                if (depthFrame != null)
                {
                    // Depth情報を入れる
                    // GetRawPixelData()はインタラクションライブラリ内で実装された拡張メソッド
                    stream.ProcessDepth(depthFrame.GetRawPixelData(), depthFrame.Timestamp);
                }
            }

            using (var skeletonFrame = e.OpenSkeletonFrame()) {
                if (skeletonFrame != null)
                {
                    var skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);

                    // スケルトン情報を入れる
                    stream.ProcessSkeleton(skeletons, kinect.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
                }
            }
        }
Example #6
0
 private void OnSkeletonFrameReady(object o, SkeletonFrameReadyEventArgs sf)
 {
     // Console.WriteLine("Skeleton");
     {
         using (SkeletonFrame skeletonFrame = sf.OpenSkeletonFrame())
         {
             if (skeletonFrame == null)
             {
                 return;
             }
             try
             {
                 skeletonFrame.CopySkeletonDataTo(skels);
                 var accelerometerReading = ks.AccelerometerGetCurrentReading();
                 inter_stream.ProcessSkeleton(skels, accelerometerReading, skeletonFrame.Timestamp);
             }
             catch (InvalidOperationException)
             {
                 Console.WriteLine("Skeleton Problems");
                 // SkeletonFrame functions may throw w
                 // into a bad state.  Ignore the frame in that case.
             }
         }
     }
 }
        /// <summary>
        /// Event handler for Kinect sensor's SkeletonFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

            // Acquire data from the SkeletonFrameReadyEventArgs...
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];

                try
                {
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    var accelerometerReading = sensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // SkeletonFrame functions may throw when the sensor gets
                    // into a bad state.  Ignore the frame in that case.
                }
            }
        }
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            if (_sensor != sender)
            {
                return;
            }

            using (var skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    try
                    {
                        skeletonFrame.CopySkeletonDataTo(_skeletons);
                        var accelerometerReading = _sensor.AccelerometerGetCurrentReading();

                        _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                    }
                }
            }
        }
Example #9
0
        static void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                }

                var users = _skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).ToList();

                if (users.Count > 0)
                {
                    string json = users.Serialize(_coordinateMapper, _mode);

                    foreach (var socket in _clients)
                    {
                        socket.Send(json);
                    }
                }
            }
        }
        private void AtualizarValoresAcelerometro()
        {
            Vector4 resultado = kinect.AccelerometerGetCurrentReading();

            labelX.Content = Math.Round(resultado.X, 3);
            labelY.Content = Math.Round(resultado.Y, 3);
            labelZ.Content = Math.Round(resultado.Z, 3);
        }
Example #11
0
        /// <summary>
        /// Evènement : A chaque nouvelle image de squelette
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            // Récupération des squelettes trouvés et envoie au interaction stream
            Skeleton[] skeletons = null;
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    interactionStream.ProcessSkeleton(skeletons, Sensor.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException) { }
            }

            // Vider les anciens os dessinés, donc rajout de l'image sur la canvas
            canvas.Children.Clear();
            canvas.Children.Add(image);
            // Si il n'y a pas encore le canvas de position, on l'ajoute
            if (!canvasDessin.Children.Contains(canvasPosition))
            {
                canvasDessin.Children.Add(canvasPosition);
            }

            if (skeletons.Length == 0) // Si pas de squelette
            {
                return;
            }

            var skels = skeletons.OrderBy(sk => sk.Position.Z);
            //var skels = skeletons.Where(sk => sk.TrackingState == SkeletonTrackingState.Tracked).OrderBy(sk => sk.Position.Z);
            bool skelDrawn = false;

            // Pour chaque squelette
            foreach (Skeleton skel in skels)
            {
                if (skel.TrackingState == SkeletonTrackingState.Tracked && !skelDrawn) // Si on trouve le squelette et on en a dessiné un
                {
                    // On dessine la main actuelle
                    if (actualHandType == InteractionHandType.Left)
                    {
                        DrawHand(skel, JointType.HandLeft);
                    }
                    else if (actualHandType == InteractionHandType.Right)
                    {
                        DrawHand(skel, JointType.HandRight);
                    }
                    skelDrawn = true;
                }
            }
        }
Example #12
0
 private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
 {
     using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
     {
         if (skeletonFrame != null)
         {
             skeletonList = new Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
             skeletonFrame.CopySkeletonDataTo(skeletonList);
             stream.ProcessSkeleton(skeletonList, sensor.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
         }
     }
 }
        private void KinectSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (var colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }

                var pixels = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(pixels);

                var stride = colorFrame.Width * 4;
                ImageSource = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
            }

            using (var depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame == null)
                {
                    return;
                }

                try
                {
                    interactionStream.ProcessDepth(depthFrame.GetRawPixelData(), depthFrame.Timestamp);
                }
                catch (InvalidOperationException ex)
                {
                    Console.Error.WriteLine(ex);
                }
            }

            using (var skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    var accelerometerReading = kinectSensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException ex)
                {
                    Console.Error.WriteLine(ex);
                }
            }
        }
Example #14
0
        /// <summary>
        /// This method will update the accelerometer display.
        /// </summary>
        private void AccelerometerAllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            // Have we already been "shut down"
            if (KinectSensor == null)
            {
                return;
            }

            if (EnableAccelerometerReading)
            {
                this.Accelerometer = KinectSensor.AccelerometerGetCurrentReading();
            }
        }
        void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skf = e.OpenSkeletonFrame())
            {
                if (skf == null)
                {
                    return;
                }

                Skeleton[] skeletons = new Skeleton[skf.SkeletonArrayLength];
                skf.CopySkeletonDataTo(skeletons);
                var accelerometerReading = kinect.AccelerometerGetCurrentReading();
                interStream.ProcessSkeleton(skeletons, accelerometerReading, skf.Timestamp);
            }
        }
Example #16
0
        public MainWindow()
        {
            {
                InitializeComponent();
            }

            this.Loaded   += new RoutedEventHandler(MainWindow_Loaded);
            this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded);

            sensor.AccelerometerGetCurrentReading();



            sensor.ColorStream.Enable();
            // initialiseren van de camera
        }
Example #17
0
        void kinectAccTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            accdata = kinect.AccelerometerGetCurrentReading();
            float  x   = accdata.X;
            float  y   = accdata.Y;
            double r   = Math.Sqrt(x * x + y * y);
            double rad = Math.Asin(x / r);
            double deg = RadianToDegree(rad);

            //Console.WriteLine("x=" + x + " , x=" + y + " , r=" + r + " , deg=" + deg);
            Dispatcher.BeginInvoke(
                (Action) delegate()
            {
                kinectangle.Angle = deg;
            }
                );
        }
Example #18
0
        private void FuncoesEsqueletoUsuario(SkeletonFrame quadro)
        {
            if (quadro == null)
            {
                return;
            }

            using (quadro)
            {
                Skeleton esqueletoUsuario = quadro.ObterEsqueletoUsuario();

                if (btnDesenhar.IsChecked)
                {
                    Skeleton[] esqueletos = new Skeleton[6];
                    quadro.CopySkeletonDataTo(esqueletos);
                    fluxoInteracao.ProcessSkeleton(esqueletos, kinect.AccelerometerGetCurrentReading(), quadro.Timestamp);
                    EsqueletoUsuarioAuxiliar esqueletoAuxiliar = new EsqueletoUsuarioAuxiliar(kinect);

                    if (configuracaoMaoDireita.DesenhoAtivo)
                    {
                        esqueletoAuxiliar.InteracaoDesenhar(esqueletoUsuario.Joints[JointType.HandRight], canvasDesenho, configuracaoMaoDireita);
                    }

                    if (configuracaoMaoEsquerda.DesenhoAtivo)
                    {
                        esqueletoAuxiliar.InteracaoDesenhar(esqueletoUsuario.Joints[JointType.HandLeft], canvasDesenho, configuracaoMaoEsquerda);
                    }
                }
                else
                {
                    foreach (IRastreador rastreador in rastreadores)
                    {
                        rastreador.Rastrear(esqueletoUsuario);
                    }

                    if (btnEsqueletoUsuario.IsChecked)
                    {
                        quadro.DesenharEsqueletoUsuario(kinect, canvasKinect);
                    }
                }
            }
        }
        /// <summary>
        /// Instantiates a new InteractionStream, feeds this InteractionStream with Skeleton- and DepthData and subscribes to the InteractionFrameReady event.
        /// </summary>
        /// <param name="kinectSensor">The Kinect sensor passed to the interaction stream instance.</param>
        /// <param name="interactionClient">The interaction client passed to the interaction stream instance.</param>
        /// <returns>An UserInfo stream that contains an action that disposes the interaction stream when the observable is disposed.</returns>
        public static IObservable <UserInfo[]> GetUserInfoObservable(this KinectSensor kinectSensor, IInteractionClient interactionClient)
        {
            if (kinectSensor == null)
            {
                throw new ArgumentNullException("kinect");
            }
            if (interactionClient == null)
            {
                throw new ArgumentNullException("interactionClient");
            }

            if (!kinectSensor.DepthStream.IsEnabled)
            {
                throw new InvalidOperationException("The depth stream is not enabled, but mandatory.");
            }
            if (!kinectSensor.SkeletonStream.IsEnabled)
            {
                throw new InvalidOperationException("The skeleton stream is not enabled, but mandatory.");
            }

            return(Observable.Create <UserInfo[]>(observer =>
            {
                var stream = new InteractionStream(kinectSensor, interactionClient);
                var obs = kinectSensor.GetAllFramesReadyObservable()
                          .SelectStreams((_, __) => Tuple.Create(_.Timestamp, __.Timestamp))
                          .Subscribe(_ =>
                {
                    stream.ProcessSkeleton(_.Item3, kinectSensor.AccelerometerGetCurrentReading(), _.Item4.Item1);
                    stream.ProcessDepth(_.Item2, _.Item4.Item2);
                });

                stream.GetInteractionFrameReadyObservable()
                .SelectUserInfo()
                .Subscribe(_ => observer.OnNext(_));

                return new Action(() =>
                {
                    obs.Dispose();
                    stream.Dispose();
                });
            }));
        }
Example #20
0
        private void KinectSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            var skeletons = new Skeleton[0];

            using (var skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletons);
                }
            }
            if (skeletons.Length == 0)
            {
                return;
            }
            var skel = skeletons.FirstOrDefault(x => x.TrackingState == SkeletonTrackingState.Tracked);

            if (skel == null)
            {
                return;
            }

            var rightHand = skel.Joints[JointType.WristRight]; lbl_rightX.Text = rightHand.Position.X.ToString(CultureInfo.InvariantCulture); lbl_rightY.Text = rightHand.Position.Y.ToString(CultureInfo.InvariantCulture); lbl_rightZ.Text = rightHand.Position.Z.ToString(CultureInfo.InvariantCulture);
            var leftHand  = skel.Joints[JointType.WristLeft]; lbl_leftX.Text = leftHand.Position.X.ToString(CultureInfo.InvariantCulture); lbl_leftY.Text = leftHand.Position.Y.ToString(CultureInfo.InvariantCulture); lbl_leftZ.Text = leftHand.Position.Z.ToString(CultureInfo.InvariantCulture);
            var centreHip = skel.Joints[JointType.HipCenter];

            skely = skel;
            if (centreHip.Position.Z - rightHand.Position.Z > 0.35)
            {
                lbl_rightRaised.Text = "Raised";
            }
            else if (centreHip.Position.Z - leftHand.Position.Z > 0.35)
            {
                lbl_leftRaised.Text = "Raised";
            }
            else
            {
                lbl_leftRaised.Text = "Lowered"; lbl_rightRaised.Text = "Lowered";
            }
            interactionStream.ProcessSkeleton(skeletons, sensor.AccelerometerGetCurrentReading(), System.DateTime.Now.Ticks);
        }
Example #21
0
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // If exception skip frame
                }
            }
        }
Example #22
0
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // SkeletonFrame functions may throw when the sensor gets
                    // into a bad state.  Ignore the frame in that case.
                }
            }
        }
        private void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
            //double handposition = 0;
            //onRaisedVoiceCommand(new VoiceCommandEventArgs("I'm in state" + state +" and I got skeleton data"));
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    inStream.ProcessSkeleton(skeletons, sensor.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
                }

                /*
                 * foreach (Skeleton skeleton in skeletons)
                 * {
                 *  if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
                 *  {
                 *      Joint wristjoint = skeleton.Joints[JointType.WristLeft];
                 *      if (wristjoint.TrackingState == JointTrackingState.Tracked)
                 *      {
                 *          if (wristjoint.Position.Y > handposition)
                 *          {
                 *              handposition = wristjoint.Position.Y;
                 *          }
                 *      }
                 *      wristjoint = skeleton.Joints[JointType.WristRight];
                 *      if (wristjoint.TrackingState == JointTrackingState.Tracked)
                 *      {
                 *          if (wristjoint.Position.Y > handposition)
                 *          {
                 *              handposition = wristjoint.Position.Y;
                 *          }
                 *      }
                 *  }
                 * }
                 */
            }
        }
        //Listener del esqueleto
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                //Si el eskeleto es null se sale del metodo
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    //se le coloca el esqueleto al areglo
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // Error al obtener el esqueleto del sensor
                }
            }
        }
Example #25
0
        //Updates the acceleration on the GUI and the server, 30 FPS may be a little fast for the GUI, but for VRPN, it probably needs to be at least that fast
        private void updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //Update the acceleration data
            bool    dataValid      = false;
            Vector4?acceleration   = null;
            int?    elevationAngle = null;

            lock (kinect)
            {
                if (kinect != null && kinect.IsRunning)
                {
                    //I wish these try/catch statements weren't necessary, but these two calls seemed to fail often
                    dataValid = true;
                    try
                    {
                        acceleration = kinect.AccelerometerGetCurrentReading();
                    }
                    catch
                    {
                        acceleration = null;
                        dataValid    = false;
                    }

                    if (dataValid)  //We can't even try to calculate the elevation angle if the accelerometer doesn't read right
                    {
                        try
                        {
                            elevationAngle = kinect.ElevationAngle;
                        }
                        catch
                        {
                            elevationAngle = null;
                            dataValid      = false;
                        }
                    }
                }
            }

            //Update the GUI
            if (dataValid)
            {
                //Update the filtered acceleration
                EigenWrapper.Matrix accelMat = new EigenWrapper.Matrix(3, 1);
                accelMat[0, 0] = acceleration.Value.X;
                accelMat[1, 0] = acceleration.Value.Y;
                accelMat[2, 0] = acceleration.Value.Z;
                accelerationFilter.IntegrateMeasurement(accelMat, DateTime.UtcNow, 0.01);
                accelFilterStarted = true;
                //lastAcceleration = acceleration.Value;
                //Transmits the acceleration data using an event
                KinectBase.AccelerationEventArgs accelE = new KinectBase.AccelerationEventArgs();
                accelE.kinectID       = this.kinectID;
                accelE.acceleration   = new Vector3D(acceleration.Value.X, acceleration.Value.Y, acceleration.Value.Z);
                accelE.elevationAngle = elevationAngle.Value;
                OnAccelerationChanged(accelE);
            }
            else
            {
                KinectBase.AccelerationEventArgs accelE = new KinectBase.AccelerationEventArgs();
                accelE.kinectID = this.kinectID;

                //Send the acceleration, if it is valid
                if (acceleration.HasValue)
                {
                    //Update the filtered acceleration
                    EigenWrapper.Matrix accelMat = new EigenWrapper.Matrix(3, 1);
                    accelMat[0, 0] = acceleration.Value.X;
                    accelMat[1, 0] = acceleration.Value.Y;
                    accelMat[2, 0] = acceleration.Value.Z;
                    accelerationFilter.IntegrateMeasurement(accelMat, DateTime.UtcNow, 0.01);
                    accelFilterStarted = true;
                    //lastAcceleration = acceleration.Value;
                    accelE.acceleration = new Vector3D(acceleration.Value.X, acceleration.Value.Y, acceleration.Value.Z);
                }
                else
                {
                    accelE.acceleration = null;
                }

                //Send the Kinect angle if it is valid
                if (elevationAngle.HasValue)
                {
                    accelE.elevationAngle = elevationAngle.Value;
                }
                else
                {
                    accelE.elevationAngle = null;
                }

                OnAccelerationChanged(accelE);
            }
        }
        private void KinectOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs eventArgs)
        {
            using (SkeletonFrame skeletonFrame = eventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    int        i          = 0;
                    Skeleton[] squelettes = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(squelettes);

                    try
                    {
                        var accelerometerReading = kinect.AccelerometerGetCurrentReading();
                        interStream.ProcessSkeleton(squelettes, accelerometerReading, skeletonFrame.Timestamp);
                    }
                    catch (InvalidOperationException)
                    {}

                    bufSkel.Clear();

                    if (skeletonFrame.SkeletonArrayLength == 0)
                    {
                        return;
                    }

                    var trackedSquelettes = squelettes.Where(skel => skel.TrackingState == SkeletonTrackingState.Tracked).OrderBy <Skeleton, float>(skel => skel.Position.Z);

                    if (trackedSquelettes.Count() == 0)
                    {
                        return;
                    }

                    squelette = trackedSquelettes.First();

                    if (squelette != null)
                    {
                        players[i] = squelette;
                        drawBone(squelette, JointType.Head, JointType.ShoulderCenter, colorSkel);

                        drawBone(squelette, JointType.ShoulderCenter, JointType.ShoulderLeft, colorSkel);
                        drawBone(squelette, JointType.ShoulderCenter, JointType.ShoulderRight, colorSkel);

                        drawBone(squelette, JointType.ShoulderCenter, JointType.Spine, colorSkel);

                        drawBone(squelette, JointType.Spine, JointType.HipCenter, colorSkel);

                        drawBone(squelette, JointType.HipCenter, JointType.HipLeft, colorSkel);
                        drawBone(squelette, JointType.HipCenter, JointType.HipRight, colorSkel);

                        drawBone(squelette, JointType.ShoulderLeft, JointType.ElbowLeft, colorSkel);
                        drawBone(squelette, JointType.ElbowLeft, JointType.WristLeft, colorSkel);
                        drawBone(squelette, JointType.WristLeft, JointType.HandLeft, colorSkel);

                        drawBone(squelette, JointType.ShoulderRight, JointType.ElbowRight, colorSkel);
                        drawBone(squelette, JointType.ElbowRight, JointType.WristRight, colorSkel);
                        drawBone(squelette, JointType.WristRight, JointType.HandRight, colorSkel);

                        // Left Leg
                        drawBone(squelette, JointType.HipLeft, JointType.KneeLeft, colorSkel);
                        drawBone(squelette, JointType.KneeLeft, JointType.AnkleLeft, colorSkel);
                        drawBone(squelette, JointType.AnkleLeft, JointType.FootLeft, colorSkel);

                        // Right Leg
                        drawBone(squelette, JointType.HipRight, JointType.KneeRight, colorSkel);
                        drawBone(squelette, JointType.KneeRight, JointType.AnkleRight, colorSkel);
                        drawBone(squelette, JointType.AnkleRight, JointType.FootRight, colorSkel);

                        if (focusSquelette)
                        {
                            ViewPort.Camera = (PerspectiveCamera)squelette2CameraConverter.Convert(players[0], null, null, null);
                        }

                        ++i;
                    }

                    lignes.Points = bufSkel;
                }
            }
        }
Example #27
0
        /// <summary>
        /// Event handler for Kinect sensor's SkeletonFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                //Bloque usado por InteractionStream
                if (skeletonFrame != null)
                {
                    //Se hace una copia del esqueleto se asigna la lectura del accelerometro y procesa interactionstream el esqueleto usando lo anterior.
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);

                    //Este bloque está sacado de:
                    //////////////////////////////////////////////////////////////////////////////////////////////////
                    //http://dotneteers.net/blogs/vbandi/archive/2013/05/03/kinect-interactions-with-wpf-part-iii-demystifying-the-interaction-stream.aspx
                    //////////////////////////////////////////////////////////////////////////////////////////////////
                    Vector4 accelerometerReading = sensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
            }

            using (DrawingContext dc = this.drawingGroup.Open())
            {
                //Dibuja los rectángulos que no están cogidos.
                puzzle.DrawPuzzle(dc);

                //Mostramos por pantalla el tiempo que nos queda.
                FormattedText t = new FormattedText(puzzle.getTiempo() + " s", CultureInfo.GetCultureInfo("es-es"), FlowDirection.LeftToRight, new Typeface("Verdana"),
                                                    24,
                                                    Brushes.Black);
                dc.DrawText(t, new Point(570, 10));

                //Comprobamos que kinect nos haya leido el esqueleto
                if (skeletons.Length != 0)
                {
                    // Coge cada esqueleto de cada frame para manejarlo dentro.
                    foreach (Skeleton skel in skeletons)
                    {
                        //Asignamos al esqueleto de este frame el estado del rastreo del esqueleto
                        //Si detecta los puntos de las articulaciones entra en este bloque.
                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            //Comprobamos si se ha acabado el tutorial.
                            if (!fin_tuto)
                            {
                                //Si el tutorial no se ha acabado dibujamos la imagen del tutorial y pintamos un circulo rojo para que al tocarlo se acabe el tutorial
                                dc.DrawImage(tutorial, new Rect(0, 0, RenderWidth, RenderHeight));
                                FormattedText ft = new FormattedText("Toque el\ncirculo para\nfin Tutorial", CultureInfo.GetCultureInfo("es-es"), FlowDirection.LeftToRight, new Typeface("Consola"),
                                                                     14,
                                                                     Brushes.Black);
                                dc.DrawText(ft, new Point(550, 60));
                                dc.DrawEllipse(Brushes.Red, null, new Point(600, 30), 30, 30);

                                //Si la mano toca el circulo.
                                if (SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).X > 560 &&
                                    SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).Y < 60)
                                {
                                    //Ponemos a true el final del tutorial y empieza a contar el tiempo.
                                    fin_tuto = true;
                                    puzzle.iniciarTiempo();
                                }
                            }
                            else
                            {   //Dibujamos la pieza que esté cogida.
                                puzzle.DrawPuzzleCogidos(dc);
                            }

                            //Si se ha acabado el tiempo para resolver el puzzle mostramos la puntuación.
                            if (puzzle.getFin())
                            {
                                FormattedText punt = new FormattedText("Puntuacion: " + puzzle.getPuntuacion() + "/100", CultureInfo.GetCultureInfo("es-es"), FlowDirection.LeftToRight, new Typeface("Verdana"),
                                                                       24,
                                                                       Brushes.Black);
                                dc.DrawText(punt, new Point(240, 240));
                            }

                            //Actualizamos el esqueleto del puzzle
                            puzzle.actualizarSkeleto(skel);

                            //Dibujamos las manos.
                            dc.DrawImage(manoDer, new Rect(this.SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).X - manoDer.Width / 2, this.SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).Y - manoDer.Height / 2, manoDer.Width, manoDer.Height));
                            dc.DrawImage(manoIzq, new Rect(this.SkeletonPointToScreen(skel.Joints[JointType.HandLeft].Position).X - manoIzq.Width / 2, this.SkeletonPointToScreen(skel.Joints[JointType.HandLeft].Position).Y - manoIzq.Height / 2, manoIzq.Width, manoIzq.Height));
                        }
                    }
                }

                // prevent drawing outside of our render area
                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));
            }
        }
Example #28
0
        private void KinectSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs args)
        {
            Skeleton[] skeletons = new Skeleton[0];
            using (SkeletonFrame skeletonFrame = args.OpenSkeletonFrame()) {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    try {
                        skeletonFrame.CopySkeletonDataTo(skeletons);
#if MOUSE_CONTROL
                        Vector4 accelReading = sensor.AccelerometerGetCurrentReading();
                        _interactionStream.ProcessSkeleton(skeletons, accelReading, skeletonFrame.Timestamp);
#endif
                    }
                    catch (InvalidOperationException) {
                        //Ignoramos el frame
                        activeSkeleton = null;
                        return;
                    }
                }
            }

            if (skeletons.Length == 0)
            {
                activeSkeleton = null;
                return;
            }

            //Retorna el primer jugador que tenga tracking del Kinect.
            Skeleton[] trackedSkeletons = (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).ToArray();
            if (trackedSkeletons.Length == 0)
            {
                activeSkeleton = null;
                return;
            }

            //Para este punto ya tenemos por lo menos un candidato para activeSkeleton
            activeSkeleton = trackedSkeletons[0];
            if (trackedSkeletons.Length > 1)
            {
                for (int i = 1; i < trackedSkeletons.Length; i++)
                {
                    if (KinectDistanceTools.FirstSkeletonIsCloserToSensor(ref trackedSkeletons[i], ref activeSkeleton, JointType.HipCenter))
                    {
                        activeSkeleton = trackedSkeletons[i];
                    }
                }
            }

#if MOUSE_CONTROL
            /*
             * El sistema de referencia que ocupa el kinect tiene como origen la posición
             * del sensor, es decir que eje Z apunta al frente del kinect, el eje X el positivo estád
             * a la derecha del kinect y el eje Y
             * */

            //A partir de ahora ya sabemos que hay un jugador detectado por el Kinect y podemos obtener alguna
            //parte del cuerpo de referencia.
            Joint rightHand = activeSkeleton.Joints[JointType.WristRight];
            Joint leftHand  = activeSkeleton.Joints[JointType.WristLeft];

            XValueRight.Text = rightHand.Position.X.ToString("F", CultureInfo.InvariantCulture);
            YValueRight.Text = rightHand.Position.Y.ToString("F", CultureInfo.InvariantCulture);
            ZValueRight.Text = rightHand.Position.Z.ToString("F", CultureInfo.InvariantCulture);

            XValueLeft.Text = leftHand.Position.X.ToString("F", CultureInfo.InvariantCulture);
            YValueLeft.Text = leftHand.Position.Y.ToString("F", CultureInfo.InvariantCulture);
            ZValueLeft.Text = leftHand.Position.Z.ToString("F", CultureInfo.InvariantCulture);

            //Queremos la mano más cercana al sensor para que controle el mouse.
            if (KinectDistanceTools.FirstIsCloserToSensor(ref rightHand, ref leftHand))
            {
                RightRaised.Text = "Activada";
                LeftRaised.Text  = "Desactivado";
                Vector2 result = mouseController.Move(ref rightHand, isClicks[(int)HandType.Right]);
                MousePos.Text = string.Format("X: {0}, Y: {1}, Click: {2}", result.x, result.y, isClicks[(int)HandType.Right] ? "Sí" : "No");
            }
            else
            {
                LeftRaised.Text  = "Activada";
                RightRaised.Text = "Desactivado";
                Vector2 result = mouseController.Move(ref leftHand, isClicks[(int)HandType.Left]);
                MousePos.Text = string.Format("X: {0}, Y: {1}, Click: {2}", result.x, result.y, isClicks[(int)HandType.Left] ? "Sí" : "No");
            }
#endif
        }
Example #29
0
        //Updates the acceleration on the GUI and the server, 30 FPS may be a little fast for the GUI, but for VRPN, it probably needs to be at least that fast
        private void updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //Update the audio beam angle, if it is using feedback mode (loudest is done automatically, and skeleton mode is done in the server core)
            if (kinect.AudioSource != null && server.serverMasterOptions.kinectOptionsList.Count > kinectID)
            {
                if (server.feedbackPosition != null && server.serverMasterOptions.kinectOptionsList[kinectID].audioTrackMode == AudioTrackingMode.Feedback)
                {
                    double angle = Math.Atan((server.feedbackPosition.Value.X - server.serverMasterOptions.kinectOptionsList[kinectID].kinectPosition.X) / (server.feedbackPosition.Value.Z - server.serverMasterOptions.kinectOptionsList[kinectID].kinectPosition.Z)) * (180.0 / Math.PI);
                    kinect.AudioSource.ManualBeamAngle = angle; // This will be rounded automatically to the nearest 10 degree increment, in the range -50 to 50 degrees
                }
            }

            //Update the acceleration data
            bool    dataValid      = false;
            Vector4 acceleration   = new Vector4();
            int     elevationAngle = 0;

            lock (kinect)
            {
                if (kinect.IsRunning)
                {
                    //TODO: Fix this, because it is messed up!
                    acceleration   = kinect.AccelerometerGetCurrentReading();
                    elevationAngle = kinect.ElevationAngle;
                    dataValid      = true;
                }
            }

            //Update the GUI
            if (dataValid)
            {
                if (isGUI && parent.kinectOptionGUIPages.Count > kinectID && parent.kinectOptionGUIPages[kinectID].IsVisible)
                {
                    //Note: This method is on a different thread from the rest of the KinectCore because of the timer, thus the need for the invoke
                    parent.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        parent.kinectOptionGUIPages[kinectID].AccelXTextBlock.Text = acceleration.X.ToString("F2");
                        parent.kinectOptionGUIPages[kinectID].AccelYTextBlock.Text = acceleration.Y.ToString("F2");
                        parent.kinectOptionGUIPages[kinectID].AccelZTextBlock.Text = acceleration.Z.ToString("F2");
                        parent.kinectOptionGUIPages[kinectID].AngleTextBlock.Text = elevationAngle.ToString();
                    }), null
                                                  );
                }

                //Update the VRPN server
                if (server.isRunning && server.serverMasterOptions.kinectOptionsList[kinectID].sendAcceleration)
                {
                    for (int i = 0; i < server.analogServers.Count; i++)
                    {
                        if (server.serverMasterOptions.analogServers[i].serverName == server.serverMasterOptions.kinectOptionsList[kinectID].accelerationServerName)
                        {
                            lock (server.analogServers[i])
                            {
                                server.analogServers[i].AnalogChannels[server.serverMasterOptions.kinectOptionsList[kinectID].accelXChannel].Value = acceleration.X;
                                server.analogServers[i].AnalogChannels[server.serverMasterOptions.kinectOptionsList[kinectID].accelYChannel].Value = acceleration.Y;
                                server.analogServers[i].AnalogChannels[server.serverMasterOptions.kinectOptionsList[kinectID].accelZChannel].Value = acceleration.Z;
                                server.analogServers[i].Report();
                            }
                            break;
                        }
                    }
                }
            }
        }