Skip to content

Commit

Permalink
Unit testability: inject all dependencies for individual ignition cor…
Browse files Browse the repository at this point in the history
…rections
  • Loading branch information
adbancroft committed Sep 30, 2024
1 parent 18966f1 commit 4ba90f0
Show file tree
Hide file tree
Showing 7 changed files with 662 additions and 556 deletions.
222 changes: 111 additions & 111 deletions speeduino/corrections.cpp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions speeduino/corrections.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ uint8_t calculateAfrTarget(const table3d16RpmLoad &afrLookUpTable, const statuse

void initialiseIgnCorrections(statuses &current);
int8_t correctionsIgn(int8_t advance);
int8_t correctionFixedTiming(int8_t advance);
int8_t correctionCrankingFixedTiming(int8_t advance);
int8_t correctionFixedTiming(int8_t advance, const config2 &page2, const config4 &page4);
int8_t correctionCrankingFixedTiming(int8_t advance, const statuses &current, const config2 &page2, const config4 &page4, const table2D &cltAdvanceLUT);

uint16_t correctionsDwell(uint16_t dwell);
uint16_t correctionsDwell(uint16_t dwell, statuses &current, const config2 &page2, const config4 &page4, const config10 &page10, const table2D &lookupTable);


#endif // CORRECTIONS_H
4 changes: 2 additions & 2 deletions speeduino/secondaryTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ void calculateSecondarySpark(void)
}

//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
currentStatus.advance = correctionFixedTiming(currentStatus.advance, configPage2, configPage4);
currentStatus.advance = correctionCrankingFixedTiming(currentStatus.advance, currentStatus, configPage2, configPage4, CLTAdvanceTable); //This overrides the regular fixed timing, must come last
}
}

Expand Down
2 changes: 1 addition & 1 deletion speeduino/speeduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ void loop(void)
currentStatus.dwell = (configPage4.dwellRun * 100U); //use fixed running dwell
}
}
currentStatus.dwell = correctionsDwell(currentStatus.dwell);
currentStatus.dwell = correctionsDwell(currentStatus.dwell, currentStatus, configPage2, configPage4, configPage10, dwellVCorrectionTable);

// Convert the dwell time to dwell angle based on the current engine speed
calculateIgnitionAngles(timeToAngleDegPerMicroSec(currentStatus.dwell));
Expand Down
39 changes: 11 additions & 28 deletions test/test_fuel/test_corrections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ extern void construct2dTable(table2D &table, uint8_t length, uint8_t *values, ui

extern uint8_t correctionWUE(statuses &current, const table2D &lookUpTable);

template <uint8_t length>
struct test_2dtable_t {
table2D lookupTable;
uint8_t bins[length];
uint8_t values[length];

test_2dtable_t() {
construct2dTable(lookupTable, length, values, bins);
}
};

struct wue_test_data_t : public test_2dtable_t<10> {
statuses current;
};
Expand Down Expand Up @@ -235,12 +224,8 @@ static void test_corrections_ASE_inactive_cranking(void)

struct ase_testdata_t {
statuses current;
table2D durationTable;
uint8_t durationBins[4];
uint8_t durationValues[4];
table2D amountTable;
uint8_t amountBins[4];
uint8_t amountValues[4];
test_2dtable_t<4> duration;
test_2dtable_t<4> amount;
config2 page2;
};

Expand All @@ -253,27 +238,25 @@ static inline void setup_correctionASE(ase_testdata_t &testData) {
testData.current.runSecs = 3;

{
construct2dTable(testData.durationTable, _countof(testData.durationBins), testData.durationValues, testData.durationBins);
TEST_DATA_P uint8_t values[] = { 10, 8, 6, 4 };
TEST_DATA_P uint8_t bins[] = {
toStorageTemperature(COOLANT_INITIAL) - 10U,
toStorageTemperature(COOLANT_INITIAL) + 10U,
toStorageTemperature(COOLANT_INITIAL) + 20U,
toStorageTemperature(COOLANT_INITIAL) + 30U
};
populate_2dtable_P(&testData.durationTable, values, bins);
populate_2dtable_P(&testData.duration.lookupTable, values, bins);
}

{
construct2dTable(testData.amountTable, _countof(testData.amountBins), testData.amountValues, testData.amountBins);
TEST_DATA_P uint8_t values[] = { 20, 30, 40, 50 };
TEST_DATA_P uint8_t bins[] = {
toStorageTemperature(COOLANT_INITIAL) - 10U,
toStorageTemperature(COOLANT_INITIAL) + 10U,
toStorageTemperature(COOLANT_INITIAL) + 20U,
toStorageTemperature(COOLANT_INITIAL) + 30U
};
populate_2dtable_P(&testData.amountTable, values, bins);
populate_2dtable_P(&testData.amount.lookupTable, values, bins);
}
}

Expand All @@ -283,7 +266,7 @@ static void test_corrections_ASE_initial(void)
setup_correctionASE(testData);

// Should be half way between the 2 table values.
TEST_ASSERT_EQUAL(125, correctionASE(testData.current, testData.durationTable, testData.amountTable, testData.page2));
TEST_ASSERT_EQUAL(125, correctionASE(testData.current, testData.duration.lookupTable, testData.amount.lookupTable, testData.page2));
TEST_ASSERT_BIT_HIGH(BIT_ENGINE_ASE, testData.current.engine);
}

Expand All @@ -297,22 +280,22 @@ static void test_corrections_ASE_taper(void) {
// Advance taper to halfway
BIT_CLEAR(testData.current.engine, BIT_ENGINE_CRANK);
for (uint8_t index=0; index<testData.page2.aseTaperTime/2U; ++index) {
(void)correctionASE(testData.current, testData.durationTable, testData.amountTable, testData.page2);
(void)correctionASE(testData.current, testData.duration.lookupTable, testData.amount.lookupTable, testData.page2);
}

// Should be half way between the interpolated table value and 100%.
TEST_ASSERT_INT_WITHIN(1, 113, correctionASE(testData.current, testData.durationTable, testData.amountTable, testData.page2));
TEST_ASSERT_INT_WITHIN(1, 113, correctionASE(testData.current, testData.duration.lookupTable, testData.amount.lookupTable, testData.page2));
TEST_ASSERT_BIT_HIGH(BIT_ENGINE_ASE, testData.current.engine);

// Final taper step
for (uint8_t index=testData.page2.aseTaperTime/2U; index<testData.page2.aseTaperTime-2U; ++index) {
(void)correctionASE(testData.current, testData.durationTable, testData.amountTable, testData.page2);
(void)correctionASE(testData.current, testData.duration.lookupTable, testData.amount.lookupTable, testData.page2);
}
TEST_ASSERT_INT_WITHIN(1, 103U, correctionASE(testData.current, testData.durationTable, testData.amountTable, testData.page2) );
TEST_ASSERT_INT_WITHIN(1, 103U, correctionASE(testData.current, testData.duration.lookupTable, testData.amount.lookupTable, testData.page2) );

// Taper finished
TEST_ASSERT_EQUAL(100U, correctionASE(testData.current, testData.durationTable, testData.amountTable, testData.page2));
TEST_ASSERT_EQUAL(100U, correctionASE(testData.current, testData.durationTable, testData.amountTable, testData.page2));
TEST_ASSERT_EQUAL(100U, correctionASE(testData.current, testData.duration.lookupTable, testData.amount.lookupTable, testData.page2));
TEST_ASSERT_EQUAL(100U, correctionASE(testData.current, testData.duration.lookupTable, testData.amount.lookupTable, testData.page2));
}

static void test_corrections_ASE(void)
Expand Down
Loading

0 comments on commit 4ba90f0

Please sign in to comment.