diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index e243d686a2..b68355dee7 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -541,9 +541,8 @@ struct LuaPid final { float get(float target, float input) { #if EFI_UNIT_TEST - extern int timeNowUs; // this is how we avoid zero dt - timeNowUs += 1000; + advanceTimeUs(1000); #endif float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt()); diff --git a/firmware/controllers/system/timer/event_queue.cpp b/firmware/controllers/system/timer/event_queue.cpp index 13596cde0a..b2664b7e49 100644 --- a/firmware/controllers/system/timer/event_queue.cpp +++ b/firmware/controllers/system/timer/event_queue.cpp @@ -17,7 +17,6 @@ #include "efitime.h" #if EFI_UNIT_TEST -extern int timeNowUs; extern bool verboseMode; #endif /* EFI_UNIT_TEST */ diff --git a/unit_tests/tests/actuators/test_etb.cpp b/unit_tests/tests/actuators/test_etb.cpp index 91a90aacfe..dcd97bfb36 100644 --- a/unit_tests/tests/actuators/test_etb.cpp +++ b/unit_tests/tests/actuators/test_etb.cpp @@ -537,20 +537,19 @@ TEST(etb, setpointLuaAdder) { etb.setLuaAdjustment(-1000); EXPECT_EQ(1, etb.getSetpoint().value_or(-1)); - extern int timeNowUs; int startTime = 1e6; - timeNowUs = startTime; + setTimeNowUs(startTime); // Adjustment works immediately after setting etb.setLuaAdjustment(10); EXPECT_EQ(60, etb.getSetpoint().value_or(-1)); // Adjustment works 0.19 second after setting - timeNowUs = startTime + 0.19 * 1e6; + setTimeNowUs(startTime + 0.19 * 1e6); EXPECT_EQ(60, etb.getSetpoint().value_or(-1)); // Adjustment resets to 0 after 0.21 second - timeNowUs = startTime + 0.21 * 1e6; + setTimeNowUs(startTime + 0.21 * 1e6); EXPECT_EQ(50, etb.getSetpoint().value_or(-1)); } @@ -763,8 +762,6 @@ TEST(etb, closedLoopPid) { EXPECT_FLOAT_EQ(etb.getClosedLoop(50, 30).value_or(-1), 75); } -extern int timeNowUs; - TEST(etb, jamDetection) { EngineTestHelper eth(engine_type_e::TEST_ENGINE); @@ -789,7 +786,7 @@ TEST(etb, jamDetection) { EtbController etb; etb.init(DC_Throttle1, nullptr, &pid, nullptr, true); - timeNowUs = 0; + setTimeNowUs(0); // Reset timer while under integrator limit EXPECT_EQ(etb.getPidState().iTerm, 0); @@ -805,13 +802,13 @@ TEST(etb, jamDetection) { EXPECT_NEAR(etb.getPidState().iTerm, 10.0f, 1e-3); // Just under time limit, no jam yet - timeNowUs = 0.9e6; + setTimeNowUs(0.9e6); etb.checkOutput(0); EXPECT_NEAR(etb.jamTimer, 0.9f, 1e-3); EXPECT_FALSE(etb.jamDetected); // Above the time limit, jam detected! - timeNowUs = 1.1e6; + setTimeNowUs(1.1e6); etb.checkOutput(0); EXPECT_TRUE(etb.jamDetected); } diff --git a/unit_tests/tests/actuators/test_fuel_pump.cpp b/unit_tests/tests/actuators/test_fuel_pump.cpp index 16a148218b..616eea41c1 100644 --- a/unit_tests/tests/actuators/test_fuel_pump.cpp +++ b/unit_tests/tests/actuators/test_fuel_pump.cpp @@ -9,8 +9,6 @@ #include "pch.h" -extern int timeNowUs; - TEST(Actuators, FuelPump) { EngineTestHelper eth(engine_type_e::TEST_ENGINE); @@ -22,7 +20,7 @@ TEST(Actuators, FuelPump) { enginePins.fuelPumpRelay.init(); // ECU just started, haven't seen trigger yet - timeNowUs = 0.5e6; + setTimeNowUs(0.5e6); dut.onIgnitionStateChanged(true); dut.onSlowCallback(); // Pump should be on! @@ -30,22 +28,22 @@ TEST(Actuators, FuelPump) { // Long time since ecu start, haven't seen trigger yet dut.onIgnitionStateChanged(true); - timeNowUs += 10e6; + advanceTimeUs(10e6); dut.onSlowCallback(); // Pump should be off! EXPECT_FALSE(efiReadPin(Gpio::A0)); // Long time since ecu start, just saw a trigger! dut.onIgnitionStateChanged(true); - timeNowUs += 10e6; - engine->triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, timeNowUs * US_TO_NT_MULTIPLIER); + advanceTimeUs(10e6); + engine->triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, getTimeNowNt()); dut.onSlowCallback(); // Pump should be on! EXPECT_TRUE(efiReadPin(Gpio::A0)); // ECU just started, and we just saw a trigger! dut.onIgnitionStateChanged(true); - engine->triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, timeNowUs * US_TO_NT_MULTIPLIER); + engine->triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, getTimeNowNt()); dut.onSlowCallback(); // Pump should be on! EXPECT_TRUE(efiReadPin(Gpio::A0)); diff --git a/unit_tests/tests/test_idle_controller.cpp b/unit_tests/tests/test_idle_controller.cpp index e210f25b37..cb76bdfc11 100644 --- a/unit_tests/tests/test_idle_controller.cpp +++ b/unit_tests/tests/test_idle_controller.cpp @@ -333,8 +333,6 @@ TEST(idle_v2, openLoopCoastingTable) { EXPECT_FLOAT_EQ(75, dut.getOpenLoop(ICP::Coasting, 1500, 0, 0, 2)); } -extern int timeNowUs; - TEST(idle_v2, closedLoopBasic) { EngineTestHelper eth(engine_type_e::TEST_ENGINE); IdleController dut; @@ -353,7 +351,7 @@ TEST(idle_v2, closedLoopBasic) { // burn one update then advance time 5 seconds to avoid difficulty from wasResetPid dut.getClosedLoop(ICP::Idling, 0, 900, 900); - timeNowUs += 5'000'000; + advanceTimeUs(5'000'000); // Test above target, should return negative EXPECT_FLOAT_EQ(-25, dut.getClosedLoop(ICP::Idling, 0, /*rpm*/ 950, /*tgt*/ 900)); @@ -381,7 +379,7 @@ TEST(idle_v2, closedLoopDeadzone) { // burn one then advance time 5 seconds to avoid difficulty from wasResetPid dut.getClosedLoop(ICP::Idling, 0, 900, 900); - timeNowUs += 5'000'000; + advanceTimeUs(5'000'000); // Test above target, should return negative EXPECT_FLOAT_EQ(-25, dut.getClosedLoop(ICP::Idling, 0, /*rpm*/ 950, /*tgt*/ 900)); diff --git a/unit_tests/tests/test_lambda_monitor.cpp b/unit_tests/tests/test_lambda_monitor.cpp index b3a2ff1a09..90da0087a4 100644 --- a/unit_tests/tests/test_lambda_monitor.cpp +++ b/unit_tests/tests/test_lambda_monitor.cpp @@ -22,9 +22,8 @@ struct MockLambdaMonitor : public LambdaMonitorBase { TEST(LambdaMonitor, Response) { MockLambdaMonitor mlm; - extern int timeNowUs; int startTime = 1e6; - timeNowUs = startTime; + setTimeNowUs(startTime); mlm.isGood = true; mlm.isRestore = false; @@ -39,13 +38,13 @@ TEST(LambdaMonitor, Response) { EXPECT_FALSE(mlm.isCut()); // 0.9 second later, still not cut - timeNowUs = startTime + 0.9e6; + setTimeNowUs(startTime + 0.9e6); mlm.update(2000, 50); EXPECT_FALSE(mlm.lambdaCurrentlyGood); EXPECT_FALSE(mlm.isCut()); // 1.1 second later, cut! - timeNowUs = startTime + 1.1e6; + setTimeNowUs(startTime + 1.1e6); mlm.update(2000, 50); EXPECT_FALSE(mlm.lambdaCurrentlyGood); EXPECT_TRUE(mlm.isCut()); diff --git a/unit_tests/tests/test_limp.cpp b/unit_tests/tests/test_limp.cpp index 3dc5b0eb37..b339b5b8f8 100644 --- a/unit_tests/tests/test_limp.cpp +++ b/unit_tests/tests/test_limp.cpp @@ -209,8 +209,6 @@ TEST(limp, boostCut) { EXPECT_TRUE(dut.allowInjection()); } -extern int timeNowUs; - TEST(limp, oilPressureFailureCase) { EngineTestHelper eth(engine_type_e::TEST_ENGINE); engineConfiguration->minOilPressureAfterStart = 200; @@ -228,12 +226,12 @@ TEST(limp, oilPressureFailureCase) { EXPECT_TRUE(dut.allowInjection()); // 4.5 seconds later, should still be allowed (even though pressure is low) - timeNowUs += 4.5e6; + advanceTimeUs(4.5e6); dut.updateState(1000, getTimeNowNt()); EXPECT_TRUE(dut.allowInjection()); // 1 second later (5.5 since start), injection should cut - timeNowUs += 1.0e6; + advanceTimeUs(1.0e6); dut.updateState(1000, getTimeNowNt()); ASSERT_FALSE(dut.allowInjection()); @@ -261,7 +259,7 @@ TEST(limp, oilPressureSuccessCase) { EXPECT_TRUE(dut.allowInjection()); // 4.5 seconds later, should still be allowed (even though pressure is low) - timeNowUs += 4.5e6; + advanceTimeUs(4.5e6); dut.updateState(1000, getTimeNowNt()); EXPECT_TRUE(dut.allowInjection()); @@ -271,12 +269,12 @@ TEST(limp, oilPressureSuccessCase) { ASSERT_TRUE(dut.allowInjection()); // 1 second later (5.5 since start), injection should be allowed since we saw pressure before the timeout - timeNowUs += 1.0e6; + advanceTimeUs(1.0e6); dut.updateState(1000, getTimeNowNt()); ASSERT_TRUE(dut.allowInjection()); // Later, we lose oil pressure, but engine should stay running - timeNowUs += 10e6; + advanceTimeUs(10e6); Sensor::setMockValue(SensorType::OilPressure, 10); dut.updateState(1000, getTimeNowNt()); ASSERT_TRUE(dut.allowInjection()); diff --git a/unit_tests/tests/test_pwm_generator.cpp b/unit_tests/tests/test_pwm_generator.cpp index 765b7e69b3..c4104c6c91 100644 --- a/unit_tests/tests/test_pwm_generator.cpp +++ b/unit_tests/tests/test_pwm_generator.cpp @@ -9,9 +9,6 @@ #define LOW_VALUE 0 #define HIGH_VALUE 1 - -extern int timeNowUs; - static int expectedTimeOfNextEvent; static void assertNextEvent(const char *msg, int expectedPinState, TestExecutor *executor, OutputPin& pin) { @@ -20,10 +17,10 @@ static void assertNextEvent(const char *msg, int expectedPinState, TestExecutor ASSERT_EQ( 1, executor->size()) << "PWM_test: schedulingQueue size"; // move time to next event timestamp - timeNowUs = expectedTimeOfNextEvent; + setTimeNowUs(expectedTimeOfNextEvent); // execute pending actions and assert that only one action was executed - ASSERT_EQ(1, executor->executeAll(timeNowUs)) << msg << " executed"; + ASSERT_EQ(1, executor->executeAll(getTimeNowUs())) << msg << " executed"; ASSERT_EQ(expectedPinState, pin.currentLogicValue) << msg << " pin state"; // assert that we have one new action in queue @@ -33,7 +30,8 @@ static void assertNextEvent(const char *msg, int expectedPinState, TestExecutor static void test100dutyCycle() { printf("*************************************** test100dutyCycle\r\n"); - expectedTimeOfNextEvent = timeNowUs = 0; + expectedTimeOfNextEvent = 0; + setTimeNowUs(0); OutputPin pin; SimplePwm pwm("test PWM1"); @@ -60,7 +58,8 @@ static void test100dutyCycle() { static void testSwitchToNanPeriod() { printf("*************************************** testSwitchToNanPeriod\r\n"); - expectedTimeOfNextEvent = timeNowUs = 0; + expectedTimeOfNextEvent = 0; + setTimeNowUs(0); OutputPin pin; SimplePwm pwm("test PWM1"); @@ -76,7 +75,7 @@ static void testSwitchToNanPeriod() { assertEqualsM2("1@1000/70", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); assertNextEvent("exec@70", LOW_VALUE, &executor, pin); - ASSERT_EQ( 600, timeNowUs) << "time1"; + ASSERT_EQ(600, getTimeNowUs()) << "time1"; expectedTimeOfNextEvent += 400; assertNextEvent("exec2@70", HIGH_VALUE, &executor, pin); @@ -96,7 +95,8 @@ TEST(PWM, testPwmGenerator) { test100dutyCycle(); testSwitchToNanPeriod(); - expectedTimeOfNextEvent = timeNowUs = 0; + expectedTimeOfNextEvent = 0; + setTimeNowUs(0); OutputPin pin; SimplePwm pwm("test PWM3"); @@ -113,7 +113,7 @@ TEST(PWM, testPwmGenerator) { assertEqualsM2("1@1000/80", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); assertNextEvent("exec@0", LOW_VALUE, &executor, pin); - ASSERT_EQ( 800, timeNowUs) << "time1"; + ASSERT_EQ(800, getTimeNowUs()) << "time1"; expectedTimeOfNextEvent += 200; assertEqualsM2("2@1000/80", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); @@ -123,18 +123,18 @@ TEST(PWM, testPwmGenerator) { assertEqualsM2("2@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); assertNextEvent("exec@1", LOW_VALUE, &executor, pin); - ASSERT_EQ( 1000, timeNowUs) << "time2"; + ASSERT_EQ(1000, getTimeNowUs()) << "time2"; expectedTimeOfNextEvent += 1000; assertEqualsM2("3@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); assertNextEvent("exec@2", LOW_VALUE /* pin value */, &executor, pin); - ASSERT_EQ( 2000, timeNowUs) << "time3"; + ASSERT_EQ(2000, getTimeNowUs()) << "time3"; expectedTimeOfNextEvent += 1000; assertEqualsM2("4@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); assertNextEvent("exec@3", LOW_VALUE /* pin value */, &executor, pin); - ASSERT_EQ( 3000, timeNowUs) << "time4"; + ASSERT_EQ(3000, getTimeNowUs()) << "time4"; expectedTimeOfNextEvent += 1000; assertEqualsM2("5@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); @@ -144,7 +144,7 @@ TEST(PWM, testPwmGenerator) { assertNextEvent("exec@5", LOW_VALUE /* pin value */, &executor, pin); expectedTimeOfNextEvent += 1000; - ASSERT_EQ( 5000, timeNowUs) << "time4"; + ASSERT_EQ(5000, getTimeNowUs()) << "time4"; assertEqualsM2("7@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0); assertNextEvent("exec@6", LOW_VALUE /* pin value */, &executor, pin); diff --git a/unit_tests/tests/trigger/test_real_cranking_miata_na6.cpp b/unit_tests/tests/trigger/test_real_cranking_miata_na6.cpp index 28661cfecc..16e75a9d8f 100644 --- a/unit_tests/tests/trigger/test_real_cranking_miata_na6.cpp +++ b/unit_tests/tests/trigger/test_real_cranking_miata_na6.cpp @@ -9,7 +9,6 @@ #include "logicdata_csv_reader.h" -extern int timeNowUs; extern WarningCodeState unitTestWarningCodeState; static void fireTriggerEvent(EngineTestHelper*eth, double timestampS, TriggerWheel channel, bool isFall) { @@ -28,9 +27,9 @@ static void fireTriggerEvent(EngineTestHelper*eth, double timestampS, TriggerWhe } Engine *engine = ð->engine; - - timeNowUs = 1'000'000 * timestampS; - printf("MIATANA: posting time=%d event=%d\n", timeNowUs, event); + + setTimeNowUs(1'000'000 * timestampS); + printf("MIATANA: posting time=%d event=%d\n", getTimeNowUs(), event); hwHandleShaftSignal((int)channel, !isFall, getTimeNowNt()); }