Gyroscope and accelerometer data from Windows?
Asked Answered
D

2

5

The Microsoft Surface Pro has a gyroscope and accelerometer, Windows 8, and the full .NET framework.

Most articles I find that talk about the motion API point to the Windows Phone 8 API.

What .NET Framework namespaces and classes should I be using to get gyroscope and accelerometer data from?

Dvina answered 12/8, 2014 at 18:45 Comment(2)
Be more clear. Do you want it for a Store app or for a regular (Win 7 style) app?Cementite
I want a regular .NET app, ideally. I will deploy the app to a Microsoft Surface Pro via a USB drive. I do not plan to deploy this app to the Windows Store or anywhere else.Dvina
L
4

I just worked based off the documentation - 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.
   }
}

Haven't tested it, but it should work for any Windows Store App - If you're trying to make it run as a console/windows forms app, you need to change the targetplatform by:

  1. Right Click your project -> Unload Project
  2. Follow the rest of this https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications
Lazybones answered 12/8, 2014 at 19:4 Comment(1)
Thanks--the link to the Intel article was especially helpful.Dvina
P
3

For the surface pro you need to use the Windows 8.1 library, instead of the Windows Phone 8.1 library.

It should be in the same Windows.Devices.Sensors namespace.

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);
}
Paganize answered 12/8, 2014 at 19:4 Comment(7)
Make sure to call [Sensor].GetDefault() to find the sensor first.Lazybones
I am assuming since he has it working for windows phone, he already does this.Paganize
@Paganize My original post was probably not clear. I do not have a working phone app. I'm just getting started working with the Surface Pro and have been confused where to start.Dvina
@Dvina Do you have the SDK installed? This is a good starting point: code.msdn.microsoft.com/windowsapps/…Paganize
@Paganize - the OP does not want a Phone or Tablet app. That's why i doubt your answer. Read the other one.Cementite
It's an Ultrabook disguised as a Tablet.Cementite
@HenkHolterman I have read the other answer and Looking at the references from the example project I used to run on a surface pro ( code.msdn.microsoft.com/windowsapps/… ) the Windows 8.1 reference is analogous to RT and desktop. Maybe this is different for windows 8, with a device running 8 RT (arm based)? On a side note, from the properties menu it shows up as an SDK not an actual dll (in addition to the .NET for Windows Store apps reference).Paganize

© 2022 - 2024 — McMap. All rights reserved.