1
0
Fork 0

shakewake: Switch to more generic last* vars

These could be used for other motion-based algorithms in the future.
Also fix includes.
This commit is contained in:
Finlay Davidson 2023-03-05 14:40:01 +01:00 committed by Riku Isokoski
parent f993311830
commit 6cf6455313
2 changed files with 17 additions and 12 deletions

View file

@ -1,5 +1,8 @@
#include "components/motion/MotionController.h" #include "components/motion/MotionController.h"
#include "os/os_cputime.h"
#include <FreeRTOS.h>
#include <task.h>
using namespace Pinetime::Controllers; using namespace Pinetime::Controllers;
void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) { 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)
} }
this->x = x; this->x = x;
lastY = this->y;
this->y = y; this->y = y;
lastZ = this->z;
this->z = z; this->z = z;
int32_t deltaSteps = nbSteps - this->nbSteps; int32_t deltaSteps = nbSteps - this->nbSteps;
this->nbSteps = nbSteps; this->nbSteps = nbSteps;
if (deltaSteps > 0) { if (deltaSteps > 0) {
@ -48,7 +54,7 @@ bool MotionController::ShouldShakeWake(uint16_t thresh) {
auto diff = xTaskGetTickCount() - lastShakeTime; auto diff = xTaskGetTickCount() - lastShakeTime;
lastShakeTime = xTaskGetTickCount(); lastShakeTime = xTaskGetTickCount();
/* 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 + (y / 2) + (x / 4) - lastYForShake - lastZForShake) / diff * 100; int32_t speed = std::abs(z + (y / 2) + (x / 4) - lastY / 2 - lastZ) / diff * 100;
//(.2 * speed) + ((1 - .2) * accumulatedSpeed); //(.2 * speed) + ((1 - .2) * accumulatedSpeed);
// implemented without floats as .25Alpha // implemented without floats as .25Alpha
accumulatedSpeed = (speed / 5) + ((accumulatedSpeed / 5) * 4); accumulatedSpeed = (speed / 5) + ((accumulatedSpeed / 5) * 4);
@ -56,9 +62,6 @@ bool MotionController::ShouldShakeWake(uint16_t thresh) {
if (accumulatedSpeed > thresh) { if (accumulatedSpeed > thresh) {
wake = true; wake = true;
} }
lastXForShake = x / 4;
lastYForShake = y / 2;
lastZForShake = z;
return wake; return wake;
} }

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <drivers/Bma421.h>
#include <components/ble/MotionService.h> #include "drivers/Bma421.h"
#include "components/ble/MotionService.h"
namespace Pinetime { namespace Pinetime {
namespace Controllers { namespace Controllers {
@ -63,17 +64,18 @@ namespace Pinetime {
private: private:
uint32_t nbSteps; uint32_t nbSteps;
uint32_t currentTripSteps = 0; uint32_t currentTripSteps = 0;
int16_t x; int16_t x;
int16_t y;
int16_t z;
int16_t lastYForWakeUp = 0; int16_t lastYForWakeUp = 0;
int16_t lastY = 0;
int16_t y;
int16_t lastZ = 0;
int16_t z;
bool isSensorOk = false; bool isSensorOk = false;
DeviceTypes deviceType = DeviceTypes::Unknown; DeviceTypes deviceType = DeviceTypes::Unknown;
Pinetime::Controllers::MotionService* service = nullptr; Pinetime::Controllers::MotionService* service = nullptr;
int16_t lastXForShake = 0;
int16_t lastYForShake = 0;
int16_t lastZForShake = 0;
int32_t accumulatedSpeed = 0; int32_t accumulatedSpeed = 0;
uint32_t lastShakeTime = 0; uint32_t lastShakeTime = 0;
}; };