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
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 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(configPage2.triggerMissingTeeth==1){targetGap=(3*(toothLastToothTime-toothLastMinusOneToothTime))>>1;}//Multiply by 1.5 (Checks for a gap 1.5x greater than the last one) (Uses bitshift to multiply by 3 then divide by 2. Much faster than multiplying by 1.5)
//else { targetGap = (10 * (toothLastToothTime - toothLastMinusOneToothTime)) >> 2; } //Multiply by 2.5 (Checks for a gap 2.5x greater than the last one)
else{targetGap=((toothLastToothTime-toothLastMinusOneToothTime))*2;}//Multiply by 2 (Checks for a gap 2x greater than the last one)
revolutionTime=(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)
//This is the current angle ATDC the engine is at. This is the last known position based on what tooth was last 'seen'. It is only accurate to the resolution of the trigger wheel (Eg 36-1 is 10 degrees)
intcrankAngle=(tempToothCurrentCount-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.
crankAngle+=((micros()-tempToothLastToothTime)/timePerDegree);//Estimate the number of degrees travelled since the last tooth
revolutionTime=(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)
//This is the current angle ATDC the engine is at. This is the last known position based on what tooth was last 'seen'. It is only accurate to the resolution of the trigger wheel (Eg 36-1 is 10 degrees)
unsignedlongtempToothLastToothTime;
inttempToothCurrentCount;
//Grab some variables that are used in the trigger code and assign them to temp variables.
noInterrupts();
tempToothCurrentCount=toothCurrentCount;
tempToothLastToothTime=toothLastToothTime;
interrupts();
intcrankAngle=(tempToothCurrentCount-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.
crankAngle+=ldiv((micros()-tempToothLastToothTime),timePerDegree).quot;//Estimate the number of degrees travelled since the last tooth
targetGap=(lastGap)>>1;//The target gap is set at half the last tooth gap
if(curGap<targetGap)//If the gap between this tooth and the last one is less than half of the previous gap, then we are very likely at the magical 3rd tooth
revolutionTime=(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)
interrupts();
returnldiv(US_IN_MINUTE,revolutionTime).quot;//Calc RPM based on last full revolution time (uses ldiv rather than div as US_IN_MINUTE is a long)
//This is the current angle ATDC the engine is at. This is the last known position based on what tooth was last 'seen'. It is only accurate to the resolution of the trigger wheel (Eg 36-1 is 10 degrees)
//Check if the last tooth seen was the reference tooth (Number 3). All others can be calculated, but tooth 3 has a unique angle
intcrankAngle;
if(tempToothCurrentCount<3)
{
crankAngle=(tempToothCurrentCount-1)*60+42;//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.
}
elseif(tempToothCurrentCount==3)
{
crankAngle=112;
}
else
{
crankAngle=(tempToothCurrentCount-2)*60+42;//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.
triggerToothAngle=180;//The number of degrees that passes from tooth to tooth (primary)
}
voidtriggerPri_4G63()
{
curTime=micros();
if(toothCurrentCount==1){toothCurrentCount==2;}
else
{
toothCurrentCount=1;//Reset the counter
toothOneMinusOneTime=toothOneTime;
toothOneTime=curTime;
currentStatus.hasSync=true;
startRevolutions++;//Counter
}
//High speed tooth logging history
toothHistory[toothHistoryIndex]=curGap;
if(toothHistoryIndex==511)
{toothHistoryIndex=0;}
else
{toothHistoryIndex++;}
toothLastMinusOneToothTime=toothLastToothTime;
toothLastToothTime=curTime;
}
voidtriggerSec_4G63()
{
return;
}
intgetRPM_4G63()
{
noInterrupts();
revolutionTime=(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)
interrupts();
returnldiv(US_IN_MINUTE,revolutionTime).quot;//Calc RPM based on last full revolution time (uses ldiv rather than div as US_IN_MINUTE is a long)
}
intgetCrankAngle_4G63(inttimePerDegree)
{
//This is the current angle ATDC the engine is at. This is the last known position based on what tooth was last 'seen'. It is only accurate to the resolution of the trigger wheel (Eg 36-1 is 10 degrees)
unsignedlongtempToothLastToothTime;
inttempToothCurrentCount;
//Grab some variables that are used in the trigger code and assign them to temp variables.
noInterrupts();
tempToothCurrentCount=toothCurrentCount;
tempToothLastToothTime=toothLastToothTime;
interrupts();
intcrankAngle=(tempToothCurrentCount-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.
crankAngle+=ldiv((micros()-tempToothLastToothTime),timePerDegree).quot;//Estimate the number of degrees travelled since the last tooth
triggerToothAngle=180;//The number of degrees that passes from tooth to tooth (primary)
toothAngles[0]=12;
toothAngles[1]=18;
toothAngles[2]=33;
toothAngles[3]=48;
toothAngles[4]=63;
toothAngles[5]=78;
toothAngles[6]=102;
toothAngles[7]=108;
toothAngles[8]=123;
toothAngles[9]=138;
toothAngles[10]=162;
toothAngles[11]=177;
toothAngles[12]=183;
toothAngles[13]=198;
toothAngles[14]=222;
toothAngles[15]=237;
toothAngles[16]=252;
toothAngles[17]=258;
toothAngles[18]=282;
toothAngles[19]=288;
toothAngles[20]=312;
toothAngles[21]=327;
toothAngles[22]=342;
toothAngles[23]=357;
toothCurrentCount=25;//We set the initial tooth value to be something that should never be reached. This indicates no sync
}
voidtriggerPri_24X()
{
if(toothCurrentCount==25){currentStatus.hasSync=false;return;}//Indicates sync has not been achieved (Still waiting for 1 revolution of the crank to take place)
curTime=micros();
curGap=curTime-toothLastToothTime;
if(toothCurrentCount==0)
{
toothCurrentCount=1;//Reset the counter
toothOneMinusOneTime=toothOneTime;
toothOneTime=curTime;
currentStatus.hasSync=true;
startRevolutions++;//Counter
}
else
{
toothCurrentCount++;//Increment the tooth counter
}
//High speed tooth logging history
toothHistory[toothHistoryIndex]=curGap;
if(toothHistoryIndex==511)
{toothHistoryIndex=0;}
else
{toothHistoryIndex++;}
toothLastToothTime=curTime;
}
voidtriggerSec_24X()
{
toothCurrentCount=0;//All we need to do is reset the tooth count back to zero, indicating that we're at the beginning of a new revolution
return;
}
intgetRPM_24X()
{
noInterrupts();
revolutionTime=(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)
interrupts();
returnldiv(US_IN_MINUTE,revolutionTime).quot;//Calc RPM based on last full revolution time (uses ldiv rather than div as US_IN_MINUTE is a long)
}
intgetCrankAngle_24X(inttimePerDegree)
{
//This is the current angle ATDC the engine is at. This is the last known position based on what tooth was last 'seen'. It is only accurate to the resolution of the trigger wheel (Eg 36-1 is 10 degrees)
unsignedlongtempToothLastToothTime;
inttempToothCurrentCount;
//Grab some variables that are used in the trigger code and assign them to temp variables.
noInterrupts();
tempToothCurrentCount=toothCurrentCount;
tempToothLastToothTime=toothLastToothTime;
interrupts();
intcrankAngle=toothAngles[(tempToothCurrentCount-1)]+configPage2.triggerAngle;//Perform a lookup of the fixed toothAngles array to find what the angle of the last tooth passed was.
crankAngle+=ldiv((micros()-tempToothLastToothTime),timePerDegree).quot;//Estimate the number of degrees travelled since the last tooth