From 6a8ab1015bb45a87844b9c8b30a5972d4394682c Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Tue, 24 Oct 2017 14:08:32 +1100 Subject: [PATCH] Move the pulse width function into the speeduino.h scope --- reference/speeduino.ini | 2 -- speeduino/speeduino.h | 6 ++++ speeduino/speeduino.ino | 57 +++++++++++++++++++++++++++++++++++-- speeduino/utils.h | 3 -- speeduino/utils.ino | 63 ----------------------------------------- 5 files changed, 61 insertions(+), 70 deletions(-) create mode 100644 speeduino/speeduino.h diff --git a/reference/speeduino.ini b/reference/speeduino.ini index 8fd9cfbd..4692e44b 100644 --- a/reference/speeduino.ini +++ b/reference/speeduino.ini @@ -2173,8 +2173,6 @@ cmdtestspk450dc = "E\x03\x0C" indicator = { boostCutOut }, "Ign Cut OFF", "Ign Cut (Boost)", white, black, red, black indicator = { sync }, "No Sync", "Sync", white, black, green, black - - ;------------------------------------------------------------------------------- [OutputChannels] diff --git a/speeduino/speeduino.h b/speeduino/speeduino.h new file mode 100644 index 00000000..1d0a76ad --- /dev/null +++ b/speeduino/speeduino.h @@ -0,0 +1,6 @@ +#ifndef SPEEDUINO_H +#define SPEEDUINO_H + +static inline unsigned int PW(int REQ_FUEL, byte VE, long MAP, int corrections, int injOpen) __attribute__((always_inline)); + +#endif diff --git a/speeduino/speeduino.ino b/speeduino/speeduino.ino index 70f6b6fc..348b13e8 100644 --- a/speeduino/speeduino.ino +++ b/speeduino/speeduino.ino @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. //https://developer.mbed.org/handbook/C-Data-Types #include //************************************************ +#include "speeduino.h" #include "globals.h" #include "utils.h" #include "table.h" @@ -910,17 +911,16 @@ void loop() { //Speed Density currentStatus.VE = get3DTableValue(&fuelTable, currentStatus.MAP, currentStatus.RPM); //Perform lookup into fuel map for RPM vs MAP value - currentStatus.PW1 = PW_SD(req_fuel_uS, currentStatus.VE, currentStatus.MAP, currentStatus.corrections, inj_opentime_uS); currentStatus.advance = get3DTableValue(&ignitionTable, currentStatus.MAP, currentStatus.RPM) - OFFSET_IGNITION; //As above, but for ignition advance } else { //Alpha-N currentStatus.VE = get3DTableValue(&fuelTable, currentStatus.TPS, currentStatus.RPM); //Perform lookup into fuel map for RPM vs TPS value - currentStatus.PW1 = PW_AN(req_fuel_uS, currentStatus.VE, currentStatus.TPS, currentStatus.corrections, inj_opentime_uS); //Calculate pulsewidth using the Alpha-N algorithm (in uS) currentStatus.advance = get3DTableValue(&ignitionTable, currentStatus.TPS, currentStatus.RPM) - OFFSET_IGNITION; //As above, but for ignition advance } + currentStatus.PW1 = PW(req_fuel_uS, currentStatus.VE, currentStatus.MAP, currentStatus.corrections, inj_opentime_uS); currentStatus.advance = correctionsIgn(currentStatus.advance); int injector1StartAngle = 0; @@ -1356,6 +1356,11 @@ void loop() ign1EndFunction ); } + if(ignition1EndAngle > crankAngle && configPage2.StgCycles == 0) + { + unsigned long uSToEnd = degreesToUS( (ignition1EndAngle - crankAngle) ); + //refreshIgnitionSchedule1( uSToEnd + fixedCrankingOverride ); + } tempCrankAngle = crankAngle - channel2IgnDegrees; if( tempCrankAngle < 0) { tempCrankAngle += CRANK_ANGLE_MAX_IGN; } @@ -1443,3 +1448,51 @@ void loop() } //Ignition schedules on } //Has sync and RPM } //loop() + +/* + This function retuns a pulsewidth time (in us) given the following: + REQ_FUEL + VE: Lookup from the main fuel table. This can either have been MAP or TPS based, depending on the algorithm used + MAP: In KPa, read from the sensor (This is used when performing a multiply of the map only. It is applicable in both Speed density and Alpha-N) + GammaE: Sum of Enrichment factors (Cold start, acceleration). This is a multiplication factor (Eg to add 10%, this should be 110) + injDT: Injector dead time. The time the injector take to open minus the time it takes to close (Both in uS) +*/ +static inline unsigned int PW(int REQ_FUEL, byte VE, long MAP, int corrections, int injOpen) +{ + //Standard float version of the calculation + //return (REQ_FUEL * (float)(VE/100.0) * (float)(MAP/100.0) * (float)(TPS/100.0) * (float)(corrections/100.0) + injOpen); + //Note: The MAP and TPS portions are currently disabled, we use VE and corrections only + uint16_t iVE, iCorrections; + uint16_t iMAP = 100; + uint16_t iAFR = 147; + + //100% float free version, does sacrifice a little bit of accuracy, but not much. + iVE = ((unsigned int)VE << 7) / 100; + if ( configPage1.multiplyMAP == true ) { + iMAP = ((unsigned int)MAP << 7) / currentStatus.baro; //Include multiply MAP (vs baro) if enabled + } + if ( (configPage1.includeAFR == true) && (configPage3.egoType == 2)) { + iAFR = ((unsigned int)currentStatus.O2 << 7) / currentStatus.afrTarget; //Include AFR (vs target) if enabled + } + iCorrections = (corrections << 7) / 100; + + + unsigned long intermediate = ((long)REQ_FUEL * (long)iVE) >> 7; //Need to use an intermediate value to avoid overflowing the long + if ( configPage1.multiplyMAP == true ) { + intermediate = (intermediate * (unsigned long)iMAP) >> 7; + } + if ( (configPage1.includeAFR == true) && (configPage3.egoType == 2) ) { + intermediate = (intermediate * (unsigned long)iAFR) >> 7; //EGO type must be set to wideband for this to be used + } + intermediate = (intermediate * (unsigned long)iCorrections) >> 7; + if (intermediate != 0) + { + //If intermeditate is not 0, we need to add the opening time (0 typically indicates that one of the full fuel cuts is active) + intermediate += injOpen; //Add the injector opening time + if ( intermediate > 65535) + { + intermediate = 65535; //Make sure this won't overflow when we convert to uInt. This means the maximum pulsewidth possible is 65.535mS + } + } + return (unsigned int)(intermediate); +} diff --git a/speeduino/utils.h b/speeduino/utils.h index 7632d05e..74f1ccb9 100644 --- a/speeduino/utils.h +++ b/speeduino/utils.h @@ -8,9 +8,6 @@ These are some utility functions and variables used through the main code uint16_t freeRam (); void setPinMapping(byte boardID); -unsigned int PW(); -unsigned int PW_SD(); -unsigned int PW_AN(); //This is dumb, but it'll do for now to get things compiling #if defined(CORE_STM32) diff --git a/speeduino/utils.ino b/speeduino/utils.ino index 1e01bd87..5cfbab79 100644 --- a/speeduino/utils.ino +++ b/speeduino/utils.ino @@ -824,66 +824,3 @@ void initialiseTriggers() break; } } -/* - 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 - GammaE: Sum of Enrichment factors (Cold start, acceleration). This is a multiplication factor (Eg to add 10%, this should be 110) - injDT: Injector dead time. The time the injector take to open minus the time it takes to close (Both in uS) - TPS: Throttle position (0% to 100%) - - This function is called by PW_SD and PW_AN for speed0density and pure Alpha-N calculations respectively. -*/ -unsigned int PW(int REQ_FUEL, byte VE, long MAP, int corrections, int injOpen) -{ - //Standard float version of the calculation - //return (REQ_FUEL * (float)(VE/100.0) * (float)(MAP/100.0) * (float)(TPS/100.0) * (float)(corrections/100.0) + injOpen); - //Note: The MAP and TPS portions are currently disabled, we use VE and corrections only - uint16_t iVE, iCorrections; - uint16_t iMAP = 100; - uint16_t iAFR = 147; - - //100% float free version, does sacrifice a little bit of accuracy, but not much. - iVE = ((unsigned int)VE << 7) / 100; - if ( configPage1.multiplyMAP == true ) { - iMAP = ((unsigned int)MAP << 7) / currentStatus.baro; //Include multiply MAP (vs baro) if enabled - } - if ( (configPage1.includeAFR == true) && (configPage3.egoType == 2)) { - iAFR = ((unsigned int)currentStatus.O2 << 7) / currentStatus.afrTarget; //Include AFR (vs target) if enabled - } - iCorrections = (corrections << 7) / 100; - - - unsigned long intermediate = ((long)REQ_FUEL * (long)iVE) >> 7; //Need to use an intermediate value to avoid overflowing the long - if ( configPage1.multiplyMAP == true ) { - intermediate = (intermediate * (unsigned long)iMAP) >> 7; - } - if ( (configPage1.includeAFR == true) && (configPage3.egoType == 2) ) { - intermediate = (intermediate * (unsigned long)iAFR) >> 7; //EGO type must be set to wideband for this to be used - } - intermediate = (intermediate * (unsigned long)iCorrections) >> 7; - if (intermediate != 0) - { - //If intermeditate is not 0, we need to add the opening time (0 typically indicates that one of the full fuel cuts is active) - intermediate += injOpen; //Add the injector opening time - if ( intermediate > 65535) - { - intermediate = 65535; //Make sure this won't overflow when we convert to uInt. This means the maximum pulsewidth possible is 65.535mS - } - } - return (unsigned int)(intermediate); - -} - -//Convenience functions for Speed Density and Alpha-N -unsigned int PW_SD(int REQ_FUEL, byte VE, long MAP, int corrections, int injOpen) -{ - return PW(REQ_FUEL, VE, MAP, corrections, injOpen); - //return PW(REQ_FUEL, VE, 100, corrections, injOpen); -} - -unsigned int PW_AN(int REQ_FUEL, byte VE, byte TPS, int corrections, int injOpen) -{ - return PW(REQ_FUEL, VE, currentStatus.MAP, corrections, injOpen); -}