summaryrefslogtreecommitdiff
path: root/common/accel-motivity.hpp
diff options
context:
space:
mode:
authorJacobPalecki <[email protected]>2020-09-22 19:59:47 -0700
committerGitHub <[email protected]>2020-09-22 19:59:47 -0700
commit77f420cf45a1a0bee00602965e687097367e2a70 (patch)
treefa088af8f2feb54df5bcb6a036715fd32d0511e8 /common/accel-motivity.hpp
parentMerge pull request #21 from JacobPalecki/GUI (diff)
parentUpdate credits (diff)
downloadrawaccel-77f420cf45a1a0bee00602965e687097367e2a70.tar.xz
rawaccel-77f420cf45a1a0bee00602965e687097367e2a70.zip
Merge pull request #22 from JacobPalecki/GUI
Replace SigmoidGain with Motivity & Cleanup
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>;
+
+}