From cfe21103ea197f98555d5002d389d0c24e7b5544 Mon Sep 17 00:00:00 2001 From: Finlay Davidson Date: Sun, 25 Jun 2023 15:59:34 +0200 Subject: motioncontroller: Add functions for analysis These are functions for converting acceleration due to gravity to angles in degrees, and some statistical analysis including the mean and variance. --- src/utility/Math.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/utility/Math.cpp (limited to 'src/utility/Math.cpp') diff --git a/src/utility/Math.cpp b/src/utility/Math.cpp new file mode 100644 index 00000000..fee4f64a --- /dev/null +++ b/src/utility/Math.cpp @@ -0,0 +1,49 @@ +#include "utility/Math.h" + +#include + +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 -- cgit v1.2.3-70-g09d2