@@ -40,15 +40,15 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
4040 service->OnNewStepCountValue (nbSteps);
4141 }
4242
43- if (service != nullptr && (this -> x != x || yHistory[0 ] != y || zHistory[0 ] != z)) {
43+ if (service != nullptr && (xHistory[ 0 ] != x || yHistory[0 ] != y || zHistory[0 ] != z)) {
4444 service->OnNewMotionValues (x, y, z);
4545 }
4646
4747 lastTime = time;
4848 time = xTaskGetTickCount ();
4949
50- lastX = this -> x ;
51- this -> x = x;
50+ xHistory++ ;
51+ xHistory[ 0 ] = x;
5252 yHistory++;
5353 yHistory[0 ] = y;
5454 zHistory++;
@@ -67,20 +67,26 @@ MotionController::AccelStats MotionController::GetAccelStats() const {
6767 AccelStats stats;
6868
6969 for (uint8_t i = 0 ; i < AccelStats::numHistory; i++) {
70+ stats.xMean += xHistory[histSize - i];
7071 stats.yMean += yHistory[histSize - i];
7172 stats.zMean += zHistory[histSize - i];
73+ stats.prevXMean += xHistory[1 + i];
7274 stats.prevYMean += yHistory[1 + i];
7375 stats.prevZMean += zHistory[1 + i];
7476 }
77+ stats.xMean /= AccelStats::numHistory;
7578 stats.yMean /= AccelStats::numHistory;
7679 stats.zMean /= AccelStats::numHistory;
80+ stats.prevXMean /= AccelStats::numHistory;
7781 stats.prevYMean /= AccelStats::numHistory;
7882 stats.prevZMean /= AccelStats::numHistory;
7983
8084 for (uint8_t i = 0 ; i < AccelStats::numHistory; i++) {
85+ stats.xVariance += (xHistory[histSize - i] - stats.xMean ) * (xHistory[histSize - i] - stats.xMean );
8186 stats.yVariance += (yHistory[histSize - i] - stats.yMean ) * (yHistory[histSize - i] - stats.yMean );
8287 stats.zVariance += (zHistory[histSize - i] - stats.zMean ) * (zHistory[histSize - i] - stats.zMean );
8388 }
89+ stats.xVariance /= AccelStats::numHistory;
8490 stats.yVariance /= AccelStats::numHistory;
8591 stats.zVariance /= AccelStats::numHistory;
8692
@@ -93,7 +99,7 @@ bool MotionController::ShouldRaiseWake() const {
9399 constexpr int16_t yThresh = -64 ;
94100 constexpr int16_t rollDegreesThresh = -45 ;
95101
96- if (x < -xThresh || x > xThresh) {
102+ if (std::abs (stats. xMean ) > xThresh) {
97103 return false ;
98104 }
99105
@@ -107,15 +113,21 @@ bool MotionController::ShouldRaiseWake() const {
107113
108114bool MotionController::ShouldShakeWake (uint16_t thresh) {
109115 /* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */
110- int32_t speed =
111- std::abs (zHistory[0 ] - zHistory[histSize - 1 ] + (yHistory[0 ] - yHistory[histSize - 1 ]) / 2 + (x - lastX) / 4 ) * 100 / (time - lastTime);
116+ int32_t speed = std::abs (zHistory[0 ] - zHistory[histSize - 1 ] + (yHistory[0 ] - yHistory[histSize - 1 ]) / 2 +
117+ (xHistory[0 ] - xHistory[histSize - 1 ]) / 4 ) *
118+ 100 / (time - lastTime);
112119 // (.2 * speed) + ((1 - .2) * accumulatedSpeed);
113120 accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5 ;
114121
115122 return accumulatedSpeed > thresh;
116123}
117124
118125bool MotionController::ShouldLowerSleep () const {
126+ if ((stats.xMean > 887 && DegreesRolled (stats.xMean , stats.zMean , stats.prevXMean , stats.prevZMean ) > 30 ) ||
127+ (stats.xMean < -887 && DegreesRolled (stats.xMean , stats.zMean , stats.prevXMean , stats.prevZMean ) < -30 )) {
128+ return true ;
129+ }
130+
119131 if (stats.yMean < 724 || DegreesRolled (stats.yMean , stats.zMean , stats.prevYMean , stats.prevZMean ) < 30 ) {
120132 return false ;
121133 }
0 commit comments