motioncontroller: Store acceleration history

Store history of acceleration values for the y and z axes.
This commit is contained in:
Finlay Davidson 2023-06-25 15:55:24 +02:00 committed by JF
parent 47ca403857
commit 3085bb3990
2 changed files with 18 additions and 17 deletions

View file

@ -9,7 +9,7 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
service->OnNewStepCountValue(nbSteps); service->OnNewStepCountValue(nbSteps);
} }
if (service != nullptr && (this->x != x || this->y != y || this->z != z)) { if (service != nullptr && (this->x != x || yHistory[0] != y || zHistory[0] != z)) {
service->OnNewMotionValues(x, y, z); service->OnNewMotionValues(x, y, z);
} }
@ -18,10 +18,10 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
lastX = this->x; lastX = this->x;
this->x = x; this->x = x;
lastY = this->y; yHistory++;
this->y = y; yHistory[0] = y;
lastZ = this->z; zHistory++;
this->z = z; zHistory[0] = z;
int32_t deltaSteps = nbSteps - this->nbSteps; int32_t deltaSteps = nbSteps - this->nbSteps;
if (deltaSteps > 0) { if (deltaSteps > 0) {
@ -31,21 +31,21 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
} }
bool MotionController::ShouldRaiseWake(bool isSleeping) { bool MotionController::ShouldRaiseWake(bool isSleeping) {
if ((x + 335) <= 670 && z < 0) { if ((x + 335) <= 670 && zHistory[0] < 0) {
if (!isSleeping) { if (!isSleeping) {
if (y <= 0) { if (yHistory[0] <= 0) {
return false; return false;
} }
lastYForRaiseWake = 0; lastYForRaiseWake = 0;
return false; return false;
} }
if (y >= 0) { if (yHistory[0] >= 0) {
lastYForRaiseWake = 0; lastYForRaiseWake = 0;
return false; return false;
} }
if (y + 230 < lastYForRaiseWake) { if (yHistory[0] + 230 < lastYForRaiseWake) {
lastYForRaiseWake = y; lastYForRaiseWake = yHistory[0];
return true; return true;
} }
} }
@ -54,7 +54,8 @@ bool MotionController::ShouldRaiseWake(bool isSleeping) {
bool MotionController::ShouldShakeWake(uint16_t thresh) { bool MotionController::ShouldShakeWake(uint16_t thresh) {
/* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */ /* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */
int32_t speed = std::abs(z - lastZ + (y - lastY) / 2 + (x - lastX) / 4) * 100 / (time - lastTime); int32_t speed =
std::abs(zHistory[0] - zHistory[histSize - 1] + (yHistory[0] - yHistory[histSize - 1]) / 2 + (x - lastX) / 4) * 100 / (time - lastTime);
// (.2 * speed) + ((1 - .2) * accumulatedSpeed); // (.2 * speed) + ((1 - .2) * accumulatedSpeed);
accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5; accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5;

View file

@ -6,6 +6,7 @@
#include "drivers/Bma421.h" #include "drivers/Bma421.h"
#include "components/ble/MotionService.h" #include "components/ble/MotionService.h"
#include "utility/CircularBuffer.h"
namespace Pinetime { namespace Pinetime {
namespace Controllers { namespace Controllers {
@ -24,11 +25,11 @@ namespace Pinetime {
} }
int16_t Y() const { int16_t Y() const {
return y; return yHistory[0];
} }
int16_t Z() const { int16_t Z() const {
return z; return zHistory[0];
} }
uint32_t NbSteps() const { uint32_t NbSteps() const {
@ -70,10 +71,9 @@ namespace Pinetime {
int16_t lastX = 0; int16_t lastX = 0;
int16_t x = 0; int16_t x = 0;
int16_t lastYForRaiseWake = 0; int16_t lastYForRaiseWake = 0;
int16_t lastY = 0; static constexpr uint8_t histSize = 8;
int16_t y = 0; Utility::CircularBuffer<int16_t, histSize> yHistory = {};
int16_t lastZ = 0; Utility::CircularBuffer<int16_t, histSize> zHistory = {};
int16_t z = 0;
int32_t accumulatedSpeed = 0; int32_t accumulatedSpeed = 0;
DeviceTypes deviceType = DeviceTypes::Unknown; DeviceTypes deviceType = DeviceTypes::Unknown;