From 27e7d249f1a66a3d65f5f4f911d25d38da2e1d1f Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Wed, 1 Jun 2016 20:39:30 +1000 Subject: [PATCH] Check in the PID controller to avoid unneeded code path --- decoders.ino | 3 +-- libs/PID_v1/PID_v1.cpp | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/decoders.ino b/decoders.ino index 6c8b41a..6d4b4d3 100644 --- a/decoders.ino +++ b/decoders.ino @@ -100,10 +100,9 @@ void triggerPri_missingTooth() //Begin the missing tooth detection //If the time between the current tooth and the last is greater than 1.5x the time between the last tooth and the tooth before that, we make the assertion that we must be at the first tooth after the gap - //if ( (curTime - toothLastToothTime) > (1.5 * (toothLastToothTime - toothLastMinusOneToothTime))) { toothCurrentCount = 1; } if(configPage2.triggerMissingTeeth == 1) { targetGap = (3 * (toothLastToothTime - toothLastMinusOneToothTime)) >> 1; } //Multiply by 1.5 (Checks for a gap 1.5x greater than the last one) (Uses bitshift to multiply by 3 then divide by 2. Much faster than multiplying by 1.5) - //else { targetGap = (10 * (toothLastToothTime - toothLastMinusOneToothTime)) >> 2; } //Multiply by 2.5 (Checks for a gap 2.5x greater than the last one) else { targetGap = ((toothLastToothTime - toothLastMinusOneToothTime)) * 2; } //Multiply by 2 (Checks for a gap 2x greater than the last one) + if ( curGap > targetGap || toothCurrentCount > triggerActualTeeth) { toothCurrentCount = 1; diff --git a/libs/PID_v1/PID_v1.cpp b/libs/PID_v1/PID_v1.cpp index 37c6aec..4d30a2b 100755 --- a/libs/PID_v1/PID_v1.cpp +++ b/libs/PID_v1/PID_v1.cpp @@ -270,7 +270,7 @@ bool integerPID::Compute() void integerPID::SetTunings(byte Kp, byte Ki, byte Kd) { if (Kp<0 || Ki<0 || Kd<0) return; - + if ( dispKp == Kp && dispKi == Ki && dispKd == Kd ) return; //Only do anything if one of the values has changed dispKp = Kp; dispKi = Ki; dispKd = Kd; /* @@ -284,7 +284,7 @@ void integerPID::SetTunings(byte Kp, byte Ki, byte Kd) ki = (long)((long)Ki * 1000) / InverseSampleTimeInSec; kd = ((long)Kd * InverseSampleTimeInSec) / 100; - if(controllerDirection ==REVERSE) + if(controllerDirection == REVERSE) { kp = (0 - kp); ki = (0 - ki); @@ -297,6 +297,7 @@ void integerPID::SetTunings(byte Kp, byte Ki, byte Kd) ******************************************************************************/ void integerPID::SetSampleTime(int NewSampleTime) { + if (SampleTime == (unsigned long)NewSampleTime) return; //If new value = old value, no action required. if (NewSampleTime > 0) { unsigned long ratioX1000 = (unsigned long)(NewSampleTime * 1000) / (unsigned long)SampleTime;