//Once the configs have been loaded, a number of one time calculations can be completed
req_fuel_uS=configPage1.reqFuel*1000;//Convert to uS and an int. This is the only variable to be used in calculations
triggerToothAngle=360/configPage2.triggerTeeth;//The number of degrees that passes from tooth to tooth
triggerActualTeeth=configPage2.triggerTeeth-configPage2.triggerMissingTeeth;//The number of physical teeth on the wheel. Doing this here saves us a calculation each time in the interrupt
req_fuel_uS=req_fuel_uS/engineSquirtsPerCycle;//The req_fuel calculation above gives the total required fuel (At VE 100%) in the full cycle. If we're doing more than 1 squirt per cycle then we need to split the amount accordingly. (Note that in a non-sequential 4-stroke setup you cannot have less than 2 squirts as you cannot determine the stroke to make the single squirt on)
//This sets the ADC (Analog to Digitial Converter) to run at 1Mhz, greatly reducing analog read times (MAP/TPS)
//1Mhz is the fastest speed permitted by the CPU without affecting accuracy
//Please see chapter 11 of 'Practical Arduino' (http://books.google.com.au/books?id=HsTxON1L6D4C&printsec=frontcover#v=onepage&q&f=false) for more details
//Can be disabled by removing the #include "fastAnalog.h" above
//Calculate the RPM based on the uS between the last 2 times tooth One was seen.
if((micros()-toothLastToothTime)<500000L)//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
{
noInterrupts();
unsignedlongrevolutionTime=(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)
currentStatus.O2=fastMap(analogRead(pinO2),0,1023,117,358);//Get the current O2 value. Calibration is from AFR values 7.35 to 22.4, then multiplied by 16 (<< 4). This is the correct calibration for an Innovate Wideband 0v - 5V unit
if(currentStatus.RPM>0)//Check if the engine is turning at all
{
//If it is, check is we're running or cranking
if(currentStatus.RPM>configPage2.crankRPM){BIT_SET(currentStatus.engine,0);BIT_CLEAR(currentStatus.engine,1);}//Sets the engine running bit, clears the engine cranking bit
else{BIT_SET(currentStatus.engine,1);BIT_CLEAR(currentStatus.engine,0);}//Sets the engine cranking bit, clears the engine running bit
currentStatus.PW=PW(req_fuel_uS,currentStatus.VE,currentStatus.MAP,100,engineInjectorDeadTime);//The 100 here is just a placeholder for any enrichment factors (Cold start, acceleration etc). To add 10% extra fuel, this would be 110
intcrankAngle=(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
//unsigned long timePerDegree = (toothLastToothTime - toothLastMinusOneToothTime) / (triggerToothAngle * configPage2.triggerTeeth); //The time (uS) it is currently taking to move 1 degree
unsignedlongtimePerDegree=fastDivide32((toothOneTime-toothOneMinusOneTime),(triggerToothAngle*configPage2.triggerTeeth));//The time (uS) it is currently taking to move 1 degree (fastDivide version)
crankAngle+=fastDivide32((micros()-toothLastToothTime),timePerDegree);//Estimate the number of degrees travelled since the last tooth (fastDivide version)
//int injectorStartAngle = 355 - (currentStatus.PW / timePerDegree); //This is a bit rough, but is based on the idea that all fuel needs to be delivered before the inlet valve opens. I am using 355 as the point at which the injector MUST be closed by. See http://www.extraefi.co.uk/sequential_fuel.html for more detail
//int ignitionStartAngle = 360 - ignitionAdvance - (configPage2.dwellRun / timePerDegree); // 360 - desired advance angle - number of degrees the dwell will take
intinjectorStartAngle=355-(fastDivide32(currentStatus.PW,timePerDegree));//As above, but using fastDivide function
(configPage2.dwellRun*100),//Dwell is stored as ms * 10. ie Dwell of 4.3ms would be 43 in configPage2. This number therefore needs to be multiplied by 100 to get dwell in uS
voidopenInjector2(){digitalWrite(pinInjector,HIGH);BIT_SET(currentStatus.squirt,1);}//Sets the relevant pin HIGH and changes the current status bit for injector 2 (2nd bit of currentStatus.squirt)
if((curTime-toothLastToothTime)<triggerFilterTime){interrupts();return;}//Debounce check. Pulses should never be less than 100uS, so if they are it means a false trigger. (A 36-1 wheel at 8000pm will have triggers approx. every 200uS)
//If the time between the current tooth and the last is greater than 1.5x the time between the last tooth and the tooth before that, we make the assertion that we must be at the first tooth after the gap
if((curTime-toothLastToothTime)>((3*(toothLastToothTime-toothLastMinusOneToothTime))>>1))//Same as above, but uses bitshift instead of multiplying by 1.5