Fix potential overflow on low res patterns at low RPMs

This commit is contained in:
Josh Stewart 2018-09-08 13:37:22 +10:00
parent e394ffebb6
commit ae5cecb8a6
3 changed files with 14 additions and 5 deletions

View File

@ -9,7 +9,8 @@
//#define fastDegreesToUS(targetDegrees) ((targetDegrees) * (unsigned long)timePerDegree) //#define fastDegreesToUS(targetDegrees) ((targetDegrees) * (unsigned long)timePerDegree)
#define fastDegreesToUS(targetDegrees) (((targetDegrees) * (unsigned long)timePerDegreex16) >> 4) #define fastDegreesToUS(targetDegrees) (((targetDegrees) * (unsigned long)timePerDegreex16) >> 4)
#define fastTimeToAngle(time) (((unsigned long)time * degreesPeruSx2048) / 2048) //Divide by 2048 will be converted at compile time to bitshift //#define fastTimeToAngle(time) (((unsigned long)time * degreesPeruSx2048) / 2048) //Divide by 2048 will be converted at compile time to bitshift
#define fastTimeToAngle(time) (((unsigned long)time * degreesPeruSx32768) / 32768) //Divide by 32768 will be converted at compile time to bitshift
#define ignitionLimits(angle) ( (((int16_t)angle) >= CRANK_ANGLE_MAX_IGN) ? (angle - CRANK_ANGLE_MAX_IGN) : ( (angle < 0) ? (angle + CRANK_ANGLE_MAX_IGN) : angle) ) #define ignitionLimits(angle) ( (((int16_t)angle) >= CRANK_ANGLE_MAX_IGN) ? (angle - CRANK_ANGLE_MAX_IGN) : ( (angle < 0) ? (angle + CRANK_ANGLE_MAX_IGN) : angle) )
@ -21,6 +22,7 @@ void doCrankSpeedCalcs();
volatile uint16_t timePerDegree; volatile uint16_t timePerDegree;
volatile uint16_t timePerDegreex16; volatile uint16_t timePerDegreex16;
volatile uint16_t degreesPeruSx2048; volatile uint16_t degreesPeruSx2048;
volatile unsigned long degreesPeruSx32768;
//These are only part of the experimental 2nd deriv calcs //These are only part of the experimental 2nd deriv calcs
byte deltaToothCount = 0; //The last tooth that was used with the deltaV calc byte deltaToothCount = 0; //The last tooth that was used with the deltaV calc

View File

@ -145,4 +145,5 @@ void doCrankSpeedCalcs()
} }
} }
degreesPeruSx2048 = 2048 / timePerDegree; degreesPeruSx2048 = 2048 / timePerDegree;
degreesPeruSx32768 = 524288 / timePerDegreex16;
} }

View File

@ -593,7 +593,7 @@ void triggerSec_BasicDistributor() { return; } //Not required
uint16_t getRPM_BasicDistributor() uint16_t getRPM_BasicDistributor()
{ {
uint16_t tempRPM; uint16_t tempRPM;
if( currentStatus.RPM < currentStatus.crankRPM ) if( currentStatus.RPM < currentStatus.crankRPM)
{ {
tempRPM = crankingGetRPM(triggerActualTeeth) << 1; //crankGetRPM uses teeth per 360 degrees. As triggerActualTeeh is total teeth in 720 degrees, we divide the tooth count by 2 tempRPM = crankingGetRPM(triggerActualTeeth) << 1; //crankGetRPM uses teeth per 360 degrees. As triggerActualTeeh is total teeth in 720 degrees, we divide the tooth count by 2
revolutionTime = revolutionTime >> 1; //Revolution time has to be divided by 2 as otherwise it would be over 720 degrees (triggerActualTeeth = nCylinders) revolutionTime = revolutionTime >> 1; //Revolution time has to be divided by 2 as otherwise it would be over 720 degrees (triggerActualTeeth = nCylinders)
@ -616,14 +616,21 @@ int getCrankAngle_BasicDistributor()
noInterrupts(); noInterrupts();
tempToothCurrentCount = toothCurrentCount; tempToothCurrentCount = toothCurrentCount;
tempToothLastToothTime = toothLastToothTime; tempToothLastToothTime = toothLastToothTime;
lastCrankAngleCalc= micros(); //micros() is no longer interrupt safe lastCrankAngleCalc = micros(); //micros() is no longer interrupt safe
interrupts(); interrupts();
int crankAngle = ((tempToothCurrentCount - 1) * triggerToothAngle) + configPage4.triggerAngle; //Number of teeth that have passed since tooth 1, multiplied by the angle each tooth represents, plus the angle that tooth 1 is ATDC. This gives accuracy only to the nearest tooth. int crankAngle = ((tempToothCurrentCount - 1) * triggerToothAngle) + configPage4.triggerAngle; //Number of teeth that have passed since tooth 1, multiplied by the angle each tooth represents, plus the angle that tooth 1 is ATDC. This gives accuracy only to the nearest tooth.
//Estimate the number of degrees travelled since the last tooth} //Estimate the number of degrees travelled since the last tooth}
elapsedTime = (lastCrankAngleCalc - tempToothLastToothTime); elapsedTime = (lastCrankAngleCalc - tempToothLastToothTime);
crankAngle += timeToAngle(elapsedTime, CRANKMATH_METHOD_INTERVAL_REV);
//if(elapsedTime < SHRT_MAX ) { crankAngle += div((int)elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
//else { crankAngle += ldiv(elapsedTime, timePerDegree).quot; }
//crankAngle += timeToAngle(elapsedTime, CRANKMATH_METHOD_INTERVAL_REV);
crankAngle += timeToAngle(elapsedTime, CRANKMATH_METHOD_INTERVAL_TOOTH);
if (crankAngle >= 720) { crankAngle -= 720; } if (crankAngle >= 720) { crankAngle -= 720; }
if (crankAngle > CRANK_ANGLE_MAX) { crankAngle -= CRANK_ANGLE_MAX; } if (crankAngle > CRANK_ANGLE_MAX) { crankAngle -= CRANK_ANGLE_MAX; }
@ -2132,7 +2139,6 @@ void triggerPri_Nissan360()
{ {
checkPerToothTiming(crankAngle, toothCurrentCount); checkPerToothTiming(crankAngle, toothCurrentCount);
} }
//if(crankAngle < CRANK_ANGLE_MAX_IGN) {
} }