From 0b50f9c85568e5b8b6ee32e40990e39cfdf515b5 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 2 Sep 2020 21:13:18 -0700 Subject: Start adding gain offsets --- common/accel-base.hpp | 3 ++- common/accel-linear.hpp | 11 +++++++++-- common/accel-naturalgain.hpp | 5 +++++ 3 files changed, 16 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/accel-base.hpp b/common/accel-base.hpp index 560c0b5..b42f23d 100644 --- a/common/accel-base.hpp +++ b/common/accel-base.hpp @@ -36,7 +36,8 @@ namespace rawaccel { } inline double operator()(double speed) const { - return 1 + fn(maxsd(speed - offset, 0)) * weight; + double offset_speed = speed - offset; + return offset_speed > 0 ? 1 + fn(offset_speed) * weight : 1; } }; diff --git a/common/accel-linear.hpp b/common/accel-linear.hpp index a943594..95ba261 100644 --- a/common/accel-linear.hpp +++ b/common/accel-linear.hpp @@ -7,11 +7,18 @@ namespace rawaccel { /// Struct to hold linear acceleration implementation. struct linear_impl { double accel; + double offset; + double subtractive_const; + double divisive_const; - linear_impl(const accel_args& args) : accel(args.accel) {} + linear_impl(const accel_args& args) : accel(args.accel), offset(args.offset) { + subtractive_const = 2 * accel * offset; + divisive_const = accel * offset * offset; + } inline double operator()(double speed) const { - return accel * speed; + double base_speed = speed + offset; + return accel * base_speed - subtractive_const + divisive_const / base_speed; } }; diff --git a/common/accel-naturalgain.hpp b/common/accel-naturalgain.hpp index 646b2bb..03d749f 100644 --- a/common/accel-naturalgain.hpp +++ b/common/accel-naturalgain.hpp @@ -13,6 +13,11 @@ namespace rawaccel { inline double operator()(double speed) const { // f(x) = k((e^(-mx)-1)/mx + 1) + if (speed <= 0) + { + return 0; + } + double scaled_speed = rate * speed; return limit * (((exp(-scaled_speed) - 1) / scaled_speed) + 1); } -- cgit v1.2.3 From c7e9641e8688c82874dbfa067f7dc8cb4d40e23d Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Thu, 3 Sep 2020 13:21:45 -0700 Subject: Change classic, natural, naturalgain to use gain offset --- common/accel-classic.hpp | 12 +++++++++--- common/accel-natural.hpp | 6 ++++-- common/accel-naturalgain.hpp | 3 ++- 3 files changed, 15 insertions(+), 6 deletions(-) (limited to 'common') diff --git a/common/accel-classic.hpp b/common/accel-classic.hpp index 4cc52ca..3f409c3 100644 --- a/common/accel-classic.hpp +++ b/common/accel-classic.hpp @@ -10,14 +10,20 @@ namespace rawaccel { struct classic_impl { double accel; double power; + double power_inc; + double offset; + double multiplicative_const; classic_impl(const accel_args& args) : - accel(args.accel), power(args.exponent - 1) - {} + accel(args.accel), power(args.exponent - 1), offset(args.offset) { + multiplicative_const = pow(accel, power); + power_inc = power + 1; + } inline double operator()(double speed) const { //f(x) = (mx)^(k-1) - return pow(accel * speed, power); + double base_speed = speed + offset; + return multiplicative_const * pow(speed, power_inc) / base_speed; } }; diff --git a/common/accel-natural.hpp b/common/accel-natural.hpp index c7d0dcd..32bc365 100644 --- a/common/accel-natural.hpp +++ b/common/accel-natural.hpp @@ -10,16 +10,18 @@ namespace rawaccel { struct natural_impl { double rate; double limit; + double offset; natural_impl(const accel_args& args) : - rate(args.accel), limit(args.limit - 1) + rate(args.accel), limit(args.limit - 1), offset(args.offset) { rate /= limit; } inline double operator()(double speed) const { // f(x) = k(1-e^(-mx)) - return limit - (limit * exp(-rate * speed)); + double base_speed = speed + offset; + return limit * (1 - ((exp(-rate * speed) * speed + offset) / base_speed)); } }; diff --git a/common/accel-naturalgain.hpp b/common/accel-naturalgain.hpp index 03d749f..5979d92 100644 --- a/common/accel-naturalgain.hpp +++ b/common/accel-naturalgain.hpp @@ -18,8 +18,9 @@ namespace rawaccel { return 0; } + double base_speed = speed + offset; double scaled_speed = rate * speed; - return limit * (((exp(-scaled_speed) - 1) / scaled_speed) + 1); + return limit * (((exp(-scaled_speed) - 1) / (base_speed * rate) ) + 1 - offset / base_speed); } }; -- cgit v1.2.3 From 01f870a493378a62ab76dcbf5dc37c0390ca7afe Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Thu, 3 Sep 2020 19:36:44 -0700 Subject: Refactor for nice gain offset --- common/accel-base.hpp | 8 +++++--- common/accel-classic.hpp | 4 ++++ common/accel-linear.hpp | 3 +++ common/accel-logarithmic.hpp | 3 +++ common/accel-natural.hpp | 4 ++++ common/accel-naturalgain.hpp | 9 ++++----- common/accel-noaccel.hpp | 1 + common/accel-sigmoid.hpp | 1 + common/accel-sigmoidgain.hpp | 1 + 9 files changed, 26 insertions(+), 8 deletions(-) (limited to 'common') diff --git a/common/accel-base.hpp b/common/accel-base.hpp index b42f23d..714162f 100644 --- a/common/accel-base.hpp +++ b/common/accel-base.hpp @@ -5,6 +5,7 @@ namespace rawaccel { /// Struct to hold arguments for an acceleration function. struct accel_args { double offset = 0; + double legacy_offset = 0; double accel = 0; double limit = 2; double exponent = 2; @@ -19,6 +20,7 @@ namespace rawaccel { template struct accel_val_base { + bool legacy_offset = false; double offset = 0; double weight = 1; Func fn; @@ -31,15 +33,15 @@ namespace rawaccel { struct additive_accel : accel_val_base { additive_accel(const accel_args& args) : accel_val_base(args) { - offset = args.offset; + legacy_offset = args.offset <= 0 && args.legacy_offset > 0; + offset = legacy_offset ? args.legacy_offset : args.offset; weight = args.weight; } inline double operator()(double speed) const { double offset_speed = speed - offset; - return offset_speed > 0 ? 1 + fn(offset_speed) * weight : 1; + return offset_speed > 0 ? ( legacy_offset ? 1 + fn.legacy_offset(offset_speed) * weight : 1 + fn(offset_speed) ) : 1; } - }; template diff --git a/common/accel-classic.hpp b/common/accel-classic.hpp index 3f409c3..1df888a 100644 --- a/common/accel-classic.hpp +++ b/common/accel-classic.hpp @@ -25,6 +25,10 @@ namespace rawaccel { double base_speed = speed + offset; return multiplicative_const * pow(speed, power_inc) / base_speed; } + + inline double legacy_offset(double speed) const { + return pow(accel * speed, power); + } }; using accel_classic = additive_accel; diff --git a/common/accel-linear.hpp b/common/accel-linear.hpp index 95ba261..2bd57b8 100644 --- a/common/accel-linear.hpp +++ b/common/accel-linear.hpp @@ -21,6 +21,9 @@ namespace rawaccel { return accel * base_speed - subtractive_const + divisive_const / base_speed; } + inline double legacy_offset(double speed) const { + return accel * speed; + } }; using accel_linear = additive_accel; diff --git a/common/accel-logarithmic.hpp b/common/accel-logarithmic.hpp index c7991c7..1ab0e53 100644 --- a/common/accel-logarithmic.hpp +++ b/common/accel-logarithmic.hpp @@ -16,6 +16,9 @@ namespace rawaccel { //f(x) = log(m*x+1) return log(accel * speed + 1); } + + // incorrect but this style is slated for removal + inline double legacy_offset(double speed) const { return operator()(speed); } }; using accel_logarithmic = additive_accel; diff --git a/common/accel-natural.hpp b/common/accel-natural.hpp index 32bc365..03700c1 100644 --- a/common/accel-natural.hpp +++ b/common/accel-natural.hpp @@ -24,6 +24,10 @@ namespace rawaccel { return limit * (1 - ((exp(-rate * speed) * speed + offset) / base_speed)); } + inline double legacy_offset(double speed) const { + return limit - (limit * exp(-rate * speed)); + } + }; using accel_natural = additive_accel; diff --git a/common/accel-naturalgain.hpp b/common/accel-naturalgain.hpp index 5979d92..cdfd1fa 100644 --- a/common/accel-naturalgain.hpp +++ b/common/accel-naturalgain.hpp @@ -13,16 +13,15 @@ namespace rawaccel { inline double operator()(double speed) const { // f(x) = k((e^(-mx)-1)/mx + 1) - if (speed <= 0) - { - return 0; - } - double base_speed = speed + offset; double scaled_speed = rate * speed; return limit * (((exp(-scaled_speed) - 1) / (base_speed * rate) ) + 1 - offset / base_speed); } + inline double legacy_offset(double speed) const { + double scaled_speed = rate * speed; + return limit * (((exp(-scaled_speed) - 1) / scaled_speed) + 1); + } }; using accel_naturalgain = additive_accel; diff --git a/common/accel-noaccel.hpp b/common/accel-noaccel.hpp index ae6f5f8..c803c2f 100644 --- a/common/accel-noaccel.hpp +++ b/common/accel-noaccel.hpp @@ -12,6 +12,7 @@ namespace rawaccel { inline double operator()(double) const { return 1; } + inline double legacy_offset(double speed) const { return operator()(speed); } }; } diff --git a/common/accel-sigmoid.hpp b/common/accel-sigmoid.hpp index c8112ee..239bd9d 100644 --- a/common/accel-sigmoid.hpp +++ b/common/accel-sigmoid.hpp @@ -21,6 +21,7 @@ namespace rawaccel { return limit / (exp(-rate * (speed - midpoint)) + 1); } + inline double legacy_offset(double speed) const { return operator()(speed); } }; using accel_sigmoid = additive_accel; diff --git a/common/accel-sigmoidgain.hpp b/common/accel-sigmoidgain.hpp index 99bb146..bed2f16 100644 --- a/common/accel-sigmoidgain.hpp +++ b/common/accel-sigmoidgain.hpp @@ -26,6 +26,7 @@ namespace rawaccel { return limit * ((log(additive_constant+exp(scaled_speed)) - integration_constant)/scaled_speed); } + inline double legacy_offset(double speed) const { return operator()(speed); } }; using accel_sigmoidgain = additive_accel; -- cgit v1.2.3