Significant performance improvement to corrections calculations

This commit is contained in:
Josh Stewart 2016-06-17 14:13:10 +10:00
parent 18ddca5b69
commit c65d0fecc0
2 changed files with 40 additions and 12 deletions

View File

@ -30,31 +30,48 @@ This is the only function that should be called from anywhere outside the file
*/
byte correctionsTotal()
{
int sumCorrections = 100;
unsigned long sumCorrections = 100;
byte activeCorrections = 0;
byte result; //temporary variable to store the result of each corrections function
//As the 'normal' case will be for each function to return 100, we only perform the division operation if the returned result is not equal to that
//The function divs100 divides a signed int by 100 in a very fast way and is used instead of div() or /
//The values returned by each of the correction functions are multipled together and then divided back to give a single 0-255 value.
currentStatus.wueCorrection = correctionWUE();
if (currentStatus.wueCorrection != 100) { sumCorrections = divs100(sumCorrections * currentStatus.wueCorrection); }
if (currentStatus.wueCorrection != 100) { sumCorrections = (sumCorrections * currentStatus.wueCorrection); activeCorrections++; }
result = correctionASE();
if (result != 100) { sumCorrections = divs100(sumCorrections * result); }
if (result != 100) { sumCorrections = (sumCorrections * result); activeCorrections++; }
result = correctionCranking();
if (result != 100) { sumCorrections = div((sumCorrections * result), 100).quot; }
if (result != 100) { sumCorrections = (sumCorrections * result); activeCorrections++; }
if (activeCorrections == 3) { sumCorrections = sumCorrections / powint(100,activeCorrections); activeCorrections = 0; } // Need to check this to ensure that sumCorrections doesn't overflow. Can occur when the number of corrections is greater than 3 (Which is 100^4) as 100^5 can overflow
currentStatus.TAEamount = correctionAccel();
if (currentStatus.TAEamount != 100) { sumCorrections = div((sumCorrections * currentStatus.TAEamount), 100).quot; }
if (currentStatus.TAEamount != 100) { sumCorrections = (sumCorrections * currentStatus.TAEamount); activeCorrections++; }
if (activeCorrections == 3) { sumCorrections = sumCorrections / powint(100,activeCorrections); activeCorrections = 0; }
result = correctionFloodClear();
if (result != 100) { sumCorrections = div((sumCorrections * result), 100).quot; }
if (result != 100) { sumCorrections = (sumCorrections * result); activeCorrections++; }
if (activeCorrections == 3) { sumCorrections = sumCorrections / powint(100,activeCorrections); activeCorrections = 0; }
currentStatus.egoCorrection = correctionsAFRClosedLoop();
if (currentStatus.egoCorrection != 100) { sumCorrections = divs100(sumCorrections * currentStatus.egoCorrection); }
if (currentStatus.egoCorrection != 100) { sumCorrections = (sumCorrections * currentStatus.egoCorrection); activeCorrections++; }
if (activeCorrections == 3) { sumCorrections = sumCorrections / powint(100,activeCorrections); activeCorrections = 0; }
currentStatus.batCorrection = correctionsBatVoltage();
if (currentStatus.batCorrection != 100) { sumCorrections = divs100(sumCorrections * currentStatus.batCorrection); }
if (currentStatus.batCorrection != 100) { sumCorrections = (sumCorrections * currentStatus.batCorrection); activeCorrections++; }
if (activeCorrections == 3) { sumCorrections = sumCorrections / powint(100,activeCorrections); activeCorrections = 0; }
currentStatus.iatCorrection = correctionsIATDensity();
if (currentStatus.iatCorrection != 100) { sumCorrections = divs100(sumCorrections * currentStatus.iatCorrection); }
if (currentStatus.iatCorrection != 100) { sumCorrections = (sumCorrections * currentStatus.iatCorrection); activeCorrections++; }
if (activeCorrections == 3) { sumCorrections = sumCorrections / powint(100,activeCorrections); activeCorrections = 0; }
currentStatus.launchCorrection = correctionsLaunch();
if (currentStatus.launchCorrection != 100) { sumCorrections = div((sumCorrections * currentStatus.launchCorrection), 100).quot; }
if (currentStatus.launchCorrection != 100) { sumCorrections = (sumCorrections * currentStatus.launchCorrection); activeCorrections++; }
bitWrite(currentStatus.squirt, BIT_SQUIRT_DFCO, correctionsDFCO());
if ( bitRead(currentStatus.squirt, BIT_SQUIRT_DFCO) ) { sumCorrections = 0; }
sumCorrections = sumCorrections / powint(100,activeCorrections);
if(sumCorrections > 255) { sumCorrections = 255; } //This is the maximum allowable increase
return (byte)sumCorrections;

11
math.h
View File

@ -82,4 +82,15 @@ unsigned long percentage(byte x, unsigned long y)
//return divu100(y * x);
}
/*
* Calculates integer power values. Same as pow() but with ints
*/
inline long powint(int factor, unsigned int exponent)
{
long product = 1;
while (exponent--)
product *= factor;
return product;
}
#endif // MATH_H