diff --git a/firmware/.cproject b/firmware/.cproject index 86ebb61d2e..e01fca04ca 100644 --- a/firmware/.cproject +++ b/firmware/.cproject @@ -140,6 +140,10 @@ + + + @@ -57,6 +59,8 @@ + + @@ -71,7 +75,7 @@ - + @@ -119,7 +123,7 @@ - + diff --git a/unit_tests/Makefile b/unit_tests/Makefile index 17041869be..77bd93a56f 100644 --- a/unit_tests/Makefile +++ b/unit_tests/Makefile @@ -71,8 +71,11 @@ PROJECT = rusefi_test include $(PROJECT_DIR)/util/util.mk include $(PROJECT_DIR)/config/engines/engines.mk include $(PROJECT_DIR)/controllers/algo/algo.mk +include $(PROJECT_DIR)/controllers/core/core.mk include $(PROJECT_DIR)/controllers/math/math.mk +include $(PROJECT_DIR)/controllers/system/system.mk include $(PROJECT_DIR)/controllers/sensors/sensors.mk +include $(PROJECT_DIR)/controllers/trigger/trigger.mk include test.mk # Define linker script file here @@ -83,6 +86,7 @@ include test.mk # setting. CSRC = $(UTILSRC) \ $(CONTROLLERS_ALGO_SRC) \ + $(CONTROLLERS_CORE_SRC) \ $(CONTROLLERS_MATH_SRC) \ $(CONTROLLERS_SENSORS_SRC) \ $(ENGINES_SRC) \ @@ -92,15 +96,16 @@ CSRC = $(UTILSRC) \ # setting. CPPSRC = $(UTILSRC_CPP) \ $(CONTROLLERS_ALGO_SRC_CPP) \ - $(PROJECT_DIR)/controllers/trigger/trigger_mazda.cpp \ - $(PROJECT_DIR)/controllers/trigger/trigger_chrysler.cpp \ - $(PROJECT_DIR)/controllers/trigger/trigger_gm.cpp \ - $(PROJECT_DIR)/controllers/trigger/trigger_decoder.cpp \ - $(PROJECT_DIR)/controllers/trigger/trigger_structure.cpp \ + $(TRIGGER_DECODERS_SRC_CPP) \ $(ENGINES_SRC_CPP) \ + $(CONTROLLERS_CORE_SRC_CPP) \ $(CONTROLLERS_MATH_SRC_CPP) \ $(CONTROLLERS_SENSORS_SRC_CPP) \ $(TEST_SRC_CPP) \ + $(PROJECT_DIR)/controllers/trigger/trigger_central.cpp \ + $(PROJECT_DIR)/controllers/trigger/rpm_calculator.cpp \ + $(PROJECT_DIR)/controllers/trigger/main_trigger_callback.cpp \ + $(PROJECT_DIR)/controllers/system/event_queue.cpp \ main.cpp # C sources to be compiled in ARM mode regardless of the global setting. @@ -131,7 +136,9 @@ INCDIR = . \ $(PROJECT_DIR)/config/engines \ $(PROJECT_DIR)/controllers/sensors \ $(PROJECT_DIR)/controllers/algo \ + $(PROJECT_DIR)/controllers/core \ $(PROJECT_DIR)/controllers/math \ + $(PROJECT_DIR)/controllers/system \ $(PROJECT_DIR)/controllers/trigger \ $(PROJECT_DIR)/ext_algo \ test_data_structures \ diff --git a/unit_tests/boards.h b/unit_tests/boards.h index 8a162b459f..b49de7bdd3 100644 --- a/unit_tests/boards.h +++ b/unit_tests/boards.h @@ -8,14 +8,7 @@ #ifndef BOARDS_H_ #define BOARDS_H_ -#define ADC_LOGIC_TPS 0 -#define ADC_LOGIC_AFR 0 -#define ADC_LOGIC_MAF 0 -#define ADC_LOGIC_MAP 0 #define ADC_CHANNEL_VREF 0 -#define ADC_CHANNEL_VBATT 0 -#define ADC_LOGIC_INTAKE_AIR 0 -#define ADC_LOGIC_COOLANT 0 float getVoltageDivided(int); float getVoltage(int channel); diff --git a/unit_tests/efifeatures.h b/unit_tests/efifeatures.h index 7712b23b37..92fe24e680 100644 --- a/unit_tests/efifeatures.h +++ b/unit_tests/efifeatures.h @@ -1,5 +1,5 @@ -/* - * efifeatures.h +/** + * @file efifeatures.h * * Created on: Mar 7, 2014 * Author: Andrey @@ -8,6 +8,8 @@ #ifndef EFIFEATURES_H_ #define EFIFEATURES_H_ +#define EFI_HISTOGRAMS TRUE + #define EFI_CLI_SUPPORT FALSE #define EFI_SUPPORT_FORD_ASPIRE TRUE @@ -18,5 +20,7 @@ #define EFI_SIGNAL_EXECUTOR_ONE_TIMER TRUE +#define EFI_SHAFT_POSITION_INPUT TRUE +#define EFI_ENGINE_CONTROL TRUE #endif /* EFIFEATURES_H_ */ diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp new file mode 100644 index 0000000000..185753f0cf --- /dev/null +++ b/unit_tests/engine_test_helper.cpp @@ -0,0 +1,38 @@ +/** + * @file engine_test_helper.cpp + * + * @date Jun 26, 2014 + * @author Andrey Belomutskiy, (c) 2012-2014 + */ + +#include "engine_test_helper.h" +#include "stddef.h" +#include "trigger_decoder.h" + +extern int timeNow; + +EngineTestHelper::EngineTestHelper(engine_type_e engineType) { + ec = &persistentConfig.engineConfiguration; + + configuration.engineConfiguration = ec; + configuration.engineConfiguration2 = &ec2; + + resetConfigurationExt(NULL, FORD_INLINE_6_1995, ec, &ec2, &persistentConfig.engineConfiguration.bc); +} + +void EngineTestHelper::fireTriggerEvents() { + for (int i = 0; i < 24; i++) { + timeNow += 5000; // 5ms + triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + timeNow += 5000; + triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_DOWN, timeNow); + } +} + +void EngineTestHelper::initTriggerShapeAndRpmCalculator() { + initializeTriggerShape(NULL, ec, &ec2); + incrementGlobalConfigurationVersion(); + + triggerCentral.addEventListener((ShaftPositionListener) &rpmShaftPositionCallback, "rpm reporter", &rpmState); + +} diff --git a/unit_tests/engine_test_helper.h b/unit_tests/engine_test_helper.h new file mode 100644 index 0000000000..818c0bb47d --- /dev/null +++ b/unit_tests/engine_test_helper.h @@ -0,0 +1,32 @@ +/** + * @file engine_test_helper.h + * + * @date Jun 26, 2014 + * @author Andrey Belomutskiy, (c) 2012-2014 + */ +#ifndef ENGINE_TEST_HELPER_H_ +#define ENGINE_TEST_HELPER_H_ + +#include "engine_configuration.h" +#include "ec2.h" +#include "trigger_central.h" +#include "rpm_calculator.h" + +class EngineTestHelper { +public: + EngineTestHelper(engine_type_e engineType); + void initTriggerShapeAndRpmCalculator(); + void fireTriggerEvents(); + + persistent_config_s persistentConfig; + engine_configuration2_s ec2; + configuration_s configuration; + + engine_configuration_s *ec; + + TriggerCentral triggerCentral; + RpmCalculator rpmState; + +}; + +#endif /* ENGINE_TEST_HELPER_H_ */ diff --git a/unit_tests/global.h b/unit_tests/global.h index 8cf7b254cf..f7bd257c0f 100644 --- a/unit_tests/global.h +++ b/unit_tests/global.h @@ -13,7 +13,13 @@ #include "efitime.h" +typedef int bool_t; + +typedef void * Logging; + #define TRUE 1 #define FALSE 0 +#define CCM_OPTIONAL + #endif /* GLOBAL_H_ */ diff --git a/unit_tests/main.cpp b/unit_tests/main.cpp index 2bab2a75af..50574f8d26 100644 --- a/unit_tests/main.cpp +++ b/unit_tests/main.cpp @@ -1,10 +1,8 @@ -/* - ============================================================================ - Name : main.c - Author : Andrey Belomutskiy - Copyright : (c) 2012-2013 - Description : First step towards unit-testing rusEfi algorithms on win32 - ============================================================================ +/** + * @file main.cpp + * @file First step towards unit-testing rusEfi algorithms + * + * @author Andrey Belomutskiy (c) 2012-2014 */ #include @@ -15,36 +13,35 @@ #include "main.h" #include "error_handling.h" #include "ec2.h" +#include "test_accel_enrichment.h" +#include "test_interpolation_3d.h" +#include "test_find_index.h" +#include "test_sensors.h" +#include "test_speed_density.h" +#include "test_fuel_map.h" extern "C" { - #include "map_resize.h" #include "test_idle_controller.h" -#include "test_interpolation_3d.h" -#include "test_find_index.h" -#include "test_fuel_map.h" #include "test_event_registry.h" -#include "test_sensors.h" #include "test_signal_executor.h" #include "test_util.h" #include "engine_configuration.h" -#include "engine_math.h" } +#include "engine_math.h" #include "test_engine_math.h" #include "test_trigger_decoder.h" static engine_configuration_s ec; engine_configuration_s *engineConfiguration = &ec; -static float absF(float value) { - return value > 0 ? value : -value; -} +int timeNow = 0; uint64_t getTimeNowUs(void) { - return 0; + return timeNow; } void assertEqualsM(const char *msg, float expected, float actual) { @@ -103,6 +100,7 @@ int main(void) { testEventRegistry(); testSensors(); testCyclicBuffer(); + testCrc(); testSignalExecutor(); @@ -119,18 +117,25 @@ int main(void) { testPinHelper(); testSetTableValue(); - printf("Success 20130429\r\n"); + testAccelEnrichment(); -// resizeMap(); + testSpeedDensity(); + testFLStack(); + + // resizeMap(); + printf("Success 20130706\r\n"); return EXIT_SUCCESS; } - int warning(obd_code_e code, const char *fmt, ...) { printf("Warning: %s\r\n", fmt); } +bool hasFirmwareError(void) { + return FALSE; +} + void firmwareError(const char *fmt, ...) { printf(fmt); exit(-1); @@ -152,3 +157,7 @@ int warning(const char *fmt, ...) { printf(fmt); exit(-1); } + +bool_t isCranking(void) { + return 0; +} diff --git a/unit_tests/main.h b/unit_tests/main.h index 5122fb4ce4..ecac1ad520 100644 --- a/unit_tests/main.h +++ b/unit_tests/main.h @@ -26,7 +26,6 @@ extern "C" #include "efitime.h" #include "boards.h" -#include "engines.h" typedef int bool_t; diff --git a/unit_tests/makelinks.bat b/unit_tests/makelinks.bat index 95cde49cba..514c51a965 100644 --- a/unit_tests/makelinks.bat +++ b/unit_tests/makelinks.bat @@ -1,6 +1,8 @@ junction controllers_algo ..\firmware\controllers\algo +junction controllers_core ..\firmware\controllers\core junction controllers_math ..\firmware\controllers\math +junction controllers_system ..\firmware\controllers\system junction controllers_trigger ..\firmware\controllers\trigger junction engines ..\firmware\config\engines junction controllers_sensors ..\firmware\controllers\sensors diff --git a/unit_tests/map_resize.c b/unit_tests/map_resize.cpp similarity index 97% rename from unit_tests/map_resize.c rename to unit_tests/map_resize.cpp index cc876a1020..6addb2b876 100644 --- a/unit_tests/map_resize.c +++ b/unit_tests/map_resize.cpp @@ -1,176 +1,175 @@ -/** - * @file map_resize.c - * - * @date Jan 12, 2014 - * @author Andrey Belomutskiy, (c) 2012-2013 - */ - -#include -#include "fuel_math.h" -#include "interpolation.h" -#include "engines.h" -#include "engine_configuration.h" -#include "main.h" -#include "idle_controller.h" - -#if 1 - -#define AD_LOAD_COUNT 16 -#define AD_RPM_COUNT 16 - - -static float ad_rpm_table[] = {/*0*/ 800.000000, - /*1*/ 1213.333374, - /*2*/ 1626.666748, - /*3*/ 2040.000000, - /*4*/ 2453.333496, - /*5*/ 2866.666748, - /*6*/ 3280.000000, - /*7*/ 3693.333496, - /*8*/ 4106.666992, - /*9*/ 4520.000000, - /*10*/ 4933.333496, - /*11*/ 5346.666992, - /*12*/ 5760.000000, - /*13*/ 6173.333496, - /*14*/ 6586.666992, - /*15*/ 7000.000000, - };; - -static float ad_maf_table[] = -{/*0*/ 1.200000, -/*1*/ 1.413333, -/*2*/ 1.626667, -/*3*/ 1.840000, -/*4*/ 2.053333, -/*5*/ 2.266667, -/*6*/ 2.480000, -/*7*/ 2.693333, -/*8*/ 2.906667, -/*9*/ 3.120000, -/*10*/ 3.333333, -/*11*/ 3.546667, -/*12*/ 3.760000, -/*13*/ 3.973333, -/*14*/ 4.186667, -/*15*/ 4.400000, -}; - -static float ad_table[AD_LOAD_COUNT][AD_RPM_COUNT] = { - /* RPM 800.000000 1213.333374 1626.666748 2040.000000 2453.333496 2866.666748 3280.000000 3693.333496 4106.666992 4520.000000 4933.333496 5346.666992 5760.000000 6173.333496 6586.666992 7000.000000*/ - /* Load 1.200000 */{ 0.662000, -7.730000, -16.722000, -23.139999, -29.398001, -31.268000, -32.108002, -30.436001, -30.896000, -26.656000, -24.704000, -25.108000, -25.132000, -25.459999, -25.459999, -25.459999}, - /* Load 1.413333 */{ 0.546000, -7.662000, -16.882000, -23.482000, -29.520000, -31.323999, -32.108002, -30.656000, -30.468000, -26.879999, -24.746000, -24.742001, -29.032000, -25.562000, -25.562000, -25.562000}, - /* Load 1.626667 */{ 0.584000, -7.870000, -16.714001, -23.025999, -29.542000, -31.166000, -32.175999, -30.540001, -30.268000, -26.416000, -24.134001, -25.007999, -24.698000, -26.167999, -26.167999, -26.167999}, - /* Load 1.840000 */{ 0.584000, -7.658000, -16.714001, -23.254000, -29.351999, -30.978001, -32.141998, -30.874001, -30.896000, -26.507999, -24.558001, -24.389999, -25.761999, -35.492001, -35.492001, -35.492001}, - /* Load 2.053333 */{ 0.584000, -7.862000, -16.538000, -23.254000, -29.232000, -31.296000, -32.520000, -30.142000, -30.388000, -25.903999, -24.370001, -24.082001, -24.792000, -24.351999, -24.351999, -24.351999}, - /* Load 2.266667 */{ -1.364000, -7.726000, -16.806000, -23.254000, -29.639999, -31.006001, -32.298000, -30.912001, -29.882000, -26.392000, -24.664000, -27.233999, -25.374001, -25.417999, -25.417999, -25.417999}, - /* Load 2.480000 */{ 1.364000, -10.490000, -16.705999, -22.441999, -28.101999, -30.238001, -32.363998, -30.719999, -30.896000, -26.608000, -24.664000, -24.431999, -24.500000, -25.510000, -25.510000, -25.510000}, - /* Load 2.693333 */{ 9.864000, -10.416000, -11.680000, -19.150000, -25.754000, -27.936001, -32.554001, -30.656000, -30.153999, -27.184000, -25.252001, -22.812000, -24.452000, -25.219999, -25.219999, -25.219999}, - /* Load 2.906667 */{ 9.866000, 5.452000, 2.854000, -17.212000, -17.552000, -20.688000, -25.660000, -27.809999, -27.691999, -27.224001, -25.882000, -25.360001, -26.100000, -27.992001, -27.992001, -27.992001}, - /* Load 3.120000 */{ 9.864000, 5.452000, 2.854000, -0.342000, -12.526000, -16.218000, -21.364000, -27.590000, -25.780001, -24.170000, -24.664000, -25.584000, -26.490000, -31.968000, -31.968000, -31.968000}, - /* Load 3.333333 */{ 9.864000, 5.516000, 2.854000, -0.226000, -2.738000, -3.816000, -11.924000, -18.808001, -21.038000, -21.538000, -21.209999, -22.228001, -25.046000, -25.156000, -25.156000, -25.156000}, - /* Load 3.546667 */{ 9.866000, 5.518000, 2.854000, 0.000000, -3.022000, -3.816000, -6.428000, -7.788000, -19.426001, -20.860001, -19.966000, -21.030001, -21.396000, -21.570000, -21.570000, -21.570000}, - /* Load 3.760000 */{ 9.864000, 5.516000, 2.772000, -0.226000, -2.732000, -3.500000, -6.798000, -8.102000, -8.660000, -9.500000, -11.788000, -20.132000, -20.072001, -20.510000, -20.510000, -20.510000}, - /* Load 3.973333 */{ 9.864000, 5.518000, 2.854000, 0.000000, -2.880000, -3.816000, -6.420000, -8.320000, -8.426000, -8.532000, -11.470000, -11.442000, -13.610000, -12.022000, -12.022000, -12.022000}, - /* Load 4.186667 */{ 9.750000, 5.518000, 2.604000, 0.000000, -2.880000, -3.654000, -6.050000, -6.888000, -8.372000, -9.364000, -11.764000, -11.732000, -11.864000, -12.376000, -12.376000, -12.376000}, - /* Load 4.400000 */{ 0.350000, 5.590000, 0.502000, 0.910000, 0.864000, 0.954000, 1.324000, -7.436000, 1.170000, 1.054000, 2.058000, 2.098000, 2.636000, -12.352000, -12.352000, -12.352000} - }; - -//float getBaseAdvance(int rpm, float key) { -// // todo: use interpolation -// int rpm_index = findIndex(ad_rpm_table, AD_RPM_COUNT, rpm); -// rpm_index = max(rpm_index, 0); -// int maf_index = findIndex(ad_maf_table, AD_LOAD_COUNT, key); -// maf_index = max(maf_index, 0); -// -// return ad_table[rpm_index][maf_index]; -//} - - - -#define newRpmSize 16 -#define newKeySize 16 - -static float newRpmBin[newRpmSize]; -static float newKeyBin[newKeySize]; - -//static float *fuel_ptrs[FUEL_LOAD_COUNT]; - -//EngineConfiguration *engineConfiguration; - -extern int needInterpolationLogging; - -void resizeMap(void) { -// float keyMin = 1.2; -// float keyMax = 4.4; -// -// float rpmMin = 800; -// float rpmMax = 7000; - -// for (int k = 0; k < FUEL_LOAD_COUNT; k++) -// fuel_ptrs[k] = engineConfiguration->fuelTable[k]; - -// for (int i = 0; i < FUEL_MAF_COUNT; i++) -// engineConfiguration->fuelKeyBins[i] = default_fuel_maf_bins[i]; -// for (int i = 0; i < FUEL_RPM_COUNT; i++) -// engineConfiguration->fuelRpmBins[i] = default_fuel_rpm_bins[i]; -// for (int k = 0; k < FUEL_MAF_COUNT; k++) { -// for (int r = 0; r < FUEL_RPM_COUNT; r++) { -// // todo: this is BAD, this needs to be fixed - TS table indexes are different from default indexes -// engineConfiguration->fuelTable[k][r] = default_fuel_table[r][k]; -// } -// } - -// assertEquals(15, interpolate3d(1.2, engineConfiguration->fuelKeyBins, FUEL_MAF_COUNT, 8000, -// engineConfiguration->fuelRpmBins, -// FUEL_RPM_COUNT, fuel_ptrs)); - - needInterpolationLogging = 0; - -// printf("static float ad_maf_table[AD_LOAD_COUNT] = {"); -// for (int i = 0; i < newKeySize; i++) { -// newKeyBin[i] = interpolate(0, keyMin, newKeySize - 1, keyMax, i); -// printf("/*%d*/ %f,\r\n", i, newKeyBin[i]); -// } -// printf("};\r\n"); -// -// printf("static float ad_rpm_table[AD_RPM_COUNT] = {"); -// for (int i = 0; i < newRpmSize; i++) { -// newRpmBin[i] = interpolate(0, rpmMin, newRpmSize - 1, rpmMax, i); -// printf("/*%d*/ %f,\r\n", i, newRpmBin[i]); -// } -// printf("};\r\n"); - - printf("static float ad_table[AD_RPM_COUNT][AD_LOAD_COUNT] = {\r\n"); - - printf("/* RPM\t\t"); - for (int r = 0; r < newRpmSize; r++) { - float rpm = newRpmBin[r]; - printf("\t%f", rpm); - } - printf("*/\r\n"); - - for (int k = 0; k < newKeySize; k++) { - float load = newKeyBin[k]; - printf("/* Load %f */{", load); - - for (int r = 0; r < newRpmSize; r++) { - float rpm = newRpmBin[r]; - - float v = ad_table[k][r]; - - printf("\t%f", v); - if (r != newRpmSize - 1) - printf(","); - - } - printf("}"); - if (k != newKeySize - 1) - printf(","); - printf("\r\n"); - } - printf("};\r\n"); - -} - -#endif +/** + * @file map_resize.c + * + * @date Jan 12, 2014 + * @author Andrey Belomutskiy, (c) 2012-2013 + */ + +#include +#include "fuel_math.h" +#include "interpolation.h" +#include "engine_configuration.h" +#include "main.h" +#include "idle_controller.h" + +#if 1 + +#define AD_LOAD_COUNT 16 +#define AD_RPM_COUNT 16 + + +static float ad_rpm_table[] = {/*0*/ 800.000000, + /*1*/ 1213.333374, + /*2*/ 1626.666748, + /*3*/ 2040.000000, + /*4*/ 2453.333496, + /*5*/ 2866.666748, + /*6*/ 3280.000000, + /*7*/ 3693.333496, + /*8*/ 4106.666992, + /*9*/ 4520.000000, + /*10*/ 4933.333496, + /*11*/ 5346.666992, + /*12*/ 5760.000000, + /*13*/ 6173.333496, + /*14*/ 6586.666992, + /*15*/ 7000.000000, + };; + +static float ad_maf_table[] = +{/*0*/ 1.200000, +/*1*/ 1.413333, +/*2*/ 1.626667, +/*3*/ 1.840000, +/*4*/ 2.053333, +/*5*/ 2.266667, +/*6*/ 2.480000, +/*7*/ 2.693333, +/*8*/ 2.906667, +/*9*/ 3.120000, +/*10*/ 3.333333, +/*11*/ 3.546667, +/*12*/ 3.760000, +/*13*/ 3.973333, +/*14*/ 4.186667, +/*15*/ 4.400000, +}; + +static float ad_table[AD_LOAD_COUNT][AD_RPM_COUNT] = { + /* RPM 800.000000 1213.333374 1626.666748 2040.000000 2453.333496 2866.666748 3280.000000 3693.333496 4106.666992 4520.000000 4933.333496 5346.666992 5760.000000 6173.333496 6586.666992 7000.000000*/ + /* Load 1.200000 */{ 0.662000, -7.730000, -16.722000, -23.139999, -29.398001, -31.268000, -32.108002, -30.436001, -30.896000, -26.656000, -24.704000, -25.108000, -25.132000, -25.459999, -25.459999, -25.459999}, + /* Load 1.413333 */{ 0.546000, -7.662000, -16.882000, -23.482000, -29.520000, -31.323999, -32.108002, -30.656000, -30.468000, -26.879999, -24.746000, -24.742001, -29.032000, -25.562000, -25.562000, -25.562000}, + /* Load 1.626667 */{ 0.584000, -7.870000, -16.714001, -23.025999, -29.542000, -31.166000, -32.175999, -30.540001, -30.268000, -26.416000, -24.134001, -25.007999, -24.698000, -26.167999, -26.167999, -26.167999}, + /* Load 1.840000 */{ 0.584000, -7.658000, -16.714001, -23.254000, -29.351999, -30.978001, -32.141998, -30.874001, -30.896000, -26.507999, -24.558001, -24.389999, -25.761999, -35.492001, -35.492001, -35.492001}, + /* Load 2.053333 */{ 0.584000, -7.862000, -16.538000, -23.254000, -29.232000, -31.296000, -32.520000, -30.142000, -30.388000, -25.903999, -24.370001, -24.082001, -24.792000, -24.351999, -24.351999, -24.351999}, + /* Load 2.266667 */{ -1.364000, -7.726000, -16.806000, -23.254000, -29.639999, -31.006001, -32.298000, -30.912001, -29.882000, -26.392000, -24.664000, -27.233999, -25.374001, -25.417999, -25.417999, -25.417999}, + /* Load 2.480000 */{ 1.364000, -10.490000, -16.705999, -22.441999, -28.101999, -30.238001, -32.363998, -30.719999, -30.896000, -26.608000, -24.664000, -24.431999, -24.500000, -25.510000, -25.510000, -25.510000}, + /* Load 2.693333 */{ 9.864000, -10.416000, -11.680000, -19.150000, -25.754000, -27.936001, -32.554001, -30.656000, -30.153999, -27.184000, -25.252001, -22.812000, -24.452000, -25.219999, -25.219999, -25.219999}, + /* Load 2.906667 */{ 9.866000, 5.452000, 2.854000, -17.212000, -17.552000, -20.688000, -25.660000, -27.809999, -27.691999, -27.224001, -25.882000, -25.360001, -26.100000, -27.992001, -27.992001, -27.992001}, + /* Load 3.120000 */{ 9.864000, 5.452000, 2.854000, -0.342000, -12.526000, -16.218000, -21.364000, -27.590000, -25.780001, -24.170000, -24.664000, -25.584000, -26.490000, -31.968000, -31.968000, -31.968000}, + /* Load 3.333333 */{ 9.864000, 5.516000, 2.854000, -0.226000, -2.738000, -3.816000, -11.924000, -18.808001, -21.038000, -21.538000, -21.209999, -22.228001, -25.046000, -25.156000, -25.156000, -25.156000}, + /* Load 3.546667 */{ 9.866000, 5.518000, 2.854000, 0.000000, -3.022000, -3.816000, -6.428000, -7.788000, -19.426001, -20.860001, -19.966000, -21.030001, -21.396000, -21.570000, -21.570000, -21.570000}, + /* Load 3.760000 */{ 9.864000, 5.516000, 2.772000, -0.226000, -2.732000, -3.500000, -6.798000, -8.102000, -8.660000, -9.500000, -11.788000, -20.132000, -20.072001, -20.510000, -20.510000, -20.510000}, + /* Load 3.973333 */{ 9.864000, 5.518000, 2.854000, 0.000000, -2.880000, -3.816000, -6.420000, -8.320000, -8.426000, -8.532000, -11.470000, -11.442000, -13.610000, -12.022000, -12.022000, -12.022000}, + /* Load 4.186667 */{ 9.750000, 5.518000, 2.604000, 0.000000, -2.880000, -3.654000, -6.050000, -6.888000, -8.372000, -9.364000, -11.764000, -11.732000, -11.864000, -12.376000, -12.376000, -12.376000}, + /* Load 4.400000 */{ 0.350000, 5.590000, 0.502000, 0.910000, 0.864000, 0.954000, 1.324000, -7.436000, 1.170000, 1.054000, 2.058000, 2.098000, 2.636000, -12.352000, -12.352000, -12.352000} + }; + +//float getBaseAdvance(int rpm, float key) { +// // todo: use interpolation +// int rpm_index = findIndex(ad_rpm_table, AD_RPM_COUNT, rpm); +// rpm_index = max(rpm_index, 0); +// int maf_index = findIndex(ad_maf_table, AD_LOAD_COUNT, key); +// maf_index = max(maf_index, 0); +// +// return ad_table[rpm_index][maf_index]; +//} + + + +#define newRpmSize 16 +#define newKeySize 16 + +static float newRpmBin[newRpmSize]; +static float newKeyBin[newKeySize]; + +//static float *fuel_ptrs[FUEL_LOAD_COUNT]; + +//EngineConfiguration *engineConfiguration; + +extern int needInterpolationLogging; + +void resizeMap(void) { +// float keyMin = 1.2; +// float keyMax = 4.4; +// +// float rpmMin = 800; +// float rpmMax = 7000; + +// for (int k = 0; k < FUEL_LOAD_COUNT; k++) +// fuel_ptrs[k] = engineConfiguration->fuelTable[k]; + +// for (int i = 0; i < FUEL_MAF_COUNT; i++) +// engineConfiguration->fuelKeyBins[i] = default_fuel_maf_bins[i]; +// for (int i = 0; i < FUEL_RPM_COUNT; i++) +// engineConfiguration->fuelRpmBins[i] = default_fuel_rpm_bins[i]; +// for (int k = 0; k < FUEL_MAF_COUNT; k++) { +// for (int r = 0; r < FUEL_RPM_COUNT; r++) { +// // todo: this is BAD, this needs to be fixed - TS table indexes are different from default indexes +// engineConfiguration->fuelTable[k][r] = default_fuel_table[r][k]; +// } +// } + +// assertEquals(15, interpolate3d(1.2, engineConfiguration->fuelKeyBins, FUEL_MAF_COUNT, 8000, +// engineConfiguration->fuelRpmBins, +// FUEL_RPM_COUNT, fuel_ptrs)); + + needInterpolationLogging = 0; + +// printf("static float ad_maf_table[AD_LOAD_COUNT] = {"); +// for (int i = 0; i < newKeySize; i++) { +// newKeyBin[i] = interpolate(0, keyMin, newKeySize - 1, keyMax, i); +// printf("/*%d*/ %f,\r\n", i, newKeyBin[i]); +// } +// printf("};\r\n"); +// +// printf("static float ad_rpm_table[AD_RPM_COUNT] = {"); +// for (int i = 0; i < newRpmSize; i++) { +// newRpmBin[i] = interpolate(0, rpmMin, newRpmSize - 1, rpmMax, i); +// printf("/*%d*/ %f,\r\n", i, newRpmBin[i]); +// } +// printf("};\r\n"); + + printf("static float ad_table[AD_RPM_COUNT][AD_LOAD_COUNT] = {\r\n"); + + printf("/* RPM\t\t"); + for (int r = 0; r < newRpmSize; r++) { + float rpm = newRpmBin[r]; + printf("\t%f", rpm); + } + printf("*/\r\n"); + + for (int k = 0; k < newKeySize; k++) { + float load = newKeyBin[k]; + printf("/* Load %f */{", load); + + for (int r = 0; r < newRpmSize; r++) { + float rpm = newRpmBin[r]; + + float v = ad_table[k][r]; + + printf("\t%f", v); + if (r != newRpmSize - 1) + printf(","); + + } + printf("}"); + if (k != newKeySize - 1) + printf(","); + printf("\r\n"); + } + printf("};\r\n"); + +} + +#endif diff --git a/unit_tests/test.mk b/unit_tests/test.mk index fe5910bf36..556541a9d1 100644 --- a/unit_tests/test.mk +++ b/unit_tests/test.mk @@ -1,15 +1,18 @@ TEST_SRC_C = boards.c \ - test_basic_math/test_find_index.c \ - test_basic_math/test_interpolation_3d.c \ - test_idle_controller.c \ - test_sensors.c + test_idle_controller.c TEST_SRC_CPP = test_util.cpp \ test_data_structures/test_event_registry.cpp \ + test_basic_math/test_find_index.cpp \ + test_basic_math/test_interpolation_3d.cpp \ test_data_structures/test_engine_math.cpp \ test_trigger_decoder.cpp \ test_fuel_map.cpp \ - test_signal_executor.cpp + engine_test_helper.cpp \ + test_speed_density.cpp \ + test_signal_executor.cpp \ + test_sensors.cpp \ + test_accel_enrichment.cpp diff --git a/unit_tests/test_accel_enrichment.cpp b/unit_tests/test_accel_enrichment.cpp new file mode 100644 index 0000000000..2b1f070d22 --- /dev/null +++ b/unit_tests/test_accel_enrichment.cpp @@ -0,0 +1,35 @@ +/** + * @file test_accel_enrichment.cpp + * + * Created on: apr 29, 2014 + * Author: Dmitry Sidin + * Author: Andrey Belomutskiy, (c) 2012-2013 + */ + +#include "main.h" +#include "accel_enrichment.h" +#include "test_accel_enrichment.h" +#include "engine_configuration.h" + +void testAccelEnrichment(void) { + + engine_configuration_s ec; + engine_configuration_s *engineConfiguration = &ec; + + printf("*************************************************** testAccelEnrichment\r\n"); + engineConfiguration->diffLoadEnrichmentCoef = 1; + + // todo: fix the implementation + // todo: add constructor which would reset state properly + AccelEnrichmemnt instance; + + for (int i = 0; i == 3; i++) + instance.updateDiffEnrichment(engineConfiguration, i); +// assertEqualsM("diff=1", 1, instance.getDiffEnrichment()); + + for (int i = 3; i == 0; i++) + instance.updateDiffEnrichment(engineConfiguration, i); + +// assertEqualsM("diff=1", 1, instance.getDiffEnrichment()); + +} diff --git a/unit_tests/test_accel_enrichment.h b/unit_tests/test_accel_enrichment.h new file mode 100644 index 0000000000..225a846664 --- /dev/null +++ b/unit_tests/test_accel_enrichment.h @@ -0,0 +1,17 @@ +/** + * @file test_accel_enrichment.h + * + * Created on: apr 29, 2014 + * Author: Dmitry Sidin + * Author: Andrey Belomutskiy, (c) 2012-2013 + */ + + +#ifndef TEST_ACCEL_ENRICHMENT_H_ +#define TEST_ACCEL_ENRICHMENT_H_ + +void testAccelEnrichment(void); + + + +#endif /* TEST_ACCEL_ENRICHMENT_H_ */ diff --git a/unit_tests/test_basic_math/test_find_index.c b/unit_tests/test_basic_math/test_find_index.cpp similarity index 96% rename from unit_tests/test_basic_math/test_find_index.c rename to unit_tests/test_basic_math/test_find_index.cpp index 1785336c4b..47d34d851a 100644 --- a/unit_tests/test_basic_math/test_find_index.c +++ b/unit_tests/test_basic_math/test_find_index.cpp @@ -1,120 +1,120 @@ -/* - * test_find_index.c - * - * Created on: Oct 30, 2013 - * Author: Andrey Belomutskiy, (c) 2012-2013 - */ - -#include "engine_math.h" -#include "main.h" -#include "interpolation.h" -#include -#include "engine_configuration.h" - -void testFindIndex(void) { - printf("*************************************************** testFindIndex\r\n"); - - float array[] = { 1, 2, 3, 4, 5 }; - int size = 4; - int result; - - printf("To the left\r\n"); - result = findIndex(array, size, -1.0); - assertEquals(-1, result); - - printf("To the right4\r\n"); - result = findIndex(array, size, 10.0); - assertEquals(3, result); - - printf("To the right5\r\n"); - result = findIndex(array, 5, 10.0); - assertEquals(4, result); - - printf("On the edge\r\n"); - result = findIndex(array, size, 4.0); - assertEquals(3, result); - - printf("Another1\r\n"); - result = findIndex(array, size, 3.9); - assertEquals(2, result); - - printf("Another2\r\n"); - result = findIndex(array, size, 4.1); - assertEquals(3, result); - - printf("Another3\r\n"); - result = findIndex(array, size, 2); - assertEquals(1, result); - - printf("Left edge1\r\n"); - result = findIndex(array, size, 1); - assertEquals(0, result); - - printf("Left edge2\r\n"); - result = findIndex(array, size, 1.1); - assertEquals(0, result); - - printf("Middle\r\n"); - result = findIndex(array, size, 3); - assertEquals(2, result); - - size = 5; // now test with off array size - - printf("Middle2\r\n"); - result = findIndex(array, size, 4); - assertEquals(3, result); - - printf("Middle2\r\n"); - result = findIndex(array, size, 3.1); - assertEquals(2, result); -} - -//static float getValue2(float key, float maf) { -// -//} - -void testInterpolate2d(void) { - printf("*************************************************** testInterpolate2d\r\n"); - - float bins4[] = { 1, 2, 3, 4 }; - float values4[] = { 1, 20, 30, 400 }; - int size = 4; - - int result; - - printf("Left size\r\n"); - result = interpolate2d(0, bins4, values4, size); - assertEquals(1, result); - - printf("Right size\r\n"); - result = interpolate2d(10, bins4, values4, size); - assertEquals(400, result); - - printf("Middle1\r\n"); - result = interpolate2d(3, bins4, values4, size); - assertEquals(30, result); - - printf("Middle1\r\n"); - result = interpolate2d(3.5, bins4, values4, size); - assertEquals(215, result); -} - -static engine_configuration_s engineConfiguration; - -void testSetTableValue(void) { - printf("*************************************************** testSetTableValue\r\n"); - - for (int i = 0; i < CLT_CURVE_SIZE; i++) { - engineConfiguration.cltFuelCorrBins[i] = -40 + i * 10; - engineConfiguration.cltFuelCorr[i] = 1; - } - - assertEquals(1, engineConfiguration.cltFuelCorr[0]); - - setTableValue(engineConfiguration.cltFuelCorrBins, engineConfiguration.cltFuelCorr, CLT_CURVE_SIZE, -40, 1.5); - assertEquals(1.5, engineConfiguration.cltFuelCorr[0]); - - setTableValue(engineConfiguration.cltFuelCorrBins, engineConfiguration.cltFuelCorr, CLT_CURVE_SIZE, -50, 1.4); - assertEquals(1.4, engineConfiguration.cltFuelCorr[0]); - -} +/* + * test_find_index.c + * + * Created on: Oct 30, 2013 + * Author: Andrey Belomutskiy, (c) 2012-2013 + */ + +#include "engine_math.h" +#include "main.h" +#include "interpolation.h" +#include +#include "engine_configuration.h" + +void testFindIndex(void) { + printf("*************************************************** testFindIndex\r\n"); + + float array[] = { 1, 2, 3, 4, 5 }; + int size = 4; + int result; + + printf("To the left\r\n"); + result = findIndex(array, size, -1.0); + assertEquals(-1, result); + + printf("To the right4\r\n"); + result = findIndex(array, size, 10.0); + assertEquals(3, result); + + printf("To the right5\r\n"); + result = findIndex(array, 5, 10.0); + assertEquals(4, result); + + printf("On the edge\r\n"); + result = findIndex(array, size, 4.0); + assertEquals(3, result); + + printf("Another1\r\n"); + result = findIndex(array, size, 3.9); + assertEquals(2, result); + + printf("Another2\r\n"); + result = findIndex(array, size, 4.1); + assertEquals(3, result); + + printf("Another3\r\n"); + result = findIndex(array, size, 2); + assertEquals(1, result); + + printf("Left edge1\r\n"); + result = findIndex(array, size, 1); + assertEquals(0, result); + + printf("Left edge2\r\n"); + result = findIndex(array, size, 1.1); + assertEquals(0, result); + + printf("Middle\r\n"); + result = findIndex(array, size, 3); + assertEquals(2, result); + + size = 5; // now test with off array size + + printf("Middle2\r\n"); + result = findIndex(array, size, 4); + assertEquals(3, result); + + printf("Middle2\r\n"); + result = findIndex(array, size, 3.1); + assertEquals(2, result); +} + +//static float getValue2(float key, float maf) { +// +//} + +void testInterpolate2d(void) { + printf("*************************************************** testInterpolate2d\r\n"); + + float bins4[] = { 1, 2, 3, 4 }; + float values4[] = { 1, 20, 30, 400 }; + int size = 4; + + int result; + + printf("Left size\r\n"); + result = interpolate2d(0, bins4, values4, size); + assertEquals(1, result); + + printf("Right size\r\n"); + result = interpolate2d(10, bins4, values4, size); + assertEquals(400, result); + + printf("Middle1\r\n"); + result = interpolate2d(3, bins4, values4, size); + assertEquals(30, result); + + printf("Middle1\r\n"); + result = interpolate2d(3.5, bins4, values4, size); + assertEquals(215, result); +} + +static engine_configuration_s engineConfiguration; + +void testSetTableValue(void) { + printf("*************************************************** testSetTableValue\r\n"); + + for (int i = 0; i < CLT_CURVE_SIZE; i++) { + engineConfiguration.cltFuelCorrBins[i] = -40 + i * 10; + engineConfiguration.cltFuelCorr[i] = 1; + } + + assertEquals(1, engineConfiguration.cltFuelCorr[0]); + + setTableValue(engineConfiguration.cltFuelCorrBins, engineConfiguration.cltFuelCorr, CLT_CURVE_SIZE, -40, 1.5); + assertEquals(1.5, engineConfiguration.cltFuelCorr[0]); + + setTableValue(engineConfiguration.cltFuelCorrBins, engineConfiguration.cltFuelCorr, CLT_CURVE_SIZE, -50, 1.4); + assertEquals(1.4, engineConfiguration.cltFuelCorr[0]); + +} diff --git a/unit_tests/test_basic_math/test_interpolation_3d.c b/unit_tests/test_basic_math/test_interpolation_3d.cpp similarity index 96% rename from unit_tests/test_basic_math/test_interpolation_3d.c rename to unit_tests/test_basic_math/test_interpolation_3d.cpp index 117d1c2073..6fa875b94c 100644 --- a/unit_tests/test_basic_math/test_interpolation_3d.c +++ b/unit_tests/test_basic_math/test_interpolation_3d.cpp @@ -1,70 +1,70 @@ -/* - * Created on: Oct 17, 2013 - * Author: Andrey Belomutskiy, (c) 2012-2013 - */ - -/** - * @file test_interpolation_3d.c - */ - -#include "test_interpolation_3d.h" -#include -#include - -#include "interpolation.h" -#include "main.h" - -float rpmBins[5] = { 100, 200, 300, 400, 500 }; -float mafBins[4] = { 1, 2, 3, 4 }; - -float map0[4] = { 1, 2, 3, 4 }; -float map1[4] = { 2, 3, 4, 5 }; -float map2[4] = { 3, 4, 200, 300 }; -float map3[4] = { 4, 200, 500, 600 }; -float map4[4] = { 4, 200, 500, 600 }; - -float *map[5] = { map0, map1, map2, map3, map4 }; - - -static float getValue(float rpm, float maf) { - return interpolate3d(rpm, rpmBins, 5, maf, mafBins, 4, map); -} - -void testInterpolate3d(void) { - printf("*************************************************** testInterpolate3d\r\n"); - float dwell; - printf("*** no interpolation here 1\r\n"); - dwell = getValue(100, 2); - assertEquals(2, dwell); - - printf("*** no interpolation here 2\r\n"); - dwell = getValue(200, 4); - assertEquals(5, dwell); - - printf("*** rpm interpolated value expected1\r\n"); - dwell = getValue(150, 2); - assertEquals(2.5, dwell); - - printf("*** rpm interpolated value expected2\r\n"); - dwell = getValue(250, 3); - assertEquals(102, dwell); - - printf("*** both rpm and maf interpolated value expected\r\n"); - dwell = getValue(335.3, 3.551); - assertEquals(361, dwell); - - printf("*** both rpm and maf interpolated value expected 2\r\n"); - dwell = getValue(410.01, 2.012); - assertEquals(203.6, dwell); - - printf("*** both rpm and maf interpolated value expected 3\r\n"); - dwell = getValue(1000000, 1000); - assertEquals(600, dwell); - - printf("*** both rpm and maf interpolated value expected 4\r\n"); - dwell = getValue(410.01, -1); - assertEquals(4, dwell); - - dwell = getValue(-1, -1); - assertEquals(1, dwell); -} +/* + * Created on: Oct 17, 2013 + * Author: Andrey Belomutskiy, (c) 2012-2013 + */ + +/** + * @file test_interpolation_3d.c + */ + +#include "test_interpolation_3d.h" +#include +#include + +#include "interpolation.h" +#include "main.h" + +float rpmBins[5] = { 100, 200, 300, 400, 500 }; +float mafBins[4] = { 1, 2, 3, 4 }; + +float map0[4] = { 1, 2, 3, 4 }; +float map1[4] = { 2, 3, 4, 5 }; +float map2[4] = { 3, 4, 200, 300 }; +float map3[4] = { 4, 200, 500, 600 }; +float map4[4] = { 4, 200, 500, 600 }; + +float *map[5] = { map0, map1, map2, map3, map4 }; + + +static float getValue(float rpm, float maf) { + return interpolate3d(rpm, rpmBins, 5, maf, mafBins, 4, map); +} + +void testInterpolate3d(void) { + printf("*************************************************** testInterpolate3d\r\n"); + float dwell; + printf("*** no interpolation here 1\r\n"); + dwell = getValue(100, 2); + assertEquals(2, dwell); + + printf("*** no interpolation here 2\r\n"); + dwell = getValue(200, 4); + assertEquals(5, dwell); + + printf("*** rpm interpolated value expected1\r\n"); + dwell = getValue(150, 2); + assertEquals(2.5, dwell); + + printf("*** rpm interpolated value expected2\r\n"); + dwell = getValue(250, 3); + assertEquals(102, dwell); + + printf("*** both rpm and maf interpolated value expected\r\n"); + dwell = getValue(335.3, 3.551); + assertEquals(361, dwell); + + printf("*** both rpm and maf interpolated value expected 2\r\n"); + dwell = getValue(410.01, 2.012); + assertEquals(203.6, dwell); + + printf("*** both rpm and maf interpolated value expected 3\r\n"); + dwell = getValue(1000000, 1000); + assertEquals(600, dwell); + + printf("*** both rpm and maf interpolated value expected 4\r\n"); + dwell = getValue(410.01, -1); + assertEquals(4, dwell); + + dwell = getValue(-1, -1); + assertEquals(1, dwell); +} diff --git a/unit_tests/test_data_structures/test_event_registry.cpp b/unit_tests/test_data_structures/test_event_registry.cpp index a39ecc9b5c..8735929320 100644 --- a/unit_tests/test_data_structures/test_event_registry.cpp +++ b/unit_tests/test_data_structures/test_event_registry.cpp @@ -1,5 +1,5 @@ /* - * test_event_registry.c + * test_event_registry.cpp * * Created on: Nov 27, 2013 * Author: Andrey Belomutskiy, (c) 2012-2013 @@ -15,44 +15,7 @@ static ActuatorEventList result; int pinDefaultState[IO_PIN_COUNT]; -void initOutputSignal(OutputSignal *signal, io_pin_e ioPin) { - signal->io_pin = ioPin; -} - extern int outputSignalCount; void testEventRegistry(void) { - printf("*************************************** testEventRegistry\r\n"); - - printf("resetting\r\n"); - resetEventList(&eventList); - OutputSignalList list; - - printf("registering 0\r\n"); - - registerActuatorEvent(&eventList, 0, list.add((io_pin_e)10), 0); - registerActuatorEvent(&eventList, 0, list.add((io_pin_e)20), 10); - assertEquals(2, eventList.size); - - printf("registering 1\r\n"); - registerActuatorEvent(&eventList, 1, list.add((io_pin_e)30), 0); - registerActuatorEvent(&eventList, 1, list.add((io_pin_e)40), 10); - assertEquals(4, eventList.size); - - printf("Looking for 0\r\n"); - findEvents(0, &eventList, &result); - assertEquals(2, result.size); - assertEquals(4, eventList.size); - - printf("Validating pins\r\n"); - assertEquals(10, result.events[0].actuator->io_pin); - assertEquals(20, result.events[1].actuator->io_pin); - - printf("Looking for 1\r\n"); - findEvents(1, &eventList, &result); - assertEquals(2, result.size); - assertEquals(4, eventList.size); - - assertEquals(30, result.events[0].actuator->io_pin); - assertEquals(40, result.events[1].actuator->io_pin); } diff --git a/unit_tests/test_engine_math.cpp b/unit_tests/test_engine_math.cpp index dbbca267f0..86739b195f 100644 --- a/unit_tests/test_engine_math.cpp +++ b/unit_tests/test_engine_math.cpp @@ -11,6 +11,7 @@ #include "engine_configuration.h" #include "ec2.h" #include "map.h" +#include "speed_density.h" extern engine_configuration_s *engineConfiguration; extern engine_configuration2_s *engineConfiguration2; @@ -22,6 +23,17 @@ void testEngineMath(void) { assertEqualsM("600 RPM", 50, getOneDegreeTimeMs(600) * 180); assertEqualsM("6000 RPM", 5, getOneDegreeTimeMs(6000) * 180); + + + assertEquals(312.5, getTCharge(1000, 0, 300, 350)); + assertEquals(313.5833, getTCharge(1000, 50, 300, 350)); + assertEquals(314.6667, getTCharge(1000, 100, 300, 350)); + + + assertEquals(312.5, getTCharge(4000, 0, 300, 350)); + assertEquals(320.0833, getTCharge(4000, 50, 300, 350)); + assertEquals(327.6667, getTCharge(4000, 100, 300, 350)); + } float getMap(void) { diff --git a/unit_tests/test_fuel_map.cpp b/unit_tests/test_fuel_map.cpp index 869b804f1e..21aa9c755c 100644 --- a/unit_tests/test_fuel_map.cpp +++ b/unit_tests/test_fuel_map.cpp @@ -14,7 +14,7 @@ #include "engine_math.h" #include "OutputSignalList.h" #include "ec2.h" - +#include "trigger_decoder.h" extern engine_configuration_s *engineConfiguration; extern engine_configuration2_s *engineConfiguration2; @@ -54,7 +54,7 @@ void testFuelMap(void) { // because all the correction tables are zero printf("*************************************************** getRunningFuel\r\n"); - assertEquals(1005.5, getRunningFuel(5, 5)); + assertEqualsM("value", 0.5, getRunningFuel(5, 5)); printf("*************************************************** setting IAT table\r\n"); for (int i = 0; i < IAT_CURVE_SIZE; i++) { @@ -81,7 +81,7 @@ void testFuelMap(void) { // 1005 * 2 for IAT correction printf("*************************************************** getRunningFuel\r\n"); - assertEquals(1005, getRunningFuel(5, 5)); + assertEqualsM("v1", 30150, getRunningFuel(5, 5)); engineConfiguration->crankingSettings.coolantTempMaxC = 65; // 8ms at 65C engineConfiguration->crankingSettings.fuelAtMaxTempMs = 8; @@ -98,19 +98,19 @@ void testFuelMap(void) { } static void confgiureFordAspireTriggerShape(trigger_shape_s * s) { - triggerShapeInit(s); + s->reset(FOUR_STROKE_CAM_SENSOR); - triggerAddEvent(s, 53.747, T_SECONDARY, TV_HIGH); - triggerAddEvent(s, 121.90, T_SECONDARY, TV_LOW); - triggerAddEvent(s, 232.76, T_SECONDARY, TV_HIGH); - triggerAddEvent(s, 300.54, T_SECONDARY, TV_LOW); - triggerAddEvent(s, 360, T_PRIMARY, TV_HIGH); + s->addEvent(53.747, T_SECONDARY, TV_HIGH); + s->addEvent(121.90, T_SECONDARY, TV_LOW); + s->addEvent(232.76, T_SECONDARY, TV_HIGH); + s->addEvent(300.54, T_SECONDARY, TV_LOW); + s->addEvent(360, T_PRIMARY, TV_HIGH); - triggerAddEvent(s, 409.8412, T_SECONDARY, TV_HIGH); - triggerAddEvent(s, 478.6505, T_SECONDARY, TV_LOW); - triggerAddEvent(s, 588.045, T_SECONDARY, TV_HIGH); - triggerAddEvent(s, 657.03, T_SECONDARY, TV_LOW); - triggerAddEvent(s, 720, T_PRIMARY, TV_LOW); + s->addEvent(409.8412, T_SECONDARY, TV_HIGH); + s->addEvent(478.6505, T_SECONDARY, TV_LOW); + s->addEvent(588.045, T_SECONDARY, TV_HIGH); + s->addEvent(657.03, T_SECONDARY, TV_LOW); + s->addEvent(720, T_PRIMARY, TV_LOW); } @@ -123,23 +123,26 @@ void testAngleResolver(void) { trigger_shape_s * ts = &engineConfiguration2->triggerShape; confgiureFordAspireTriggerShape(ts); - assertEqualsM("shape size", 10, ts->size); + + ts->setTriggerShapeSynchPointIndex(0); + + assertEqualsM("shape size", 10, ts->getSize()); OutputSignalList list; - resetEventList(&ae); + ae.resetEventList(); printf("*************************************************** testAngleResolver 0\r\n"); - registerActuatorEventExt(engineConfiguration, &engineConfiguration2->triggerShape, &ae, list.add(INJECTOR_1_OUTPUT), 53 - 175); + registerActuatorEventExt(engineConfiguration, &engineConfiguration2->triggerShape, ae.getNextActuatorEvent(), list.add(INJECTOR_1_OUTPUT), 53 - 175); assertEqualsM("size", 1, ae.size); assertEquals(1, list.getSize()); - assertEquals(0, ae.events[0].eventIndex); - assertEquals(53, ae.events[0].angleOffset); + assertEquals(0, ae.events[0].position.eventIndex); + assertEquals(53, ae.events[0].position.angleOffset); printf("*************************************************** testAngleResolver 2\r\n"); - resetEventList(&ae); - registerActuatorEventExt(engineConfiguration, &engineConfiguration2->triggerShape, &ae, list.add(INJECTOR_1_OUTPUT), 51 + 180 - 175); - assertEquals(2, ae.events[0].eventIndex); - assertEquals(51.9870, ae.events[0].angleOffset); + ae.resetEventList(); + registerActuatorEventExt(engineConfiguration, &engineConfiguration2->triggerShape, ae.getNextActuatorEvent(), list.add(INJECTOR_1_OUTPUT), 51 + 180 - 175); + assertEquals(2, ae.events[0].position.eventIndex); + assertEquals(51.9870, ae.events[0].position.angleOffset); } void testPinHelper(void) { diff --git a/unit_tests/test_idle_controller.c b/unit_tests/test_idle_controller.c index d8fa0e8b25..5ab5f5280b 100644 --- a/unit_tests/test_idle_controller.c +++ b/unit_tests/test_idle_controller.c @@ -7,11 +7,6 @@ #include - void idleDebug(char *msg, int value) { printf("%s\r\n", msg); } - -void isCranking(void) { - return; -} diff --git a/unit_tests/test_idle_controller.h b/unit_tests/test_idle_controller.h index 75a204acaf..64104678cb 100644 --- a/unit_tests/test_idle_controller.h +++ b/unit_tests/test_idle_controller.h @@ -2,6 +2,6 @@ #define TEST_IDLE_CONTROLLER_H void idleDebug(char *msg, int value); -void isCranking(void); +bool_t isCranking(void); #endif diff --git a/unit_tests/test_sensors.c b/unit_tests/test_sensors.cpp similarity index 77% rename from unit_tests/test_sensors.c rename to unit_tests/test_sensors.cpp index 33c8bb43a8..f6f5ee5d5e 100644 --- a/unit_tests/test_sensors.c +++ b/unit_tests/test_sensors.cpp @@ -1,52 +1,57 @@ -/** - * @file test_sensors.c - * - * @date Dec 7, 2013 - * @author Andrey Belomutskiy, (c) 2012-2013 - */ - -#include "main.h" -#include "thermistors.h" -#include "allsensors.h" - -static ThermistorConf tc; - -static void testMapDecoding(void) { - assertEqualsM("denso 0 volts", -6.64, getMAPValueHonda_Denso183(0)); - assertEquals(31.244, getMAPValueHonda_Denso183(1)); - - assertEqualsM("MPX_4250 0 volts", 8, getMAPValueMPX_4250(0)); - assertEquals(58.4, getMAPValueMPX_4250(1)); -} - -void testTpsRateOfChange(void) { - print("************************************************** testTpsRateOfChange\r\n"); - saveTpsState(0, 0); - saveTpsState(CH_FREQUENCY, 50); - assertEquals(50, getTpsRateOfChange()); - - saveTpsState(2 * CH_FREQUENCY, 50); - assertEquals(0, getTpsRateOfChange()); - - saveTpsState(3 * CH_FREQUENCY, 75); - assertEquals(25, getTpsRateOfChange()); - - -} - -void testSensors(void) { - print("************************************************** testSensors\r\n"); - testMapDecoding(); - testTpsRateOfChange(); - - setThermistorConfiguration(&tc, 32, 9500, 75, 2100, 120, 1000); - - prepareThermistorCurve(&tc); - - assertEquals(-0.003, tc.s_h_a); - assertEquals(0.001, tc.s_h_b); - assertEquals(0.0, tc.s_h_c); - - float t = convertResistanceToKelvinTemperature(2100, &tc); - assertEquals(75 + KELV, t); -} +/** + * @file test_sensors.c + * + * @date Dec 7, 2013 + * @author Andrey Belomutskiy, (c) 2012-2013 + */ + +#include "main.h" +#include "thermistors.h" +#include "allsensors.h" + +static ThermistorConf tc; + +static void testMapDecoding(void) { + + air_pressure_sensor_config_s s; + s.sensorType = MT_DENSO183; + + assertEqualsM("denso 0 volts", -6.64, decodePressure(0, &s)); + assertEquals(31.244, decodePressure(1, &s)); + + s.sensorType = MT_MPX4250; + assertEqualsM("MPX_4250 0 volts", 8, decodePressure(0, &s)); + assertEquals(58.4, decodePressure(1, &s)); +} + +void testTpsRateOfChange(void) { + print("************************************************** testTpsRateOfChange\r\n"); + saveTpsState(0, 0); + saveTpsState(CH_FREQUENCY, 50); + assertEquals(50, getTpsRateOfChange()); + + saveTpsState(2 * CH_FREQUENCY, 50); + assertEquals(0, getTpsRateOfChange()); + + saveTpsState(3 * CH_FREQUENCY, 75); + assertEquals(25, getTpsRateOfChange()); + + +} + +void testSensors(void) { + print("************************************************** testSensors\r\n"); + testMapDecoding(); + testTpsRateOfChange(); + + setThermistorConfiguration(&tc, 32, 9500, 75, 2100, 120, 1000); + + prepareThermistorCurve(&tc); + + assertEquals(-0.003, tc.s_h_a); + assertEquals(0.001, tc.s_h_b); + assertEquals(0.0, tc.s_h_c); + + float t = convertResistanceToKelvinTemperature(2100, &tc); + assertEquals(75 + KELV, t); +} diff --git a/unit_tests/test_signal_executor.cpp b/unit_tests/test_signal_executor.cpp index 9d862e5fa6..56bfba681b 100644 --- a/unit_tests/test_signal_executor.cpp +++ b/unit_tests/test_signal_executor.cpp @@ -1,5 +1,5 @@ /** - * @file test_signal_executor.c + * @file test_signal_executor.cpp * * @date Nov 28, 2013 * @author Andrey Belomutskiy, (c) 2012-2013 @@ -14,22 +14,19 @@ #include "utlist.h" #include "event_queue.h" -extern OutputSignal *st_output_list; - static io_pin_e testLastToggledPin; static int testToggleCounter; -int getRevolutionCounter(void) { - return 0; -} - void setOutputPinValue(io_pin_e pin, int value) { // this is a test implementation of the method - we use it to see what's going on testLastToggledPin = pin; testToggleCounter++; } +EventQueue schedulingQueue; + void scheduleTask(scheduling_s *scheduling, int delayUs, schfunc_t callback, void *param) { + schedulingQueue.insertTask(scheduling, getTimeNowUs(), delayUs, callback, param); } void initSignalExecutorImpl(void) { @@ -43,13 +40,76 @@ static void callback(void *a) { callbackCounter++; } -void testSignalExecutor() { +static int complexTestNow; + +typedef struct { + scheduling_s s; + int period; +} TestPwm; + +static void complexCallback(TestPwm *testPwm) { + callbackCounter++; + + eq.insertTask(&testPwm->s, complexTestNow, testPwm->period, (schfunc_t)complexCallback, testPwm); +} + +static void testSignalExecutor2(void) { + print("*************************************** testSignalExecutor2\r\n"); + eq.clear(); + TestPwm p1; + TestPwm p2; + p1.period = 2; + p2.period = 3; + + complexTestNow = 0; + callbackCounter = 0; + eq.insertTask(&p1.s, 0, 0, (schfunc_t)complexCallback, &p1); + eq.insertTask(&p2.s, 0, 0, (schfunc_t)complexCallback, &p2); + eq.executeAll(complexTestNow); + assertEqualsM("callbackCounter #1", 2, callbackCounter); + assertEquals(2, eq.size()); + + eq.executeAll(complexTestNow = 2); + assertEqualsM("callbackCounter #2", 3, callbackCounter); + assertEquals(2, eq.size()); + + eq.executeAll(complexTestNow = 3); + assertEqualsM("callbackCounter #3", 4, callbackCounter); + assertEquals(2, eq.size()); + +} + +void testSignalExecutor(void) { print("*************************************** testSignalExecutor\r\n"); assertEquals(EMPTY_QUEUE, eq.getNextEventTime(0)); scheduling_s s1; scheduling_s s2; + scheduling_s s3; + eq.insertTask(&s1, 0, 10, callback, NULL); + eq.insertTask(&s2, 0, 11, callback, NULL); + eq.insertTask(&s3, 0, 12, callback, NULL); + callbackCounter = 0; + eq.executeAll(10); + assertEquals(1, callbackCounter); + callbackCounter = 0; + eq.executeAll(11); + assertEquals(1, callbackCounter); + eq.clear(); + + eq.insertTask(&s1, 0, 12, callback, NULL); + eq.insertTask(&s2, 0, 11, callback, NULL); + eq.insertTask(&s3, 0, 10, callback, NULL); + callbackCounter = 0; + eq.executeAll(10); + assertEquals(1, callbackCounter); + callbackCounter = 0; + eq.executeAll(11); + assertEquals(1, callbackCounter); + eq.clear(); + + callbackCounter = 0; eq.insertTask(&s1, 0, 10, callback, NULL); assertEquals(10, eq.getNextEventTime(0)); @@ -77,4 +137,5 @@ void testSignalExecutor() { eq.executeAll(11); assertEquals(2, callbackCounter); + testSignalExecutor2(); } diff --git a/unit_tests/test_speed_density.cpp b/unit_tests/test_speed_density.cpp new file mode 100644 index 0000000000..6b05cdcded --- /dev/null +++ b/unit_tests/test_speed_density.cpp @@ -0,0 +1,31 @@ +/** + * @file test_speed_density.cpp + * + * @date Jun 26, 2014 + * @author Andrey Belomutskiy, (c) 2012-2014 + */ + +#include "main.h" +#include "engine_test_helper.h" +#include "speed_density.h" +#include "test_speed_density.h" + +void testSpeedDensity(void) { + printf("*************************************************** testSpeedDensity\r\n"); + EngineTestHelper eth(FORD_INLINE_6_1995); + + eth.ec->triggerConfig.totalToothCount = 8; + eth.initTriggerShapeAndRpmCalculator(); + + eth.fireTriggerEvents(); + assertEqualsM("RPM", 1500, eth.rpmState.rpm()); + + // 427 cubic inches, that's a LOT of engine + eth.ec->displacement = 6.99728; + eth.ec->cylindersCount = 8; + + eth.ec->injectorFlow = gramm_second_to_cc_minute(5.303); + + // 0.01414 sec or 14.14 ms + assertEquals(0.01414, sdMath(eth.ec, 0.92, 98, 12.5, 293.16)); +} diff --git a/unit_tests/test_speed_density.h b/unit_tests/test_speed_density.h new file mode 100644 index 0000000000..e4dd8cd546 --- /dev/null +++ b/unit_tests/test_speed_density.h @@ -0,0 +1,13 @@ +/** + * @file test_speed_density.h + * + * @date Jun 26, 2014 + * @author Andrey Belomutskiy, (c) 2012-2014 + */ + +#ifndef TEST_SPEED_DENSITY_H_ +#define TEST_SPEED_DENSITY_H_ + +void testSpeedDensity(void); + +#endif /* TEST_SPEED_DENSITY_H_ */ diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index fde3d9c257..b21a1988a2 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -14,6 +14,20 @@ #include "dodge_neon.h" #include "ford_1995_inline_6.h" #include "mazda_323.h" +#include "rpm_calculator.h" +#include "event_queue.h" +#include "algo.h" + +#include "trigger_central.h" +#include "main_trigger_callback.h" +#include "engine.h" +#include "advance_map.h" +#include "engine_test_helper.h" +#include "speed_density.h" + +Engine engine; + +extern int timeNow; extern "C" { void sendOutConfirmation(char *value, int i); @@ -28,7 +42,8 @@ int getTheAngle(engine_type_e engineType) { engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(engineType, ec, &ec2, &persistentConfig.boardConfiguration); + initDataStructures(ec); + resetConfigurationExt(NULL, engineType, ec, &ec2, &persistentConfig.engineConfiguration.bc); trigger_shape_s * shape = &ec2.triggerShape; return findTriggerZeroEventIndex(shape, &ec->triggerConfig); @@ -44,18 +59,18 @@ static void testDodgeNeonDecoder(void) { engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(DODGE_NEON_1995, ec, &ec2, &persistentConfig.boardConfiguration); + resetConfigurationExt(NULL, DODGE_NEON_1995, ec, &ec2, &persistentConfig.engineConfiguration.bc); + assertEquals(8, ec2.triggerShape.getTriggerShapeSynchPointIndex()); trigger_shape_s * shape = &ec2.triggerShape; - trigger_state_s state; - clearTriggerState(&state); -// -// assertFalseM("1 shaft_is_synchronized", state.shaft_is_synchronized); -// -// int r = 0; + TriggerState state; + + assertFalseM("1 shaft_is_synchronized", state.shaft_is_synchronized); + + int r = 0; // processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, r + 60); // assertFalseM("2 shaft_is_synchronized", state.shaft_is_synchronized); // still no synchronization -// + // processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r + 210); // assertFalseM("3 shaft_is_synchronized", state.shaft_is_synchronized); // still no synchronization // @@ -87,6 +102,11 @@ static void testDodgeNeonDecoder(void) { // processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r + 630); } +static void assertTriggerPosition(event_trigger_position_s *position, int eventIndex, float angleOffset) { + assertEqualsM("eventIndex", eventIndex, position->eventIndex); + assertEqualsM("angleOffset", angleOffset, position->angleOffset); +} + static void test1995FordInline6TriggerDecoder(void) { printf("*************************************************** test1995FordInline6TriggerDecoder\r\n"); @@ -98,45 +118,57 @@ static void test1995FordInline6TriggerDecoder(void) { engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(FORD_INLINE_6_1995, ec, &ec2, &persistentConfig.boardConfiguration); - - ActuatorEventList *ecl = &ec2.engineEventConfiguration.ignitionEvents; - assertEqualsM("ignition events size", 6, ecl->size); - assertEqualsM("event index", 0, ecl->events[0].eventIndex); - assertEquals(0, ecl->events[0].angleOffset); - - assertEqualsM("event index", 10, ecl->events[5].eventIndex); - assertEquals(0, ecl->events[5].angleOffset); - - trigger_state_s state; - clearTriggerState(&state); + resetConfigurationExt(NULL, FORD_INLINE_6_1995, ec, &ec2, &persistentConfig.engineConfiguration.bc); + assertEqualsM("triggerShapeSynchPointIndex", 0, ec2.triggerShape.getTriggerShapeSynchPointIndex()); trigger_shape_s * shape = &ec2.triggerShape; + event_trigger_position_s position; + assertEqualsM("globalTriggerAngleOffset", 0, ec->globalTriggerAngleOffset); + findTriggerPosition(ec, shape, &position, 0); + assertTriggerPosition(&position, 0, 0); + + findTriggerPosition(ec, shape, &position, 200); + assertTriggerPosition(&position, 3, 20); + + findTriggerPosition(ec, shape, &position, 360); + assertTriggerPosition(&position, 6, 0); + + + IgnitionEventList *ecl = &ec2.engineEventConfiguration.ignitionEvents[0]; + assertEqualsM("ignition events size", 6, ecl->size); + assertEqualsM("event index", 0, ecl->events[0].dwellPosition.eventIndex); + assertEquals(0, ecl->events[0].dwellPosition.angleOffset); + + assertEqualsM("event index", 10, ecl->events[5].dwellPosition.eventIndex); + assertEquals(0, ecl->events[5].dwellPosition.angleOffset); + + TriggerState state; + assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); int r = 10; - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r); assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); // still no synchronization - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, ++r); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, ++r); assertTrue(state.shaft_is_synchronized); // first signal rise synchronize - assertEquals(0, state.current_index); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r++); - assertEquals(1, state.current_index); + assertEquals(0, state.getCurrentIndex()); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r++); + assertEquals(1, state.getCurrentIndex()); for (int i = 2; i < 10;) { - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, r++); - assertEqualsM("even", i++, state.current_index); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r++); - assertEqualsM("odd", i++, state.current_index); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, r++); + assertEqualsM("even", i++, state.getCurrentIndex()); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r++); + assertEqualsM("odd", i++, state.getCurrentIndex()); } - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, r++); - assertEquals(10, state.current_index); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, r++); + assertEquals(10, state.getCurrentIndex()); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r++); - assertEquals(11, state.current_index); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, r++); + assertEquals(11, state.getCurrentIndex()); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, r++); - assertEquals(0, state.current_index); // new revolution + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, r++); + assertEquals(0, state.getCurrentIndex()); // new revolution assertEqualsM("running dwell", 0.5, getSparkDwellMsT(ec, 2000)); } @@ -149,7 +181,8 @@ void testFordAspire(void) { persistent_config_s persistentConfig; engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(FORD_ASPIRE_1996, ec, &ec2, &persistentConfig.boardConfiguration); + resetConfigurationExt(NULL, FORD_ASPIRE_1996, ec, &ec2, &persistentConfig.engineConfiguration.bc); + assertEquals(4, ec2.triggerShape.getTriggerShapeSynchPointIndex()); assertEquals(800, ec->fuelRpmBins[0]); assertEquals(7000, ec->fuelRpmBins[15]); @@ -169,8 +202,8 @@ void testMazda323(void) { persistent_config_s persistentConfig; engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(MAZDA_323, ec, &ec2, &persistentConfig.boardConfiguration); - + resetConfigurationExt(NULL, MAZDA_323, ec, &ec2, &persistentConfig.engineConfiguration.bc); + assertEquals(0, ec2.triggerShape.getTriggerShapeSynchPointIndex()); } void testMazdaMianaNbDecoder(void) { @@ -179,56 +212,126 @@ void testMazdaMianaNbDecoder(void) { persistent_config_s persistentConfig; engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(MAZDA_MIATA_NB, ec, &ec2, &persistentConfig.boardConfiguration); + resetConfigurationExt(NULL, MAZDA_MIATA_NB, ec, &ec2, &persistentConfig.engineConfiguration.bc); + assertEquals(11, ec2.triggerShape.getTriggerShapeSynchPointIndex()); - trigger_state_s state; - clearTriggerState(&state); + TriggerState state; trigger_shape_s * shape = &ec2.triggerShape; int a = 0; - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 20); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 20); assertFalseM("0a shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 340); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 340); assertFalseM("0b shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 360); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 360); assertFalseM("0c shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 380); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 380); assertFalseM("0d shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 400); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 400); assertTrueM("0e shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 720); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 720); assertTrueM("0f shaft_is_synchronized", state.shaft_is_synchronized); a = 720; - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 20); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 20); assertTrueM("1a shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 340); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 340); assertTrueM("1b shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 360); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 360); assertTrueM("1c shaft_is_synchronized", state.shaft_is_synchronized); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 380); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 380); assertTrueM("1d shaft_is_synchronized", state.shaft_is_synchronized); - assertEquals(5, state.current_index); + assertEquals(5, state.getCurrentIndex()); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 400); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, a + 400); assertTrueM("1e shaft_is_synchronized", state.shaft_is_synchronized); - assertEquals(0, state.current_index); + assertEquals(0, state.getCurrentIndex()); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 720); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, a + 720); assertTrueM("1f shaft_is_synchronized", state.shaft_is_synchronized); + event_trigger_position_s position; + assertEqualsM("globalTriggerAngleOffset", 276, ec->globalTriggerAngleOffset); + findTriggerPosition(ec, shape, &position, 0); + assertTriggerPosition(&position, 7, 46); + + findTriggerPosition(ec, shape, &position, 180); + assertTriggerPosition(&position, 13, 46); + + findTriggerPosition(ec, shape, &position, 360); + assertTriggerPosition(&position, 17, 46); + + findTriggerPosition(ec, shape, &position, 444); + assertTriggerPosition(&position, 0, 0); + + findTriggerPosition(ec, shape, &position, 444.1); + assertTriggerPosition(&position, 0, 0.1); + + findTriggerPosition(ec, shape, &position, 445); + assertTriggerPosition(&position, 0, 1); + + findTriggerPosition(ec, shape, &position, 494); + assertTriggerPosition(&position, 3, 0); + + findTriggerPosition(ec, shape, &position, 719); + assertTriggerPosition(&position, 7, 45); + + ec->globalTriggerAngleOffset = 0; + findTriggerPosition(ec, shape, &position, 0); + assertTriggerPosition(&position, 0, 0); + + ec->globalTriggerAngleOffset = 10; + findTriggerPosition(ec, shape, &position, 0); + assertTriggerPosition(&position, 0, 10); + + findTriggerPosition(ec, shape, &position, -10); + assertTriggerPosition(&position, 0, 0); +} + +static void testCitroen(void) { + printf("*************************************************** testCitroen\r\n"); + + persistent_config_s persistentConfig; + engine_configuration_s *ec = &persistentConfig.engineConfiguration; + engine_configuration2_s ec2; + assertEquals(0, ec2.triggerShape.getTriggerShapeSynchPointIndex()); + + resetConfigurationExt(NULL, CITROEN_TU3JP, ec, &ec2, &persistentConfig.engineConfiguration.bc); + + assertEquals(0, ec2.triggerShape.getTriggerShapeSynchPointIndex()); +} + +static void testRoverV8(void) { + printf("*************************************************** testRoverV8\r\n"); + + persistent_config_s persistentConfig; + engine_configuration_s *ec = &persistentConfig.engineConfiguration; + engine_configuration2_s ec2; + resetConfigurationExt(NULL, ROVER_V8, ec, &ec2, &persistentConfig.engineConfiguration.bc); + + assertEquals(0, ec2.triggerShape.getTriggerShapeSynchPointIndex()); +} + +static void testMiniCooper(void) { + printf("*************************************************** testMiniCooper\r\n"); + + persistent_config_s persistentConfig; + engine_configuration_s *ec = &persistentConfig.engineConfiguration; + engine_configuration2_s ec2; + resetConfigurationExt(NULL, MINI_COOPER_R50, ec, &ec2, &persistentConfig.engineConfiguration.bc); + } -void testFordEscortGt(void) { +static void testFordEscortGt(void) { printf("*************************************************** testFordEscortGt\r\n"); persistent_config_s persistentConfig; engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(FORD_ESCORT_GT, ec, &ec2, &persistentConfig.boardConfiguration); + resetConfigurationExt(NULL, FORD_ESCORT_GT, ec, &ec2, &persistentConfig.engineConfiguration.bc); } void testGY6_139QMB(void) { @@ -237,25 +340,127 @@ void testGY6_139QMB(void) { persistent_config_s persistentConfig; engine_configuration_s *ec = &persistentConfig.engineConfiguration; engine_configuration2_s ec2; - resetConfigurationExt(GY6_139QMB, ec, &ec2, &persistentConfig.boardConfiguration); + resetConfigurationExt(NULL, GY6_139QMB, ec, &ec2, &persistentConfig.engineConfiguration.bc); - trigger_state_s state; - clearTriggerState(&state); + TriggerState state; assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); trigger_shape_s * shape = &ec2.triggerShape; assertFalseM("shaft_is_synchronized", state.shaft_is_synchronized); - assertEquals(0, state.current_index); + assertEquals(0, state.getCurrentIndex()); int now = 0; - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, now++); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_UP, now++); assertTrueM("shaft_is_synchronized", state.shaft_is_synchronized); - assertEquals(0, state.current_index); + assertEquals(0, state.getCurrentIndex()); - processTriggerEvent(&state, shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, now++); + state.decodeTriggerEvent(shape, &ec->triggerConfig, SHAFT_PRIMARY_DOWN, now++); assertTrueM("shaft_is_synchronized", state.shaft_is_synchronized); - assertEquals(1, state.current_index); + assertEquals(1, state.getCurrentIndex()); +} + +extern EventQueue schedulingQueue; + +static void testRpmCalculator(void) { + printf("*************************************************** testRpmCalculator\r\n"); + + EngineTestHelper eth(FORD_INLINE_6_1995); + + engine_configuration_s *ec = ð.persistentConfig.engineConfiguration; + + engine_configuration2_s *ec2 = ð.ec2; + + ec->triggerConfig.totalToothCount = 8; + eth.initTriggerShapeAndRpmCalculator(); + + configuration_s configuration = { ec, ec2 }; + timeNow = 0; + assertEquals(0, eth.rpmState.rpm()); + + eth.fireTriggerEvents(); + assertEqualsM("RPM", 1500, eth.rpmState.rpm()); + + assertEqualsM("index #1", 15, eth.triggerCentral.triggerState.getCurrentIndex()); + + + static MainTriggerCallback triggerCallbackInstance; + triggerCallbackInstance.init(ec, ec2); + eth.triggerCentral.addEventListener((ShaftPositionListener)&onTriggerEvent, "main loop", &triggerCallbackInstance); + + engine.rpmCalculator = ð.rpmState; + prepareTimingMap(); + + timeNow += 5000; // 5ms + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + assertEqualsM("index #2", 0, eth.triggerCentral.triggerState.getCurrentIndex()); + assertEqualsM("queue size", 2, schedulingQueue.size()); + assertEqualsM("ev 1", 695000, schedulingQueue.getForUnitText(0)->momentUs); + assertEqualsM("ev 2", 245000, schedulingQueue.getForUnitText(1)->momentUs); + schedulingQueue.clear(); + + timeNow += 5000; + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_DOWN, timeNow); + timeNow += 5000; // 5ms + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + timeNow += 5000; + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_DOWN, timeNow); + assertEqualsM("index #3", 3, eth.triggerCentral.triggerState.getCurrentIndex()); + assertEqualsM("queue size 3", 4, schedulingQueue.size()); + assertEquals(258333, schedulingQueue.getForUnitText(0)->momentUs); + assertEquals(257833, schedulingQueue.getForUnitText(1)->momentUs); + assertEqualsM("ev 5", 708333, schedulingQueue.getForUnitText(2)->momentUs); + assertEqualsM("3/3", 258333, schedulingQueue.getForUnitText(3)->momentUs); + schedulingQueue.clear(); + + timeNow += 5000; + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_DOWN, timeNow); + timeNow += 5000; // 5ms + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + timeNow += 5000; // 5ms + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + assertEqualsM("index #4", 6, eth.triggerCentral.triggerState.getCurrentIndex()); + assertEqualsM("queue size 4", 4, schedulingQueue.size()); + assertEqualsM("4/0", 271666, schedulingQueue.getForUnitText(0)->momentUs); + schedulingQueue.clear(); + + timeNow += 5000; + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_DOWN, timeNow); + assertEqualsM("queue size 5", 1, schedulingQueue.size()); + assertEqualsM("5/1", 284500, schedulingQueue.getForUnitText(0)->momentUs); + schedulingQueue.clear(); + + timeNow += 5000; // 5ms + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + assertEqualsM("queue size 6", 3, schedulingQueue.size()); + assertEqualsM("6/0", 285000, schedulingQueue.getForUnitText(0)->momentUs); + assertEqualsM("6/1", 735000, schedulingQueue.getForUnitText(1)->momentUs); + assertEqualsM("6/0", 285000, schedulingQueue.getForUnitText(2)->momentUs); + schedulingQueue.clear(); + + timeNow += 5000; + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_DOWN, timeNow); + assertEqualsM("queue size 7", 0, schedulingQueue.size()); + schedulingQueue.clear(); + + timeNow += 5000; // 5ms + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + assertEqualsM("queue size 8", 4, schedulingQueue.size()); + assertEqualsM("8/0", 298333, schedulingQueue.getForUnitText(0)->momentUs); + assertEqualsM("8/1", 297833, schedulingQueue.getForUnitText(1)->momentUs); + assertEqualsM("8/2", 748333, schedulingQueue.getForUnitText(2)->momentUs); + assertEqualsM("8/3", 298333, schedulingQueue.getForUnitText(3)->momentUs); + schedulingQueue.clear(); + + timeNow += 5000; + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_DOWN, timeNow); + assertEqualsM("queue size 9", 0, schedulingQueue.size()); + schedulingQueue.clear(); + + timeNow += 5000; // 5ms + eth.triggerCentral.handleShaftSignal(&configuration, SHAFT_PRIMARY_UP, timeNow); + assertEqualsM("queue size 10", 0, schedulingQueue.size()); + schedulingQueue.clear(); } void testTriggerDecoder(void) { @@ -263,8 +468,8 @@ void testTriggerDecoder(void) { engine_configuration2_s ec2; - initializeSkippedToothTriggerShapeExt(&ec2, 2, 0); - assertEqualsM("shape size", ec2.triggerShape.size, 4); + initializeSkippedToothTriggerShapeExt(&ec2, 2, 0, FOUR_STROKE_CAM_SENSOR); + assertEqualsM("shape size", ec2.triggerShape.getSize(), 4); assertEquals(ec2.triggerShape.wave.switchTimes[0], 0.25); assertEquals(ec2.triggerShape.wave.switchTimes[1], 0.5); assertEquals(ec2.triggerShape.wave.switchTimes[2], 0.75); @@ -276,7 +481,11 @@ void testTriggerDecoder(void) { testMazdaMianaNbDecoder(); testGY6_139QMB(); testFordEscortGt(); + testMiniCooper(); + testRoverV8(); + testCitroen(); -// testMazda323(); + testMazda323(); + + testRpmCalculator(); } - diff --git a/unit_tests/test_util.cpp b/unit_tests/test_util.cpp index b62b9ec7dd..e8a4c4424e 100644 --- a/unit_tests/test_util.cpp +++ b/unit_tests/test_util.cpp @@ -13,27 +13,40 @@ #include "histogram.h" #include "malfunction_central.h" -#include "eficonsole_logic.h" +#include "cli_registry.h" #include "nmea.h" #include "efilib2.h" +#include "crc.h" +#include "fl_stack.h" + +void testCrc(void) { + assertEquals(4, efiRound(4.4, 1)); + assertEquals(1.2, efiRound(1.2345, 0.1)); + + print("*************************************** testCrc\r\n"); + + const char * A = "A"; + + assertEqualsM("crc8", 168, calc_crc((const crc_t *)A, 1)); + int c = crc32(A, 1); + printf("crc32(A)=%x\r\n", c); + assertEqualsM("crc32", 0xd3d99e8b, crc32(A, 1)); +} static cyclic_buffer sb; - void testOverflow64Counter(void) { print("*************************************** testOverflow64Counter\r\n"); Overflow64Counter o; - assertEquals(0, o.get()); - o.offer(10); - assertEquals(10, o.get()); + assertEquals(0, o.get(0, true)); + assertEquals(10, o.get(10, true)); - o.offer(20); - assertEquals(20, o.get()); + assertEquals(20, o.get(20, true)); - o.offer(0); // overflow - assertEquals(4294967296, o.get()); + // overflow + assertEquals(4294967296, o.get(0, true)); } void testCyclicBuffer(void) { @@ -292,3 +305,11 @@ void testConsoleLogic(void) { //addConsoleActionSSS("GPS", testGpsParser); } +void testFLStack(void) { + print("******************************************* testFLStack\r\n"); + + FLStack stack; + assertEquals(0, stack.size()); + + +} diff --git a/unit_tests/test_util.h b/unit_tests/test_util.h index 0bd3c24cb6..d47f0e9caf 100644 --- a/unit_tests/test_util.h +++ b/unit_tests/test_util.h @@ -13,15 +13,14 @@ extern "C" { #endif /* __cplusplus */ - - +void testCrc(void); void testCyclicBuffer(void); void testOverflow64Counter(void); void testHistogram(void); void testMalfunctionCentral(void); void testConsoleLogic(void); void testGpsParser(void); - +void testFLStack(void); #ifdef __cplusplus }