diff --git a/firmware/controllers/actuators/idle_thread.cpp b/firmware/controllers/actuators/idle_thread.cpp index 274435989b..2adaa64758 100644 --- a/firmware/controllers/actuators/idle_thread.cpp +++ b/firmware/controllers/actuators/idle_thread.cpp @@ -478,7 +478,7 @@ float IdleController::getClosedLoop(IIdleController::Phase phase, float tpsPos, float crankingTaper = getCrankingTaperFraction(); // Determine what operation phase we're in - idling or not - auto phase = determinePhase(rpm, targetRpm, tps, getVehicleSpeed(), crankingTaper); + auto phase = determinePhase(rpm, targetRpm, tps, getVehicleSpeed(PASS_ENGINE_PARAMETER_SIGNATURE), crankingTaper); m_lastPhase = phase; engine->engineState.isAutomaticIdle = tps.Valid && engineConfiguration->idleMode == IM_AUTO; diff --git a/firmware/controllers/algo/dynoview.cpp b/firmware/controllers/algo/dynoview.cpp index e27c5a2bcb..16d2633931 100644 --- a/firmware/controllers/algo/dynoview.cpp +++ b/firmware/controllers/algo/dynoview.cpp @@ -18,7 +18,7 @@ void DynoView::update(vssSrc src) { efitimeus_t timeNow, deltaTime = 0.0; float speed,deltaSpeed = 0.0; timeNow = getTimeNowUs(); - speed = getVehicleSpeed(); + speed = getVehicleSpeed(PASS_ENGINE_PARAMETER_SIGNATURE); if (src == ICU) { speed = efiRound(speed,1.0); } else { @@ -163,4 +163,4 @@ void updateDynoViewCan(DECLARE_ENGINE_PARAMETER_SIGNATURE) { dynoInstance.update(CAN); } -#endif /* EFI_DYNO_VIEW */ \ No newline at end of file +#endif /* EFI_DYNO_VIEW */ diff --git a/firmware/controllers/algo/launch_control.cpp b/firmware/controllers/algo/launch_control.cpp index 38a97f4f3d..6418051e38 100644 --- a/firmware/controllers/algo/launch_control.cpp +++ b/firmware/controllers/algo/launch_control.cpp @@ -55,7 +55,7 @@ bool LaunchControlBase::isInsideSwitchCondition() const { * then we have to return true, and trust that we would disable by other condition! */ bool LaunchControlBase::isInsideSpeedCondition() const { - int speed = getVehicleSpeed(); + int speed = getVehicleSpeed(PASS_ENGINE_PARAMETER_SIGNATURE); return (CONFIG(launchSpeedTreshold) > speed) || (!(CONFIG(launchActivationMode) == ALWAYS_ACTIVE_LAUNCH)); } diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 4206efc699..3688a429f3 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -395,7 +395,7 @@ void applyNewHardwareSettings(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } #endif -#if EFI_VEHICLE_SPEED +#if EFI_VEHICLE_SPEED && ! EFI_UNIT_TEST startVSSPins(); #endif /* EFI_VEHICLE_SPEED */ diff --git a/firmware/hw_layer/vehicle_speed.cpp b/firmware/hw_layer/vehicle_speed.cpp index 4e896e22d8..45c64ab912 100644 --- a/firmware/hw_layer/vehicle_speed.cpp +++ b/firmware/hw_layer/vehicle_speed.cpp @@ -7,6 +7,10 @@ #include "vehicle_speed.h" +#define DEFAULT_MOCK_SPEED -1 + +static float mockVehicleSpeed = DEFAULT_MOCK_SPEED; // in kilometers per hour + #if EFI_VEHICLE_SPEED #include "engine.h" @@ -15,20 +19,16 @@ #include "pin_repository.h" #include "can_vss.h" + +// todo: move from static into Engine GOD object in order for tests reset static efitick_t lastSignalTimeNt = 0; static efitick_t vssDiff = 0; -#define DEFAULT_MOCK_SPEED -1 -static float mockVehicleSpeed = DEFAULT_MOCK_SPEED; // in kilometers per hour - -void setMockVehicleSpeed(float speedKPH) { - mockVehicleSpeed = speedKPH; -} /** * @return vehicle speed, in kilometers per hour */ -float getVehicleSpeed(void) { +float getVehicleSpeed(DECLARE_ENGINE_PARAMETER_SIGNATURE) { if (mockVehicleSpeed != DEFAULT_MOCK_SPEED) return mockVehicleSpeed; #if EFI_CAN_SUPPORT @@ -36,7 +36,7 @@ float getVehicleSpeed(void) { return getVehicleCanSpeed(); } #endif /* EFI_CAN_SUPPORT */ - if (!hasVehicleSpeedSensor()) + if (!hasVehicleSpeedSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) return 0; efitick_t nowNt = getTimeNowNt(); if (nowNt - lastSignalTimeNt > NT_PER_SECOND) @@ -45,13 +45,20 @@ float getVehicleSpeed(void) { return engineConfiguration->vehicleSpeedCoef * NT_PER_SECOND / vssDiff; } -static void vsAnaWidthCallback(void) { +// todo: make this method public and invoke this method from test +void vsCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { engine->engineState.vssEventCounter++; efitick_t nowNt = getTimeNowNt(); vssDiff = nowNt - lastSignalTimeNt; lastSignalTimeNt = nowNt; } +#if ! EFI_UNIT_TEST + +static void vsAnaWidthCallback() { + vsCallback(PASS_ENGINE_PARAMETER_SIGNATURE); +} + static void speedInfo(void) { efiPrintf("VSS input at %s", hwPortname(CONFIG(vehicleSpeedSensorInputPin))); @@ -59,17 +66,18 @@ static void speedInfo(void) { efiPrintf("c=%.2f eventCounter=%d speed=%.2f", engineConfiguration->vehicleSpeedCoef, engine->engineState.vssEventCounter, - getVehicleSpeed()); + getVehicleSpeed(PASS_ENGINE_PARAMETER_SIGNATURE)); efiPrintf("vss diff %d", vssDiff); } +#endif // EFI_UNIT_TEST -bool hasVehicleSpeedSensor() { +bool hasVehicleSpeedSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return (isBrainPinValid(CONFIG(vehicleSpeedSensorInputPin))); } #if HAL_VSS_USE_PAL static void vsExtiCallback(void *) { - vsAnaWidthCallback(); + vsAnaWidthCallback(engine); } #endif /* HAL_VSS_USE_PAL */ @@ -81,6 +89,8 @@ void stopVSSPins(void) { #endif /* HAL_VSS_USE_PAL, HAL_USE_ICU */ } +#if ! EFI_UNIT_TEST + void startVSSPins(void) { if (!hasVehicleSpeedSensor()) { return; @@ -92,34 +102,41 @@ void startVSSPins(void) { #elif HAL_USE_ICU digital_input_s* vehicleSpeedInput = startDigitalCapture("VSS", CONFIG(vehicleSpeedSensorInputPin)); - vehicleSpeedInput->widthListeners.registerCallback((VoidInt) vsAnaWidthCallback, NULL); + vehicleSpeedInput->widthListeners.registerCallback((VoidInt) vsAnaWidthCallback, nullptr); #else #error "HAL_USE_ICU or HAL_VSS_USE_PAL should be enabled to use EFI_VEHICLE_SPEED" #endif /* HAL_VSS_USE_PAL, HAL_USE_ICU */ } +#endif void initVehicleSpeed() { +#if ! EFI_UNIT_TEST addConsoleAction("speedinfo", speedInfo); startVSSPins(); +#endif // EFI_UNIT_TEST } #else /* EFI_VEHICLE_SPEED */ #if EFI_UNIT_TEST -static float mockVehicleSpeed = 0.0; // in kilometers per hour -void setMockVehicleSpeed(float speedKPH) { - mockVehicleSpeed = speedKPH; -} -float getVehicleSpeed(void) { +float getVehicleSpeed(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // Mock return to be used in unit tests return mockVehicleSpeed; } #else -float getVehicleSpeed(void) { +float getVehicleSpeed(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // no VSS support return 0; } #endif /* EFI_UNIT_TEST */ #endif /* EFI_VEHICLE_SPEED */ + + + + +void setMockVehicleSpeed(float speedKPH) { + mockVehicleSpeed = speedKPH; +} + diff --git a/firmware/hw_layer/vehicle_speed.h b/firmware/hw_layer/vehicle_speed.h index 641852a2b4..fb300806bf 100644 --- a/firmware/hw_layer/vehicle_speed.h +++ b/firmware/hw_layer/vehicle_speed.h @@ -12,9 +12,9 @@ /** * @return vehicle speed, in kilometers per hour */ -float getVehicleSpeed(void); +float getVehicleSpeed(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initVehicleSpeed(); void setMockVehicleSpeed(float speedKPH); -bool hasVehicleSpeedSensor(); +bool hasVehicleSpeedSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE); void stopVSSPins(void); void startVSSPins(void);