Click here to Skip to main content
15,881,828 members
Articles / Mobile Apps / Windows Phone 7

Accelerometer Support on WP7 Emulator

Rate me:
Please Sign up or sign in to vote.
4.97/5 (18 votes)
20 Dec 2010CPOL6 min read 48.3K   540   31   15
The Windows Phone 7 emulator doesn't support accelerometer. This project will help you to test accelerometer enabled applications on the Windows Phone 7 emulator!

Contents

Introduction

As all we know, there is no support for accelerometer in Windows Phone 7 Emulator. If you are creating some accelerometer bases application, you have to deploy it to your Windows Phone powered device and then test it. Of course, there are projects which are using some hardware such as Wii or XBox 360 controller to emulate mobile device movement. But when you try to use it, it strikes you that they have one weak point - they need hardware! Some day, when I had more free time, I decided to find some solution for this. After a couple of hours, I have found one and this is it... I want you to introduce WP7 Accelerometer Emulator project. Let's take a closer look at what it is.

Accelerometer force vectors

Accelerometer force vectors. Source: The windows Phone Developer Blog

How It Works? (End User Perspective)

Solution consists of three projects which are connected each other:

  1. MaciejGrabek.WP7Accelerometer - This is the core library to use in your projects. It contains WP7AccelerometerProvider class which provides all functionality given by real Accelerometer from Microsoft.Devices.Sensors, i.e., Start and Stop methods and ReadingChanged event which provides information about gravity for each axis. There is no IAccelerometer interface to implement, so I simply copied names of all used items from accelerator class manifest.
  2. MaciejGrabek.WP7AccelerometerEmulatorUI - This project allows you to emulate position of phone by simple use of three scrolls for X, Y and Z rotation. It sends all required data to Proxy.
  3. MaciejGrabek.WP7AccelerometerEmulatorProxy - This proxy is used to receive data from UI and allows to read it from WP7AccelerometerProvider.

If you are running your application on emulator, then WP7AccelerometerProvider is trying to read data from Proxy - if something goes wrong on retrieving this data (for example, proxy is unavailable), it simply switches into random data mode. You can retry receiving data from proxy by simple use of Stop and Start methods. Another way is to restart application. Of course, if you will run the application on device it won't use proxy, but it will provide you hardware built in accelerometer data.

How To Use It?

It is very simple - like using real accelerometer.

When you create WP7 application, the first thing you need to do is add a reference to MaciejGrabek.WP7Accelerometer library (or project). Then create an accelerometer object like below:

C#
using MaciejGrabek.WP7Accelerometer;

var acc = new WP7AccelerometerProvider();

Now simply add handling to AccelerometerProviderReadingChanged event and consume data provided by WP7AccelerometerReadingEventArgs class:

C#
acc.ReadingChanged += OnAccelerometerProviderReadingChanged;

private void OnAccelerometerProviderReadingChanged
	(object sender, WP7AccelerometerReadingEventArgs args)
{
    //do what you want with data
    ProcessAccelerometerData(args.X, args.Y, args.Z, args.Timestamp);
}

Start WP7AccelerometerProvider and be ready to get the data.

C#
try
{
    acc.Start();
}
catch (Exception exc)
{
    txtblk.Text = exc.Message;
}

This is all you need to do in the application. Now let's see the control panel. To do this, you need to start MaciejGrabek.WP7AccelerometerEmulatorUI and MaciejGrabek.WP7AccelerometerEmulatorProxy (it should start automatically with MaciejGrabek.WP7AccelerometerEmulatorUI, but if not be sure to do this manually - otherwise, you will get random data). Run your WP7 application on emulator and on MaciejGrabek.WP7AccelerometerEmulatorUI mark "Send data to emulator" checkbox and have fun! If you will leave unchecked "Send data to emulator" AccelerometerProvider will simply read what is on proxy without any change on interface. The result is shown below:

WP7 Accelerometer Emulator UI

WP7 Accelerometer Emulator UI

As you can see, it is very simple and intuitive to use.

For the end of this part, let's compare Microsoft.Devices.Sensors.Accelerometer and MaciejGrabek.WP7Accelerometer.WP7AccelerometerProvider:

Accelerometer WP7AccelerometerProvider
C#
public SensorState State { get; }
C#
public SensorState State { get; }
C#
public event EventHandler ReadingChanged; 
C#
//AccelerometerReadingEventArgs
public DateTimeOffset Timestamp { get; }
public double X { get; }
public double Y { get; }
public double Z { get; }
C#
public event EventHandler ReadingChanged; 
C#
//WP7AccelerometerReadingEventArgs
public DateTimeOffset Timestamp { get; }
public double X { get; }
public double Y { get; }
public double Z { get; }
C#
public void Dispose();
C#
public void Dispose();
C#
public void Start();
C#
public void Start();
C#
public void Stop();
C#
public void Stop();

As you can see, both classes are very similar. There is no interface to implement (for example IAccelerometer) so the only way was to create "similar" class. This is all that is important for the "end user" of this emulator. Now let's see how it really works. :)

Under the Hood

First of all, I should explain a little bit more about data flow in this project. As you can see below, the main communication is one way. It means that all that is important for UI project is to send data to the proxy. From the other side, the WP7AccelerometerProvider is responsible to read this data from proxy or from device (which has higher priority).

Image 4

Proxy

As a proxy, I decided to use WCF service that allows to get data using simple "HTTP GET". Here is how service contract and its implementation look.

C#
[ServiceContract(Namespace = "http://maciejgrabek.com/WP7AccelerometerEmulatorProxy")]
public interface IAccelerometerEmulatorProxy
{
    [OperationContract]
    [WebGet]
    string GetData();

    [OperationContract]
    void SetData(float x, float y, float z);
}
C#
[AspNetCompatibilityRequirements(RequirementsMode = 
	AspNetCompatibilityRequirementsMode.Allowed)]
public class AccelerometerEmulatorProxy : IAccelerometerEmulatorProxy
{
    public string GetData()
    {
        AccelerometerVector v = HttpRuntime.Cache
        	[AccelerometerVector.CacheKey] as AccelerometerVector;
        if (v != null)
        {
            return v.ToString();
        }
        else
        {
            return AccelerometerVector.Empty.ToString();
        }
    }
    
    public void SetData(float x, float y, float z)
    {
        AccelerometerVector v = new AccelerometerVector()
        {
            X = x,
            Y = y,
            Z = z
        };
        HttpRuntime.Cache.Remove(AccelerometerVector.CacheKey);
        HttpRuntime.Cache.Add(AccelerometerVector.CacheKey,
            v,
            null,
            DateTime.MaxValue,
            Cache.NoSlidingExpiration,
            CacheItemPriority.Default,
            delegate { });
    }
}

The important thing is to use [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] and [WebGet] to enable this kind of communication with WCF service.
As you can see, to store data from UI, I am using built in cache because it is quick and efficient. The whole logic is not any kind of rocket science, because I followed the "Keep It Simple" rule. :)

UI

On MaciejGrabek.WP7AccelerometerEmulatorUI, there are some XNA classes based calculations, which are required to get proper values of gravity force base vectors. 3D graphic computations are quite a different topic so I will let it for XNA fans. After all operations data is sent to Proxy where it is stored and ready to transfer to Windows Phone Emulator. All these calculations and data transfer is executed 10 times per second. If you want to change, there is method called InitializeAndRunTimer where you can change frequency of timer, but I thing that 10Hz is quite ok.

C#
private void InitializeAndRunTimer()
{
    _timer = new DispatcherTimer();
    _timer.Interval = TimeSpan.FromMilliseconds(100);
    _timer.Tick += delegate
    {
        PerformSingleStep();
    };
    _timer.Start();
}

Core

This is the time to see the core library which is responsible for reading data from "proper" source. Of course, I am thinking about MaciejGrabek.WP7Accelerometer and WP7AccelerometerProvider class. As I mentioned before, it is similar to real Accelerometer definition. The most important thing is to recognize current environment. You can use in this case Microsoft.Devices.Environment.DeviceType property and <doce>Microsoft.Devices.DeviceType enumerator. If it is equal to DeviceType.Device, then we are using Accelerometer, otherwise (DeviceType.Emulator) we are initializing WebClient to get data from Proxy. As an example, we can use Stop() method implementation:

C#
[SecuritySafeCritical]
public void Stop()
{
    if (Microsoft.Devices.Environment.DeviceType == DeviceType.Device)
    {
        _accelerometer.Stop();
    }
    else
    {
        this._timer.Stop();
    }
}

When you are creating WP7AccelerometerProvider instance, you can specify two parameters. First of them is dataUrl with default value http://localhost:9876/AccelerometerEmulatorProxy.svc/GetData and the second one is refreshRate which is set to 100 (it means 10Hz). Of course, there is no sense to set this value less than 33, because current display refresh rate is 33 times per second so even if we will read data, it won't be visible for user.

When I was testing this application, I found some caching problem on emulator or web server side. Even if the data was changed on server, there was no way to get new version of it. That was the reason to use a little trick:

C#
if (!this._dataUrlError)
{
    if (!this._client.IsBusy)
        this._client.DownloadStringAsync(new Uri(String.Format("{0}?{1}", 
		this._dataUrl, this._rand.NextDouble())), null);
}

When we add some random item to URL, then we can be sure that our request will return not cached data. If there is some problem with data reading from proxy WP7AccelerometerProvider is using Random generator to get some data. The only way to retry reading from proxy is to call Stop() and Start() methods on WP7AccelerometerProvider instance or simply restarting application.

Where To Find It?

I have attached code with binaries, but I will modify and extend it in future. If you want to get the current version, you can simply download MaciejGrabek.WP7AccelerometerEmulator and use it in your projects. I will put all updates of code on this place so if you are interested follow project site. There is, of course, some demo to see exactly how to use it and it is attached to the provided solution.

What Will Appear in Future Versions?

  1. Sometimes, you can see Gimbal Lock effect - I am currently trying to solve it
  2. Add mouse control for rotation
  3. Add movement sequences (you can record movement like shaking, rolling, etc., save it, and quick run every time you need)
  4. Add additional acceleration for movement (shake, etc.)

Fell free to comment!

Useful Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Poland Poland
I am working as a .NET Developer for QXL Poland - one of the bigest e-commerce company in Poland and Europe. I am creating portals, SOA based systems.
Since 2008 I am founder and leader of offline .NET Developers User Group in Toruń (Poland). In free time I am posting on my blog.

In 2010 I won Microsoft Technology Summit Speaker Idol competition and I will have my own session on MTS 2011 - the bigest IT conference in Poland.

Comments and Discussions

 
GeneralNewer MSDN article Pin
MikePelton15-Apr-11 1:34
MikePelton15-Apr-11 1:34 
GeneralMy vote of 5 Pin
jehanzeb22-Feb-11 0:04
jehanzeb22-Feb-11 0:04 
GeneralThat is freaking cool Pin
Don Kackman22-Dec-10 5:10
Don Kackman22-Dec-10 5:10 
GeneralWell done! Pin
Zavoloka Sergey20-Dec-10 4:36
Zavoloka Sergey20-Dec-10 4:36 
GeneralRe: Well done! Pin
maciekgrabek20-Dec-10 6:31
maciekgrabek20-Dec-10 6:31 
Thanks!
I saw those tools to, but they doesn't fit to my needs - take something into your hand and rotate it in front of web cam. What if I have build in web cam in my laptop? I need to hold "model" in front of my eyes and make some gymnastic to see monitor :P
GeneralMy vote of 5 Pin
Tamok18-Dec-10 13:23
Tamok18-Dec-10 13:23 
GeneralMy vote of 5 Pin
SledgeHammer0118-Dec-10 7:42
SledgeHammer0118-Dec-10 7:42 
GeneralMy vote of 5 Pin
RaviRanjanKr17-Dec-10 3:16
professionalRaviRanjanKr17-Dec-10 3:16 
GeneralMy vote of 5 Pin
Nish Nishant15-Dec-10 5:13
sitebuilderNish Nishant15-Dec-10 5:13 
GeneralMy vote of 5 Pin
prasad0215-Dec-10 4:06
prasad0215-Dec-10 4:06 
GeneralInteresting! Pin
Indivara15-Dec-10 3:19
professionalIndivara15-Dec-10 3:19 
GeneralRe: Interesting! Pin
maciekgrabek15-Dec-10 9:47
maciekgrabek15-Dec-10 9:47 
GeneralRe: Interesting! Pin
Indivara15-Dec-10 13:50
professionalIndivara15-Dec-10 13:50 
GeneralMy vote of 5 Pin
DrABELL14-Dec-10 16:21
DrABELL14-Dec-10 16:21 
GeneralRe: My vote of 5 Pin
maciekgrabek14-Dec-10 20:12
maciekgrabek14-Dec-10 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.