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?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jim
  • 11,229
  • 20
  • 79
  • 114
  • Be more clear. Do you want it for a Store app or for a regular (Win 7 style) app? – H H Aug 12 '14 at 19:42
  • 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. – Jim Aug 12 '14 at 20:50

2 Answers2

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
Levi Fuller
  • 13,631
  • 4
  • 38
  • 44
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);
}
wbennett
  • 2,545
  • 21
  • 12
  • Make sure to call [Sensor].GetDefault() to find the sensor first. – Levi Fuller Aug 12 '14 at 19:05
  • I am assuming since he has it working for windows phone, he already does this. – wbennett Aug 12 '14 at 19:07
  • @wbennett 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. – Jim Aug 12 '14 at 20:51
  • @Jim Do you have the SDK installed? This is a good starting point: http://code.msdn.microsoft.com/windowsapps/Accelerometer-Sensor-Sample-22982671 – wbennett Aug 12 '14 at 20:54
  • @wbennett - the OP does _not_ want a Phone or Tablet app. That's why i doubt your answer. Read the other one. – H H Aug 12 '14 at 21:07
  • It's an Ultrabook disguised as a Tablet. – H H Aug 12 '14 at 21:11
  • @HenkHolterman I have read the other answer and Looking at the references from the example project I used to run on a surface pro ( http://code.msdn.microsoft.com/windowsapps/Accelerometer-Sensor-Sample-22982671 ) 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). – wbennett Aug 12 '14 at 21:38