setTimeNowUs(): replace global fiddling with a functino call

This commit is contained in:
Matthew Kennedy 2024-04-27 01:59:38 -07:00
parent 5f52f5fc9f
commit 80358ad54e
15 changed files with 63 additions and 68 deletions

View File

@ -494,9 +494,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());

View File

@ -17,7 +17,6 @@
#include "efitime.h"
#if EFI_UNIT_TEST
extern int timeNowUs;
extern bool verboseMode;
#endif /* EFI_UNIT_TEST */

View File

@ -110,4 +110,9 @@ efitimeus_t getTimeNowUs();
*/
int64_t getTimeNowS();
#if EFI_UNIT_TEST
void setTimeNowUs(int us);
void advanceTimeUs(int us);
#endif
#endif /* __cplusplus */

View File

@ -20,8 +20,6 @@
extern WaveChart waveChart;
#endif /* EFI_ENGINE_SNIFFER */
extern int timeNowUs;
extern WarningCodeState unitTestWarningCodeState;
extern engine_configuration_s & activeConfiguration;
extern bool printTriggerDebug;
@ -31,7 +29,7 @@ extern int minCrankingRpm;
EngineTestHelperBase::EngineTestHelperBase(Engine * eng, engine_configuration_s * econfig, persistent_config_s * pers) {
// todo: make this not a global variable, we need currentTimeProvider interface on engine
timeNowUs = 0;
setTimeNowUs(0);
minCrankingRpm = 0;
EnableToothLogger();
if (engine || engineConfiguration || config) {
@ -226,7 +224,7 @@ void EngineTestHelper::clearQueue() {
}
int EngineTestHelper::executeActions() {
return engine.executor.executeAll(timeNowUs);
return engine.executor.executeAll(getTimeNowUs());
}
void EngineTestHelper::moveTimeForwardMs(float deltaTimeMs) {
@ -241,7 +239,7 @@ void EngineTestHelper::moveTimeForwardUs(int deltaTimeUs) {
if (printTriggerDebug || printFuelDebug) {
printf("moveTimeForwardUs %.1fms\r\n", deltaTimeUs / 1000.0);
}
timeNowUs += deltaTimeUs;
advanceTimeUs(deltaTimeUs);
}
void EngineTestHelper::moveTimeForwardAndInvokeEventsSec(int deltaTimeSeconds) {
@ -255,7 +253,7 @@ void EngineTestHelper::moveTimeForwardAndInvokeEventsUs(int deltaTimeUs) {
if (printTriggerDebug || printFuelDebug) {
printf("moveTimeForwardAndInvokeEventsUs %.1fms\r\n", deltaTimeUs / 1000.0);
}
setTimeAndInvokeEventsUs(timeNowUs + deltaTimeUs);
setTimeAndInvokeEventsUs(getTimeNowUs() + deltaTimeUs);
}
void EngineTestHelper::setTimeAndInvokeEventsUs(int targetTime) {
@ -270,11 +268,11 @@ void EngineTestHelper::setTimeAndInvokeEventsUs(int targetTime) {
// next event is too far in the future
break;
}
timeNowUs = nextEventTime;
engine.executor.executeAll(timeNowUs);
setTimeNowUs(nextEventTime);
engine.executor.executeAll(getTimeNowUs());
}
timeNowUs = targetTime;
setTimeNowUs(targetTime);
}
void EngineTestHelper::fireTriggerEvents(int count) {

View File

@ -69,7 +69,7 @@ namespace chibios_rt {
struct virtual_timer_t;
#define UNIT_TEST_BUSY_WAIT_CALLBACK() { timeNowUs++; }
#define UNIT_TEST_BUSY_WAIT_CALLBACK() { advanceTimeUs(1); }
#define chsnprintf snprintf
#define chvsnprintf vsnprintf

View File

@ -9,12 +9,20 @@
bool verboseMode = false;
int timeNowUs = 0;
static int timeNowUs = 0;
efitick_t getTimeNowNt() {
return (efitimeus_t)timeNowUs * US_TO_NT_MULTIPLIER;
}
void setTimeNowUs(int us) {
timeNowUs = us;
}
void advanceTimeUs(int us) {
timeNowUs += us;
}
void initLogging(LoggingWithStorage *logging, const char *name) {
}

View File

@ -521,20 +521,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));
}
@ -739,8 +738,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);
@ -765,7 +762,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);
@ -781,13 +778,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);
}

View File

@ -47,8 +47,6 @@ TEST(etb, integrated) {
ASSERT_NEAR(destination, 130.2554, EPS3D);
}
extern int timeNowUs;
// TEST(etb, intermittentTps) {
// EngineTestHelper eth(engine_type_e::TEST_ENGINE); // we have a destructor so cannot move EngineTestHelper into utility method
// EtbController *etb = initEtbIntegratedTest();

View File

@ -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));

View File

@ -165,8 +165,7 @@ TEST(fuelCut, delay) {
const float normalInjDuration = 1.5f;
extern int timeNowUs;
timeNowUs = 1e6;
setTimeNowUs(1e6);
// process
eth.engine.periodicFastCallback();
@ -182,14 +181,14 @@ TEST(fuelCut, delay) {
EXPECT_NORMAL();
// Change nothing else, but advance time and update again
timeNowUs += 0.9e6;
advanceTimeUs(0.9e6);
eth.engine.periodicFastCallback();
// too soon, still no cut
EXPECT_NORMAL();
// Change nothing else, but advance time and update again
timeNowUs += 0.2e6;
advanceTimeUs(0.2e6);
eth.engine.periodicFastCallback();
// Should now be cut!

View File

@ -364,8 +364,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;
@ -384,7 +382,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));
@ -412,7 +410,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));

View File

@ -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());

View File

@ -132,8 +132,6 @@ TEST(limp, boostCut) {
EXPECT_TRUE(dut.allowInjection());
}
extern int timeNowUs;
TEST(limp, oilPressureFailureCase) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
engineConfiguration->minOilPressureAfterStart = 200;
@ -151,12 +149,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());
@ -184,7 +182,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());
@ -194,12 +192,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());

View File

@ -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.m_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);

View File

@ -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) {
@ -27,8 +26,8 @@ static void fireTriggerEvent(EngineTestHelper*eth, double timestampS, TriggerWhe
event = SHAFT_SECONDARY_FALLING;
}
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());
}