void ReadMotion() { if (Motion.IsSupported) { MotionReading reading = sensor.CurrentValue; Vector3 gravity = reading.Gravity; gravityX.Text = "" + gravity.X; gravityY.Text = "" + gravity.Y; gravityZ.Text = "" + gravity.Z; AttitudeReading attitude = reading.Attitude; attitudeX.Text = "" + attitude.Pitch; attitudeY.Text = "" + attitude.Roll; attitudeZ.Text = "" + attitude.Yaw; } }
async void _motion_CurrentValueChanged(object sender, SensorReadingEventArgs <MotionReading> e) { AttitudeReading attitude = e.SensorReading.Attitude; Vector3 deviceAcceleration = e.SensorReading.DeviceAcceleration; Vector3 deviceRotationRate = e.SensorReading.DeviceRotationRate; Vector3 gravity = e.SensorReading.Gravity; String showstr = ""; // 在 UI 上显示相关参数 showstr = "yaw: " + MathHelper.ToDegrees(attitude.Yaw).ToString("0.0"); showstr += Environment.NewLine; showstr += "pitch: " + MathHelper.ToDegrees(attitude.Pitch).ToString("0.0"); showstr += Environment.NewLine; showstr += "roll: " + MathHelper.ToDegrees(attitude.Roll).ToString("0.0"); showstr += Environment.NewLine; showstr += "deviceAcceleration: \n" + deviceAcceleration.ToString(); showstr += Environment.NewLine; showstr += "deviceRotationRate: \n" + deviceRotationRate.ToString(); showstr += Environment.NewLine; showstr += "Gravity: \n" + gravity.ToString(); showstr += Environment.NewLine; showstr += e.SensorReading.Timestamp; lblMsg.Dispatcher.BeginInvoke(() => { lblMsg.Text = "MotionReading:\n" + showstr; }); if (post) { String poststr = "{'yaw':" + MathHelper.ToDegrees(attitude.Yaw).ToString("0.0") + ",'pitch':" + MathHelper.ToDegrees(attitude.Pitch).ToString("0.0") + ",'roll':" + MathHelper.ToDegrees(attitude.Roll).ToString("0.0") + ",'dacx':" + deviceAcceleration.X + ",'dacy':" + deviceAcceleration.Y + ",'dacz':" + deviceAcceleration.Z + ",'drx':" + deviceRotationRate.X + ",'dry':" + deviceRotationRate.Y + ",'drz':" + deviceRotationRate.Z + ",'gx':" + gravity.X + ",'gy':" + gravity.Y + ",'gz':" + gravity.Z + "}"; string s = await pb1.sender(poststr); //string s = await pb1.sender("{ 'DeviceId':'dev-01', 'Temperature':" + (++index).ToString() + " }"); PostMotionStatus.Dispatcher.BeginInvoke(() => { PostMotionStatus.Text = "Motion: " + s + " No." + (++index).ToString() + " message"; }); } }
void sensor_CurrentValueChanged(object sender, SensorReadingEventArgs <MotionReading> e) { MotionReading reading = e.SensorReading; Dispatcher.BeginInvoke(() => { Vector3 acceleration = reading.DeviceAcceleration; // height of control = 400; height of postive bar = 200; max value = 3; // set scale at 200/3 = 67 accelX.Value = acceleration.X; accelY.Value = acceleration.Y; accelZ.Value = acceleration.Z; Vector3 gravity = reading.Gravity; // height of control = 400; height of postive bar = 200; max value = 1; // set scale at 200/1 = 200 gravityX.Value = gravity.X; gravityY.Value = gravity.Y; gravityZ.Value = gravity.Z; Vector3 rotation = reading.DeviceRotationRate; // height of control = 400; height of postive bar = 200; reasonable max value = 2pi = 6.25 (1.5 rotation per second) // set scale at 200/6.25 = 32 gyroX.Value = rotation.X; gyroY.Value = rotation.Y; gyroZ.Value = rotation.Z; AttitudeReading attitude = reading.Attitude; attitudeX.Value = attitude.Pitch; // 0->pi 200/3.14 = 64 attitudeY.Value = attitude.Roll; // 0->pi/2 200/1.57 = 128 attitudeZ.Value = attitude.Yaw; // 0->2pi 200/6.28 = 32 Vector3 worldSpacePoint = new Vector3(0.0f, 10.0f, 0.0f); Vector3 bodySpacePoint = Vector3.Transform(worldSpacePoint, attitude.RotationMatrix); point.Text = string.Format("Attitude: Transform of (0.0, 10.0, 0.0) = ({0:F1}, {1:F1}, {2:F1})", bodySpacePoint.X, bodySpacePoint.Y, bodySpacePoint.Z); }); }
private static float GetCameraDirection(AttitudeReading e) { return GetCameraDirection(e.Roll, e.Yaw, e.Pitch); }