speeduino/speeduino/secondaryTables.cpp

224 lines
9.5 KiB
C++
Raw Normal View History

#include "globals.h"
#include "secondaryTables.h"
#include "corrections.h"
2022-11-05 15:43:29 -07:00
void calculateSecondaryFuel(void)
{
//If the secondary fuel table is in use, also get the VE value from there
BIT_CLEAR(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Clear the bit indicating that the 2nd fuel table is in use.
if(configPage10.fuel2Mode > 0)
{
if(configPage10.fuel2Mode == FUEL2_MODE_MULTIPLY)
{
currentStatus.VE2 = getVE2();
//Fuel 2 table is treated as a % value. Table 1 and 2 are multiplied together and divided by 100
uint16_t combinedVE = ((uint16_t)currentStatus.VE1 * (uint16_t)currentStatus.VE2) / 100;
if(combinedVE <= 255) { currentStatus.VE = combinedVE; }
else { currentStatus.VE = 255; }
}
else if(configPage10.fuel2Mode == FUEL2_MODE_ADD)
{
currentStatus.VE2 = getVE2();
//Fuel tables are added together, but a check is made to make sure this won't overflow the 8-bit VE value
uint16_t combinedVE = (uint16_t)currentStatus.VE1 + (uint16_t)currentStatus.VE2;
if(combinedVE <= 255) { currentStatus.VE = combinedVE; }
else { currentStatus.VE = 255; }
}
else if(configPage10.fuel2Mode == FUEL2_MODE_CONDITIONAL_SWITCH )
{
if(configPage10.fuel2SwitchVariable == FUEL2_CONDITION_RPM)
{
if(currentStatus.RPM > configPage10.fuel2SwitchValue)
{
BIT_SET(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Set the bit indicating that the 2nd fuel table is in use.
currentStatus.VE2 = getVE2();
currentStatus.VE = currentStatus.VE2;
}
}
else if(configPage10.fuel2SwitchVariable == FUEL2_CONDITION_MAP)
{
if(currentStatus.MAP > configPage10.fuel2SwitchValue)
{
BIT_SET(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Set the bit indicating that the 2nd fuel table is in use.
currentStatus.VE2 = getVE2();
currentStatus.VE = currentStatus.VE2;
}
}
else if(configPage10.fuel2SwitchVariable == FUEL2_CONDITION_TPS)
{
if(currentStatus.TPS > configPage10.fuel2SwitchValue)
{
BIT_SET(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Set the bit indicating that the 2nd fuel table is in use.
currentStatus.VE2 = getVE2();
currentStatus.VE = currentStatus.VE2;
}
}
else if(configPage10.fuel2SwitchVariable == FUEL2_CONDITION_ETH)
{
if(currentStatus.ethanolPct > configPage10.fuel2SwitchValue)
{
BIT_SET(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Set the bit indicating that the 2nd fuel table is in use.
currentStatus.VE2 = getVE2();
currentStatus.VE = currentStatus.VE2;
}
}
}
else if(configPage10.fuel2Mode == FUEL2_MODE_INPUT_SWITCH)
{
if(digitalRead(pinFuel2Input) == configPage10.fuel2InputPolarity)
{
BIT_SET(currentStatus.status3, BIT_STATUS3_FUEL2_ACTIVE); //Set the bit indicating that the 2nd fuel table is in use.
currentStatus.VE2 = getVE2();
currentStatus.VE = currentStatus.VE2;
}
}
}
}
2022-11-05 15:43:29 -07:00
void calculateSecondarySpark(void)
{
//Same as above but for the secondary ignition table
BIT_CLEAR(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE); //Clear the bit indicating that the 2nd spark table is in use.
if(configPage10.spark2Mode > 0)
{
if(configPage10.spark2Mode == SPARK2_MODE_MULTIPLY)
{
2020-12-12 20:36:01 -08:00
BIT_SET(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE);
currentStatus.advance2 = getAdvance2();
//make sure we don't have a negative value in the multiplier table (sharing a signed 8 bit table)
if(currentStatus.advance2 < 0) { currentStatus.advance2 = 0; }
//Spark 2 table is treated as a % value. Table 1 and 2 are multiplied together and divided by 100
int16_t combinedAdvance = ((int16_t)currentStatus.advance1 * (int16_t)currentStatus.advance2) / 100;
//make sure we don't overflow and accidentally set negative timing, currentStatus.advance can only hold a signed 8 bit value
if(combinedAdvance <= 127) { currentStatus.advance = combinedAdvance; }
else { currentStatus.advance = 127; }
}
else if(configPage10.spark2Mode == SPARK2_MODE_ADD)
{
BIT_SET(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE); //Set the bit indicating that the 2nd spark table is in use.
currentStatus.advance2 = getAdvance2();
//Spark tables are added together, but a check is made to make sure this won't overflow the 8-bit VE value
int16_t combinedAdvance = (int16_t)currentStatus.advance1 + (int16_t)currentStatus.advance2;
//make sure we don't overflow and accidentally set negative timing, currentStatus.advance can only hold a signed 8 bit value
if(combinedAdvance <= 127) { currentStatus.advance = combinedAdvance; }
else { currentStatus.advance = 127; }
}
else if(configPage10.spark2Mode == SPARK2_MODE_CONDITIONAL_SWITCH )
{
if(configPage10.spark2SwitchVariable == SPARK2_CONDITION_RPM)
{
if(currentStatus.RPM > configPage10.spark2SwitchValue)
{
BIT_SET(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE); //Set the bit indicating that the 2nd spark table is in use.
currentStatus.advance2 = getAdvance2();
currentStatus.advance = currentStatus.advance2;
}
}
2020-12-12 20:36:01 -08:00
else if(configPage10.spark2SwitchVariable == SPARK2_CONDITION_MAP)
{
2020-12-12 20:36:01 -08:00
if(currentStatus.MAP > configPage10.spark2SwitchValue)
{
BIT_SET(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE); //Set the bit indicating that the 2nd spark table is in use.
currentStatus.advance2 = getAdvance2();
currentStatus.advance = currentStatus.advance2;
}
}
else if(configPage10.spark2SwitchVariable == SPARK2_CONDITION_TPS)
{
2020-12-12 20:36:01 -08:00
if(currentStatus.TPS > configPage10.spark2SwitchValue)
{
BIT_SET(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE); //Set the bit indicating that the 2nd spark table is in use.
currentStatus.advance2 = getAdvance2();
currentStatus.advance = currentStatus.advance2;
}
}
else if(configPage10.spark2SwitchVariable == SPARK2_CONDITION_ETH)
{
if(currentStatus.ethanolPct > configPage10.spark2SwitchValue)
{
BIT_SET(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE); //Set the bit indicating that the 2nd spark table is in use.
currentStatus.advance2 = getAdvance2();
currentStatus.advance = currentStatus.advance2;
}
}
}
else if(configPage10.spark2Mode == SPARK2_MODE_INPUT_SWITCH)
{
if(digitalRead(pinSpark2Input) == configPage10.spark2InputPolarity)
{
BIT_SET(currentStatus.spark2, BIT_SPARK2_SPARK2_ACTIVE); //Set the bit indicating that the 2nd spark table is in use.
currentStatus.advance2 = getAdvance2();
currentStatus.advance = currentStatus.advance2;
}
}
//Apply the fixed timing correction manually. This has to be done again here if any of the above conditions are met to prevent any of the seconadary calculations applying instead of fixec timing
currentStatus.advance = correctionFixedTiming(currentStatus.advance);
currentStatus.advance = correctionCrankingFixedTiming(currentStatus.advance); //This overrides the regular fixed timing, must come last
}
}
/**
* @brief Looks up and returns the VE value from the secondary fuel table
*
* This performs largely the same operations as getVE() however the lookup is of the secondary fuel table and uses the secondary load source
* @return byte
*/
2022-11-05 15:43:29 -07:00
byte getVE2(void)
{
byte tempVE = 100;
if( configPage10.fuel2Algorithm == LOAD_SOURCE_MAP)
{
//Speed Density
currentStatus.fuelLoad2 = currentStatus.MAP;
}
else if (configPage10.fuel2Algorithm == LOAD_SOURCE_TPS)
{
//Alpha-N
currentStatus.fuelLoad2 = currentStatus.TPS * 2;
}
else if (configPage10.fuel2Algorithm == LOAD_SOURCE_IMAPEMAP)
{
//IMAP / EMAP
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
currentStatus.fuelLoad2 = ((int16_t)currentStatus.MAP * 100U) / currentStatus.EMAP;
}
else { currentStatus.fuelLoad2 = currentStatus.MAP; } //Fallback position
tempVE = get3DTableValue(&fuelTable2, currentStatus.fuelLoad2, currentStatus.RPM); //Perform lookup into fuel map for RPM vs MAP value
return tempVE;
}
/**
* @brief Performs a lookup of the second ignition advance table. The values used to look this up will be RPM and whatever load source the user has configured
*
* @return byte The current target advance value in degrees
*/
2022-11-05 15:43:29 -07:00
byte getAdvance2(void)
{
byte tempAdvance = 0;
if (configPage10.spark2Algorithm == LOAD_SOURCE_MAP) //Check which fuelling algorithm is being used
{
//Speed Density
currentStatus.ignLoad2 = currentStatus.MAP;
}
else if(configPage10.spark2Algorithm == LOAD_SOURCE_TPS)
{
//Alpha-N
currentStatus.ignLoad2 = currentStatus.TPS * 2;
}
else if (configPage10.spark2Algorithm == LOAD_SOURCE_IMAPEMAP)
{
//IMAP / EMAP
currentStatus.ignLoad2 = (currentStatus.MAP * 100) / currentStatus.EMAP;
}
else { currentStatus.ignLoad2 = currentStatus.MAP; }
tempAdvance = get3DTableValue(&ignitionTable2, currentStatus.ignLoad2, currentStatus.RPM) - OFFSET_IGNITION; //As above, but for ignition advance
//Perform the corrections calculation on the secondary advance value, only if it uses a switched mode
if( (configPage10.spark2SwitchVariable == SPARK2_MODE_CONDITIONAL_SWITCH) || (configPage10.spark2SwitchVariable == SPARK2_MODE_INPUT_SWITCH) ) { tempAdvance = correctionsIgn(tempAdvance); }
return tempAdvance;
}