From 3bd945f546adf205df5c251b121fdaeb1f6f91a0 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Wed, 25 May 2022 11:02:55 +1000 Subject: [PATCH] Add unit test for basic TAE --- platformio.ini | 5 +- speeduino/corrections.ino | 2 +- speeduino/globals.h | 1 + speeduino/sensors.h | 3 -- speeduino/sensors.ino | 4 +- test/test_decoders/test_decoders.cpp | 1 + test/test_fuel/test_corrections.cpp | 72 +++++++++++++++++++++++++++- test/test_fuel/test_corrections.h | 3 +- 8 files changed, 80 insertions(+), 11 deletions(-) diff --git a/platformio.ini b/platformio.ini index 1147475b..2fd48f4a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,7 +15,8 @@ framework=arduino build_unflags = -Os build_flags = -O3 -ffast-math -fshort-enums -funroll-loops -Wall -Wextra -std=c99 lib_deps = EEPROM, Time -test_build_project_src = true +;test_build_project_src = true +test_build_src = yes debug_tool = simavr test_ignore = test_table3d_native @@ -26,7 +27,7 @@ framework=arduino build_unflags = -Os build_flags = -O3 -ffast-math -Wall -Wextra -std=c99 lib_deps = EEPROM, Time -test_build_project_src = true +test_build_src = yes [env:teensy35] ;platform=teensy diff --git a/speeduino/corrections.ino b/speeduino/corrections.ino index 50c7f423..0c5fa65e 100644 --- a/speeduino/corrections.ino +++ b/speeduino/corrections.ino @@ -365,7 +365,7 @@ uint16_t correctionAccel() else if(configPage2.aeMode == AE_MODE_TPS) { //Get the TPS rate change - TPS_change = (currentStatus.TPS - TPSlast); + TPS_change = (currentStatus.TPS - currentStatus.TPSlast); //TPS_rateOfChange = ldiv(1000000, (TPS_time - TPSlast_time)).quot * TPS_change; //This is the % per second that the TPS has moved TPS_rateOfChange = (TPS_READ_FREQUENCY * TPS_change) / 2; //This is the % per second that the TPS has moved, adjusted for the 0.5% resolution of the TPS if(TPS_rateOfChange >= 0) { currentStatus.tpsDOT = TPS_rateOfChange / 10; } //The TAE bins are divided by 10 in order to allow them to be stored in a byte diff --git a/speeduino/globals.h b/speeduino/globals.h index ca045414..de89856d 100644 --- a/speeduino/globals.h +++ b/speeduino/globals.h @@ -637,6 +637,7 @@ struct statuses { byte TPS; /**< The current TPS reading (0% - 100%). Is the tpsADC value after the calibration is applied */ byte tpsADC; /**< byte (valued: 0-255) representation of the TPS. Downsampled from the original 10-bit (0-1023) reading, but before any calibration is applied */ byte tpsDOT; /**< TPS delta over time. Measures the % per second that the TPS is changing. Value is divided by 10 to be stored in a byte */ + byte TPSlast; /**< The previous TPS reading */ byte mapDOT; /**< MAP delta over time. Measures the kpa per second that the MAP is changing. Value is divided by 10 to be stored in a byte */ volatile int rpmDOT; /**< RPM delta over time (RPM increase / s ?) */ byte VE; /**< The current VE value being used in the fuel calculation. Can be the same as VE1 or VE2, or a calculated value of both. */ diff --git a/speeduino/sensors.h b/speeduino/sensors.h index 38f5f35a..bfadf33c 100644 --- a/speeduino/sensors.h +++ b/speeduino/sensors.h @@ -52,9 +52,6 @@ unsigned long EMAPrunningValue; //As above but for EMAP unsigned int MAPcount; //Number of samples taken in the current MAP cycle uint32_t MAPcurRev; //Tracks which revolution we're sampling on bool auxIsEnabled; -byte TPSlast; /**< The previous TPS reading */ -unsigned long TPS_time; //The time the TPS sample was taken -unsigned long TPSlast_time; //The time the previous TPS sample was taken uint16_t MAPlast; /**< The previous MAP reading */ unsigned long MAP_time; //The time the MAP sample was taken unsigned long MAPlast_time; //The time the previous MAP sample was taken diff --git a/speeduino/sensors.ino b/speeduino/sensors.ino index 24021b58..72a612e0 100644 --- a/speeduino/sensors.ino +++ b/speeduino/sensors.ino @@ -397,8 +397,7 @@ static inline void readMAP() void readTPS(bool useFilter) { - TPSlast = currentStatus.TPS; - TPSlast_time = TPS_time; + currentStatus.TPSlast = currentStatus.TPS; #if defined(ANALOG_ISR) byte tempTPS = fastMap1023toX(AnChannel[pinTPS-A0], 255); //Get the current raw TPS ADC value and map it into a byte #else @@ -439,7 +438,6 @@ void readTPS(bool useFilter) else { currentStatus.CTPSActive = digitalRead(pinCTPS); } //Inverted mode (5v activates closed throttle position sensor) } else { currentStatus.CTPSActive = 0; } - TPS_time = micros(); } void readCLT(bool useFilter) diff --git a/test/test_decoders/test_decoders.cpp b/test/test_decoders/test_decoders.cpp index 109426c8..1fd37fd2 100644 --- a/test/test_decoders/test_decoders.cpp +++ b/test/test_decoders/test_decoders.cpp @@ -1,5 +1,6 @@ #include +#include #include #include "missing_tooth/missing_tooth.h" diff --git a/test/test_fuel/test_corrections.cpp b/test/test_fuel/test_corrections.cpp index ade67a24..7724f277 100644 --- a/test/test_fuel/test_corrections.cpp +++ b/test/test_fuel/test_corrections.cpp @@ -8,6 +8,7 @@ void testCorrections() { test_corrections_WUE(); test_corrections_dfco(); + test_corrections_TAE(); //TPS based accel enrichment corrections /* RUN_TEST(test_corrections_cranking); //Not written yet RUN_TEST(test_corrections_ASE); //Not written yet @@ -140,7 +141,7 @@ void setup_DFCO_on() correctionDFCO(); dfcoTaper = 20; } - +//********************************************************************************************************************** void test_corrections_dfco_on(void) { //Test under ideal conditions that DFCO goes active @@ -185,3 +186,72 @@ void test_corrections_dfco() RUN_TEST(test_corrections_dfco_off_TPS); RUN_TEST(test_corrections_dfco_off_delay); } +//********************************************************************************************************************** +//Setup a basic TAE enrichment curve, threshold etc that are common to all tests. Specifica values maybe updated in each individual test +void test_corrections_TAE_setup() +{ + configPage2.aeMode = AE_MODE_TPS; //Set AE to TPS + + configPage4.taeValues[0] = 70; + configPage4.taeValues[1] = 103; + configPage4.taeValues[2] = 124; + configPage4.taeValues[3] = 136; + + //Note: These values are divided by 10 + configPage4.taeBins[0] = 0; + configPage4.taeBins[1] = 8; + configPage4.taeBins[2] = 22; + configPage4.taeBins[3] = 97; + + configPage2.taeThresh = 0; + + //Divided by 100 + configPage2.aeTaperMin = 10; //1000 + configPage2.aeTaperMax = 50; //5000 + + BIT_CLEAR(currentStatus.engine, BIT_ENGINE_ACC); //Make sure AE is turned off +} + +void test_corrections_TAE_no_taper() +{ + //Disable the taper + currentStatus.RPM = 2000; + configPage2.aeTaperMin = 50; //5000 + configPage2.aeTaperMax = 60; //6000 + + currentStatus.TPSlast = 0; + currentStatus.TPS = 50; //25% actual value + + uint16_t accelValue = correctionAccel(); //Run the AE calcs + + TEST_ASSERT_EQUAL(75, currentStatus.tpsDOT); //DOT is 750%/s (25 * 30), value divided by 10; + TEST_ASSERT_EQUAL((100+132), accelValue); + +} + +void test_corrections_TAE_50pc_taper() +{ + //RPM is 50% of the way through the taper range + currentStatus.RPM = 3000; + configPage2.aeTaperMin = 10; //1000 + configPage2.aeTaperMax = 50; //5000 + + currentStatus.TPSlast = 0; + currentStatus.TPS = 50; //25% actual value + + uint16_t accelValue = correctionAccel(); //Run the AE calcs + + TEST_ASSERT_EQUAL(75, currentStatus.tpsDOT); //DOT is 750%/s (25 * 30), value divided by 10; + TEST_ASSERT_EQUAL((100+66), accelValue); + +} + +void test_corrections_TAE() +{ + test_corrections_TAE_setup(); + + + RUN_TEST(test_corrections_TAE_no_taper); + BIT_CLEAR(currentStatus.engine, BIT_ENGINE_ACC); //Flag must be cleared between tests + RUN_TEST(test_corrections_TAE_50pc_taper); +} \ No newline at end of file diff --git a/test/test_fuel/test_corrections.h b/test/test_fuel/test_corrections.h index 0f148915..d9ef1bb1 100644 --- a/test/test_fuel/test_corrections.h +++ b/test/test_fuel/test_corrections.h @@ -9,4 +9,5 @@ void test_corrections_bat(void); void test_corrections_iatdensity(void); void test_corrections_baro(void); void test_corrections_launch(void); -void test_corrections_dfco(void); \ No newline at end of file +void test_corrections_dfco(void); +void test_corrections_TAE(void); \ No newline at end of file