@@ -40,15 +40,15 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
40
40
service->OnNewStepCountValue (nbSteps);
41
41
}
42
42
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)) {
44
44
service->OnNewMotionValues (x, y, z);
45
45
}
46
46
47
47
lastTime = time ;
48
48
time = xTaskGetTickCount ();
49
49
50
- lastX = this -> x ;
51
- this -> x = x;
50
+ xHistory++ ;
51
+ xHistory[ 0 ] = x;
52
52
yHistory++;
53
53
yHistory[0 ] = y;
54
54
zHistory++;
@@ -67,20 +67,26 @@ MotionController::AccelStats MotionController::GetAccelStats() const {
67
67
AccelStats stats;
68
68
69
69
for (uint8_t i = 0 ; i < AccelStats::numHistory; i++) {
70
+ stats.xMean += xHistory[histSize - i];
70
71
stats.yMean += yHistory[histSize - i];
71
72
stats.zMean += zHistory[histSize - i];
73
+ stats.prevXMean += xHistory[1 + i];
72
74
stats.prevYMean += yHistory[1 + i];
73
75
stats.prevZMean += zHistory[1 + i];
74
76
}
77
+ stats.xMean /= AccelStats::numHistory;
75
78
stats.yMean /= AccelStats::numHistory;
76
79
stats.zMean /= AccelStats::numHistory;
80
+ stats.prevXMean /= AccelStats::numHistory;
77
81
stats.prevYMean /= AccelStats::numHistory;
78
82
stats.prevZMean /= AccelStats::numHistory;
79
83
80
84
for (uint8_t i = 0 ; i < AccelStats::numHistory; i++) {
85
+ stats.xVariance += (xHistory[histSize - i] - stats.xMean ) * (xHistory[histSize - i] - stats.xMean );
81
86
stats.yVariance += (yHistory[histSize - i] - stats.yMean ) * (yHistory[histSize - i] - stats.yMean );
82
87
stats.zVariance += (zHistory[histSize - i] - stats.zMean ) * (zHistory[histSize - i] - stats.zMean );
83
88
}
89
+ stats.xVariance /= AccelStats::numHistory;
84
90
stats.yVariance /= AccelStats::numHistory;
85
91
stats.zVariance /= AccelStats::numHistory;
86
92
@@ -93,7 +99,7 @@ bool MotionController::ShouldRaiseWake() const {
93
99
constexpr int16_t yThresh = -64 ;
94
100
constexpr int16_t rollDegreesThresh = -45 ;
95
101
96
- if (x < -xThresh || x > xThresh) {
102
+ if (std::abs (stats. xMean ) > xThresh) {
97
103
return false ;
98
104
}
99
105
@@ -107,15 +113,21 @@ bool MotionController::ShouldRaiseWake() const {
107
113
108
114
bool MotionController::ShouldShakeWake (uint16_t thresh) {
109
115
/* 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);
112
119
// (.2 * speed) + ((1 - .2) * accumulatedSpeed);
113
120
accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5 ;
114
121
115
122
return accumulatedSpeed > thresh;
116
123
}
117
124
118
125
bool 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
+
119
131
if (stats.yMean < 724 || DegreesRolled (stats.yMean , stats.zMean , stats.prevYMean , stats.prevZMean ) < 30 ) {
120
132
return false ;
121
133
}
0 commit comments