Using accelerometer in PhoneGap applications
Using device accelerometer in PhoneGap is quite easy. PhoneGap API gives you example on how to:
Answering the question, “What does the accelerometer actually measuers” could be a little bit harder.
Note, that this element shouldn’t be called accelerometer. It should be called something like “forceometer” or “gravitometer“. Why? Simply, because it measures the gravity not actual acceleration.
Reading
If you’re to lazy to go to PhoneGap API, here you have a quick, dirty example:
[code language=”javascript”]
var watchID = 0;
function onAccSuccess(acceleration)
{
var data = ‘Acceleration X: ‘ + acceleration.x + ‘G.<br />’ +
‘Acceleration Y: ‘ + acceleration.y + ‘G.<br />’ +
‘Acceleration Z: ‘ + acceleration.z + ‘G.<br />’ +
‘Timestamp: ‘ + acceleration.timestamp + ‘.’;
$(‘#accelerationLabel’).html(data);
};
function onAccError()
{
$(‘#accelerationLabel’).html(‘Houston, we have a problem with an acceleration!’);
};
jQuery(function($)
{
$(‘#buttonAccStart’).click(function()
{
var options = { frequency: 3000 }; //In milliseconds, means: update every 3 seconds here.
watchID = navigator.accelerometer.watchAcceleration(onAccSuccess, onAccError, options);
$(‘#buttonAccStop’).attr(‘disabled’, ”);
$(‘#buttonAccStart’).attr(‘disabled’, ‘disabled’);
});
$(‘#buttonAccStop’).click(function()
{
navigator.accelerometer.clearWatch(watchID);
$(‘#buttonAccStart’).attr(‘disabled’, ”);
$(‘#buttonAccStop’).attr(‘disabled’, ‘disabled’);
});
});
[/code]
To get it working, add something like this to your page:
[code language=”html”]
<div id="accelerationLabel">[waiting]</div>
<button id="buttonAccStart" type="button">Start</button>
<button id="buttonAccStop" type="button">Stop!</button>
[/code]
Recalculating
Mobile accelerometer measuers gravity toward natural Earth’s gravity (around 9.81 m/s2). So, in a free-fall values read from it will be (0, 0, 0)
. To cite Ali, I don’t know how much you want to throw your phone up and down to test it? :]
If your phone is resting on the table face-up, you should get values of (0, 0, -1)
for the x, y, and z axes.
Why -1
on Z axis
? You’re getting a -1
on the Z axis
because gravity is acting on the device, applying a constant acceleration of 1G
on it ([source]/source).
If you want to get actual acceleration, you have to do some simple maths. Here is Javascript (PhoneGap) translation out of ObjectiveC iPhone example from StackOverflow:
[code language=”javascript”]
var
g = 9.80665,
x = acceleration.x * g,
y = acceleration.y * g,
z = (acceleration.z + 1) * g;
[/code]
Adding +1
to z axis is explained above.
You should put that, in front of onAccSuccess
function. To get results in metres per second quadrad (m/s2), that is a real acceleration, this function should finally look something like that:
[code language=”javascript”]
function onAccSuccess(acceleration)
{
var
g = 9.80665,
x = acceleration.x * g,
y = acceleration.y * g,
z = (acceleration.z + 1) * g,
data = ‘Acceleration X: ‘ + x + ‘ m/s2.<br />’ +
‘Acceleration Y: ‘ + y + ‘ m/s2.<br />’ +
‘Acceleration Z: ‘ + z + ‘ m/s2.<br />’ +
‘Timestamp: ‘ + acceleration.timestamp + ‘.’ ;
$(‘#accelerationLabel’).html(data);
};
[/code]
Final words
Note something obvious. This is a mobile device, with a tiny (precisely speaking, its dimensions are only 3×5 mm) accelerometer on-board, not a real scientific measurement device. Here is a technical documentation for LIS302DL MEMS
motion sensor used on board iPhone 4 and further. So, don’t expect to much out of it. It is quite noisy and values, that you’re getting from it, jitter around even when the phone is static.
As stated in above mentioned documentation and here, it actually “dies” at about 2.3G. Is that much? Well… for example, if you clap your hands together, you can exceed this acceleration.
Oh, by the way, here is some PDF presentation, again on above mentioned chip. But, since each and every “frame” of possible PowerPoint presentation is extracted there to a separate page (so, for example, if you see list having seven points, you’ll get seven pages out of it — one per each point added), reading it is quite hard and not to much comfortable.