Skip to content

Commit 6cf6455

Browse files
FintasticManRiksu9000
authored andcommitted
shakewake: Switch to more generic last* vars
These could be used for other motion-based algorithms in the future. Also fix includes.
1 parent f993311 commit 6cf6455

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/components/motion/MotionController.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "components/motion/MotionController.h"
2-
#include "os/os_cputime.h"
2+
3+
#include <FreeRTOS.h>
4+
#include <task.h>
5+
36
using namespace Pinetime::Controllers;
47

58
void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
@@ -12,8 +15,11 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
1215
}
1316

1417
this->x = x;
18+
lastY = this->y;
1519
this->y = y;
20+
lastZ = this->z;
1621
this->z = z;
22+
1723
int32_t deltaSteps = nbSteps - this->nbSteps;
1824
this->nbSteps = nbSteps;
1925
if (deltaSteps > 0) {
@@ -48,17 +54,14 @@ bool MotionController::ShouldShakeWake(uint16_t thresh) {
4854
auto diff = xTaskGetTickCount() - lastShakeTime;
4955
lastShakeTime = xTaskGetTickCount();
5056
/* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */
51-
int32_t speed = std::abs(z + (y / 2) + (x / 4) - lastYForShake - lastZForShake) / diff * 100;
57+
int32_t speed = std::abs(z + (y / 2) + (x / 4) - lastY / 2 - lastZ) / diff * 100;
5258
//(.2 * speed) + ((1 - .2) * accumulatedSpeed);
5359
// implemented without floats as .25Alpha
5460
accumulatedSpeed = (speed / 5) + ((accumulatedSpeed / 5) * 4);
5561

5662
if (accumulatedSpeed > thresh) {
5763
wake = true;
5864
}
59-
lastXForShake = x / 4;
60-
lastYForShake = y / 2;
61-
lastZForShake = z;
6265
return wake;
6366
}
6467

src/components/motion/MotionController.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#pragma once
22

33
#include <cstdint>
4-
#include <drivers/Bma421.h>
5-
#include <components/ble/MotionService.h>
4+
5+
#include "drivers/Bma421.h"
6+
#include "components/ble/MotionService.h"
67

78
namespace Pinetime {
89
namespace Controllers {
@@ -63,17 +64,18 @@ namespace Pinetime {
6364
private:
6465
uint32_t nbSteps;
6566
uint32_t currentTripSteps = 0;
67+
6668
int16_t x;
69+
int16_t lastYForWakeUp = 0;
70+
int16_t lastY = 0;
6771
int16_t y;
72+
int16_t lastZ = 0;
6873
int16_t z;
69-
int16_t lastYForWakeUp = 0;
74+
7075
bool isSensorOk = false;
7176
DeviceTypes deviceType = DeviceTypes::Unknown;
7277
Pinetime::Controllers::MotionService* service = nullptr;
7378

74-
int16_t lastXForShake = 0;
75-
int16_t lastYForShake = 0;
76-
int16_t lastZForShake = 0;
7779
int32_t accumulatedSpeed = 0;
7880
uint32_t lastShakeTime = 0;
7981
};

0 commit comments

Comments
 (0)