Performance improvements (Div operations in getCrankAngle)

This commit is contained in:
Josh Stewart 2015-11-25 16:55:36 +11:00
parent 8b5d171092
commit 9b69a583c2
4 changed files with 49 additions and 15 deletions

View File

@ -1,3 +1,5 @@
#include <limits.h>
volatile unsigned long curTime;
volatile unsigned int curGap;
volatile unsigned long curTime2;
@ -28,3 +30,5 @@ int toothAngles[24]; //An array for storing fixed tooth angles. Currently sized
//Used for identifying long and short pulses on the 4G63 (And possibly other) trigger patterns
#define LONG 0;
#define SHORT 1;

View File

@ -119,7 +119,10 @@ int getCrankAngle_missingTooth(int timePerDegree)
interrupts();
int crankAngle = (tempToothCurrentCount - 1) * triggerToothAngle + configPage2.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.
crankAngle += ( (micros() - tempToothLastToothTime) / timePerDegree); //Estimate the number of degrees travelled since the last tooth
//Estimate the number of degrees travelled since the last tooth}
long elapsedTime = micros() - tempToothLastToothTime;
if(elapsedTime < SHRT_MAX ) { crankAngle += div(elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
else { ldiv(elapsedTime, timePerDegree).quot; }
if (crankAngle >= 720) { crankAngle -= 720; }
else if (crankAngle > 360) { crankAngle -= 360; }
@ -199,7 +202,11 @@ int getCrankAngle_DualWheel(int timePerDegree)
if(tempToothCurrentCount == 0) { tempToothCurrentCount = configPage2.triggerTeeth; }
int crankAngle = (tempToothCurrentCount - 1) * triggerToothAngle + configPage2.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.
crankAngle += ( (micros() - tempToothLastToothTime) / timePerDegree); //Estimate the number of degrees travelled since the last tooth
//Estimate the number of degrees travelled since the last tooth}
long elapsedTime = micros() - tempToothLastToothTime;
if(elapsedTime < SHRT_MAX ) { crankAngle += div(elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
else { ldiv(elapsedTime, timePerDegree).quot; }
if (crankAngle >= 720) { crankAngle -= 720; }
if (crankAngle > 360) { crankAngle -= 360; }
@ -261,7 +268,11 @@ int getCrankAngle_BasicDistributor(int timePerDegree)
interrupts();
int crankAngle = (tempToothCurrentCount - 1) * triggerToothAngle + configPage2.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.
crankAngle += ldiv( (micros() - tempToothLastToothTime), timePerDegree).quot; //Estimate the number of degrees travelled since the last tooth
//Estimate the number of degrees travelled since the last tooth}
long elapsedTime = micros() - tempToothLastToothTime;
if(elapsedTime < SHRT_MAX ) { crankAngle += div(elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
else { ldiv(elapsedTime, timePerDegree).quot; }
if (crankAngle >= 720) { crankAngle -= 720; }
if (crankAngle > 360) { crankAngle -= 360; }
if (crankAngle < 0) { crankAngle += 360; }
@ -341,7 +352,11 @@ int getCrankAngle_GM7X(int timePerDegree)
crankAngle = (tempToothCurrentCount - 2) * triggerToothAngle + 42; //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.
}
crankAngle += ldiv( (micros() - tempToothLastToothTime), timePerDegree).quot; //Estimate the number of degrees travelled since the last tooth
//Estimate the number of degrees travelled since the last tooth}
long elapsedTime = micros() - tempToothLastToothTime;
if(elapsedTime < SHRT_MAX ) { crankAngle += div(elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
else { ldiv(elapsedTime, timePerDegree).quot; }
if (crankAngle > 360) { crankAngle -= 360; }
return crankAngle;
@ -447,7 +462,12 @@ int getRPM_4G63()
interrupts();
int crankAngle = toothAngles[(tempToothCurrentCount - 1)] + configPage2.triggerAngle; //Perform a lookup of the fixed toothAngles array to find what the angle of the last tooth passed was.
crankAngle += ldiv( (micros() - tempToothLastToothTime), timePerDegree).quot; //Estimate the number of degrees travelled since the last tooth
//Estimate the number of degrees travelled since the last tooth}
long elapsedTime = micros() - tempToothLastToothTime;
if(elapsedTime < SHRT_MAX ) { crankAngle += div(elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
else { ldiv(elapsedTime, timePerDegree).quot; }
if (crankAngle >= 720) { crankAngle -= 720; }
if (crankAngle > 360) { crankAngle -= 360; }
if (crankAngle < 0) { crankAngle += 360; }
@ -540,7 +560,13 @@ int getCrankAngle_24X(int timePerDegree)
int crankAngle;
if (toothCurrentCount == 0) { crankAngle = 0 + configPage2.triggerAngle; } //This is the special case to handle when the 'last tooth' seen was the cam tooth. 0 is the angle at which the crank tooth goes high (Within 360 degrees).
else { crankAngle = toothAngles[(tempToothCurrentCount - 1)] + configPage2.triggerAngle;} //Perform a lookup of the fixed toothAngles array to find what the angle of the last tooth passed was.
crankAngle += ldiv( (micros() - tempToothLastToothTime), timePerDegree).quot; //Estimate the number of degrees travelled since the last tooth
//Estimate the number of degrees travelled since the last tooth}
long elapsedTime = micros() - tempToothLastToothTime;
if(elapsedTime < SHRT_MAX ) { crankAngle += div(elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
else { ldiv(elapsedTime, timePerDegree).quot; }
if (crankAngle > 360) { crankAngle -= 360; }
return crankAngle;
@ -620,7 +646,12 @@ int getCrankAngle_Jeep2000(int timePerDegree)
int crankAngle;
if (toothCurrentCount == 0) { crankAngle = 146 + configPage2.triggerAngle; } //This is the special case to handle when the 'last tooth' seen was the cam tooth. 146 is the angle at which the crank tooth goes high.
else { crankAngle = toothAngles[(tempToothCurrentCount - 1)] + configPage2.triggerAngle;} //Perform a lookup of the fixed toothAngles array to find what the angle of the last tooth passed was.
crankAngle += ldiv( (micros() - tempToothLastToothTime), timePerDegree).quot; //Estimate the number of degrees travelled since the last tooth
//Estimate the number of degrees travelled since the last tooth}
long elapsedTime = micros() - tempToothLastToothTime;
if(elapsedTime < SHRT_MAX ) { crankAngle += div(elapsedTime, timePerDegree).quot; } //This option is much faster, but only available for smaller values of elapsedTime
else { ldiv(elapsedTime, timePerDegree).quot; }
if (crankAngle > 360) { crankAngle -= 360; }
return crankAngle;

View File

@ -263,15 +263,14 @@ ISR(TIMER5_COMPA_vect, ISR_NOBLOCK) //ignitionSchedule1
ignitionSchedule1.startTime = micros();
ignitionSchedule1.StartCallback();
ign1LastRev = startRevolutions;
unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule1.duration >> 2); //Divide by 4
OCR5A = absoluteTimeout;
OCR5A = TCNT5 + (ignitionSchedule1.duration >> 2); //Divide by 4
}
else if (ignitionSchedule1.Status == RUNNING)
{
ignitionSchedule1.Status = OFF; //Turn off the schedule
ignitionSchedule1.EndCallback();
ignitionCount += 1; //Increment the igintion counter
TIMSK5 &= ~(1 << OCIE5A); //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
TIMSK5 &= ~(1 << OCIE5A); //Turn off this output compare unit
}
}
ISR(TIMER5_COMPB_vect, ISR_NOBLOCK) //ignitionSchedule2

View File

@ -983,7 +983,7 @@ void loop()
//Likewise for the ignition
//Perform an initial check to see if the ignition is turned on (Ignition only turns on after a preset number of cranking revolutions and:
//Check for hard cut rev limit (If we're above the hardcut limit, we simply don't set a spark schedule)
crankAngle = getCrankAngle(timePerDegree); //Refresh with the latest crank angle
//crankAngle = getCrankAngle(timePerDegree); //Refresh with the latest crank angle
if(ignitionOn && (currentStatus.RPM < ((unsigned int)(configPage2.HardRevLim) * 100) ))
{
//if ( (ignition1StartAngle > crankAngle))// && ign1LastRev != startRevolutions)
@ -1063,10 +1063,10 @@ void openInjector1() { digitalWrite(pinInjector1, HIGH); BIT_SET(currentStatus.s
void closeInjector1() { digitalWrite(pinInjector1, LOW); BIT_CLEAR(currentStatus.squirt, BIT_SQUIRT_INJ1); }
//void openInjector1() { *inj1_pin_port |= (inj1_pin_mask); ; BIT_SET(currentStatus.squirt, 0); }
//void closeInjector1() { *inj1_pin_port &= ~(inj1_pin_mask); BIT_CLEAR(currentStatus.squirt, 0); }
void beginCoil1Charge() { digitalWrite(pinCoil1, coilHIGH); BIT_SET(currentStatus.spark, 0); digitalWrite(pinTachOut, LOW); }
void endCoil1Charge() { digitalWrite(pinCoil1, coilLOW); BIT_CLEAR(currentStatus.spark, 0); }
//void beginCoil1Charge() { *ign1_pin_port |= (ign1_pin_mask); }
//void endCoil1Charge() { *ign1_pin_port &= ~(ign1_pin_mask); }
//void beginCoil1Charge() { digitalWrite(pinCoil1, coilHIGH); BIT_SET(currentStatus.spark, 0); digitalWrite(pinTachOut, LOW); }
//void endCoil1Charge() { digitalWrite(pinCoil1, coilLOW); BIT_CLEAR(currentStatus.spark, 0); }
void beginCoil1Charge() { *ign1_pin_port |= (ign1_pin_mask); }
void endCoil1Charge() { *ign1_pin_port &= ~(ign1_pin_mask); }
void openInjector2() { digitalWrite(pinInjector2, HIGH); BIT_SET(currentStatus.squirt, BIT_SQUIRT_INJ2); } //Sets the relevant pin HIGH and changes the current status bit for injector 2 (2nd bit of currentStatus.squirt)
void closeInjector2() { digitalWrite(pinInjector2, LOW); BIT_CLEAR(currentStatus.squirt, BIT_SQUIRT_INJ2); }