From 7e048b96f29b7efed25a1f3fe046d67ed101c495 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Tue, 6 May 2014 21:07:49 +1000 Subject: [PATCH] More complete corrections code. Large cleanup in a few areas --- corrections.h | 3 ++- corrections.ino | 41 +++++++++++++++++++++++++++++++------ globals.h | 34 +++++++++++++++--------------- reference/speeduino 0.1.ini | 10 ++++----- speeduino.ino | 17 +++++---------- timers.h | 2 +- timers.ino | 18 +++++++++------- utils.h | 4 ++-- 8 files changed, 78 insertions(+), 51 deletions(-) diff --git a/corrections.h b/corrections.h index f0d375d8..d3bd31c7 100644 --- a/corrections.h +++ b/corrections.h @@ -2,9 +2,10 @@ All functions in the gamma file return */ -static byte numCorrections = 2; +//static byte numCorrections = 2; byte correctionsTotal(); byte correctionWUE(); //Warmup enrichment byte correctionASE(); //After Start Enrichment byte correctionAccel(); //Acceleration Enrichment +byte correctionsFloodClear(); //Check for flood clear on cranking diff --git a/corrections.ino b/corrections.ino index cf817a52..0e546fde 100644 --- a/corrections.ino +++ b/corrections.ino @@ -2,14 +2,18 @@ byte correctionsTotal() { - int sumCorrections = (correctionWUE() * correctionASE()) / 100; - sumCorrections = (sumCorrections * correctionAccel()) / 100; + int sumCorrections = 100; + sumCorrections = div((sumCorrections * correctionWUE()), 100).quot; + sumCorrections = div((sumCorrections * correctionASE()), 100).quot; + sumCorrections = div((sumCorrections * correctionAccel()), 100).quot; + sumCorrections = div((sumCorrections * correctionFloodClear()), 100).quot; return sumCorrections; } byte correctionWUE() { - + //Not yet implemented + return 100; } byte correctionASE() @@ -19,10 +23,10 @@ byte correctionASE() { BIT_SET(currentStatus.engine,3); //Mark ASE as active. return configPage1.asePct; - } else - { - BIT_CLEAR(currentStatus.engine,3); //Mark ASE as inactive. } + + BIT_CLEAR(currentStatus.engine,3); //Mark ASE as inactive. + return 100; } @@ -36,4 +40,29 @@ byte correctionAccel() { int rateOfChange = (1000000 / (currentLoopTime - previousLoopTime)) * (currentStatus.TPS - currentStatus.TPSlast); //This is the % per second that the TPS has moved + if (rateOfChange > configPage1.tpsThresh) + { + return 100 + table2D_getValue(taeTable, rateOfChange); + } + + return 100; +} + +/* +Simple check to see whether we are cranking with the TPS above the flood clear threshold +This function always returns either 100 or 0 +*/ + +byte correctionFloodClear() +{ + if(BIT_CHECK(currentStatus.engine, BIT_ENGINE_CRANK)) + { + //Engine is currently cranking, check what the TPS is + if(currentStatus.TPS >= configPage2.floodClear) + { + //Engine is cranking and TPS is above threshold. Cut all fuel + return 0; + } + } + return 100; } diff --git a/globals.h b/globals.h index d1ca0084..96f5efd7 100644 --- a/globals.h +++ b/globals.h @@ -8,26 +8,26 @@ const byte page_size = 128; //Handy bitsetting macros #define BIT_SET(a,b) ((a) |= (1<<(b))) #define BIT_CLEAR(a,b) ((a) &= ~(1<<(b))) +#define BIT_CHECK(var,pos) ((var) & (1<<(pos))) -//Define masks for engine -#define ENGINE_RUN 1 // Engine running -#define ENGINE_CRANK 2 // Engine cranking -#define ENGINE_ASE 4 // after start enrichment (ASE) -#define ENGINE_WARMUP 8 // Engine in warmup -#define ENGINE_TPS 16 // in TPS acceleration mode -#define ENGINE_ACC 32 // in deceleration mode -#define ENGINE_MAP 64 // in MAP acceleration mode -#define ENGINE_IDLE 128 // idle on +//Define bit positions within engine virable +#define BIT_ENGINE_RUN 0 // Engine running +#define BIT_ENGINE_CRANK 1 // Engine cranking +#define BIT_ENGINE_ASE 2 // after start enrichment (ASE) +#define BIT_ENGINE_WARMUP 3 // Engine in warmup +#define BIT_ENGINE_TPS 4 // in TPS acceleration mode +#define BIT_ENGINE_ACC 5 // in deceleration mode +#define BIT_ENGINE_MAP 6 // in MAP acceleration mode +#define BIT_ENGINE_IDLE 7 // idle on //Define masks for Squirt -#define SQUIRT_INJ1 1 //inj1 Squirt -#define SQUIRT_INJ2 2 //inj2 Squirt -#define SQUIRT_SCHSQRT 4 //Scheduled to squirt -#define SQUIRT_SQRTING 8 //Squirting -#define SQUIRT_INJ2SCHED 16 -#define SQUIRT_INJ2SQRT 32 //Injector2 (Schedule2) -#define SQUIRT_BOOSTCTRLOFF 64 //Squirting Injector 2 - +#define BIT_SQUIRT_INJ1 0 //inj1 Squirt +#define BIT_SQUIRT_INJ2 1 //inj2 Squirt +#define BIT_SQUIRT_SCHSQRT 2 //Scheduled to squirt +#define BIT_SQUIRT_SQRTING 3 //Squirting +#define BIT_SQUIRT_INJ2SCHED 4 +#define BIT_SQUIRT_INJ2SQRT 5 //Injector2 (Schedule2) +#define BIT_SQUIRT_BOOSTCTRLOFF 6 //Squirting Injector 2 //The status struct contains the current values for all 'live' variables //In current version this is 64 bytes diff --git a/reference/speeduino 0.1.ini b/reference/speeduino 0.1.ini index cb49d196..5a1b6d9e 100644 --- a/reference/speeduino 0.1.ini +++ b/reference/speeduino 0.1.ini @@ -90,13 +90,14 @@ asePct = scalar, U08, 82, "%", 1.0, 0.0, 0.0, 95.0, 0 aseCount = scalar, U08, 83, "", 1.0, 0.0, 0.0, 254.0, 0 wueBins = array, U08, 84, [ 10], "%", 1.0, 0.0, 100.0, 255.0, 0 - taeBins = array, U08, 94, [ 4], "ms", 0.1, 0.0, 0.0, 25.5, 1 + ;taeBins = array, U08, 94, [ 4], "ms", 0.1, 0.0, 0.0, 25.5, 1 + ;Note that these are currently unused. See the taeBins and taeRates variables in table 2 taeBins1 = scalar, U08, 94, "ms", 0.1, 0.0, 0.0, 25.5, 1 taeBins2 = scalar, U08, 95, "ms", 0.1, 0.0, 0.0, 25.5, 1 taeBins3 = scalar, U08, 96, "ms", 0.1, 0.0, 0.0, 25.5, 1 taeBins4 = scalar, U08, 97, "ms", 0.1, 0.0, 0.0, 25.5, 1 taeColdA = scalar, U08, 98, "ms", 0.1, 0.0, 0.0, 25.5, 1 - tpsThresh = scalar, U08, 99, "v/s",0.1953125, 0.0, 0.2, 49.8, 3 + tpsThresh = scalar, U08, 99, "%/s", 1.0, 0.0, 0.0, 255.0, 0 taeTime = scalar, U08, 100, "ms", 0.1, 0.0, 0.0, 25.5, 1 tdePct = scalar, U08, 101, "%", 1.0, 0.0, 0.0, 255.0, 0 #if CELSIUS @@ -202,7 +203,7 @@ ;TPS based acceleration enrichment taeBins = array, U08, 103, [ 4], "%", 1, 0.00000, 0.00, 255, 0 - taeRates = array, U08, 107, [ 4], "%/s", 1.0000, 0.00000, 0.00, 255.0, 1 ; 4 bytes + taeRates = array, U08, 107, [ 4], "%/s", 1. 0000, 0.00000, 0.00, 255.0, 1 ; 4 bytes wueRates = array, U08, 111, [ 10], "C", 1.0, 0.0, 100.0, 255.0, 0 unused121 = scalar, U08, 121, "none", 0, 0, 0, 0, 0 unused122 = scalar, U08, 122, "none", 0, 0, 0, 0, 0 @@ -294,8 +295,7 @@ field = "Accel Time", taeTime field = "Cold Accel Enrichment", taeColdA field = "Cold Accel Mult", taeColdM - field = "Decel Fuel Amount", tdePct - field = "Acceleration Enrichment Bins (ms)" + field = "Decel Fuel Amount", tdePct dialog = accelEnrichments_north, "", xAxis panel = time_accel_tpsdot_curve diff --git a/speeduino.ino b/speeduino.ino index d3ecfb61..b0b3ec4d 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -87,20 +87,13 @@ void setup() //dummyIgnitionTable(&ignitionTable); loadConfig(); - - //Repoint the 2D table structs to the config pages + //Repoint the 2D table structs to the config pages that were just loaded taeTable.xSize = 4; taeTable.values = configPage2.taeValues; taeTable.axisX = configPage2.taeBins; WUETable.xSize = 10; WUETable.values = configPage1.wueValues; WUETable.axisX = configPage2.wueBins; - /* - //Initialise table sizes (Must be done before the call to loadConfig()) - table2D_setSize(&taeTable, 4); //TPS acceleration enrichment (4x X axis points) - table2D_setSize(&WUETable, 10); //Warm Up Enrichment (10x X axis points) - //3D tables are currently 8x8 fixed size and so don't need initialising (This is on the TODO list to change) - */ //Need to check early on whether the coil charging is inverted. If this is not set straight away it can cause an unwanted spark at bootup if(configPage2.IgInv == 1) { coilHIGH = LOW, coilLOW = HIGH; } @@ -249,13 +242,13 @@ void loop() //If it is, check is we're running or cranking if(currentStatus.RPM > configPage2.crankRPM) { //Sets the engine running bit, clears the engine cranking bit - BIT_SET(currentStatus.engine, 0); - BIT_CLEAR(currentStatus.engine, 1); + BIT_SET(currentStatus.engine, BIT_ENGINE_RUN); + BIT_CLEAR(currentStatus.engine, BIT_ENGINE_CRANK); } else { //Sets the engine cranking bit, clears the engine running bit - BIT_SET(currentStatus.engine, 1); - BIT_CLEAR(currentStatus.engine, 0); + BIT_SET(currentStatus.engine, BIT_ENGINE_CRANK); + BIT_CLEAR(currentStatus.engine, BIT_ENGINE_RUN); currentStatus.runSecs = 0; //We're cranking (hopefully), so reset the engine run time to prompt ASE. } } diff --git a/timers.h b/timers.h index b935add8..b078a5f0 100644 --- a/timers.h +++ b/timers.h @@ -17,7 +17,7 @@ Hence we will preload the timer with 99 cycles to leave 156 until overflow (~10m */ -volatile int loopGen; +volatile int loop250ms; volatile int loopSec; diff --git a/timers.ino b/timers.ino index 0b79c255..41f2ca38 100644 --- a/timers.ino +++ b/timers.ino @@ -1,5 +1,9 @@ +/* +Timers are used for having actions performed repeatedly at a fixed interval (Eg every 100ms) +They should not be confused with Schedulers, which are for performing an action once at a given point of time in the future - +Timers are typically low resolution (Compared to Schedulers), with maximum frequency currently being approximately every 10ms +*/ void initialiseTimers() { @@ -14,20 +18,20 @@ void initialiseTimers() //Timer2 Overflow Interrupt Vector, called when the timer overflows. -//SHOULD execute every ~10ms. +//Executes every ~10ms. ISR(TIMER2_OVF_vect) { //Increment Loop Counters - loopGen++; + loop250ms++; loopSec++; //Loop executed every 250ms loop (10ms x 25 = 250ms) - if (loopGen == 25) + //Anything inside this if statement will run every 250ms. + if (loop250ms == 25) { - loopGen = 0; //Reset Counter. - //INSERT 250ms Code here. + loop250ms = 0; //Reset Counter. } //Loop executed every 1 second (10ms x 100 = 1000ms) @@ -38,7 +42,7 @@ ISR(TIMER2_OVF_vect) //************************************************************************************************************************************************** //This updates the runSecs variable //If the engine is running or cranking, we need ot update the run time counter. - if (((currentStatus.engine & ENGINE_RUN) || (currentStatus.engine & ENGINE_CRANK))) + if ((BIT_CHECK(currentStatus.engine, BIT_ENGINE_RUN) || BIT_CHECK(currentStatus.engine, BIT_ENGINE_CRANK))) { //NOTE - There is a potential for a ~1sec gap between engine crank starting and ths runSec number being incremented. This may delay ASE! if (currentStatus.runSecs <= 254) //Ensure we cap out at 255 and don't overflow. (which would reset ASE) { currentStatus.runSecs++; } //Increment our run counter by 1 second. diff --git a/utils.h b/utils.h index 535b878b..30859e75 100644 --- a/utils.h +++ b/utils.h @@ -15,7 +15,7 @@ int AIRDEN(int MAP, int temp) } /* -This function retuns a pulsewidth time (in us) using a hybrid Alpha-N algorithm, given the following: +This function retuns a pulsewidth time (in us) using a either Alpha-N or Speed Density algorithms, given the following: REQ_FUEL VE: Lookup from the main MAP vs RPM fuel table MAP: In KPa, read from the sensor @@ -45,7 +45,7 @@ int PW(int REQ_FUEL, byte VE, byte MAP, int corrections, int injOpen, byte TPS) } -//Convenience function for Speed Density +//Convenience functions for Speed Density and Alpha-N int PW_SD(int REQ_FUEL, byte VE, byte MAP, int corrections, int injOpen) { return PW(REQ_FUEL, VE, MAP, corrections, injOpen, 100); //Just use 1 in place of the TPS