InfiniTime.git

commit 9e128c838e8447049264ac4e9e6c882053ed4229

Author: Isaac <114504394+ic-27@users.noreply.github.com>

ShakeWake: Fixed instant wake after sleep issue in certain positions (#1691)

* ShakeWake: Fixed instant wake after sleep issue in certain positions

Add lastX var to track the previous x acceleration for correct calculation of speed.

Reorder axes for clarity.

---------

Co-authored-by: Isaac <114504394+isaacc27@users.noreply.github.com>
Co-authored-by: FintasticMan <52415484+FintasticMan@users.noreply.github.com>

 src/components/motion/MotionController.cpp | 3 ++-
 src/components/motion/MotionController.h | 1 +


diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp
index 9d16e00d2e4adf7eea606801b2588b544746c22e..ef3cf811889e95aafa6f5bd4f1e344301cdf2803 100644
--- a/src/components/motion/MotionController.cpp
+++ b/src/components/motion/MotionController.cpp
@@ -16,6 +16,7 @@
   lastTime = time;
   time = xTaskGetTickCount();
 
+  lastX = this->x;
   this->x = x;
   lastY = this->y;
   this->y = y;
@@ -53,7 +54,7 @@ }
 
 bool MotionController::ShouldShakeWake(uint16_t thresh) {
   /* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */
-  int32_t speed = std::abs(z + (y / 2) + (x / 4) - lastY / 2 - lastZ) / (time - lastTime) * 100;
+  int32_t speed = std::abs(z - lastZ + (y / 2) - (lastY / 2) + (x / 4) - (lastX / 4)) / (time - lastTime) * 100;
   //(.2 * speed) + ((1 - .2) * accumulatedSpeed);
   // implemented without floats as .25Alpha
   accumulatedSpeed = (speed / 5) + ((accumulatedSpeed / 5) * 4);




diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h
index 87dbcd1f03722d1fe1a4858d1edd46478d8e758d..c524fef33fd4757f8316a0fa0887734802295ded 100644
--- a/src/components/motion/MotionController.h
+++ b/src/components/motion/MotionController.h
@@ -67,6 +67,7 @@
       TickType_t lastTime = 0;
       TickType_t time = 0;
 
+      int16_t lastX = 0;
       int16_t x = 0;
       int16_t lastYForRaiseWake = 0;
       int16_t lastY = 0;