Skip to content

Commit 76e79df

Browse files
FintasticManRiksu9000
authored andcommitted
shakewake: Switch to more generic timekeeping
Could be used for other motion-based algorithms in the future.
1 parent a434637 commit 76e79df

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

src/components/motion/MotionController.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "components/motion/MotionController.h"
22

3-
#include <FreeRTOS.h>
43
#include <task.h>
54

65
using namespace Pinetime::Controllers;
@@ -14,6 +13,9 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
1413
service->OnNewMotionValues(x, y, z);
1514
}
1615

16+
lastTime = time;
17+
time = xTaskGetTickCount();
18+
1719
this->x = x;
1820
lastY = this->y;
1921
this->y = y;
@@ -50,10 +52,8 @@ bool MotionController::Should_RaiseWake(bool isSleeping) {
5052
}
5153

5254
bool MotionController::ShouldShakeWake(uint16_t thresh) {
53-
auto diff = xTaskGetTickCount() - lastShakeTime;
54-
lastShakeTime = xTaskGetTickCount();
5555
/* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */
56-
int32_t speed = std::abs(z + (y / 2) + (x / 4) - lastY / 2 - lastZ) / diff * 100;
56+
int32_t speed = std::abs(z + (y / 2) + (x / 4) - lastY / 2 - lastZ) / (time - lastTime) * 100;
5757
//(.2 * speed) + ((1 - .2) * accumulatedSpeed);
5858
// implemented without floats as .25Alpha
5959
accumulatedSpeed = (speed / 5) + ((accumulatedSpeed / 5) * 4);

src/components/motion/MotionController.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <cstdint>
44

5+
#include <FreeRTOS.h>
6+
57
#include "drivers/Bma421.h"
68
#include "components/ble/MotionService.h"
79

@@ -65,19 +67,20 @@ namespace Pinetime {
6567
uint32_t nbSteps;
6668
uint32_t currentTripSteps = 0;
6769

70+
TickType_t lastTime = 0;
71+
TickType_t time = 0;
72+
6873
int16_t x;
6974
int16_t lastYForWakeUp = 0;
7075
int16_t lastY = 0;
7176
int16_t y;
7277
int16_t lastZ = 0;
7378
int16_t z;
79+
int32_t accumulatedSpeed = 0;
7480

7581
bool isSensorOk = false;
7682
DeviceTypes deviceType = DeviceTypes::Unknown;
7783
Pinetime::Controllers::MotionService* service = nullptr;
78-
79-
int32_t accumulatedSpeed = 0;
80-
uint32_t lastShakeTime = 0;
8184
};
8285
}
8386
}

0 commit comments

Comments
 (0)