Skip to content

Mirror of Apache Cordova Plugin device-motion - to test making the frequency faster for Android

License

Notifications You must be signed in to change notification settings

bayhall/cordova-plugin-device-motion-fast

 
 

Repository files navigation

title description
Device Motion
Access accelerometer data, and phone orientation.
Android 4.4 Android 5.1 iOS 9.3 iOS 10.0

cordova-plugin-device-motion-fast

THIS IS A FORK OF apache/cordova-plugin-device-motion TO ADD EXTRA DATA CAPTURE TO THE ACCELEROMETER

This plugin provides access to the device's accelerometer and orientation at atleast 50Hz. The accelerometer is a motion sensor that detects the change (delta) in movement relative to the current device orientation, in three dimensions along the x, y, and z axis. The "hacked" plugin also returns roll, pitch, and yaw for iOS, and also roll, pitch, and azimuth for Android.

Access is via a global navigator.accelerometer object.

Although the object is attached to the global scoped navigator, it is not available until after the deviceready event.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(navigator.accelerometer);
}

Report issues with this plugin on the Apache Cordova issue tracker

Installation

cordova plugin add cordova-plugin-device-motion

Supported Platforms

  • Android
  • iOS

Methods

  • navigator.accelerometer.getCurrentAcceleration
  • navigator.accelerometer.watchAcceleration
  • navigator.accelerometer.clearWatch

Objects

  • Acceleration

navigator.accelerometer.getCurrentAcceleration

Get the current acceleration along the x, y, and z axes. Also returns _roll, _pitch, and yaw to compliment acceleration with phone position.

These acceleration values are returned to the accelerometerSuccess callback function.

navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);

Example

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Orientation Roll: ' + acceleration.roll + '\n' +
          'Orientation Pitch: ' + acceleration.pitch + '\n' +
          'Orientation Yaw: ' + acceleration.yaw + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
}

function onError() {
    alert('onError!');
}

navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

Browser Quirks

Values for X, Y, Z motion are all randomly generated in order to simulate the accelerometer.

Android Quirks

The accelerometer is called with the SENSOR_DELAY_GAME flag, which limits the maximum readout frequency to 50Hz, depending on the device. Values for period corresponding to higher frequencies will result in duplicate samples. More details can be found in the Android API Guide.

iOS Quirks

  • iOS doesn't recognize the concept of getting the current acceleration at any given point.

  • You must watch the acceleration and capture the data at given time intervals.

  • Thus, the getCurrentAcceleration function yields the last value reported from a watchAccelerometer call.

navigator.accelerometer.watchAcceleration

Retrieves the device's current Acceleration at a regular interval, executing the accelerometerSuccess callback function each time. Specify the interval in milliseconds via the acceleratorOptions object's frequency parameter.

The returned watch ID references the accelerometer's watch interval, and can be used with navigator.accelerometer.clearWatch to stop watching the accelerometer.

var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
                                                       accelerometerError,
                                                       accelerometerOptions);
  • accelerometerOptions: An object with the following optional keys:
    • frequency: requested frequency of calls to accelerometerSuccess with acceleration data in Milliseconds. (Number) (Default: 10000)

Example

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
}

function onError() {
    alert('onError!');
}

var options = { frequency: 3000 };  // Update every 3 seconds

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

Android Quirks

The API returns Azimuth for yaw.

navigator.accelerometer.clearWatch

Stop watching the Acceleration referenced by the watchID parameter.

navigator.accelerometer.clearWatch(watchID);
  • watchID: The ID returned by navigator.accelerometer.watchAcceleration.

Example

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

// ... later on ...

navigator.accelerometer.clearWatch(watchID);

Acceleration

Contains Accelerometer data captured at a specific point in time. Acceleration values include the effect of gravity (9.81 m/s^2), so that when a device lies flat and facing up, x, y, and z values returned should be 0, 0, and 9.81.

Properties

  • x: Amount of acceleration on the x-axis. (in m/s^2) (Number)
  • y: Amount of acceleration on the y-axis. (in m/s^2) (Number)
  • z: Amount of acceleration on the z-axis. (in m/s^2) (Number)
  • roll: Angle of rotation about the y axis. (in radians) (Number)
  • pitch: Angle of rotation about the x axis. (in radians) (Number)
  • yaw: Angle of rotation about the -z axis (Android returns Azimuth). (in radians) (Number)
  • timestamp: Creation timestamp in milliseconds. (DOMTimeStamp)

About

Mirror of Apache Cordova Plugin device-motion - to test making the frequency faster for Android

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 49.3%
  • Java 23.6%
  • Objective-C 12.5%
  • C# 9.7%
  • C++ 4.9%