From 17cc712326c3a827927daad6af1ed85aa0f1853b Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Wed, 25 Jan 2017 11:10:12 +1100 Subject: [PATCH] Considerably better rpmDOT (rpm/s) calculation. Faster and more accurate --- globals.h | 2 +- speeduino.ino | 6 ++---- timers.h | 2 ++ timers.ino | 14 +++++++++++++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/globals.h b/globals.h index e2bb6e7..7663f51 100644 --- a/globals.h +++ b/globals.h @@ -152,7 +152,7 @@ struct statuses { unsigned long TPSlast_time; //The time the previous TPS sample was taken byte tpsADC; //0-255 byte representation of the TPS byte tpsDOT; - int rpmDOT; + volatile int rpmDOT; byte VE; byte O2; byte O2_2; diff --git a/speeduino.ino b/speeduino.ino index 94501ea..718068d 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "math.h" #include "corrections.h" #include "timers.h" -//#include "display.h" +//#include "display.h" #include "decoders.h" #include "idle.h" #include "auxiliaries.h" @@ -806,10 +806,8 @@ void loop() unsigned long timeToLastTooth = (currentLoopTime - toothLastToothTime); if ( (timeToLastTooth < MAX_STALL_TIME) || (toothLastToothTime > currentLoopTime) ) //Check how long ago the last tooth was seen compared to now. If it was more than half a second ago then the engine is probably stopped. toothLastToothTime can be greater than currentLoopTime if a pulse occurs between getting the lastest time and doing the comparison { - int lastRPM = currentStatus.RPM; //Need to record this for rpmDOT calculation currentStatus.RPM = currentStatus.longRPM = getRPM(); //Long RPM is included here if(fuelPumpOn == false) { digitalWrite(pinFuelPump, HIGH); fuelPumpOn = true; } //Check if the fuel pump is on and turn it on if it isn't. - currentStatus.rpmDOT = ldiv(1000000, (currentLoopTime - previousLoopTime)).quot * (currentStatus.RPM - lastRPM); //This is the RPM per second that the engine has accelerated/decelleratedin the last loop } else { @@ -846,7 +844,7 @@ void loop() { readTPS(); - //Check for launching (clutch) can be done around here too + //Check for launching/flat shift (clutch) can be done around here too previousClutchTrigger = clutchTrigger; if(configPage3.launchHiLo) { clutchTrigger = digitalRead(pinLaunch); } else { clutchTrigger = !digitalRead(pinLaunch); } diff --git a/timers.h b/timers.h index 3b660d6..f21727a 100644 --- a/timers.h +++ b/timers.h @@ -19,10 +19,12 @@ Hence we will preload the timer with 131 cycles to leave 125 until overflow (1ms #ifndef TIMERS_H #define TIMERS_H +volatile int loop100ms; volatile int loop250ms; volatile int loopSec; volatile unsigned int dwellLimit_uS; +volatile uint16_t lastRPM_100ms; //Need to record this for rpmDOT calculation #if defined (CORE_TEENSY) IntervalTimer lowResTimer; diff --git a/timers.ino b/timers.ino index 9934267..e7be9b5 100644 --- a/timers.ino +++ b/timers.ino @@ -40,6 +40,7 @@ void initialiseTimers() #endif dwellLimit_uS = (1000 * configPage2.dwellLimit); + lastRPM_100ms = 0; } @@ -53,10 +54,11 @@ void oneMSInterval() //Most ARM chips can simply call a function { //Increment Loop Counters + loop100ms++; loop250ms++; loopSec++; -unsigned long targetOverdwellTime; + unsigned long targetOverdwellTime; //Overdwell check targetOverdwellTime = micros() - dwellLimit_uS; //Set a target time in the past that all coil charging must have begun after. If the coil charge began before this time, it's been running too long @@ -67,6 +69,16 @@ unsigned long targetOverdwellTime; if(ignitionSchedule3.Status == RUNNING) { if(ignitionSchedule3.startTime < targetOverdwellTime && configPage2.useDwellLim) { endCoil3Charge(); } } if(ignitionSchedule4.Status == RUNNING) { if(ignitionSchedule4.startTime < targetOverdwellTime && configPage2.useDwellLim) { endCoil4Charge(); } } if(ignitionSchedule5.Status == RUNNING) { if(ignitionSchedule5.startTime < targetOverdwellTime && configPage2.useDwellLim) { endCoil5Charge(); } } + + //Loop executed every 100ms loop + //Anything inside this if statement will run every 100ms. + if (loop100ms == 100) + { + loop100ms = 0; //Reset counter + + currentStatus.rpmDOT = (currentStatus.RPM - lastRPM_100ms) * 10; //This is the RPM per second that the engine has accelerated/decelleratedin the last loop + lastRPM_100ms = currentStatus.RPM; //Record the current RPM for next calc + } //Loop executed every 250ms loop (1ms x 250 = 250ms) //Anything inside this if statement will run every 250ms.