summaryrefslogtreecommitdiff
path: root/common/accel-motivity.hpp
diff options
context:
space:
mode:
authorJacob Palecki <[email protected]>2020-09-22 13:08:31 -0700
committerJacob Palecki <[email protected]>2020-09-22 13:08:31 -0700
commit45285413a94c9c081098c672e69e9811ac5262b7 (patch)
tree4c89d57a30226e2d1c007547fa5cf787118f3158 /common/accel-motivity.hpp
parentFix bug & rename x axis to input speed (diff)
downloadrawaccel-45285413a94c9c081098c672e69e9811ac5262b7.tar.xz
rawaccel-45285413a94c9c081098c672e69e9811ac5262b7.zip
Rename experiment two to motivity
Diffstat (limited to 'common/accel-motivity.hpp')
-rw-r--r--common/accel-motivity.hpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/common/accel-motivity.hpp b/common/accel-motivity.hpp
new file mode 100644
index 0000000..a37d1ce
--- /dev/null
+++ b/common/accel-motivity.hpp
@@ -0,0 +1,89 @@
+#pragma once
+
+#include <math.h>
+
+#include "accel-base.hpp"
+
+#define RA_LOOKUP
+
+namespace rawaccel {
+
+ constexpr size_t LUT_SIZE = 601;
+
+ struct si_pair {
+ double slope = 0;
+ double intercept = 0;
+ };
+
+ /// <summary> Struct to hold sigmoid (s-shaped) gain implementation. </summary>
+ struct motivity_impl {
+ double rate;
+ double limit;
+ double midpoint;
+ double subtractive_constant;
+
+ motivity_impl(const accel_args& args) :
+ rate(pow(10,args.rate)), limit(2*log10(args.limit)), midpoint(log10(args.midpoint))
+ {
+ subtractive_constant = limit / 2;
+ }
+
+ inline double operator()(double speed) const {
+ double log_speed = log10(speed);
+ return pow(10, limit / (exp(-rate * (log_speed - midpoint)) + 1) - subtractive_constant);
+
+ }
+
+ inline double legacy_offset(double speed) const { return operator()(speed); }
+
+ inline double apply(si_pair* lookup, double speed) const
+ {
+ si_pair pair = lookup[map(speed)];
+ return pair.slope + pair.intercept / speed;
+ }
+
+ inline int map(double speed) const
+ {
+ int index = speed > 0 ? (int)(100 * log10(speed) + 201) : 0;
+
+ if (index < 0) return 0;
+ if (index >= LUT_SIZE) return LUT_SIZE - 1;
+
+ return index;
+ }
+
+ inline void fill(si_pair* lookup) const
+ {
+ double lookup_speed = 0;
+ double gain_integral_speed = 0;
+ double gain = 0;
+ double intercept = 0;
+ double output = 0;
+ double x = -2;
+
+ lookup[0] = {};
+
+ for (size_t i = 1; i < LUT_SIZE; i++)
+ {
+ x += 0.01;
+
+ lookup_speed = pow(10, x);
+
+ while (gain_integral_speed < lookup_speed)
+ {
+ gain_integral_speed += 0.001;
+ gain = operator()(gain_integral_speed);
+ output += gain * 0.001;
+ }
+
+ intercept = output - gain * lookup_speed;
+
+ lookup[i] = { gain, intercept };
+ }
+
+ }
+ };
+
+ using accel_motivity = nonadditive_accel<motivity_impl>;
+
+}