2
2
3
3
#include < task.h>
4
4
5
+ #include " utility/Math.h"
6
+
5
7
using namespace Pinetime ::Controllers;
6
8
9
+ namespace {
10
+ constexpr inline int32_t Clamp (int32_t val, int32_t min, int32_t max) {
11
+ return val < min ? min : (val > max ? max : val);
12
+ }
13
+
14
+ // only returns meaningful values if inputs are acceleration due to gravity
15
+ int16_t DegreesRolled (int16_t y, int16_t z, int16_t prevY, int16_t prevZ) {
16
+ int16_t prevYAngle = Pinetime::Utility::Asin (Clamp (prevY * 32 , -32767 , 32767 ));
17
+ int16_t yAngle = Pinetime::Utility::Asin (Clamp (y * 32 , -32767 , 32767 ));
18
+
19
+ if (z < 0 && prevZ < 0 ) {
20
+ return yAngle - prevYAngle;
21
+ }
22
+ if (prevZ < 0 ) {
23
+ if (y < 0 ) {
24
+ return -prevYAngle - yAngle - 180 ;
25
+ }
26
+ return -prevYAngle - yAngle + 180 ;
27
+ }
28
+ if (z < 0 ) {
29
+ if (y < 0 ) {
30
+ return prevYAngle + yAngle + 180 ;
31
+ }
32
+ return prevYAngle + yAngle - 180 ;
33
+ }
34
+ return prevYAngle - yAngle;
35
+ }
36
+ }
37
+
7
38
void MotionController::Update (int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
8
39
if (this ->nbSteps != nbSteps && service != nullptr ) {
9
40
service->OnNewStepCountValue (nbSteps);
@@ -23,13 +54,39 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
23
54
zHistory++;
24
55
zHistory[0 ] = z;
25
56
57
+ stats = GetAccelStats ();
58
+
26
59
int32_t deltaSteps = nbSteps - this ->nbSteps ;
27
60
if (deltaSteps > 0 ) {
28
61
currentTripSteps += deltaSteps;
29
62
}
30
63
this ->nbSteps = nbSteps;
31
64
}
32
65
66
+ MotionController::AccelStats MotionController::GetAccelStats () const {
67
+ AccelStats stats;
68
+
69
+ for (uint8_t i = 0 ; i < AccelStats::numHistory; i++) {
70
+ stats.yMean += yHistory[histSize - i];
71
+ stats.zMean += zHistory[histSize - i];
72
+ stats.prevYMean += yHistory[1 + i];
73
+ stats.prevZMean += zHistory[1 + i];
74
+ }
75
+ stats.yMean /= AccelStats::numHistory;
76
+ stats.zMean /= AccelStats::numHistory;
77
+ stats.prevYMean /= AccelStats::numHistory;
78
+ stats.prevZMean /= AccelStats::numHistory;
79
+
80
+ for (uint8_t i = 0 ; i < AccelStats::numHistory; i++) {
81
+ stats.yVariance += (yHistory[histSize - i] - stats.yMean ) * (yHistory[histSize - i] - stats.yMean );
82
+ stats.zVariance += (zHistory[histSize - i] - stats.zMean ) * (zHistory[histSize - i] - stats.zMean );
83
+ }
84
+ stats.yVariance /= AccelStats::numHistory;
85
+ stats.zVariance /= AccelStats::numHistory;
86
+
87
+ return stats;
88
+ }
89
+
33
90
bool MotionController::ShouldRaiseWake (bool isSleeping) {
34
91
if ((x + 335 ) <= 670 && zHistory[0 ] < 0 ) {
35
92
if (!isSleeping) {
0 commit comments