diff options
| author | a1xd <[email protected]> | 2021-03-30 18:27:02 -0400 |
|---|---|---|
| committer | a1xd <[email protected]> | 2021-03-30 18:27:02 -0400 |
| commit | fa3ebfb1eb054ba88824a908c996094bb98e85c5 (patch) | |
| tree | bf52cf6d5de16714dba11e96719ce1434a686779 /common/accel-motivity.hpp | |
| parent | put utility in namespace (diff) | |
| download | rawaccel-fa3ebfb1eb054ba88824a908c996094bb98e85c5.tar.xz rawaccel-fa3ebfb1eb054ba88824a908c996094bb98e85c5.zip | |
refactor lut/motivity
Diffstat (limited to 'common/accel-motivity.hpp')
| -rw-r--r-- | common/accel-motivity.hpp | 114 |
1 files changed, 35 insertions, 79 deletions
diff --git a/common/accel-motivity.hpp b/common/accel-motivity.hpp index a3cb027..0c15598 100644 --- a/common/accel-motivity.hpp +++ b/common/accel-motivity.hpp @@ -1,100 +1,56 @@ #pragma once -#include "rawaccel-base.hpp" +#include "accel-lookup.hpp" #include <math.h> -#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 { - double rate; - double limit; + struct sigmoid { + double accel; + double motivity; double midpoint; - double subtractive_constant; - - motivity(const accel_args& args) : - rate(pow(10,args.accel_motivity)), - limit(2*log10(args.limit)), - midpoint(log10(args.midpoint)) - { - subtractive_constant = limit / 2; - } + double constant; - double operator()(double speed) const - { - double log_speed = log10(speed); - return pow(10, limit / (exp(-rate * (log_speed - midpoint)) + 1) - subtractive_constant); + sigmoid(const accel_args& args) : + accel(exp(args.accel_motivity)), + motivity(2 * log(args.motivity)), + midpoint(log(args.midpoint)), + constant(-motivity / 2) {} - } - - double apply(si_pair* lookup, double speed) const + double operator()(double x) const { - si_pair pair = lookup[map(speed)]; - return pair.slope + pair.intercept / speed; + double denom = exp(accel * (midpoint - log(x))) + 1; + return exp(motivity / denom + constant); } + }; - 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; + /// <summary> Struct to hold sigmoid (s-shaped) gain implementation. </summary> + struct motivity : binlog_lut { - return index; - } + using binlog_lut::operator(); - void fill(si_pair* lookup) const + motivity(const accel_args& args) : + binlog_lut(args) { - double lookup_speed = 0; - double integral_interval = 0; - double gain_integral_speed = 0; - double gain_integral_speed_prev = 0; - double gain = 0; - double intercept = 0; - double output = 0; - double output_prev = 0; - double x = -2; - - double logarithm_interval = 0.01; - double integral_intervals_per_speed = 10; - double integral_interval_factor = pow(10, logarithm_interval) / integral_intervals_per_speed; - - lookup[0] = {}; - - for (size_t i = 1; i < LUT_SIZE; i++) - { - x += logarithm_interval; - - // Each lookup speed will be 10^0.01 = 2.33% higher than the previous - // To get 10 integral intervals per speed, set interval to 0.233% - lookup_speed = pow(10, x); - integral_interval = lookup_speed * integral_interval_factor; - - while (gain_integral_speed < lookup_speed) - { - output_prev = output; - gain_integral_speed_prev = gain_integral_speed; - gain_integral_speed += integral_interval; - gain = operator()(gain_integral_speed); - output += gain * integral_interval; + double sum = 0; + double a = 0; + auto sigmoid_sum = [&, sig = sigmoid(args)](double b) mutable { + double interval = (b - a) / args.lut_args.partitions; + for (int i = 1; i <= args.lut_args.partitions; i++) { + sum += sig(a + i * interval) * interval; } - - intercept = output_prev - gain_integral_speed_prev * gain; - - lookup[i] = { gain, intercept }; - } - + a = b; + return sum; + }; + + fill([&](double x) { + double y = sigmoid_sum(x); + if (!this->transfer) y /= x; + return y; + }); } + }; } |