Skip to content

Commit

Permalink
Limit scope of all table2D instances (none need to be global).
Browse files Browse the repository at this point in the history
  • Loading branch information
adbancroft committed Dec 22, 2024
1 parent df83cfc commit 6a78f8f
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 75 deletions.
3 changes: 3 additions & 0 deletions speeduino/auxiliaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A full copy of the license may be found in the projects root directory
#include "src/PID_v1/PID_v1.h"
#include "decoders.h"
#include "timers.h"
#include "utilities.h"

static long vvt1_pwm_value;
static long vvt2_pwm_value;
Expand Down Expand Up @@ -53,6 +54,7 @@ uint16_t fan_pwm_max_count; //Used for variable PWM frequency
volatile unsigned int fan_pwm_cur_value;
long fan_pwm_value;
#endif
static constexpr table2D fanPWMTable(_countof(configPage9.PWMFanDuty), configPage9.PWMFanDuty, configPage6.fanPWMBins);

bool acIsEnabled;
bool acStandAloneFanIsEnabled;
Expand All @@ -71,6 +73,7 @@ bool vvtIsHot;
bool vvtTimeHold;
uint16_t vvt_pwm_max_count; //Used for variable PWM frequency
uint16_t boost_pwm_max_count; //Used for variable PWM frequency
static constexpr table2D flexBoostTable(_countof(configPage10.flexBoostAdj), configPage10.flexBoostAdj, configPage10.flexBoostBins);

//Old PID method. Retained in case the new one has issues
//integerPID boostPID(&MAPx100, &boost_pwm_target_value, &boostTargetx100, configPage6.boostKP, configPage6.boostKI, configPage6.boostKD, DIRECT);
Expand Down
1 change: 0 additions & 1 deletion speeduino/comms_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,6 @@ void receiveCalibration(byte tableID)
break;
case 2:
//O2 table
//pnt_TargetTable = (byte *)&o2CalibrationTable;
pnt_TargetTable_values = (uint8_t *)&o2Calibration_values;
pnt_TargetTable_bins = (uint16_t *)&o2Calibration_bins;
OFFSET = 0;
Expand Down
21 changes: 21 additions & 0 deletions speeduino/corrections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ There are 2 top level functions that call more detailed corrections for Fuel and
#include "timers.h"
#include "maths.h"
#include "sensors.h"
#include "unit_testing.h"
#include "utilities.h"
#include "src/PID_v1/PID_v1.h"

long PID_O2, PID_output, PID_AFRTarget;
Expand All @@ -53,6 +55,25 @@ uint8_t idleAdvTaper;
uint8_t crankingEnrichTaper;
uint8_t dfcoTaper;

TESTABLE_STATIC constexpr table2D taeTable(_countof(configPage4.taeValues), configPage4.taeValues, configPage4.taeBins);
TESTABLE_STATIC constexpr table2D maeTable(_countof(configPage4.maeRates), configPage4.maeRates, configPage4.maeBins);
TESTABLE_STATIC constexpr table2D WUETable(_countof(configPage2.wueValues), configPage2.wueValues, configPage4.wueBins);
TESTABLE_STATIC constexpr table2D ASETable(_countof(configPage2.asePct), configPage2.asePct, configPage2.aseBins);
TESTABLE_STATIC constexpr table2D ASECountTable(_countof(configPage2.aseCount), configPage2.aseCount, configPage2.aseBins);
TESTABLE_STATIC constexpr table2D crankingEnrichTable(_countof(configPage10.crankingEnrichValues), configPage10.crankingEnrichValues, configPage10.crankingEnrichBins);
TESTABLE_STATIC constexpr table2D dwellVCorrectionTable(_countof(configPage4.dwellCorrectionValues), configPage4.dwellCorrectionValues, configPage6.voltageCorrectionBins);
TESTABLE_STATIC constexpr table2D injectorVCorrectionTable(_countof(configPage6.injVoltageCorrectionValues), configPage6.injVoltageCorrectionValues, configPage6.voltageCorrectionBins);
TESTABLE_STATIC constexpr table2D IATDensityCorrectionTable(_countof(configPage6.airDenRates), configPage6.airDenRates, configPage6.airDenBins);
TESTABLE_STATIC constexpr table2D baroFuelTable(_countof(configPage4.baroFuelValues), configPage4.baroFuelValues, configPage4.baroFuelBins);
TESTABLE_STATIC constexpr table2D IATRetardTable(_countof(configPage4.iatRetValues), configPage4.iatRetValues, configPage4.iatRetBins);
TESTABLE_STATIC constexpr table2D idleAdvanceTable(_countof(configPage4.idleAdvValues), configPage4.idleAdvValues, configPage4.idleAdvBins);
TESTABLE_STATIC constexpr table2D CLTAdvanceTable(_countof(configPage4.cltAdvValues), configPage4.cltAdvValues, configPage4.cltAdvBins);
TESTABLE_STATIC constexpr table2D flexFuelTable(_countof(configPage10.flexFuelAdj), configPage10.flexFuelAdj, configPage10.flexFuelBins);
TESTABLE_STATIC constexpr table2D flexAdvTable(_countof(configPage10.flexAdvAdj), configPage10.flexAdvAdj, configPage10.flexAdvBins);
TESTABLE_STATIC constexpr table2D flexBoostTable(_countof(configPage10.flexBoostAdj), configPage10.flexBoostAdj, configPage10.flexBoostBins);
TESTABLE_STATIC constexpr table2D fuelTempTable(_countof(configPage10.fuelTempValues), configPage10.fuelTempValues, configPage10.fuelTempBins);
TESTABLE_STATIC constexpr table2D wmiAdvTable(_countof(configPage10.wmiAdvAdj), configPage10.wmiAdvAdj, configPage10.wmiAdvBins);

/** Initialise instances and vars related to corrections (at ECU boot-up).
*/
void initialiseCorrections(void)
Expand Down
3 changes: 3 additions & 0 deletions speeduino/engineProtection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#include "globals.h"
#include "engineProtection.h"
#include "maths.h"
#include "utilities.h"

byte oilProtStartTime = 0;
static constexpr table2D oilPressureProtectTable(_countof(configPage10.oilPressureProtMins), configPage10.oilPressureProtMins, configPage10.oilPressureProtRPM);
static constexpr table2D coolantProtectTable(_countof(configPage9.coolantProtRPM), configPage9.coolantProtRPM, configPage9.coolantProtTemp);

byte checkEngineProtect(void)
{
Expand Down
30 changes: 1 addition & 29 deletions speeduino/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,7 @@ trimTable3d trim6Table; ///< 6x6 Fuel trim 6 map
trimTable3d trim7Table; ///< 6x6 Fuel trim 7 map
trimTable3d trim8Table; ///< 6x6 Fuel trim 8 map
struct table3d4RpmLoad dwellTable; ///< 4x4 Dwell map
struct table2D taeTable(_countof(configPage4.taeValues), configPage4.taeValues, configPage4.taeBins);
struct table2D maeTable(_countof(configPage4.maeRates), configPage4.maeRates, configPage4.maeBins);
struct table2D WUETable(_countof(configPage2.wueValues), configPage2.wueValues, configPage4.wueBins);
struct table2D ASETable(_countof(configPage2.asePct), configPage2.asePct, configPage2.aseBins);
struct table2D ASECountTable(_countof(configPage2.aseCount), configPage2.aseCount, configPage2.aseBins);
struct table2D PrimingPulseTable(_countof(configPage2.primePulse), configPage2.primePulse, configPage2.primeBins);
struct table2D crankingEnrichTable(_countof(configPage10.crankingEnrichValues), configPage10.crankingEnrichValues, configPage10.crankingEnrichBins);
struct table2D dwellVCorrectionTable(_countof(configPage4.dwellCorrectionValues), configPage4.dwellCorrectionValues, configPage6.voltageCorrectionBins);
struct table2D injectorVCorrectionTable(_countof(configPage6.injVoltageCorrectionValues), configPage6.injVoltageCorrectionValues, configPage6.voltageCorrectionBins);
struct table2D injectorAngleTable(_countof(configPage2.injAng), configPage2.injAng, configPage2.injAngRPM);
struct table2D IATDensityCorrectionTable(_countof(configPage6.airDenRates), configPage6.airDenRates, configPage6.airDenBins);
struct table2D baroFuelTable(_countof(configPage4.baroFuelValues), configPage4.baroFuelValues, configPage4.baroFuelBins);
struct table2D IATRetardTable(_countof(configPage4.iatRetValues), configPage4.iatRetValues, configPage4.iatRetBins);
struct table2D idleTargetTable(_countof(configPage6.iacCLValues), configPage6.iacCLValues, configPage6.iacBins);
struct table2D idleAdvanceTable(_countof(configPage4.idleAdvValues), configPage4.idleAdvValues, configPage4.idleAdvBins);
struct table2D CLTAdvanceTable(_countof(configPage4.cltAdvValues), configPage4.cltAdvValues, configPage4.cltAdvBins);
struct table2D rotarySplitTable(_countof(configPage10.rotarySplitValues), configPage10.rotarySplitValues, configPage10.rotarySplitBins);
struct table2D flexFuelTable(_countof(configPage10.flexFuelAdj), configPage10.flexFuelAdj, configPage10.flexFuelBins);
struct table2D flexAdvTable(_countof(configPage10.flexAdvAdj), configPage10.flexAdvAdj, configPage10.flexAdvBins);
struct table2D flexBoostTable(_countof(configPage10.flexBoostAdj), configPage10.flexBoostAdj, configPage10.flexBoostBins);
struct table2D fuelTempTable(_countof(configPage10.fuelTempValues), configPage10.fuelTempValues, configPage10.fuelTempBins);
struct table2D oilPressureProtectTable(_countof(configPage10.oilPressureProtMins), configPage10.oilPressureProtMins, configPage10.oilPressureProtRPM);
struct table2D wmiAdvTable(_countof(configPage10.wmiAdvAdj), configPage10.wmiAdvAdj, configPage10.wmiAdvBins);
struct table2D coolantProtectTable(_countof(configPage9.coolantProtRPM), configPage9.coolantProtRPM, configPage9.coolantProtTemp);
struct table2D fanPWMTable(_countof(configPage9.PWMFanDuty), configPage9.PWMFanDuty, configPage6.fanPWMBins);
struct table2D rollingCutTable(_countof(configPage15.rollingProtCutPercent), configPage15.rollingProtCutPercent, configPage15.rollingProtRPMDelta);


/// volatile inj*_pin_port and inj*_pin_mask vars are for the direct port manipulation of the injectors, coils and aux outputs.
volatile PORT_TYPE *inj1_pin_port;
Expand Down Expand Up @@ -243,13 +218,10 @@ struct config15 configPage15;

uint16_t cltCalibration_bins[32];
uint16_t cltCalibration_values[32];
struct table2D cltCalibrationTable(_countof(cltCalibration_values), cltCalibration_values, cltCalibration_bins);
uint16_t iatCalibration_bins[32];
uint16_t iatCalibration_values[32];
struct table2D iatCalibrationTable(_countof(iatCalibration_values), iatCalibration_values, iatCalibration_bins);
uint16_t o2Calibration_bins[32];
uint8_t o2Calibration_values[32];
struct table2D o2CalibrationTable(_countof(o2Calibration_values), o2Calibration_values, o2Calibration_bins);

//These function do checks on a pin to determine if it is already in use by another (higher importance) active function
bool pinIsOutput(byte pin)
Expand Down
32 changes: 0 additions & 32 deletions speeduino/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,32 +439,6 @@ extern trimTable3d trim7Table; //6x6 Fuel trim 7 map
extern trimTable3d trim8Table; //6x6 Fuel trim 8 map

extern struct table3d4RpmLoad dwellTable; //4x4 Dwell map
extern struct table2D taeTable; //4 bin TPS Acceleration Enrichment map (2D)
extern struct table2D maeTable;
extern struct table2D WUETable; //10 bin Warm Up Enrichment map (2D)
extern struct table2D ASETable; //4 bin After Start Enrichment map (2D)
extern struct table2D ASECountTable; //4 bin After Start duration map (2D)
extern struct table2D PrimingPulseTable; //4 bin Priming pulsewidth map (2D)
extern struct table2D crankingEnrichTable; //4 bin cranking Enrichment map (2D)
extern struct table2D dwellVCorrectionTable; //6 bin dwell voltage correction (2D)
extern struct table2D injectorVCorrectionTable; //6 bin injector voltage correction (2D)
extern struct table2D injectorAngleTable; //4 bin injector timing curve (2D)
extern struct table2D IATDensityCorrectionTable; //9 bin inlet air temperature density correction (2D)
extern struct table2D baroFuelTable; //8 bin baro correction curve (2D)
extern struct table2D IATRetardTable; //6 bin ignition adjustment based on inlet air temperature (2D)
extern struct table2D idleTargetTable; //10 bin idle target table for idle timing (2D)
extern struct table2D idleAdvanceTable; //6 bin idle advance adjustment table based on RPM difference (2D)
extern struct table2D CLTAdvanceTable; //6 bin ignition adjustment based on coolant temperature (2D)
extern struct table2D rotarySplitTable; //8 bin ignition split curve for rotary leading/trailing (2D)
extern struct table2D flexFuelTable; //6 bin flex fuel correction table for fuel adjustments (2D)
extern struct table2D flexAdvTable; //6 bin flex fuel correction table for timing advance (2D)
extern struct table2D flexBoostTable; //6 bin flex fuel correction table for boost adjustments (2D)
extern struct table2D fuelTempTable; //6 bin fuel temperature correction table for fuel adjustments (2D)
extern struct table2D oilPressureProtectTable;
extern struct table2D wmiAdvTable; //6 bin wmi correction table for timing advance (2D)
extern struct table2D coolantProtectTable; //6 bin coolant temperature protection table for engine protection (2D)
extern struct table2D fanPWMTable;
extern struct table2D rollingCutTable;

//These are for the direct port manipulation of the injectors, coils and aux outputs
extern volatile PORT_TYPE *inj1_pin_port;
Expand Down Expand Up @@ -1572,19 +1546,13 @@ extern struct config9 configPage9;
extern struct config10 configPage10;
extern struct config13 configPage13;
extern struct config15 configPage15;
//extern byte cltCalibrationTable[CALIBRATION_TABLE_SIZE]; /**< An array containing the coolant sensor calibration values */
//extern byte iatCalibrationTable[CALIBRATION_TABLE_SIZE]; /**< An array containing the inlet air temperature sensor calibration values */
//extern byte o2CalibrationTable[CALIBRATION_TABLE_SIZE]; /**< An array containing the O2 sensor calibration values */

extern uint16_t cltCalibration_bins[32];
extern uint16_t cltCalibration_values[32];
extern uint16_t iatCalibration_bins[32];
extern uint16_t iatCalibration_values[32];
extern uint16_t o2Calibration_bins[32];
extern uint8_t o2Calibration_values[32]; // Note 8-bit values
extern struct table2D cltCalibrationTable; /**< A 32 bin array containing the coolant temperature sensor calibration values */
extern struct table2D iatCalibrationTable; /**< A 32 bin array containing the inlet air temperature sensor calibration values */
extern struct table2D o2CalibrationTable; /**< A 32 bin array containing the O2 sensor calibration values */

bool pinIsOutput(byte pin);
bool pinIsUsed(byte pin);
Expand Down
8 changes: 4 additions & 4 deletions speeduino/idle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ volatile PINMASK_TYPE idle2_pin_mask;
volatile PORT_TYPE *idleUpOutput_pin_port;
volatile PINMASK_TYPE idleUpOutput_pin_mask;

static struct table2D iacPWMTable(_countof(configPage6.iacOLPWMVal), configPage6.iacOLPWMVal, configPage6.iacBins);
static struct table2D iacStepTable(_countof(configPage6.iacOLStepVal), configPage6.iacOLStepVal, configPage6.iacBins);
static constexpr table2D iacPWMTable(_countof(configPage6.iacOLPWMVal), configPage6.iacOLPWMVal, configPage6.iacBins);
static constexpr table2D iacStepTable(_countof(configPage6.iacOLStepVal), configPage6.iacOLStepVal, configPage6.iacBins);
//Open loop tables specifically for cranking
static struct table2D iacCrankStepsTable(_countof(configPage6.iacCrankSteps), configPage6.iacCrankSteps, configPage6.iacCrankBins);
static struct table2D iacCrankDutyTable(_countof(configPage6.iacCrankDuty), configPage6.iacCrankDuty, configPage6.iacCrankBins);
static constexpr table2D iacCrankStepsTable(_countof(configPage6.iacCrankSteps), configPage6.iacCrankSteps, configPage6.iacCrankBins);
static constexpr table2D iacCrankDutyTable(_countof(configPage6.iacCrankDuty), configPage6.iacCrankDuty, configPage6.iacCrankBins);

/*
These functions cover the PWM and stepper idle control
Expand Down
1 change: 0 additions & 1 deletion speeduino/idle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define IDLE_H

#include "globals.h"
#include "table2d.h"
#include BOARD_H //Note that this is not a real file, it is defined in globals.h.

#define IAC_ALGORITHM_NONE 0U
Expand Down
3 changes: 3 additions & 0 deletions speeduino/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ A full copy of the license may be found in the projects root directory
#include "scheduledIO.h"
#include "timers.h"
#include "schedule_calcs.h"
#include "utilities.h"

FuelSchedule fuelSchedule1(FUEL1_COUNTER, FUEL1_COMPARE, FUEL1_TIMER_DISABLE, FUEL1_TIMER_ENABLE);
FuelSchedule fuelSchedule2(FUEL2_COUNTER, FUEL2_COMPARE, FUEL2_TIMER_DISABLE, FUEL2_TIMER_ENABLE);
Expand Down Expand Up @@ -282,6 +283,8 @@ void refreshIgnitionSchedule1(unsigned long timeToEnd)
}
}

static constexpr table2D PrimingPulseTable(_countof(configPage2.primePulse), configPage2.primePulse, configPage2.primeBins);

/** Perform the injector priming pulses.
* Set these to run at an arbitrary time in the future (100us).
* The prime pulse value is in ms*10, so need to multiple by 100 to get to uS
Expand Down
4 changes: 4 additions & 0 deletions speeduino/sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ A full copy of the license may be found in the projects root directory
#include "utilities.h"
#include "unit_testing.h"

static constexpr table2D cltCalibrationTable(_countof(cltCalibration_values), cltCalibration_values, cltCalibration_bins);
static constexpr table2D iatCalibrationTable(_countof(iatCalibration_values), iatCalibration_values, iatCalibration_bins);
static constexpr table2D o2CalibrationTable(_countof(o2Calibration_values), o2Calibration_values, o2Calibration_bins);

/**
* @brief A specialist function to map a value in the range [0, 1023] (I.e. 10-bit) to a different range.
*
Expand Down
7 changes: 7 additions & 0 deletions speeduino/speeduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ uint32_t rollingCutLastRev = 0; /**< Tracks whether we're on the same or a diffe

uint16_t staged_req_fuel_mult_pri = 0;
uint16_t staged_req_fuel_mult_sec = 0;

static constexpr table2D injectorAngleTable(_countof(configPage2.injAng), configPage2.injAng, configPage2.injAngRPM);
static constexpr table2D rotarySplitTable(_countof(configPage10.rotarySplitValues), configPage10.rotarySplitValues, configPage10.rotarySplitBins);
static constexpr table2D rollingCutTable(_countof(configPage15.rollingProtCutPercent), configPage15.rollingProtCutPercent, configPage15.rollingProtRPMDelta);
static constexpr table2D idleTargetTable(_countof(configPage6.iacCLValues), configPage6.iacCLValues, configPage6.iacBins);

#ifndef UNIT_TEST // Scope guard for unit testing

void setup(void)
{
currentStatus.initialisationComplete = false; //Tracks whether the initialiseAll() function has run completely
Expand Down
16 changes: 16 additions & 0 deletions test/test_fuel/test_corrections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "sensors_map_structs.h"

extern byte correctionWUE(void);
extern table2D WUETable; ///< 10 bin Warm Up Enrichment map (2D)

static void setup_wue_table(void) {
initialiseCorrections();
Expand Down Expand Up @@ -81,6 +82,7 @@ static void test_corrections_WUE(void)
}

extern uint16_t correctionCranking(void);
extern table2D crankingEnrichTable; ///< 4 bin cranking Enrichment map (2D)

static void setup_correctionCranking_table(void) {
initialiseCorrections();
Expand Down Expand Up @@ -203,6 +205,9 @@ static void test_corrections_ASE_inactive_cranking(void)
TEST_ASSERT_BIT_LOW(BIT_ENGINE_ASE, currentStatus.engine);
}

extern table2D ASETable; ///< 4 bin After Start Enrichment map (2D)
extern table2D ASECountTable; ///< 4 bin After Start duration map (2D)

static inline void setup_correctionASE(void) {
initialiseCorrections();

Expand Down Expand Up @@ -603,6 +608,7 @@ static void test_corrections_closedloop(void)
}

uint8_t correctionFlex(void);
extern table2D flexFuelTable; ///< 6 bin flex fuel correction table for fuel adjustments (2D)

static void setupFlexFuelTable(void) {
initialiseCorrections();
Expand All @@ -627,6 +633,7 @@ static void test_corrections_flex_flex_on(void) {
}

uint8_t correctionFuelTemp(void);
extern table2D fuelTempTable; ///< 6 bin flex fuel correction table for fuel adjustments (2D)

static void setupFuelTempTable(void) {
initialiseCorrections();
Expand Down Expand Up @@ -659,6 +666,7 @@ static void test_corrections_flex(void)
}

uint8_t correctionBatVoltage(void);
extern table2D injectorVCorrectionTable; ///< 6 bin injector voltage correction (2D)

static void setup_battery_correction(void) {
initialiseCorrections();
Expand Down Expand Up @@ -953,6 +961,8 @@ static void setup_AE(void) {
reset_AE();
}

extern table2D taeTable; ///< 4 bin TPS Acceleration Enrichment map (2D)

static void setup_TAE()
{
setup_AE();
Expand Down Expand Up @@ -1166,6 +1176,9 @@ extern map_last_read_t& getMapLast(void);

//**********************************************************************************************************************
//Setup a basic MAE enrichment curve, threshold etc that are common to all tests. Specifica values maybe updated in each individual test

extern table2D maeTable;

static void setup_MAE(void)
{
setup_AE();
Expand Down Expand Up @@ -1503,6 +1516,9 @@ extern byte correctionIATDensity(void);

extern byte correctionBaro(void);

extern table2D IATDensityCorrectionTable; ///< 9 bin inlet air temperature density correction (2D)
extern table2D baroFuelTable; ///< 8 bin baro correction curve (2D)

static void test_corrections_correctionsFuel_ae_modes(void) {
setup_TAE();
populate_2dtable(&injectorVCorrectionTable, 100, 100);
Expand Down
Loading

0 comments on commit 6a78f8f

Please sign in to comment.