0

Good day all,

I am trying to capture the values of accelerationIncludingGravity in javascript just before it stops generating any more, that is, when the device is stationary. I know that there is an event called motionEnded generated in Xcode to capture this, but I would like to perform the same thing using javascript to make it device independent.

Thanks in advance for the effort.

Here is the full HTML code ... Most of it is for measuring the last DeviceMotion event...

<script type="text/javascript">

var x = 0, y = 0, vx = 0, vy = 0, ax = 0, ay = 0, accX = 0, accY = 0, accZ = 0, readingStarted = 0, readingCompleted = 0, buttonPress = 0, test = 0, toleranceCounter = 0;

function throttle (callback, limit) {
    var wait = false;                  // Initially, we're not waiting
    return function () {               // We return a throttled function
        if (!wait) {                   // If we're not waiting
            callback.call();           // Execute users function
            wait = true;               // Prevent future invocations
            setTimeout(function () {   // After a period of time
                wait = false;          // And allow future invocations
            }, limit);
      }
   }
}

function myFunction() {
    if(buttonPress == 0)
    {
        readingStarted = 0;
        readingCompleted = 0;
        toleranceCounter = 0;
        test = 0;
        window.addEventListener('devicemotion', throttle (handleMotionEvent, 200), false);  
    }
    if  ((buttonPress < 6)  && ((readingStarted == 0) || (readingCompleted ==1)))
    {
        readingStarted = 0;
        readingCompleted = 0;
        toleranceCounter = 0;
        buttonPress = buttonPress + 1;
    }
    document.getElementById("buttonp").innerHTML = buttonPress;
    document.getElementById("test").innerHTML = test;
}

function handleMotionEvent(e)
{
        test = test + 1;
        if (readingCompleted == 1)
        {
            accX = (accX + e.accelerationIncludingGravity.x) / 2;
            accY = (accY + e.accelerationIncludingGravity.y) / 2;
            accZ = (accZ + e.accelerationIncludingGravity.z) / 2;
            passToPHP2(accX, accY, accZ);
        }
        else if (  (readingStarted == 0) && (readingCompleted == 0) &&
               (((accX - e.accelerationIncludingGravity.x) > 0.1) || ((accX - e.accelerationIncludingGravity.x) < -0.1) ||
               ((e.accelerationIncludingGravity.x - accX) > 0.1) || ((e.accelerationIncludingGravity.x - accX) < -0.1) ||
              ((accY - e.accelerationIncludingGravity.y) > 0.1) || ((accY - e.accelerationIncludingGravity.y) < -0.1) ||
               ((e.accelerationIncludingGravity.y - accY) > 0.1) || ((e.accelerationIncludingGravity.y - accY) < -0.1) ||
              ((accZ - e.accelerationIncludingGravity.z) > 0.1) || ((accZ - e.accelerationIncludingGravity.z) < -0.1) ||
               ((e.accelerationIncludingGravity.z - accZ) > 0.1) || ((e.accelerationIncludingGravity.z - accZ) < -0.1))
          )
        {
            accX =  e.accelerationIncludingGravity.x;
            accY =  e.accelerationIncludingGravity.y;
            accZ =  e.accelerationIncludingGravity.z;
            readingStarted = 1;

            if ( e.rotationRate ) {
                document.getElementById("rotationAlpha").innerHTML = e.rotationRate.alpha;
                document.getElementById("rotationBeta").innerHTML = e.rotationRate.beta;
                document.getElementById("rotationGamma").innerHTML = e.rotationRate.gamma;
            }       
        }
        else if (  (readingStarted == 1) && (readingCompleted == 0) && (toleranceCounter < 5) &&  
               !(((accX - e.accelerationIncludingGravity.x) > 0.1) || ((accX - e.accelerationIncludingGravity.x) < -0.1) ||
               ((e.accelerationIncludingGravity.x - accX) > 0.1) || ((e.accelerationIncludingGravity.x - accX) < -0.1) ||
              ((accY - e.accelerationIncludingGravity.y) > 0.1) || ((accY - e.accelerationIncludingGravity.y) < -0.1) ||
               ((e.accelerationIncludingGravity.y - accY) > 0.1) || ((e.accelerationIncludingGravity.y - accY) < -0.1) ||
              ((accZ - e.accelerationIncludingGravity.z) > 0.1) || ((accZ - e.accelerationIncludingGravity.z) < -0.1) ||
               ((e.accelerationIncludingGravity.z - accZ) > 0.1) || ((e.accelerationIncludingGravity.z - accZ) < -0.1))
          )
        {
            accX = (accX + e.accelerationIncludingGravity.x) / 2;
            accY = (accY + e.accelerationIncludingGravity.y) / 2;
            accZ = (accZ + e.accelerationIncludingGravity.z) / 2;
            toleranceCounter = toleranceCounter + 1;
        }
        else if (  (readingStarted == 1) && (readingCompleted == 0) && (toleranceCounter == 5) &&    
               !(((accX - e.accelerationIncludingGravity.x) > 0.1) || ((accX - e.accelerationIncludingGravity.x) < -0.1) ||
               ((e.accelerationIncludingGravity.x - accX) > 0.1) || ((e.accelerationIncludingGravity.x - accX) < -0.1) ||
              ((accY - e.accelerationIncludingGravity.y) > 0.1) || ((accY - e.accelerationIncludingGravity.y) < -0.1) ||
               ((e.accelerationIncludingGravity.y - accY) > 0.1) || ((e.accelerationIncludingGravity.y - accY) < -0.1) ||
              ((accZ - e.accelerationIncludingGravity.z) > 0.1) || ((accZ - e.accelerationIncludingGravity.z) < -0.1) ||
               ((e.accelerationIncludingGravity.z - accZ) > 0.1) || ((e.accelerationIncludingGravity.z - accZ) < -0.1))
          )
        {
            accX = (accX + e.accelerationIncludingGravity.x) / 2;
            accY = (accY + e.accelerationIncludingGravity.y) / 2;
            accZ = (accZ + e.accelerationIncludingGravity.z) / 2;
            passToPHP(accX, accY, accZ);
            readingCompleted = 1;
        }
        else if (  (readingStarted == 1) && (readingCompleted == 0) &&
               (((accX - e.accelerationIncludingGravity.x) > 0.1) || ((accX - e.accelerationIncludingGravity.x) < -0.1) ||
               ((e.accelerationIncludingGravity.x - accX) > 0.1) || ((e.accelerationIncludingGravity.x - accX) < -0.1) ||
              ((accY - e.accelerationIncludingGravity.y) > 0.1) || ((accY - e.accelerationIncludingGravity.y) < -0.1) ||
               ((e.accelerationIncludingGravity.y - accY) > 0.1) || ((e.accelerationIncludingGravity.y - accY) < -0.1) ||
              ((accZ - e.accelerationIncludingGravity.z) > 0.1) || ((accZ - e.accelerationIncludingGravity.z) < -0.1) ||
               ((e.accelerationIncludingGravity.z - accZ) > 0.1) || ((e.accelerationIncludingGravity.z - accZ) < -0.1))
          )
        {
            accX = (accX + e.accelerationIncludingGravity.x) / 2;
            accY = (accY + e.accelerationIncludingGravity.y) / 2;
            accZ = (accZ + e.accelerationIncludingGravity.z) / 2;
            toleranceCounter = 0;
        }
        document.getElementById("accelerationX").innerHTML = accX;
        document.getElementById("accelerationY").innerHTML = accY;
        document.getElementById("accelerationZ").innerHTML = accZ;
}

function passToPHP(x, y, z)
{
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "http://localhost/acc.php?accX=" + x + "&accY=" + y + "&accZ=" + z, true);
  xhttp.send();
}

function passToPHP2(x, y, z)
{
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "http://localhost/acc2.php?accX=" + x + "&accY=" + y + "&accZ=" + z, true);
  xhttp.send();
}

</script>
sb_ykz61m
  • 1
  • 4
  • Welcome to Stackoverflow! Can you please elaborate your question having your effort like code or something so that people could get your problem early and help you? Thanks! – Enamul Hassan Mar 29 '16 at 00:54
  • Create a function then run it on every Event you want, in this case it sounds like all the Events. – StackSlave Mar 29 '16 at 01:03
  • There are too many DeviceMotion events generated and it becomes difficult for a code that wants to capture only the last DeviceMotion event after the device becomes stationary. The catch is also to identify the DeviceMotion event after which there is no other DeviceMotion event till the device moves again. – sb_ykz61m Mar 29 '16 at 10:17
  • Thanks manetsus for suggesting to include the code.. My code is little long and cannot be accommodated in the small number of characters ... How do I attach the full code? – sb_ykz61m Mar 31 '16 at 00:34

0 Answers0