From affaf86f0d1d8a8c4d7ee456385587e7fa37f53d Mon Sep 17 00:00:00 2001 From: mikeller Date: Sat, 9 Jun 2018 22:05:05 +1200 Subject: [PATCH 1/2] Improved 'applyDeadband()', added 'fapplyDeadband()' at @diehertz' request. --- src/main/common/maths.c | 22 ++++++++-------------- src/main/common/maths.h | 4 ++-- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/common/maths.c b/src/main/common/maths.c index dd443ee80b..506d6b7282 100644 --- a/src/main/common/maths.c +++ b/src/main/common/maths.c @@ -118,28 +118,22 @@ float powerf(float base, int exp) { return result; } -int32_t applyDeadband(int32_t value, int32_t deadband) +int32_t applyDeadband(const int32_t value, const int32_t deadband) { if (ABS(value) < deadband) { - value = 0; - } else if (value > 0) { - value -= deadband; - } else if (value < 0) { - value += deadband; + return 0; } - return value; + + return value >= 0 ? value - deadband : value + deadband; } -float fapplyDeadband(float value, float deadband) +float fapplyDeadband(const float value, const float deadband) { if (fabsf(value) < deadband) { - value = 0; - } else if (value > 0) { - value -= deadband; - } else if (value < 0) { - value += deadband; + return 0; } - return value; + + return value >=0 ? value - deadband : value + deadband; } void devClear(stdev_t *dev) diff --git a/src/main/common/maths.h b/src/main/common/maths.h index 19842ebb40..8095e1c9cf 100644 --- a/src/main/common/maths.h +++ b/src/main/common/maths.h @@ -90,8 +90,8 @@ typedef union { int gcd(int num, int denom); float powerf(float base, int exp); -int32_t applyDeadband(int32_t value, int32_t deadband); -float fapplyDeadband(float value, float deadband); +int32_t applyDeadband(const int32_t value, const int32_t deadband); +float fapplyDeadband(const float value, const float deadband); void devClear(stdev_t *dev); void devPush(stdev_t *dev, float x); From bc048bc23fd31c1559fbc88553b77072fce97077 Mon Sep 17 00:00:00 2001 From: mikeller Date: Sun, 10 Jun 2018 01:59:29 +1200 Subject: [PATCH 2/2] Fixes from review. --- src/main/common/maths.c | 2 +- src/main/common/maths.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/common/maths.c b/src/main/common/maths.c index 506d6b7282..565684be82 100644 --- a/src/main/common/maths.c +++ b/src/main/common/maths.c @@ -133,7 +133,7 @@ float fapplyDeadband(const float value, const float deadband) return 0; } - return value >=0 ? value - deadband : value + deadband; + return value >= 0 ? value - deadband : value + deadband; } void devClear(stdev_t *dev) diff --git a/src/main/common/maths.h b/src/main/common/maths.h index 8095e1c9cf..19842ebb40 100644 --- a/src/main/common/maths.h +++ b/src/main/common/maths.h @@ -90,8 +90,8 @@ typedef union { int gcd(int num, int denom); float powerf(float base, int exp); -int32_t applyDeadband(const int32_t value, const int32_t deadband); -float fapplyDeadband(const float value, const float deadband); +int32_t applyDeadband(int32_t value, int32_t deadband); +float fapplyDeadband(float value, float deadband); void devClear(stdev_t *dev); void devPush(stdev_t *dev, float x);