【问题标题】:Gyroscope and accelerometer data from Windows?来自 Windows 的陀螺仪和加速度计数据?
【发布时间】:2014-10-05 23:12:41
【问题描述】:

Microsoft Surface Pro 具有陀螺仪和加速度计、Windows 8 和完整的 .NET 框架。

我发现大多数关于运动 API 的文章都指向 Windows Phone 8 API。

我应该使用哪些 .NET Framework 命名空间和类来获取陀螺仪和加速度计数据?

【问题讨论】:

  • 更清楚。您希望它用于商店应用还是常规(Win 7 风格)应用?
  • 理想情况下,我想要一个常规的 .NET 应用程序。我将通过 USB 驱动器将该应用程序部署到 Microsoft Surface Pro。我不打算将此应用程序部署到 Windows 应用商店或其他任何地方。

标签: c# .net motion-detection


【解决方案1】:

我刚刚根据文档工作 - http://msdn.microsoft.com/en-us/library/ie/windows.devices.sensors

using Windows.Devices.Sensors;

private Accelerometer _accelerometer;

private void DoStuffWithAccel()
{
   _accelerometer = Accelerometer.GetDefault();
   if (_accelerometer != null)
   {
      AccelerometerReading reading = _accelerometer.GetCurrentReading();
      if (reading != null)
      double xreading = reading.AccelerationX;
      ... etc.
   }
}

尚未对其进行测试,但它应该适用于任何 Windows 应用商店应用程序 - 如果您试图使其作为控制台/Windows 窗体应用程序运行,您需要通过以下方式更改目标平台:

  1. 右键单击您的项目 -> 卸载项目
  2. 关注此https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications 的其余部分

【讨论】:

  • 谢谢——英特尔文章的链接特别有用。
【解决方案2】:

对于 Surface Pro,您需要使用 Windows 8.1 库,而不是 Windows Phone 8.1 库。

它应该在同一个 Windows.Devices.Sensors 命名空间中。

using Windows.Devices.Sensors;
...
//if you aren't already doing so, and you want the default sensor
private void Init()
{
    _accelerometer = Accelerometer.GetDefault();   
    _gyrometer = Gyrometer.GetDefault();
}
...
private void DisplayAccelReading(object sender, object args)
{
    AccelerometerReading reading = _accelerometer.GetCurrentReading();
    if (reading == null)
        return;

    ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
    ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
    ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
}
...
private void DisplayGyroReading(object sender, object args)
{
    GyrometerReading reading = _gyrometer.GetCurrentReading();
    if (reading == null)
        return;

    ScenarioOutput_AngVelX.Text = 
                  String.Format("{0,5:0.00}", reading.AngularVelocityX);
    ScenarioOutput_AngVelY.Text = 
                  String.Format("{0,5:0.00}", reading.AngularVelocityY);
    ScenarioOutput_AngVelZ.Text = 
                  String.Format("{0,5:0.00}", reading.AngularVelocityZ);
}

【讨论】:

  • 确保首先调用 [Sensor].GetDefault() 来找到传感器。
  • 我假设因为他已经为 windows phone 工作了,他已经这样做了。
  • @wbennett 我原来的帖子可能不清楚。我没有可用的手机应用程序。我刚刚开始使用 Surface Pro,一直不知道从哪里开始。
  • @Jim 你有安装 SDK 吗?这是一个很好的起点:code.msdn.microsoft.com/windowsapps/…
  • @wbennett - OP 确实想要手机或平板电脑应用程序。这就是为什么我怀疑你的回答。阅读另一篇。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 2017-11-10
  • 1970-01-01
相关资源
最近更新 更多