Skip to content

Commit

Permalink
motioncontroller: Store history and add functions for analysis
Browse files Browse the repository at this point in the history
These are functions for converting acceleration due to gravity to angles
in degrees, and some statistical analysis including the mean and
variance.
  • Loading branch information
FintasticMan committed Jun 17, 2023
1 parent b6de2c9 commit 3fbe193
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ list(APPEND SOURCE_FILES

buttonhandler/ButtonHandler.cpp
touchhandler/TouchHandler.cpp

utility/Math.cpp
)

list(APPEND RECOVERY_SOURCE_FILES
Expand Down Expand Up @@ -559,6 +561,8 @@ list(APPEND RECOVERY_SOURCE_FILES
components/fs/FS.cpp
buttonhandler/ButtonHandler.cpp
touchhandler/TouchHandler.cpp

utility/Math.cpp
)

list(APPEND RECOVERYLOADER_SOURCE_FILES
Expand Down Expand Up @@ -678,6 +682,7 @@ set(INCLUDE_FILES
components/motor/MotorController.h
buttonhandler/ButtonHandler.h
touchhandler/TouchHandler.h
utility/Math.h
)

include_directories(
Expand Down
80 changes: 69 additions & 11 deletions src/components/motion/MotionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,45 @@

#include <task.h>

#include "utility/Math.h"

using namespace Pinetime::Controllers;

namespace {
constexpr inline int32_t Clamp(int32_t val, int32_t min, int32_t max) {
return val < min ? min : (val > max ? max : val);
}

// only returns meaningful values if inputs are acceleration due to gravity
int16_t DegreesRolled(int16_t y, int16_t z, int16_t prevY, int16_t prevZ) {
int16_t prevYAngle = Pinetime::Utility::Asin(Clamp(prevY * 32, -32767, 32767));
int16_t yAngle = Pinetime::Utility::Asin(Clamp(y * 32, -32767, 32767));

if (z < 0 && prevZ < 0) {
return yAngle - prevYAngle;
}
if (prevZ < 0) {
if (y < 0) {
return -prevYAngle - yAngle - 180;
}
return -prevYAngle - yAngle + 180;
}
if (z < 0) {
if (y < 0) {
return prevYAngle + yAngle + 180;
}
return prevYAngle + yAngle - 180;
}
return prevYAngle - yAngle;
}
}

void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
if (this->nbSteps != nbSteps && service != nullptr) {
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);
}

Expand All @@ -18,10 +49,12 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)

lastX = this->x;
this->x = x;
lastY = this->y;
this->y = y;
lastZ = this->z;
this->z = z;
yHistory++;
yHistory[0] = y;
zHistory++;
zHistory[0] = z;

stats = GetAccelStats();

int32_t deltaSteps = nbSteps - this->nbSteps;
if (deltaSteps > 0) {
Expand All @@ -30,22 +63,46 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
this->nbSteps = nbSteps;
}

MotionController::AccelStats MotionController::GetAccelStats() const {
AccelStats stats;

for (uint8_t i = 0; i < AccelStats::numHistory; i++) {
stats.yMean += yHistory[histSize - i];
stats.zMean += zHistory[histSize - i];
stats.prevYMean += yHistory[1 + i];
stats.prevZMean += zHistory[1 + i];
}
stats.yMean /= AccelStats::numHistory;
stats.zMean /= AccelStats::numHistory;
stats.prevYMean /= AccelStats::numHistory;
stats.prevZMean /= AccelStats::numHistory;

for (uint8_t i = 0; i < AccelStats::numHistory; i++) {
stats.yVariance += (yHistory[histSize - i] - stats.yMean) * (yHistory[histSize - i] - stats.yMean);
stats.zVariance += (zHistory[histSize - i] - stats.zMean) * (zHistory[histSize - i] - stats.zMean);
}
stats.yVariance /= AccelStats::numHistory;
stats.zVariance /= AccelStats::numHistory;

return stats;
}

bool MotionController::ShouldRaiseWake(bool isSleeping) {
if ((x + 335) <= 670 && z < 0) {
if ((x + 335) <= 670 && zHistory[0] < 0) {
if (!isSleeping) {
if (y <= 0) {
if (yHistory[0] <= 0) {
return false;
}
lastYForRaiseWake = 0;
return false;
}

if (y >= 0) {
if (yHistory[0] >= 0) {
lastYForRaiseWake = 0;
return false;
}
if (y + 230 < lastYForRaiseWake) {
lastYForRaiseWake = y;
if (yHistory[0] + 230 < lastYForRaiseWake) {
lastYForRaiseWake = yHistory[0];
return true;
}
}
Expand All @@ -54,7 +111,8 @@ bool MotionController::ShouldRaiseWake(bool isSleeping) {

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(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);
accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5;

Expand Down
30 changes: 23 additions & 7 deletions src/components/motion/MotionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "drivers/Bma421.h"
#include "components/ble/MotionService.h"
#include "utility/CircularBuffer.h"

namespace Pinetime {
namespace Controllers {
Expand All @@ -24,11 +25,11 @@ namespace Pinetime {
}

int16_t Y() const {
return y;
return yHistory[0];
}

int16_t Z() const {
return z;
return zHistory[0];
}

uint32_t NbSteps() const {
Expand Down Expand Up @@ -67,13 +68,28 @@ namespace Pinetime {
TickType_t lastTime = 0;
TickType_t time = 0;

int16_t lastX = 0;
struct AccelStats {
static constexpr uint8_t numHistory = 2;

int16_t yMean = 0;
int16_t zMean = 0;
int16_t prevYMean = 0;
int16_t prevZMean = 0;

uint32_t yVariance = 0;
uint32_t zVariance = 0;
};

AccelStats GetAccelStats() const;

AccelStats stats = {};

int16_t x = 0;
int16_t lastX = 0;
int16_t lastYForRaiseWake = 0;
int16_t lastY = 0;
int16_t y = 0;
int16_t lastZ = 0;
int16_t z = 0;
static constexpr uint8_t histSize = 8;
Utility::CircularBuffer<int16_t, histSize> yHistory = {};
Utility::CircularBuffer<int16_t, histSize> zHistory = {};
int32_t accumulatedSpeed = 0;

DeviceTypes deviceType = DeviceTypes::Unknown;
Expand Down
49 changes: 49 additions & 0 deletions src/utility/Math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "utility/Math.h"

#include <lvgl/src/lv_misc/lv_math.h>

using namespace Pinetime::Utility;

#ifndef PINETIME_IS_RECOVERY

int16_t Pinetime::Utility::Asin(int16_t arg) {
int16_t a = arg < 0 ? -arg : arg;

int16_t angle = 45;
int16_t low = 0;
int16_t high = 90;
while (low <= high) {
int16_t sinAngle = _lv_trigo_sin(angle);
int16_t sinAngleSub = _lv_trigo_sin(angle - 1);
int16_t sinAngleAdd = _lv_trigo_sin(angle + 1);

if (a >= sinAngleSub && a <= sinAngleAdd) {
if (a <= (sinAngleSub + sinAngle) / 2) {
angle--;
} else if (a > (sinAngle + sinAngleAdd) / 2) {
angle++;
}
break;
}

if (a < sinAngle) {
high = angle - 1;
}

else {
low = angle + 1;
}

angle = (low + high) / 2;
}

return arg < 0 ? -angle : angle;
}

#else

int16_t Pinetime::Utility::Asin(int16_t /*arg*/) {
return 0;
}

#endif
10 changes: 10 additions & 0 deletions src/utility/Math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <cstdint>

namespace Pinetime {
namespace Utility {
// returns the arcsin of `arg`. asin(-32767) = -90, asin(32767) = 90
int16_t Asin(int16_t arg);
}
}

0 comments on commit 3fbe193

Please sign in to comment.