speeduino/speeduino/engineProtection.cpp

224 lines
8.0 KiB
C++
Raw Permalink Normal View History

#include "globals.h"
#include "engineProtection.h"
Performance: optimize division (#1082) * Add udiv_32_16 * Apply udiv_32_16() where possible * Convert udiv_32_16 to assembler It's worth 20 loop/s * Remove unused functions * Remove degreesPeruSx2048 - unused * Remove angleToTime - replace with direct calls 1. Drop angleToTime() It's slow, only partially implemented and adds zero value (and has MISRA violations) 2. Consistent function naming 3. Doxygen * triggerPri_Nissan360 shouldn't set timePerDegree. It will be overwritten every loop by doCrankSpeedCalcs() * Use angleToTimeMicroSecPerDegree() instead of timePerDegree No loss in performance Increased injection open/close time accuracy (so unit test values must change) Can remove timePerDegree global. * Hide (encapsulate) crank math globals. * Base all angle to time conversions on decoder computed variables. This is within 2us of the revolution based method and is much faster - which is essentially zero percent change. * Performance: move calculation of degreesPeruSx32768 into decoders. Remove doCrankSpeedCalcs() - it's doing nothing at the moment. * Apply libdivide to triggerSetEndTeeth functions. Since triggerToothAngle is set once at initialization time, we can generate the libdivide struct once and reuse it many times. * Remove lastToothCalcAdvance - unused * Replace 16-bit division with shift * Replace 32-bit divison with 16-bit division * Avoid 32-bit division; use div100() * inline percentage() * Optimize div100() * MISRA fixes * Replace magic numbers with #defs * Replace libdivide structs with inline constants No perf or memory changes * Use fixed types for PWM max count variables * Accurate rounded integer division * Formalise rounding behavior (DIV_ROUND_CORRECT) * Apply DIV_ROUND_CORRECT to DIV_ROUND_CLOSEST(), UDIV_ROUND_CLOSEST(), div100(), div360(), percentage() & halfPercentage() * Add, fix & improve unit tests * Add udiv_32_16_closest() * Perf: Limit percentage calculations to 16-bits * MISRA fixes * Add compare_executiontime() to encapsulate common perf testing code * Signed to unsigned division * Convert ignitionLimits() to an inline function. Slight speed up, probably due to removing multiple evaluations of macro arguments. * Split unit tests up. * udiv_32_16 - check for valid parameters
2023-11-05 14:10:08 -08:00
#include "maths.h"
byte oilProtStartTime = 0;
2022-11-05 15:43:29 -07:00
byte checkEngineProtect(void)
{
byte protectActive = 0;
if(checkBoostLimit() || checkOilPressureLimit() || checkAFRLimit() )
{
if( currentStatus.RPMdiv100 > configPage4.engineProtectMaxRPM ) { protectActive = 1; }
}
return protectActive;
}
2022-11-05 15:43:29 -07:00
byte checkRevLimit(void)
{
//Hardcut RPM limit
byte currentLimitRPM = 255; //Default to no limit (In case PROTECT_CUT_OFF is selected)
BIT_CLEAR(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_RPM);
BIT_CLEAR(currentStatus.spark, BIT_SPARK_HRDLIM);
2022-04-09 16:07:21 -07:00
BIT_CLEAR(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_COOLANT);
if (configPage6.engineProtectType != PROTECT_CUT_OFF)
{
2022-04-09 16:07:21 -07:00
if(configPage9.hardRevMode == HARD_REV_FIXED)
{
currentLimitRPM = configPage4.HardRevLim;
2022-04-09 16:07:21 -07:00
if ( (currentStatus.RPMdiv100 >= configPage4.HardRevLim) || ((softLimitTime > configPage4.SoftLimMax) && (currentStatus.RPMdiv100 >= configPage4.SoftRevLim)) )
{
BIT_SET(currentStatus.spark, BIT_SPARK_HRDLIM); //Legacy and likely to be removed at some point
BIT_SET(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_RPM);
}
else { BIT_CLEAR(currentStatus.spark, BIT_SPARK_HRDLIM); }
}
else if(configPage9.hardRevMode == HARD_REV_COOLANT )
{
currentLimitRPM = (int16_t)(table2D_getValue(&coolantProtectTable, currentStatus.coolant + CALIBRATION_TEMPERATURE_OFFSET));
if(currentStatus.RPMdiv100 > currentLimitRPM)
2022-04-09 16:07:21 -07:00
{
BIT_SET(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_COOLANT);
BIT_SET(currentStatus.spark, BIT_SPARK_HRDLIM); //Legacy and likely to be removed at some point
BIT_SET(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_RPM);
}
}
}
return currentLimitRPM;
}
2022-11-05 15:43:29 -07:00
byte checkBoostLimit(void)
{
byte boostLimitActive = 0;
BIT_CLEAR(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_MAP);
BIT_CLEAR(currentStatus.spark, BIT_SPARK_BOOSTCUT);
BIT_CLEAR(currentStatus.status1, BIT_STATUS1_BOOSTCUT);
if (configPage6.engineProtectType != PROTECT_CUT_OFF) {
//Boost cutoff is very similar to launchControl, but with a check against MAP rather than a switch
if( (configPage6.boostCutEnabled > 0) && (currentStatus.MAP > (configPage6.boostLimit * 2)) ) //The boost limit is divided by 2 to allow a limit up to 511kPa
{
boostLimitActive = 1;
BIT_SET(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_MAP);
/*
switch(configPage6.boostCutType)
{
case 1:
BIT_SET(currentStatus.spark, BIT_SPARK_BOOSTCUT);
BIT_CLEAR(currentStatus.status1, BIT_STATUS1_BOOSTCUT);
BIT_SET(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_MAP);
break;
case 2:
BIT_SET(currentStatus.status1, BIT_STATUS1_BOOSTCUT);
BIT_CLEAR(currentStatus.spark, BIT_SPARK_BOOSTCUT);
break;
case 3:
BIT_SET(currentStatus.spark, BIT_SPARK_BOOSTCUT);
BIT_SET(currentStatus.status1, BIT_STATUS1_BOOSTCUT);
break;
default:
//Shouldn't ever happen, but just in case, disable all cuts
BIT_CLEAR(currentStatus.status1, BIT_STATUS1_BOOSTCUT);
BIT_CLEAR(currentStatus.spark, BIT_SPARK_BOOSTCUT);
}
*/
}
}
return boostLimitActive;
}
2022-11-05 15:43:29 -07:00
byte checkOilPressureLimit(void)
{
byte oilProtectActive = 0;
bool alreadyActive = BIT_CHECK(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_OIL);
BIT_CLEAR(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_OIL); //Will be set true below if required
if (configPage6.engineProtectType != PROTECT_CUT_OFF)
{
if( (configPage10.oilPressureProtEnbl == true) && (configPage10.oilPressureEnable == true) )
{
byte oilLimit = table2D_getValue(&oilPressureProtectTable, currentStatus.RPMdiv100);
if(currentStatus.oilPressure < oilLimit)
{
//Check if this is the first time we've been below the limit
Performance: optimize division (#1082) * Add udiv_32_16 * Apply udiv_32_16() where possible * Convert udiv_32_16 to assembler It's worth 20 loop/s * Remove unused functions * Remove degreesPeruSx2048 - unused * Remove angleToTime - replace with direct calls 1. Drop angleToTime() It's slow, only partially implemented and adds zero value (and has MISRA violations) 2. Consistent function naming 3. Doxygen * triggerPri_Nissan360 shouldn't set timePerDegree. It will be overwritten every loop by doCrankSpeedCalcs() * Use angleToTimeMicroSecPerDegree() instead of timePerDegree No loss in performance Increased injection open/close time accuracy (so unit test values must change) Can remove timePerDegree global. * Hide (encapsulate) crank math globals. * Base all angle to time conversions on decoder computed variables. This is within 2us of the revolution based method and is much faster - which is essentially zero percent change. * Performance: move calculation of degreesPeruSx32768 into decoders. Remove doCrankSpeedCalcs() - it's doing nothing at the moment. * Apply libdivide to triggerSetEndTeeth functions. Since triggerToothAngle is set once at initialization time, we can generate the libdivide struct once and reuse it many times. * Remove lastToothCalcAdvance - unused * Replace 16-bit division with shift * Replace 32-bit divison with 16-bit division * Avoid 32-bit division; use div100() * inline percentage() * Optimize div100() * MISRA fixes * Replace magic numbers with #defs * Replace libdivide structs with inline constants No perf or memory changes * Use fixed types for PWM max count variables * Accurate rounded integer division * Formalise rounding behavior (DIV_ROUND_CORRECT) * Apply DIV_ROUND_CORRECT to DIV_ROUND_CLOSEST(), UDIV_ROUND_CLOSEST(), div100(), div360(), percentage() & halfPercentage() * Add, fix & improve unit tests * Add udiv_32_16_closest() * Perf: Limit percentage calculations to 16-bits * MISRA fixes * Add compare_executiontime() to encapsulate common perf testing code * Signed to unsigned division * Convert ignitionLimits() to an inline function. Slight speed up, probably due to removing multiple evaluations of macro arguments. * Split unit tests up. * udiv_32_16 - check for valid parameters
2023-11-05 14:10:08 -08:00
if(oilProtStartTime == 0) { oilProtStartTime = div100(millis()); }
/* Check if countdown has reached its target, if so then instruct to cut */
Performance: optimize division (#1082) * Add udiv_32_16 * Apply udiv_32_16() where possible * Convert udiv_32_16 to assembler It's worth 20 loop/s * Remove unused functions * Remove degreesPeruSx2048 - unused * Remove angleToTime - replace with direct calls 1. Drop angleToTime() It's slow, only partially implemented and adds zero value (and has MISRA violations) 2. Consistent function naming 3. Doxygen * triggerPri_Nissan360 shouldn't set timePerDegree. It will be overwritten every loop by doCrankSpeedCalcs() * Use angleToTimeMicroSecPerDegree() instead of timePerDegree No loss in performance Increased injection open/close time accuracy (so unit test values must change) Can remove timePerDegree global. * Hide (encapsulate) crank math globals. * Base all angle to time conversions on decoder computed variables. This is within 2us of the revolution based method and is much faster - which is essentially zero percent change. * Performance: move calculation of degreesPeruSx32768 into decoders. Remove doCrankSpeedCalcs() - it's doing nothing at the moment. * Apply libdivide to triggerSetEndTeeth functions. Since triggerToothAngle is set once at initialization time, we can generate the libdivide struct once and reuse it many times. * Remove lastToothCalcAdvance - unused * Replace 16-bit division with shift * Replace 32-bit divison with 16-bit division * Avoid 32-bit division; use div100() * inline percentage() * Optimize div100() * MISRA fixes * Replace magic numbers with #defs * Replace libdivide structs with inline constants No perf or memory changes * Use fixed types for PWM max count variables * Accurate rounded integer division * Formalise rounding behavior (DIV_ROUND_CORRECT) * Apply DIV_ROUND_CORRECT to DIV_ROUND_CLOSEST(), UDIV_ROUND_CLOSEST(), div100(), div360(), percentage() & halfPercentage() * Add, fix & improve unit tests * Add udiv_32_16_closest() * Perf: Limit percentage calculations to 16-bits * MISRA fixes * Add compare_executiontime() to encapsulate common perf testing code * Signed to unsigned division * Convert ignitionLimits() to an inline function. Slight speed up, probably due to removing multiple evaluations of macro arguments. * Split unit tests up. * udiv_32_16 - check for valid parameters
2023-11-05 14:10:08 -08:00
if( (uint8_t(div100(millis())) >= (uint16_t(oilProtStartTime + configPage10.oilPressureProtTime)) ) || (alreadyActive > 0) )
{
BIT_SET(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_OIL);
oilProtectActive = 1;
}
}
else { oilProtStartTime = 0; } //Reset the timer
}
}
return oilProtectActive;
}
2022-11-05 15:43:29 -07:00
byte checkAFRLimit(void)
{
static bool checkAFRLimitActive = false;
static bool afrProtectCountEnabled = false;
static unsigned long afrProtectCount = 0;
static constexpr char X2_MULTIPLIER = 2;
static constexpr char X100_MULTIPLIER = 100;
/*
To use this function, a wideband sensor is required.
First of all, check whether engine protection is enabled,
thereafter check whether AFR protection is enabled and at last
if wideband sensor is used.
After confirmation, the following conditions has to be met:
- MAP above x kPa
- RPM above x
- TPS above x %
- AFR threshold (AFR target + defined maximum deviation)
- Time before cut
See afrProtect variables in globals.h for more information.
If all conditions above are true, a specified time delay is starting
to count down in which leads to the engine protection function
to be activated using selected protection cut method (e.g. ignition,
fuel or both).
For reactivation, the following condition has to be met:
- TPS below x %
*/
/*
Do 3 checks here;
- whether engine protection is enabled
- whether AFR protection is enabled
- whether wideband sensor is used
*/
if(configPage6.engineProtectType != PROTECT_CUT_OFF && configPage9.afrProtectEnabled && configPage6.egoType == EGO_TYPE_WIDE) {
/* Conditions */
bool mapCondition = (currentStatus.MAP >= (configPage9.afrProtectMinMAP * X2_MULTIPLIER)) ? true : false;
bool rpmCondition = (currentStatus.RPMdiv100 >= configPage9.afrProtectMinRPM) ? true : false;
bool tpsCondition = (currentStatus.TPS >= configPage9.afrProtectMinTPS) ? true : false;
/*
Depending on selected mode, this could either be fixed AFR value or a
value set to be the maximum deviation from AFR target table.
1 = fixed value mode, 2 = target table mode
*/
bool afrCondition;
switch(configPage9.afrProtectEnabled)
{
case 1: afrCondition = (currentStatus.O2 >= configPage9.afrProtectDeviation) ? true : false; break; /* Fixed value */
case 2: afrCondition = (currentStatus.O2 >= (currentStatus.afrTarget + configPage9.afrProtectDeviation)) ? true : false; break; /* Deviation from target table */
default: afrCondition = false; /* Unknown mode. Shouldn't even get here */
}
/* Check if conditions above are fulfilled */
if(mapCondition && rpmCondition && tpsCondition && afrCondition)
{
/* All conditions fulfilled - start counter for 'protection delay' */
if(!afrProtectCountEnabled)
{
afrProtectCountEnabled = true;
afrProtectCount = millis();
}
/* Check if countdown has reached its target, if so then instruct to cut */
if(millis() >= (afrProtectCount + (configPage9.afrProtectCutTime * X100_MULTIPLIER)))
{
checkAFRLimitActive = true;
BIT_SET(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_AFR);
}
}
else
{
/* Conditions have presumably changed - deactivate and reset counter */
if(afrProtectCountEnabled)
{
afrProtectCountEnabled = false;
afrProtectCount = 0;
}
}
/* Check if condition for reactivation is fulfilled */
if(checkAFRLimitActive && (currentStatus.TPS <= configPage9.afrProtectReactivationTPS))
{
checkAFRLimitActive = false;
afrProtectCountEnabled = false;
BIT_CLEAR(currentStatus.engineProtectStatus, ENGINE_PROTECT_BIT_AFR);
}
}
return checkAFRLimitActive;
}