From e9e78db25b086d464beac2bfd61beb21c6f4200c Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:04:52 +0100 Subject: [PATCH 1/2] Unconditionally update motion --- src/systemtask/SystemTask.cpp | 21 ++++----------------- src/systemtask/SystemTask.h | 1 - 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 8e0435e372..0a38f03fbc 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -183,8 +183,6 @@ void SystemTask::Work() { #pragma clang diagnostic push #pragma ide diagnostic ignored "EndlessLoop" while (true) { - UpdateMotion(); - Messages msg; if (xQueueReceive(systemTasksMsgQueue, &msg, 100) == pdTRUE) { switch (msg) { @@ -316,9 +314,7 @@ void SystemTask::Work() { } break; case Messages::OnNewDay: - // We might be sleeping (with TWI device disabled. - // Remember we'll have to reset the counter next time we're awake - stepCounterMustBeReset = true; + motionSensor.ResetStepCounter(); break; case Messages::OnNewHour: using Pinetime::Controllers::AlarmController; @@ -362,6 +358,7 @@ void SystemTask::Work() { } } + UpdateMotion(); if (isBleDiscoveryTimerRunning) { if (bleDiscoveryTimer == 0) { isBleDiscoveryTimerRunning = false; @@ -429,18 +426,8 @@ void SystemTask::GoToSleep() { }; void SystemTask::UpdateMotion() { - // Only consider disabling motion updates specifically in the Sleeping state - // AOD needs motion on to show up to date step counts - if (state == SystemTaskState::Sleeping && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) || - settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake) || - motionController.GetService()->IsMotionNotificationSubscribed())) { - return; - } - - if (stepCounterMustBeReset) { - motionSensor.ResetStepCounter(); - stepCounterMustBeReset = false; - } + // Unconditionally update motion + // Reading steps/motion characteristics must return up to date information even when not subscribed to notifications auto motionValues = motionSensor.Process(); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 0060e36096..ca5c6d6c50 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -131,7 +131,6 @@ namespace Pinetime { void GoToRunning(); void GoToSleep(); void UpdateMotion(); - bool stepCounterMustBeReset = false; static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000); SystemMonitor monitor; From 614440f51dca5634a53ceb92d0a51d13f139b89d Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Sat, 28 Jun 2025 20:07:25 +0100 Subject: [PATCH 2/2] Unconditionally calculate shake speed --- src/components/motion/MotionController.cpp | 19 ++++++++----------- src/components/motion/MotionController.h | 1 - src/systemtask/SystemTask.cpp | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 72507ac5cd..6cfff61f9d 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -54,6 +54,14 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) zHistory++; zHistory[0] = z; + // Update accumulated speed + // Currently polling at 10Hz, if this ever goes faster scalar and EMA might need adjusting + int32_t speed = std::abs(zHistory[0] - zHistory[histSize - 1] + ((yHistory[0] - yHistory[histSize - 1]) / 2) + + ((xHistory[0] - xHistory[histSize - 1]) / 4)) * + 100 / (time - lastTime); + // integer version of (.2 * speed) + ((1 - .2) * accumulatedSpeed); + accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5; + stats = GetAccelStats(); int32_t deltaSteps = nbSteps - this->nbSteps; @@ -111,17 +119,6 @@ bool MotionController::ShouldRaiseWake() const { return DegreesRolled(stats.yMean, stats.zMean, stats.prevYMean, stats.prevZMean) < rollDegreesThresh; } -bool MotionController::ShouldShakeWake(uint16_t thresh) { - /* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */ - int32_t speed = std::abs(zHistory[0] - zHistory[histSize - 1] + (yHistory[0] - yHistory[histSize - 1]) / 2 + - (xHistory[0] - xHistory[histSize - 1]) / 4) * - 100 / (time - lastTime); - // (.2 * speed) + ((1 - .2) * accumulatedSpeed); - accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5; - - return accumulatedSpeed > thresh; -} - bool MotionController::ShouldLowerSleep() const { if ((stats.xMean > 887 && DegreesRolled(stats.xMean, stats.zMean, stats.prevXMean, stats.prevZMean) > 30) || (stats.xMean < -887 && DegreesRolled(stats.xMean, stats.zMean, stats.prevXMean, stats.prevZMean) < -30)) { diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index be0241d32e..ad95f31ffe 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -44,7 +44,6 @@ namespace Pinetime { return currentTripSteps; } - bool ShouldShakeWake(uint16_t thresh); bool ShouldRaiseWake() const; bool ShouldLowerSleep() const; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 0a38f03fbc..8c979f399b 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -437,7 +437,7 @@ void SystemTask::UpdateMotion() { if ((settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) && motionController.ShouldRaiseWake()) || (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake) && - motionController.ShouldShakeWake(settingsController.GetShakeThreshold()))) { + motionController.CurrentShakeSpeed() > settingsController.GetShakeThreshold())) { GoToRunning(); } }