diff --git a/corrections.ino b/corrections.ino index 61ec5b68..2aa21a32 100644 --- a/corrections.ino +++ b/corrections.ino @@ -69,7 +69,6 @@ When the enrichment is turned on, it runs at that amount for a fixed period of t byte correctionAccel() { //First, check whether the accel. enrichment is already running - if( BIT_CHECK(currentStatus.engine, BIT_ENGINE_ACC) ) { //If it is currently running, check whether it should still be running or whether it's reached it's end time @@ -84,10 +83,9 @@ byte correctionAccel() return 100 + currentStatus.TAEamount; } - //If TAE isn't currently turned on, need to check whether it needs to be turned on int rateOfChange = ldiv(1000000, (currentLoopTime - previousLoopTime)).quot * (currentStatus.TPS - currentStatus.TPSlast); //This is the % per second that the TPS has moved - currentStatus.tpsDOT = div(rateOfChange, 10).quot; //The TAE bins are divided by 10 in order to allow them to be stored in a byte. + currentStatus.tpsDOT = divs10(rateOfChange); //The TAE bins are divided by 10 in order to allow them to be stored in a byte. if (currentStatus.tpsDOT > (configPage1.tpsThresh * 10)) { diff --git a/math.h b/math.h index e081b30c..0ccee976 100644 --- a/math.h +++ b/math.h @@ -1,7 +1,37 @@ //Replace the standard arduino map() function to use the div function instead -long fastMap(int x, int in_min, int in_max, int out_min, int out_max) +int fastMap(int x, int in_min, int in_max, int out_min, int out_max) { - return div( ((x - in_min) * (out_max - out_min)) , ((in_max - in_min) + out_min) ).quot; + return div( ((x - in_min) * (out_max - out_min)) , (in_max - in_min) ).quot + out_min; } +/* +The following are all fast versions of specific divisions +*/ + +//Unsigned divide by 10 +unsigned divu10(unsigned n) { + unsigned q, r; + q = (n >> 1) + (n >> 2); + q = q + (q >> 4); + q = q + (q >> 8); + q = q + (q >> 16); + q = q >> 3; + r = n - q*10; + return q + ((r + 6) >> 4); +// return q + (r > 9); +} + +//Signed divide by 10 +int divs10(int n) { + int q, r; + n = n + (n>>31 & 9); + q = (n >> 1) + (n >> 2); + q = q + (q >> 4); + q = q + (q >> 8); + q = q + (q >> 16); + q = q >> 3; + r = n - q*10; + return q + ((r + 6) >> 4); +// return q + (r > 9); +} diff --git a/speeduino.ino b/speeduino.ino index 3df295b4..0e47a140 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -269,11 +269,11 @@ void loop() //----------------------------------------------------------------------------------------------------- currentStatus.TPSlast = currentStatus.TPS; currentStatus.MAP = map(analogRead(pinMAP), 0, 1023, 10, 255); //Get the current MAP value - currentStatus.tpsADC = map(analogRead(pinTPS), 0, 1023, 0, 255); //Get the current raw TPS ADC value and map it into a byte - currentStatus.TPS = map(currentStatus.tpsADC, configPage1.tpsMin, configPage1.tpsMax, 0, 100); //Take the raw TPS ADC value and convert it into a TPS% based on the calibrated values + currentStatus.tpsADC = fastMap(analogRead(pinTPS), 0, 1023, 0, 255); //Get the current raw TPS ADC value and map it into a byte + currentStatus.TPS = fastMap(currentStatus.tpsADC, configPage1.tpsMin, configPage1.tpsMax, 0, 100); //Take the raw TPS ADC value and convert it into a TPS% based on the calibrated values - //The IAT and CLT readings can be done less frequently. This still runs about 10 times per second - if ((mainLoopCount & 127) == 1) + //The IAT and CLT readings can be done less frequently. This still runs about 4 times per second + if ((mainLoopCount & 255) == 1) { currentStatus.cltADC = map(analogRead(pinCLT), 0, 1023, 0, 511); //Get the current raw CLT value currentStatus.iatADC = map(analogRead(pinIAT), 0, 1023, 0, 511); //Get the current raw IAT value @@ -357,26 +357,27 @@ void loop() //BEGIN INJECTION TIMING //Determine next firing angles //1 - injector1StartAngle = 355 - ( div(currentStatus.PW, timePerDegree).quot ); //This is a little primitive, but is based on the idea that all fuel needs to be delivered before the inlet valve opens. I am using 355 as the point at which the injector MUST be closed by. See http://www.extraefi.co.uk/sequential_fuel.html for more detail + int PWdivTimerPerDegree = div(currentStatus.PW, timePerDegree).quot; //This variable is used multiple times, so only do the division once. + injector1StartAngle = 355 - ( PWdivTimerPerDegree ); //This is a little primitive, but is based on the idea that all fuel needs to be delivered before the inlet valve opens. I am using 355 as the point at which the injector MUST be closed by. See http://www.extraefi.co.uk/sequential_fuel.html for more detail //Repeat the above for each cylinder //2 if (configPage1.nCylinders == 2) { - injector2StartAngle = (355 + channel2Degrees - ( div(currentStatus.PW, timePerDegree).quot )); + injector2StartAngle = (355 + channel2Degrees - ( PWdivTimerPerDegree )); if(injector2StartAngle > 360) {injector2StartAngle -= 360;} } //3 - else if (configPage1.nCylinders == 4) + else if (configPage1.nCylinders == 3) { - injector2StartAngle = (355 + channel2Degrees - ( div(currentStatus.PW, timePerDegree).quot )); + injector2StartAngle = (355 + channel2Degrees - ( PWdivTimerPerDegree )); if(injector2StartAngle > 360) {injector2StartAngle -= 360;} - injector3StartAngle = (355 + channel3Degrees - ( div(currentStatus.PW, timePerDegree).quot )); + injector3StartAngle = (355 + channel3Degrees - ( PWdivTimerPerDegree )); if(injector3StartAngle > 360) {injector3StartAngle -= 360;} } //4 else if (configPage1.nCylinders == 4) { - injector2StartAngle = (355 + channel2Degrees - ( div(currentStatus.PW, timePerDegree).quot )); + injector2StartAngle = (355 + channel2Degrees - ( PWdivTimerPerDegree )); if(injector2StartAngle > 360) {injector2StartAngle -= 360;} }