protected virtual void OnAccelerationChanged(KinectBase.AccelerationEventArgs e) { if (AccelerationChanged != null) { AccelerationChanged(this, e); } }
void kinectCore_AccelerationChanged(object sender, KinectBase.AccelerationEventArgs e) { //Don't bother calling the disptacher (which is kind of expensive) if the control is hidden if (IsInitialized && Visibility == System.Windows.Visibility.Visible) { this.Dispatcher.BeginInvoke((Action)(() => { if (e.acceleration.HasValue) { AccelXTextBlock.Text = e.acceleration.Value.X.ToString("F2"); AccelYTextBlock.Text = e.acceleration.Value.Y.ToString("F2"); AccelZTextBlock.Text = e.acceleration.Value.Z.ToString("F2"); } else { AccelXTextBlock.Text = "N/A"; AccelYTextBlock.Text = "N/A"; AccelZTextBlock.Text = "N/A"; } if (e.elevationAngle.HasValue) { AngleTextBlock.Text = e.elevationAngle.Value.ToString(); } else { AngleTextBlock.Text = "N/A"; } }), null); } }
//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); } }
//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.IsRunning) { //I wish these try/catch statements weren't necessary, but these two calls seemed to failed 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) { //Transmits the acceleration data using an event lastAcceleration = acceleration.Value; 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) { 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); } }