From 7ebebd692255f1ff7e624f544ba76ded9ebde18a Mon Sep 17 00:00:00 2001 From: alxrMironov <30952168+alxrMironov@users.noreply.github.com> Date: Thu, 12 Aug 2021 22:16:51 +0300 Subject: [PATCH] Begin "vehicleSpeed" refactoring. Module switched to "FunctionalPointerSensor" class. #3106 #3107 #3108. (#3132) * Add new sensor type "VehicleSpeed" * Add "Vehicle speed" sensor to name list * Add Vehicle speed sensor initialization with function pointer sensor. * Add init_vehicle_speed_sensor.cpp to Makefile * Replace dependencies from "getVehicleSpeed" to OOP-like "Sensor::get()" * Add warning and TODO to "mock vehicle speed" call * Replace get & mock "VehicleSpeed" methods in tests * Remove #warning directive (cause error by current settings) * Remove stray tab * Remove explicitly type casting. * Add "InitVehicleSpeedSensor" function declaration to init.h * Add "VehicleSpeedSensor" initialization call into "InitNewSensors" Co-authored-by: alxrMironov <330OMcorporative> --- firmware/console/status_loop.cpp | 2 +- .../controllers/actuators/idle_thread.cpp | 3 ++- firmware/controllers/algo/dynoview.cpp | 2 +- firmware/controllers/algo/launch_control.cpp | 2 +- firmware/controllers/can/can_dash.cpp | 11 +++++--- firmware/controllers/can/can_verbose.cpp | 2 +- firmware/controllers/can/obd2.cpp | 2 +- firmware/controllers/sensors/sensor.cpp | 2 ++ firmware/controllers/sensors/sensor_type.h | 4 ++- firmware/controllers/settings.cpp | 1 + firmware/init/init.h | 1 + firmware/init/init.mk | 1 + firmware/init/sensor/init_sensors.cpp | 1 + .../init/sensor/init_vehicle_speed_sensor.cpp | 26 +++++++++++++++++++ unit_tests/tests/test_dynoview.cpp | 14 +++++----- unit_tests/tests/test_idle_controller.cpp | 6 ++--- unit_tests/tests/test_launch.cpp | 12 ++++----- 17 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 firmware/init/sensor/init_vehicle_speed_sensor.cpp diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index ef3882affd..c3119181e5 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -689,7 +689,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ tsOutputChannels->isCylinderCleanupActivated = engine->isCylinderCleanupMode; #if EFI_VEHICLE_SPEED - float vehicleSpeed = getVehicleSpeed(); + float vehicleSpeed = Sensor::get(SensorType::VehicleSpeed).value_or(0); tsOutputChannels->vehicleSpeedKph = vehicleSpeed; tsOutputChannels->speedToRpmRatio = vehicleSpeed / rpm; diff --git a/firmware/controllers/actuators/idle_thread.cpp b/firmware/controllers/actuators/idle_thread.cpp index 2adaa64758..7cf630ecbe 100644 --- a/firmware/controllers/actuators/idle_thread.cpp +++ b/firmware/controllers/actuators/idle_thread.cpp @@ -478,7 +478,8 @@ 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(PASS_ENGINE_PARAMETER_SIGNATURE), crankingTaper); + float vehicleSpeed = Sensor::get(SensorType::VehicleSpeed).value_or(0); + auto phase = determinePhase(rpm, targetRpm, tps, vehicleSpeed, 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 d13143329b..68efc11e16 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(PASS_ENGINE_PARAMETER_SIGNATURE); + speed = Sensor::get(SensorType::VehicleSpeed).value_or(0); if (src == ICU) { speed = efiRound(speed,1.0); } else { diff --git a/firmware/controllers/algo/launch_control.cpp b/firmware/controllers/algo/launch_control.cpp index 6418051e38..6e2b9e6ed2 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(PASS_ENGINE_PARAMETER_SIGNATURE); + int speed = Sensor::get(SensorType::VehicleSpeed).value_or(0); return (CONFIG(launchSpeedTreshold) > speed) || (!(CONFIG(launchActivationMode) == ALWAYS_ACTIVE_LAUNCH)); } diff --git a/firmware/controllers/can/can_dash.cpp b/firmware/controllers/can/can_dash.cpp index 6dcc99298f..c783d6cb4c 100644 --- a/firmware/controllers/can/can_dash.cpp +++ b/firmware/controllers/can/can_dash.cpp @@ -173,7 +173,7 @@ void canMazdaRX8(CanCycle cycle) { { CanTxMessage msg(CAN_MAZDA_RX_RPM_SPEED); - float kph = getVehicleSpeed(); + float kph = Sensor::get(SensorType::VehicleSpeed).value_or(0); msg.setShortValue(SWAP_UINT16(GET_RPM() * 4), 0); msg.setShortValue(0xFFFF, 2); @@ -497,7 +497,8 @@ void canDashboardBMWE90(CanCycle cycle) } { //E90_SPEED - float mph = getVehicleSpeed() * 0.6213712; + auto vehicleSpeed = Sensor::get(SensorType::VehicleSpeed).value_or(0); + float mph = vehicleSpeed * 0.6213712; mph_ctr = ((TIME_I2MS(chVTGetSystemTime()) - mph_timer) / 50); mph_a = (mph_ctr * mph / 2); mph_2a = mph_a + mph_last; @@ -753,7 +754,8 @@ void canDashboardHaltech(CanCycle cycle) { { CanTxMessage msg(0x36C, 8); /* Wheel Speed Front Left */ - tmp = (getVehicleSpeed() * 10 ); + auto vehicleSpeed = Sensor::get(SensorType::VehicleSpeed).value_or(0); + tmp = (vehicleSpeed * 10 ); msg[0] = (tmp >> 8); msg[1] = (tmp & 0x00ff); /* Wheel Speed Front Right */ @@ -815,7 +817,8 @@ void canDashboardHaltech(CanCycle cycle) { { CanTxMessage msg(0x370, 8); /* Vehicle Speed */ - tmp = (getVehicleSpeed() * 10 ); + auto vehicleSpeed = Sensor::get(SensorType::VehicleSpeed).value_or(0); + tmp = (vehicleSpeed * 10 ); msg[0] = (tmp >> 8); msg[1] = (tmp & 0x00ff); /* unused */ diff --git a/firmware/controllers/can/can_verbose.cpp b/firmware/controllers/can/can_verbose.cpp index 72f74c370b..ee8810dd71 100644 --- a/firmware/controllers/can/can_verbose.cpp +++ b/firmware/controllers/can/can_verbose.cpp @@ -63,7 +63,7 @@ static void populateFrame(Speeds& msg) { msg.injDuty = getInjectorDutyCycle(rpm); msg.coilDuty = getCoilDutyCycle(rpm); - msg.vssKph = getVehicleSpeed(); + msg.vssKph = Sensor::get(SensorType::VehicleSpeed).value_or(0); } struct PedalAndTps { diff --git a/firmware/controllers/can/obd2.cpp b/firmware/controllers/can/obd2.cpp index c77eb9f389..a54b6abcff 100644 --- a/firmware/controllers/can/obd2.cpp +++ b/firmware/controllers/can/obd2.cpp @@ -136,7 +136,7 @@ static void handleGetDataRequest(const CANRxFrame& rx) { obdSendValue(_1_MODE, pid, 2, GET_RPM() * ODB_RPM_MULT); // rotation/min. (A*256+B)/4 break; case PID_SPEED: - obdSendValue(_1_MODE, pid, 1, getVehicleSpeed()); + obdSendValue(_1_MODE, pid, 1, Sensor::get(SensorType::VehicleSpeed).value_or(0)); break; case PID_TIMING_ADVANCE: { float timing = engine->engineState.timingAdvance; diff --git a/firmware/controllers/sensors/sensor.cpp b/firmware/controllers/sensors/sensor.cpp index 3dfd34ce31..476dd4c4c0 100644 --- a/firmware/controllers/sensors/sensor.cpp +++ b/firmware/controllers/sensors/sensor.cpp @@ -49,6 +49,8 @@ static const char* const s_sensorNames[] = { "Aux 2", "Aux 3", "Aux 4", + + "Vehicle speed", }; // This struct represents one sensor in the registry. diff --git a/firmware/controllers/sensors/sensor_type.h b/firmware/controllers/sensors/sensor_type.h index 37118d0b85..4e22eddfb4 100644 --- a/firmware/controllers/sensors/sensor_type.h +++ b/firmware/controllers/sensors/sensor_type.h @@ -73,6 +73,8 @@ enum class SensorType : unsigned char { Aux3 = 32, Aux4 = 33, + VehicleSpeed = 34, + // Leave me at the end! - PlaceholderLast = 34, + PlaceholderLast = 35, }; diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 6ed448f4a4..89a5115f68 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -1052,6 +1052,7 @@ const command_f_s commandsF[] = { {"fsio_curve_2_value", setFsioCurve2Value}, #if EFI_PROD_CODE #if EFI_VEHICLE_SPEED + //todo: This function become deprecated soon {"mock_vehicle_speed", setMockVehicleSpeed}, #endif /* EFI_VEHICLE_SPEED */ #if EFI_IDLE_CONTROL diff --git a/firmware/init/init.h b/firmware/init/init.h index 9ef5688fae..8d0041cf1d 100644 --- a/firmware/init/init.h +++ b/firmware/init/init.h @@ -29,6 +29,7 @@ void initFlexSensor(DECLARE_CONFIG_PARAMETER_SIGNATURE); void initFuelLevel(DECLARE_CONFIG_PARAMETER_SIGNATURE); void initBaro(DECLARE_CONFIG_PARAMETER_SIGNATURE); void initAuxSensors(DECLARE_CONFIG_PARAMETER_SIGNATURE); +void initVehicleSpeedSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE); // Sensor reconfiguration void reconfigureVbatt(DECLARE_CONFIG_PARAMETER_SIGNATURE); diff --git a/firmware/init/init.mk b/firmware/init/init.mk index 700b604fa3..5559b67c11 100644 --- a/firmware/init/init.mk +++ b/firmware/init/init.mk @@ -12,3 +12,4 @@ INIT_SRC_CPP = $(PROJECT_DIR)/init/sensor/init_sensors.cpp \ $(PROJECT_DIR)/init/sensor/init_baro.cpp \ $(PROJECT_DIR)/init/sensor/init_fuel_level.cpp \ $(PROJECT_DIR)/init/sensor/init_aux.cpp \ + $(PROJECT_DIR)/init/sensor/init_vehicle_speed_sensor.cpp \ diff --git a/firmware/init/sensor/init_sensors.cpp b/firmware/init/sensor/init_sensors.cpp index 44f652a21f..21b5d73dd6 100644 --- a/firmware/init/sensor/init_sensors.cpp +++ b/firmware/init/sensor/init_sensors.cpp @@ -23,6 +23,7 @@ void initNewSensors(DECLARE_ENGINE_PARAMETER_SIGNATURE) { initFlexSensor(PASS_CONFIG_PARAMETER_SIGNATURE); initBaro(PASS_CONFIG_PARAMETER_SIGNATURE); initAuxSensors(PASS_CONFIG_PARAMETER_SIGNATURE); + initVehicleSpeedSensor(PASS_ENGINE_PARAMETER_SIGNATURE); #if !EFI_UNIT_TEST initFuelLevel(PASS_CONFIG_PARAMETER_SIGNATURE); diff --git a/firmware/init/sensor/init_vehicle_speed_sensor.cpp b/firmware/init/sensor/init_vehicle_speed_sensor.cpp new file mode 100644 index 0000000000..9fee288306 --- /dev/null +++ b/firmware/init/sensor/init_vehicle_speed_sensor.cpp @@ -0,0 +1,26 @@ +#include "pch.h" + +#include "init.h" +#include "vehicle_speed.h" +#include "function_pointer_sensor.h" + +struct GetVehicleSpeedWrapper { + DECLARE_ENGINE_PTR; + + float getVehicleSpeed() { + return ::getVehicleSpeed(PASS_ENGINE_PARAMETER_SIGNATURE); + } +}; + +static GetVehicleSpeedWrapper vehicleSpeedWrapper; + +static FunctionPointerSensor vehicleSpeedSensor(SensorType::VehicleSpeed, +[]() { + return vehicleSpeedWrapper.getVehicleSpeed(); +}); + +void initVehicleSpeedSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { + INJECT_ENGINE_REFERENCE(&vehicleSpeedWrapper); + + vehicleSpeedSensor.Register(); +} \ No newline at end of file diff --git a/unit_tests/tests/test_dynoview.cpp b/unit_tests/tests/test_dynoview.cpp index 124966e0e9..30b23ad679 100644 --- a/unit_tests/tests/test_dynoview.cpp +++ b/unit_tests/tests/test_dynoview.cpp @@ -27,11 +27,11 @@ TEST(DynoView, VSS_T1) { engineConfiguration->vehicleWeight = 900; eth.moveTimeForwardMs(50); - setMockVehicleSpeed(18.0 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 18.0); dut.update(ICU); eth.moveTimeForwardAndInvokeEventsSec(20); - setMockVehicleSpeed(126.0 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 126.0); dut.update(ICU); ASSERT_EQ(1.5, dut.getAcceleration()); @@ -47,7 +47,7 @@ TEST(DynoView, algo) { engineConfiguration->vehicleWeight = 900; //to capture vss - setMockVehicleSpeed(35*3.6 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 35*3.6); dut.update(ICU); dut.setAcceleration(1.5); @@ -73,12 +73,12 @@ TEST(DynoView, VSS_fast) { engine->rpmCalculator.mockRpm = 2200; eth.moveTimeForwardMs(50); - setMockVehicleSpeed(50.0 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 50.0); dut.update(CAN); //delay 50ms eth.moveTimeForwardMs(50); - setMockVehicleSpeed(50.252 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 50.252); dut.update(CAN); ASSERT_EQ(1259, dut.getEngineForce()); @@ -97,12 +97,12 @@ TEST(DynoView, VSS_Torque) { engine->rpmCalculator.mockRpm = 2200; eth.moveTimeForwardMs(50); - setMockVehicleSpeed(80.0 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 80.0); dut.update(CAN); //delay 50ms eth.moveTimeForwardMs(50); - setMockVehicleSpeed(80.504 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 80.504); dut.update(CAN); ASSERT_EQ(242, dut.getEngineTorque()); diff --git a/unit_tests/tests/test_idle_controller.cpp b/unit_tests/tests/test_idle_controller.cpp index 374cf1a8e1..1253121640 100644 --- a/unit_tests/tests/test_idle_controller.cpp +++ b/unit_tests/tests/test_idle_controller.cpp @@ -425,7 +425,7 @@ TEST(idle_v2, IntegrationManual) { float expectedClt = 37; Sensor::setMockValue(SensorType::DriverThrottleIntent, expectedTps.Value); Sensor::setMockValue(SensorType::Clt, expectedClt); - setMockVehicleSpeed(15 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 15.0); ENGINE(rpmCalculator.mockRpm) = 950; // Target of 1000 rpm @@ -460,7 +460,7 @@ TEST(idle_v2, IntegrationAutomatic) { float expectedClt = 37; Sensor::setMockValue(SensorType::DriverThrottleIntent, expectedTps.Value); Sensor::setMockValue(SensorType::Clt, expectedClt); - setMockVehicleSpeed(15 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 15.0); ENGINE(rpmCalculator.mockRpm) = 950; // Target of 1000 rpm @@ -498,7 +498,7 @@ TEST(idle_v2, IntegrationClamping) { float expectedClt = 37; Sensor::setMockValue(SensorType::DriverThrottleIntent, expectedTps.Value); Sensor::setMockValue(SensorType::Clt, expectedClt); - setMockVehicleSpeed(15 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 15.0); ENGINE(rpmCalculator.mockRpm) = 950; // Target of 1000 rpm diff --git a/unit_tests/tests/test_launch.cpp b/unit_tests/tests/test_launch.cpp index 4f1711dfcc..779676d238 100644 --- a/unit_tests/tests/test_launch.cpp +++ b/unit_tests/tests/test_launch.cpp @@ -35,10 +35,10 @@ TEST(LaunchControl, VSSCondition) { engineConfiguration->launchActivationMode = ALWAYS_ACTIVE_LAUNCH; engineConfiguration->launchSpeedTreshold = 30; engineConfiguration->launchDisableBySpeed = 1; - setMockVehicleSpeed(10 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 10.0); EXPECT_TRUE(dut.isInsideSpeedCondition()); - setMockVehicleSpeed(40 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 40.0); EXPECT_FALSE(dut.isInsideSpeedCondition()); } @@ -114,7 +114,7 @@ TEST(LaunchControl, CombinedCondition) { //valid TPS Sensor::setMockValue(SensorType::DriverThrottleIntent, 20.0f); - setMockVehicleSpeed(10 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 10.0); engine->rpmCalculator.mockRpm = 1200; EXPECT_FALSE(dut.isLaunchConditionMet(1200)); @@ -122,7 +122,7 @@ TEST(LaunchControl, CombinedCondition) { engine->rpmCalculator.mockRpm = 3200; EXPECT_TRUE(dut.isLaunchConditionMet(3200)); - setMockVehicleSpeed(40 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 40.0); EXPECT_FALSE(dut.isLaunchConditionMet(3200)); } @@ -146,7 +146,7 @@ TEST(LaunchControl, CompleteRun) { //valid TPS Sensor::setMockValue(SensorType::DriverThrottleIntent, 20.0f); - setMockVehicleSpeed(10 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 10.0); engine->rpmCalculator.mockRpm = 1200; //update condition check @@ -184,7 +184,7 @@ TEST(LaunchControl, CompleteRun) { EXPECT_TRUE(spark); EXPECT_FALSE(fuel); - setMockVehicleSpeed(40 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::VehicleSpeed, 40.0); updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); spark = false; fuel = false;