public override void SensorChangedProcess(SensorEvent e) { // Check if we even want to read this sensor switch (e.Sensor.Type) { case SensorType.Accelerometer: _accelerometer = e.Values.ToArray(); break; case SensorType.MagneticField: _geomagnetic = e.Values.ToArray(); break; case SensorType.Gravity: _gravity = e.Values.ToArray(); break; } if (_gravity != null && _geomagnetic != null && _accelerometer != null) { var R = new float[16]; var I = new float[16]; SensorManager.GetRotationMatrix(R, I, _gravity, _geomagnetic); var relativacc = new float[4]; var inv = new float[16]; relativacc[0] = _accelerometer[0]; relativacc[1] = _accelerometer[1]; relativacc[2] = _accelerometer[2]; relativacc[3] = 0; var A_W = new float[4]; Matrix.InvertM(inv, 0, R, 0); Matrix.MultiplyMV(A_W, 0, inv, 0, relativacc, 0); Value = new Vector3(A_W); // Value = new Vector3(_accelerometer); ValueHistory.Enqueue(Value); if (OnValueChanged != null) { OnValueChanged(Value); } } }
public override void SensorChangedProcess(SensorEvent e) { switch (e.Sensor.Type) { case SensorType.MagneticField: mMagnetic = e.Values.ToArray(); break; case SensorType.Gravity: mGravity = e.Values.ToArray(); break; } if (mGravity != null && mMagnetic != null) { var success = SensorManager.GetRotationMatrix(mRotationMatrix, null, mGravity, mMagnetic); if (success) { // inclination is the degree of tilt by the device independent of orientation (portrait or landscape) // if less than 25 or more than 155 degrees the device is considered lying flat float inclination = (float)Math.Acos(mRotationMatrix[8]); if (inclination < TWENTY_FIVE_DEGREE_IN_RADIAN || inclination > ONE_FIFTY_FIVE_DEGREE_IN_RADIAN) { // mFacing is undefined, so we need to clear the history ClearRotHist(); mFacing = float.NaN; } else { SetRotHist(); // mFacing = azimuth is in radian mFacing = FindFacing(); } Value = mFacing; ValueHistory.Enqueue(Value); if (OnValueChanged != null) { OnValueChanged(Value); } } } }