dorukeker / gyronorm.js

JavaScript project for accessing and normalizing the accelerometer and gyroscope data on mobile devices
MIT License
641 stars 65 forks source link

Gyronorm within Web Worker #52

Closed StephenCathcart closed 6 years ago

StephenCathcart commented 6 years ago

Hi,

I'm in need of running gyronorm.js within a web worker. I'm running gyronorm within a cordova application and the main background thread slows down when in the background, updating once per second. If I place gyronorm within a web worker I'm getting an error thrown saying that the window object us undefined (due to web workers having no access to the window object). Is there a way around this to get the script to work in that context? Cheers

dorukeker commented 6 years ago

Hello there. Since you mention Cordova I assume you are packing the app to run as a native app on the mobile devices; is that correct?

Gyronorm is intended only for web applications running in the browser. I assume there are better (and more accurate) solutions to access the deviceMotion and deviceOrientation data in a Cordova app.

Quick Googloing return there: https://www.npmjs.com/package/cordova-plugin-gyroscope https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device-motion/

StephenCathcart commented 6 years ago

Thank you for getting back to me. You're correct, I'm packing the app to run as a native app. Interestingly I switched to your library from the cordova-plugin-device-motion as I was having the same issue.

I've been digging around today and I believe the slowness is not due to either your library or the cordova plugin, but due to chrome (and other browsers) limiting functions such as setInterval to once per second when the tab is inactive (in the background). I created a barebones Ionic app with a setInterval loop running every 50ms, this slows down to exactly 1 second intervals when the tab is out of focus.

https://content.pivotal.io/blog/chrome-and-firefox-throttle-settimeout-setinterval-in-inactive-tabs https://stackoverflow.com/questions/5927284/how-can-i-make-setinterval-also-work-when-a-tab-is-inactive-in-chrome

I've came across a small library that uses web workers to avoid browsers throttling timers:

https://github.com/turuslan/HackTimer

This may provide a solution to my problem. I'll update this issue with the outcome of this in case anyone else comes across this problem.

Thanks again!

dorukeker commented 6 years ago

Perfect! I close the issue for now. You can still update for future reference. Good luck!

StephenCathcart commented 6 years ago

As suspected the issue was down to a combination of chrome throttling timers and also iOS exiting the application when suspended. To get this library working nicely in an Ionic setting add this script to the index.html file:

<script src="assets/js/hack-timer.min.js"></script>

Next, update the config.xml file as follows:

<platform name="ios">
    // other stuff
    <config-file parent="UIApplicationExitsOnSuspend" target="*-Info.plist">
        <true />
    </config-file>
</platform>

Using the value of true for that property name seemed a bit counter-intuitive to keep the application running but that works. After these changes I was able to get back to capturing motion and orientation data at 50hz even with the phone locked.

Thanks Doruk Eker.

(P.S the reason for not using the cordova gyroscope and motion plugins is due to the official docs deprecation notice stating with the W3C Device Motion and Orientation API now being supported on iOS, Android and Windows devices, the plugins are no longer needed)

https://www.npmjs.com/package/cordova-plugin-device-motion