Skip to content

Commit 684f28b

Browse files
committed
lowersleep: Improve algorithm by checking wrist angle
Inspired by #827 (comment).
1 parent c2c53bc commit 684f28b

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/components/motion/MotionController.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -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,20 @@ bool MotionController::ShouldRaiseWake() const {
107113

108114
bool 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

118125
bool MotionController::ShouldLowerSleep() const {
126+
if (std::abs(stats.xMean) > 887 && std::abs(DegreesRolled(stats.xMean, stats.zMean, stats.prevXMean, stats.prevZMean)) > 30) {
127+
return true;
128+
}
129+
119130
if (stats.yMean < 724 || DegreesRolled(stats.yMean, stats.zMean, stats.prevYMean, stats.prevZMean) < 30) {
120131
return false;
121132
}

src/components/motion/MotionController.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Pinetime {
2121
void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps);
2222

2323
int16_t X() const {
24-
return x;
24+
return xHistory[0];
2525
}
2626

2727
int16_t Y() const {
@@ -76,11 +76,14 @@ namespace Pinetime {
7676
struct AccelStats {
7777
static constexpr uint8_t numHistory = 2;
7878

79+
int16_t xMean = 0;
7980
int16_t yMean = 0;
8081
int16_t zMean = 0;
82+
int16_t prevXMean = 0;
8183
int16_t prevYMean = 0;
8284
int16_t prevZMean = 0;
8385

86+
uint32_t xVariance = 0;
8487
uint32_t yVariance = 0;
8588
uint32_t zVariance = 0;
8689
};
@@ -89,9 +92,8 @@ namespace Pinetime {
8992

9093
AccelStats stats = {};
9194

92-
int16_t lastX = 0;
93-
int16_t x = 0;
9495
static constexpr uint8_t histSize = 8;
96+
Utility::CircularBuffer<int16_t, histSize> xHistory = {};
9597
Utility::CircularBuffer<int16_t, histSize> yHistory = {};
9698
Utility::CircularBuffer<int16_t, histSize> zHistory = {};
9799
int32_t accumulatedSpeed = 0;

0 commit comments

Comments
 (0)