From a9c2a2dec7206d585bf517115221c0af3840953f Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Thu, 4 Jun 2015 00:31:02 +1000 Subject: [PATCH] Minor performance tweaks --- decoders.ino | 8 +++----- speeduino.ino | 12 ++++++------ table.ino | 16 +++++++++------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/decoders.ino b/decoders.ino index 382c4e88..a0264a18 100644 --- a/decoders.ino +++ b/decoders.ino @@ -36,11 +36,10 @@ void triggerPri_missingTooth() { // http://www.msextra.com/forums/viewtopic.php?f=94&t=22976 // http://www.megamanual.com/ms2/wheel.htm - noInterrupts(); //Turn off interrupts whilst in this routine curTime = micros(); curGap = curTime - toothLastToothTime; - if ( curGap < triggerFilterTime ) { interrupts(); return; } //Debounce check. Pulses should never be less than triggerFilterTime, so if they are it means a false trigger. (A 36-1 wheel at 8000pm will have triggers approx. every 200uS) + if ( curGap < triggerFilterTime ) { return; } //Debounce check. Pulses should never be less than triggerFilterTime, so if they are it means a false trigger. (A 36-1 wheel at 8000pm will have triggers approx. every 200uS) toothCurrentCount++; //Increment the tooth counter //High speed tooth logging history @@ -68,7 +67,6 @@ void triggerPri_missingTooth() toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; - interrupts(); //Turn interrupts back on } void triggerSec_missingTooth(){ return; } //This function currently is not used @@ -78,14 +76,14 @@ int getRPM_missingTooth() noInterrupts(); unsigned long revolutionTime = (toothOneTime - toothOneMinusOneTime); //The time in uS that one revolution would take at current speed (The time tooth 1 was last seen, minus the time it was seen prior to that) interrupts(); - return ldiv(US_IN_MINUTE, revolutionTime).quot; //Calc RPM based on last full revolution time (uses ldiv rather than div as US_IN_MINUTE is a long) + return (US_IN_MINUTE / revolutionTime); //Calc RPM based on last full revolution time (uses ldiv rather than div as US_IN_MINUTE is a long) } int getCrankAngle_missingTooth(int timePerDegree) { //This is the current angle ATDC the engine is at. This is the last known position based on what tooth was last 'seen'. It is only accurate to the resolution of the trigger wheel (Eg 36-1 is 10 degrees) int crankAngle = (toothCurrentCount - 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() - toothLastToothTime), timePerDegree).quot; //Estimate the number of degrees travelled since the last tooth + crankAngle += ( (micros() - toothLastToothTime) / timePerDegree); //Estimate the number of degrees travelled since the last tooth if (crankAngle > 360) { crankAngle -= 360; } return crankAngle; diff --git a/speeduino.ino b/speeduino.ino index 299baf03..3474c8f6 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -42,8 +42,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #endif #include "fastAnalog.h" -#define DIGITALIO_NO_MIX_ANALOGWRITE -#include #include struct config1 configPage1; @@ -342,8 +340,9 @@ void loop() { currentStatus.TPSlast = currentStatus.TPS; currentStatus.TPSlast_time = currentStatus.TPS_time; - currentStatus.tpsADC = fastMap1023toX(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 + int tpsTemp = analogRead(pinTPS); + currentStatus.tpsADC = fastMap1023toX(tpsTemp, 0, 1023, 0, 255); //Get the current raw TPS ADC value and map it into a byte + currentStatus.TPS = fastMap1023toX(tpsTemp, configPage1.tpsMin, configPage1.tpsMax, 0, 100); //Take the raw TPS ADC value and convert it into a TPS% based on the calibrated values currentStatus.TPS_time = currentLoopTime; } @@ -353,7 +352,8 @@ void loop() 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 currentStatus.O2ADC = map(analogRead(pinO2), 0, 1023, 0, 511); //Get the current O2 value. Calibration is from AFR values 7.35 to 22.4. This is the correct calibration for an Innovate Wideband 0v - 5V unit. Proper calibration is still a WIP - currentStatus.battery10 = map(analogRead(pinBat), 0, 1023, 0, 245); //Get the current raw Battery value. Permissible values are from 0v to 24.5v (245) + //currentStatus.battery10 = map(analogRead(pinBat), 0, 1023, 0, 245); //Get the current raw Battery value. Permissible values are from 0v to 24.5v (245) + currentStatus.battery10 = fastMap1023toX(analogRead(pinBat), 0, 1023, 0, 245); //Get the current raw Battery value. Permissible values are from 0v to 24.5v (245) //currentStatus.batADC = map(analogRead(pinBat), 0, 1023, 0, 255); //Get the current raw Battery value currentStatus.coolant = cltCalibrationTable[currentStatus.cltADC] - CALIBRATION_TEMPERATURE_OFFSET; //Temperature calibration values are stored as positive bytes. We subtract 40 from them to allow for negative temperatures @@ -423,7 +423,7 @@ void loop() int tempStartAngle; //How fast are we going? Need to know how long (uS) it will take to get from one tooth to the next. We then use that to estimate how far we are between the last tooth and the next one - 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 + 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 / ) //Determine the current crank angle int crankAngle = getCrankAngle(timePerDegree); diff --git a/table.ino b/table.ino index cd1d2dea..44dfefeb 100644 --- a/table.ino +++ b/table.ino @@ -141,14 +141,14 @@ int get3DTableValue(struct table3D fromTable, int Y, int X) // This is because the important tables (fuel and injection) will have the highest RPM at the top of the X axis, so starting there will mean the best case occurs when the RPM is highest (And hence the CPU is needed most) int xMinValue = fromTable.axisX[0]; int xMaxValue = fromTable.axisX[fromTable.xSize-1]; - int xMin = 0; - int xMax = 0; + byte xMin = 0; + byte xMax = 0; //If the requested X value is greater/small than the maximum/minimum bin, reset X to be that value if(X > xMaxValue) { X = xMaxValue; } if(X < xMinValue) { X = xMinValue; } - for (int x = fromTable.xSize-1; x >= 0; x--) + for (byte x = fromTable.xSize-1; x >= 0; x--) { //Checks the case where the X value is exactly what was requested if ( (X == fromTable.axisX[x]) || (x == 0) ) @@ -173,14 +173,14 @@ int get3DTableValue(struct table3D fromTable, int Y, int X) //Loop through the Y axis bins for the min/max pair int yMaxValue = fromTable.axisY[0]; int yMinValue = fromTable.axisY[fromTable.ySize-1]; - int yMin = 0; - int yMax = 0; + byte yMin = 0; + byte yMax = 0; //If the requested Y value is greater/small than the maximum/minimum bin, reset Y to be that value if(Y > yMaxValue) { Y = yMaxValue; } if(Y < yMinValue) { Y = yMinValue; } - for (int y = fromTable.ySize-1; y >= 0; y--) + for (byte y = fromTable.ySize-1; y >= 0; y--) { //Checks the case where the Y value is exactly what was requested if ( (Y == fromTable.axisY[y]) || (y==0) ) @@ -257,7 +257,9 @@ int get3DTableValue(struct table3D fromTable, int Y, int X) if (yMaxValue == yMinValue) { q = ((long)(Y - yMinValue) << 8); } else - { q = ((long)(Y - yMaxValue) << 8) / (yMinValue - yMaxValue); } + { + q = ((long)(Y - yMaxValue) << 8) / (yMinValue - yMaxValue); + } int m = ((257-p) * (257-q)) >> 8; int n = (p * (257-q)) >> 8;