From 372f6da64eae7a76630d2f4e876f3a9f2b617c96 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Fri, 5 Jun 2015 18:02:47 +1000 Subject: [PATCH] Switch 2D tables to pointers --- corrections.ino | 6 +++--- decoders.ino | 5 +++-- speeduino.ino | 2 +- table.h | 5 ++++- table.ino | 50 ++++++++++++++++++++++++------------------------- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/corrections.ino b/corrections.ino index 50d5fd7a..69f039fa 100644 --- a/corrections.ino +++ b/corrections.ino @@ -54,7 +54,7 @@ byte correctionWUE() //Possibly reduce the frequency this runs at (Costs about 50 loops per second) if (currentStatus.coolant > (WUETable.axisX[9] - CALIBRATION_TEMPERATURE_OFFSET)) { BIT_CLEAR(currentStatus.engine, BIT_ENGINE_WARMUP); return 100; } //This prevents us doing the 2D lookup if we're already up to temp BIT_SET(currentStatus.engine, BIT_ENGINE_WARMUP); - return table2D_getValue(WUETable, currentStatus.coolant + CALIBRATION_TEMPERATURE_OFFSET); + return table2D_getValue(&WUETable, currentStatus.coolant + CALIBRATION_TEMPERATURE_OFFSET); } /* @@ -121,7 +121,7 @@ byte correctionAccel() { BIT_SET(currentStatus.engine, BIT_ENGINE_ACC); //Mark accleration enrichment as active. currentStatus.TAEEndTime = micros() + ((unsigned long)configPage1.taeTime * 10000); //Set the time in the future where the enrichment will be turned off. taeTime is stored as mS / 10, so multiply it by 100 to get it in uS - return 100 + table2D_getValue(taeTable, currentStatus.tpsDOT); + return 100 + table2D_getValue(&taeTable, currentStatus.tpsDOT); } //If we reach here then TAE is neither on, nor does it need to be turned on. @@ -154,7 +154,7 @@ Uses a 2D enrichment table (WUETable) where the X axis is engine temp and the Y byte correctionsBatVoltage() { if (currentStatus.battery10 > (injectorVCorrectionTable.axisX[5])) { return injectorVCorrectionTable.values[injectorVCorrectionTable.xSize-1]; } //This prevents us doing the 2D lookup if the voltage is above maximum - return table2D_getValue(injectorVCorrectionTable, currentStatus.battery10); + return table2D_getValue(&injectorVCorrectionTable, currentStatus.battery10); } /* diff --git a/decoders.ino b/decoders.ino index a0264a18..152511b8 100644 --- a/decoders.ino +++ b/decoders.ino @@ -36,10 +36,11 @@ void triggerPri_missingTooth() { // http://www.msextra.com/forums/viewtopic.php?f=94&t=22976 // http://www.megamanual.com/ms2/wheel.htm + noInterrupts(); curTime = micros(); curGap = curTime - toothLastToothTime; - 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) + 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) toothCurrentCount++; //Increment the tooth counter //High speed tooth logging history @@ -66,7 +67,7 @@ void triggerPri_missingTooth() toothLastMinusOneToothTime = toothLastToothTime; toothLastToothTime = curTime; - + interrupts(); } void triggerSec_missingTooth(){ return; } //This function currently is not used diff --git a/speeduino.ino b/speeduino.ino index a29782cf..6c7b9ec9 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -485,7 +485,7 @@ void loop() if ( BIT_CHECK(currentStatus.engine, BIT_ENGINE_CRANK) ) { currentStatus.dwell = (configPage2.dwellCrank * 100); } else { currentStatus.dwell = (configPage2.dwellRun * 100); } //Pull battery voltage based dwell correction and apply if needed - currentStatus.dwellCorrection = table2D_getValue(dwellVCorrectionTable, currentStatus.battery10); + currentStatus.dwellCorrection = table2D_getValue(&dwellVCorrectionTable, currentStatus.battery10); if (currentStatus.dwellCorrection != 100) { currentStatus.dwell = divs100(currentStatus.dwell) * currentStatus.dwellCorrection; } int dwellAngle = (div(currentStatus.dwell, timePerDegree).quot ); //Convert the dwell time to dwell angle based on the current engine speed diff --git a/table.h b/table.h index c71e30e4..1548d215 100644 --- a/table.h +++ b/table.h @@ -18,6 +18,9 @@ struct table2D { int *values16; int *axisX16; + + //Store the last X and Y coordinates in the table. This is used to make the next check faster + byte lastXMax, lastXMin; }; void table2D_setSize(struct table2D targetTable, byte newSize); @@ -50,6 +53,6 @@ Eg: 2x2 table */ int get3DTableValue(struct table3D *fromTable, int, int); -int table2D_getValue(struct table2D, int); +int table2D_getValue(struct table2D *fromTable, int); #endif // TABLE_H diff --git a/table.ino b/table.ino index 0ee362a9..13fb64e2 100644 --- a/table.ino +++ b/table.ino @@ -35,21 +35,21 @@ ie: Given a value on the X axis, it returns a Y value that coresponds to the poi This function must take into account whether a table contains 8-bit or 16-bit values. Unfortunately this means many of the lines are duplicated depending on this */ -int table2D_getValue(struct table2D fromTable, int X) +int table2D_getValue(struct table2D *fromTable, int X) { int xMinValue, xMaxValue; - if (fromTable.valueSize == SIZE_BYTE) + if (fromTable->valueSize == SIZE_BYTE) { //Byte version - xMinValue = fromTable.axisX[0]; - xMaxValue = fromTable.axisX[fromTable.xSize-1]; + xMinValue = fromTable->axisX[0]; + xMaxValue = fromTable->axisX[fromTable->xSize-1]; } else { //int version - xMinValue = fromTable.axisX16[0]; - xMaxValue = fromTable.axisX16[fromTable.xSize-1]; + xMinValue = fromTable->axisX16[0]; + xMaxValue = fromTable->axisX16[fromTable->xSize-1]; } int xMin = 0; int xMax = 0; @@ -58,21 +58,21 @@ int table2D_getValue(struct table2D fromTable, int X) if(X > xMaxValue) { X = xMaxValue; } if(X < xMinValue) { X = xMinValue; } - for (int x = fromTable.xSize-1; x >= 0; x--) + for (int x = fromTable->xSize-1; x >= 0; x--) { - if (fromTable.valueSize == SIZE_BYTE) + if (fromTable->valueSize == SIZE_BYTE) { //Byte version //Checks the case where the X value is exactly what was requested - if ( (X == fromTable.axisX[x]) || (x == 0) ) + if ( (X == fromTable->axisX[x]) || (x == 0) ) { - return fromTable.values[x]; //Simply return the coresponding value + return fromTable->values[x]; //Simply return the coresponding value } //Normal case - if ( (X <= fromTable.axisX[x]) && (X > fromTable.axisX[x-1]) ) + if ( (X <= fromTable->axisX[x]) && (X > fromTable->axisX[x-1]) ) { - xMaxValue = fromTable.axisX[x]; - xMinValue = fromTable.axisX[x-1]; + xMaxValue = fromTable->axisX[x]; + xMinValue = fromTable->axisX[x-1]; xMax = x; xMin = x-1; break; @@ -81,15 +81,15 @@ int table2D_getValue(struct table2D fromTable, int X) else { //int version - if ( (X == fromTable.axisX16[x]) || (x == 0) ) + if ( (X == fromTable->axisX16[x]) || (x == 0) ) { - return fromTable.values16[x]; //Simply return the coresponding value + return fromTable->values16[x]; //Simply return the coresponding value } //Normal case - if ( (X <= fromTable.axisX16[x]) && (X > fromTable.axisX16[x-1]) ) + if ( (X <= fromTable->axisX16[x]) && (X > fromTable->axisX16[x-1]) ) { - xMaxValue = fromTable.axisX16[x]; - xMinValue = fromTable.axisX16[x-1]; + xMaxValue = fromTable->axisX16[x]; + xMinValue = fromTable->axisX16[x-1]; xMax = x; xMin = x-1; break; @@ -107,23 +107,23 @@ int table2D_getValue(struct table2D fromTable, int X) //Non-Float version int yVal; - if (fromTable.valueSize == SIZE_BYTE) + if (fromTable->valueSize == SIZE_BYTE) { //Byte version - yVal = ((long)(m << 6) / n) * (abs(fromTable.values[xMax] - fromTable.values[xMin])); + yVal = ((long)(m << 6) / n) * (abs(fromTable->values[xMax] - fromTable->values[xMin])); yVal = (yVal >> 6); - if (fromTable.values[xMax] > fromTable.values[xMin]) { yVal = fromTable.values[xMin] + yVal; } - else { yVal = fromTable.values[xMin] - yVal; } + if (fromTable->values[xMax] > fromTable->values[xMin]) { yVal = fromTable->values[xMin] + yVal; } + else { yVal = fromTable->values[xMin] - yVal; } } else { //int version - yVal = ((long)(m << 6) / n) * (abs(fromTable.values16[xMax] - fromTable.values16[xMin])); + yVal = ((long)(m << 6) / n) * (abs(fromTable->values16[xMax] - fromTable->values16[xMin])); yVal = (yVal >> 6); - if (fromTable.values[xMax] > fromTable.values16[xMin]) { yVal = fromTable.values16[xMin] + yVal; } - else { yVal = fromTable.values16[xMin] - yVal; } + if (fromTable->values[xMax] > fromTable->values16[xMin]) { yVal = fromTable->values16[xMin] + yVal; } + else { yVal = fromTable->values16[xMin] - yVal; } } return yVal;