From 2517d9755551169658880e41fb50965b0c32ba9a Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Sun, 8 Nov 2015 22:56:54 +1100 Subject: [PATCH] Move toothDeltaT out of interrupts --- decoders.h | 1 - decoders.ino | 9 ++------- speeduino.ino | 5 +++-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/decoders.h b/decoders.h index 7495506f..9000f15b 100644 --- a/decoders.h +++ b/decoders.h @@ -13,7 +13,6 @@ volatile unsigned long toothOneTime = 0; //The time (micros()) that tooth 1 last volatile unsigned long toothOneMinusOneTime = 0; //The 2nd to last time (micros()) that tooth 1 last triggered volatile unsigned int toothHistory[TOOTH_LOG_BUFFER]; volatile unsigned int toothHistoryIndex = 0; -volatile long toothDeltaT; //Represents the change in time taken between the last 2 teeth compared to the previous 2. Positive value represents accleration, negative = deccleration volatile byte secondaryToothCount; //Used for identifying the current secondary (Usually cam) tooth for patterns with multiple secondary teeth volatile unsigned long secondaryLastToothTime = 0; //The time (micros()) that the last tooth was registered (Cam input) diff --git a/decoders.ino b/decoders.ino index 4c58a60d..0cd45da8 100644 --- a/decoders.ino +++ b/decoders.ino @@ -24,7 +24,7 @@ toothLastToothTime - The time (In uS) that the last primary tooth was 'seen' inline void addToothLogEntry(unsigned long time) { //High speed tooth logging history - toothHistory[toothHistoryIndex] = curGap; + toothHistory[toothHistoryIndex] = time; if(toothHistoryIndex == (TOOTH_LOG_BUFFER-1)) { toothHistoryIndex = 0; BIT_CLEAR(currentStatus.squirt, BIT_SQUIRT_TOOTHLOG1READY); } //The tooth log ready bit is cleared to ensure that we only get a set of concurrent values. else @@ -96,7 +96,6 @@ void triggerPri_missingTooth() startRevolutions++; //Counter } - toothDeltaT = (toothLastToothTime - toothLastMinusOneToothTime) - (curTime - toothLastToothTime); //Positive value = accleration, Negative = decceleration toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; } @@ -160,7 +159,6 @@ void triggerPri_DualWheel() addToothLogEntry(curGap); - toothDeltaT = (toothLastToothTime - toothLastMinusOneToothTime) - (curTime - toothLastToothTime); //Positive value = accleration, Negative = decceleration toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; } @@ -240,7 +238,6 @@ void triggerPri_BasicDistributor() addToothLogEntry(curGap); - toothDeltaT = (toothLastToothTime - toothLastMinusOneToothTime) - (curTime -toothLastToothTime); //Positive value = accleration, Negative = decceleration toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; } @@ -308,7 +305,6 @@ void triggerPri_GM7X() } } - toothDeltaT = (toothLastToothTime - toothLastMinusOneToothTime) - (curTime - toothLastToothTime); //Positive value = accleration, Negative = decceleration toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; } @@ -396,7 +392,7 @@ void triggerPri_4G63() //Whilst this is an uneven tooth pattern, if the specific angle between the last 2 teeth is specified, 1st deriv prediction can be used if(toothCurrentCount == 1 || toothCurrentCount == 3) { triggerToothAngle = 70; } else { triggerToothAngle = 110; } - toothDeltaT = (toothLastToothTime - toothLastMinusOneToothTime) - (curTime - toothLastToothTime); //Positive value = accleration, Negative = decceleration + toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; } @@ -595,7 +591,6 @@ void triggerPri_Jeep2000() addToothLogEntry(curGap); - toothDeltaT = (toothLastToothTime - toothLastMinusOneToothTime) - (curTime - toothLastToothTime); //Positive value = accleration, Negative = decceleration toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; } diff --git a/speeduino.ino b/speeduino.ino index 37d98068..b58bf890 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -730,12 +730,13 @@ void loop() //We use a 1st Deriv accleration prediction, but only when there is an even spacing between primary sensor teeth //Any decoder that has uneven spacing has its triggerToothAngle set to 0 /* - if(triggerToothAngle > 0) + if(triggerToothAngle > 0 && toothHistoryIndex >= 3) //toothHistoryIndex must be greater than or equal to 3 as we need the last 3 entries { + long toothDeltaT = toothHistory[toothHistoryIndex-1] - toothHistory[toothHistoryIndex]; //Positive value = accleration, Negative = decceleration long toothAccel = toothDeltaT / triggerToothAngle; //An amount represengint the current acceleration or decceleration of the crank in degrees per uS per uS timePerDegree = ldiv( 166666L, currentStatus.RPM ).quot + (toothAccel * (micros() - toothLastToothTime)); //There is a small amount of rounding in this calculation, however it is less than 0.001 of a uS (Faster as ldiv than / ) } - else */ + else*/ { timePerDegree = ldiv( 166666L, currentStatus.RPM ).quot; //There is a small amount of rounding in this calculation, however it is less than 0.001 of a uS (Faster as ldiv than / ) }