Skip to content

Instantly share code, notes, and snippets.

@Klempnertommy
Last active June 29, 2020 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Klempnertommy/4ee61acd5ec1c66f5466aa8953bb0237 to your computer and use it in GitHub Desktop.
Save Klempnertommy/4ee61acd5ec1c66f5466aa8953bb0237 to your computer and use it in GitHub Desktop.
/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Name: Basic Distributor
Desc: Tooth equal to the number of cylinders are evenly spaced on the cam. No position sensing (Distributor is retained) so crank angle is a made up figure based purely on the first teeth to be seen
Note: This is a very simple decoder. See www.megamanual.com/ms2/GM_7pinHEI.htm
*/
void triggerSetup_BasicDistributor()
{
triggerActualTeeth = configPage2.nCylinders;
if(triggerActualTeeth == 0) { triggerActualTeeth = 1; }
triggerToothAngle = 360 / triggerActualTeeth; //The number of degrees that passes from tooth to tooth
triggerFilterTime = 60000000L / MAX_RPM / configPage2.nCylinders; // Minimum time required between teeth
triggerFilterTime = triggerFilterTime / 2; //Safety margin
triggerFilterTime = 0;
secondDerivEnabled = false;
decoderIsSequential = false;
toothCurrentCount = 0; //Default value
decoderHasFixedCrankingTiming = true;
triggerToothAngleIsCorrect = true;
if(configPage2.nCylinders <= 4) { MAX_STALL_TIME = (1851UL * triggerToothAngle); }//Minimum 90rpm. (1851uS is the time per degree at 90rpm). This uses 90rpm rather than 50rpm due to the potentially very high stall time on a 4 cylinder if we wait that long.
else { MAX_STALL_TIME = (3200UL * triggerToothAngle); } //Minimum 50rpm. (3200uS is the time per degree at 50rpm).
}
void triggerPri_BasicDistributor()
{
curTime = micros();
curGap = curTime - toothLastToothTime;
if ( (curGap >= triggerFilterTime) )
{
if(currentStatus.hasSync == true) { setFilter(curGap); } //Recalc the new filter value
else { triggerFilterTime = 0; } //If we don't yet have sync, ensure that the filter won't prevent future valid pulses from being ignored.
if( (toothCurrentCount == triggerActualTeeth) || (currentStatus.hasSync == false) ) //Check if we're back to the beginning of a revolution
{
toothCurrentCount = 1; //Reset the counter
toothOneMinusOneTime = toothOneTime;
toothOneTime = curTime;
currentStatus.hasSync = true;
currentStatus.startRevolutions++; //Counter
}
else
{
if( (toothCurrentCount < triggerActualTeeth) ) { toothCurrentCount++; } //Increment the tooth counter
else
{
//This means toothCurrentCount is greater than triggerActualTeeth, which is bad.
//If we have sync here then there's a problem. Throw a sync loss
if( currentStatus.hasSync == true )
{
currentStatus.syncLossCounter++;
currentStatus.hasSync = false;
}
}
}
validTrigger = true; //Flag this pulse as being a valid trigger (ie that it passed filters)
if ( configPage4.ignCranklock && BIT_CHECK(currentStatus.engine, BIT_ENGINE_CRANK) )
{
endCoil1Charge();
endCoil2Charge();
endCoil3Charge();
endCoil4Charge();
}
if(configPage2.perToothIgn == true)
{
uint16_t crankAngle = ( (toothCurrentCount-1) * triggerToothAngle ) + configPage4.triggerAngle;
crankAngle = ignitionLimits((crankAngle));
if(toothCurrentCount > (triggerActualTeeth)) { checkPerToothTiming(crankAngle, (toothCurrentCount - (triggerActualTeeth))); }
else { checkPerToothTiming(crankAngle, toothCurrentCount); }
}
toothLastMinusOneToothTime = toothLastToothTime;
toothLastToothTime = curTime;
} //Trigger filter
}
void triggerSec_BasicDistributor() { return; } //Not required
uint16_t getRPM_BasicDistributor()
{
uint16_t tempRPM;
if( currentStatus.RPM < currentStatus.crankRPM)
{
tempRPM = crankingGetRPM(triggerActualTeeth);
}
else { tempRPM = stdGetRPM(360); }
MAX_STALL_TIME = revolutionTime << 1; //Set the stall time to be twice the current RPM. This is a safe figure as there should be no single revolution where this changes more than this
if(triggerActualTeeth == 1) { MAX_STALL_TIME = revolutionTime << 1; } //Special case for 1 cylinder engines that only get 1 pulse every 720 degrees
if(MAX_STALL_TIME < 366667UL) { MAX_STALL_TIME = 366667UL; } //Check for 50rpm minimum
return tempRPM;
}
int getCrankAngle_BasicDistributor()
{
//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)
unsigned long tempToothLastToothTime;
int tempToothCurrentCount;
//Grab some variables that are used in the trigger code and assign them to temp variables.
noInterrupts();
tempToothCurrentCount = toothCurrentCount;
tempToothLastToothTime = toothLastToothTime;
lastCrankAngleCalc = micros(); //micros() is no longer interrupt safe
interrupts();
int crankAngle = ((tempToothCurrentCount - 1) * triggerToothAngle) + configPage4.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.
//Estimate the number of degrees travelled since the last tooth}
elapsedTime = (lastCrankAngleCalc - tempToothLastToothTime);
//crankAngle += timeToAngle(elapsedTime, CRANKMATH_METHOD_INTERVAL_REV);
crankAngle += timeToAngle(elapsedTime, CRANKMATH_METHOD_INTERVAL_TOOTH);
if (crankAngle >= 360) { crankAngle -= 360; }
if (crankAngle > CRANK_ANGLE_MAX) { crankAngle -= CRANK_ANGLE_MAX; }
if (crankAngle < 0) { crankAngle += CRANK_ANGLE_MAX; }
return crankAngle;
}
void triggerSetEndTeeth_BasicDistributor()
{
int tempEndAngle = (ignition1EndAngle - configPage4.triggerAngle);
tempEndAngle = ignitionLimits((tempEndAngle));
if( (tempEndAngle > 360) || (tempEndAngle <= 0) )
{
ignition1EndTooth = 2;
ignition2EndTooth = 1;
}
else
{
ignition1EndTooth = 1;
ignition2EndTooth = 2;
}
lastToothCalcAdvance = currentStatus.advance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment