diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index dfd1da8b37..de4345f1a0 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -76,6 +76,7 @@ #include "electronic_throttle.h" #include "tps.h" +#include "sensor.h" #include "io_pins.h" #include "engine_configuration.h" #include "pwm_generator_logic.h" @@ -108,8 +109,6 @@ static bool startupPositionError = false; #define STARTUP_NEUTRAL_POSITION_ERROR_THRESHOLD 5 -extern percent_t mockPedalPosition; - static percent_t directPwmValue = NAN; static percent_t currentEtbDuty; @@ -180,6 +179,14 @@ void EtbController::PeriodicTask() { return; } + auto pedalPosition = Sensor::get(SensorType::AcceleratorPedal); + + if (!pedalPosition.Valid) { + m_motor->set(0); + return; + } + + percent_t actualThrottlePosition = getTPSWithIndex(m_myIndex PASS_ENGINE_PARAMETER_SUFFIX); if (engine->etbAutoTune) { @@ -203,10 +210,8 @@ void EtbController::PeriodicTask() { } - percent_t pedalPosition = getPedalPosition(PASS_ENGINE_PARAMETER_SIGNATURE); - int rpm = GET_RPM(); - engine->engineState.targetFromTable = pedal2tpsMap.getValue(rpm / RPM_1_BYTE_PACKING_MULT, pedalPosition); + engine->engineState.targetFromTable = pedal2tpsMap.getValue(rpm / RPM_1_BYTE_PACKING_MULT, pedalPosition.Value); percent_t etbIdleAddition = CONFIG(useETBforIdleControl) ? engine->engineState.idle.etbIdleAddition : 0; percent_t targetPosition = engine->engineState.targetFromTable + etbIdleAddition; @@ -305,12 +310,6 @@ static void showEthInfo(void) { scheduleMsg(&logger, "etbAutoTune=%d", engine->etbAutoTune); - scheduleMsg(&logger, "throttlePedal=%.2f %.2f/%.2f @%s", - getPedalPosition(PASS_ENGINE_PARAMETER_SIGNATURE), - engineConfiguration->throttlePedalUpVoltage, - engineConfiguration->throttlePedalWOTVoltage, - getPinNameByAdcChannel("tPedal", engineConfiguration->throttlePedalPositionAdcChannel, pinNameBuffer)); - scheduleMsg(&logger, "TPS=%.2f", getTPS(PASS_ENGINE_PARAMETER_SIGNATURE)); @@ -371,8 +370,6 @@ static void etbReset() { } etbPidReset(); - - mockPedalPosition = MOCK_UNDEFINED; } #endif /* EFI_PROD_CODE */ diff --git a/firmware/controllers/actuators/idle_thread.cpp b/firmware/controllers/actuators/idle_thread.cpp index 4f14bb29e0..4a197843e5 100644 --- a/firmware/controllers/actuators/idle_thread.cpp +++ b/firmware/controllers/actuators/idle_thread.cpp @@ -39,6 +39,7 @@ #include "engine.h" #include "periodic_task.h" #include "allsensors.h" +#include "sensor.h" #if ! EFI_UNIT_TEST #include "stepper.h" @@ -217,15 +218,14 @@ static bool isOutOfAutomaticIdleCondition(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return !engine->engineState.idle.throttlePedalUpState; } - percent_t inputPosition; + const auto [valid, pos] = Sensor::get(SensorType::DriverThrottleIntent); - if (hasPedalPositionSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) { - inputPosition = getPedalPosition(PASS_ENGINE_PARAMETER_SIGNATURE); - } else { - inputPosition = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE); + // Disable auto idle in case of TPS/Pedal failure + if (!valid) { + return true; } - return inputPosition > CONFIG(idlePidDeactivationTpsThreshold); + return pos > CONFIG(idlePidDeactivationTpsThreshold); } /** diff --git a/firmware/controllers/can/can_rx.cpp b/firmware/controllers/can/can_rx.cpp index 8f4e966f2a..b4b269efe2 100644 --- a/firmware/controllers/can/can_rx.cpp +++ b/firmware/controllers/can/can_rx.cpp @@ -27,7 +27,6 @@ static void printPacket(const CANRxFrame& rx, Logging* logger) { } volatile float aemXSeriesLambda = 0; -volatile float canPedal = 0; volatile float canMap = 0; static CanSensorBase* s_head = nullptr; @@ -58,9 +57,6 @@ void processCanRxMessage(const CANRxFrame& frame, Logging* logger, efitick_t now // AEM x-series lambda sensor reports in 0.0001 lambda per bit uint16_t lambdaInt = SWAP_UINT16(frame.data16[0]); aemXSeriesLambda = 0.0001f * lambdaInt; - } else if (frame.EID == CONFIG(verboseCanBaseAddress) + CAN_PEDAL_TPS_OFFSET) { - int16_t pedalScaled = *reinterpret_cast(&frame.data8[0]); - canPedal = pedalScaled / (1.0 * PACK_MULT_PERCENT); } else if (frame.EID == CONFIG(verboseCanBaseAddress) + CAN_SENSOR_1_OFFSET) { int16_t mapScaled = *reinterpret_cast(&frame.data8[0]); canMap = mapScaled / (1.0 * PACK_MULT_PRESSURE); diff --git a/firmware/controllers/can/can_verbose.cpp b/firmware/controllers/can/can_verbose.cpp index 55edee7849..f51c71d16f 100644 --- a/firmware/controllers/can/can_verbose.cpp +++ b/firmware/controllers/can/can_verbose.cpp @@ -81,7 +81,7 @@ struct PedalAndTps { static void populateFrame(PedalAndTps& msg) { - msg.pedal = getPedalPosition(); + msg.pedal = Sensor::get(SensorType::AcceleratorPedal).value_or(-1); msg.tps1 = Sensor::get(SensorType::Tps1).value_or(-1); msg.tps2 = Sensor::get(SensorType::Tps2).value_or(-1); } diff --git a/firmware/controllers/engine_controller.h b/firmware/controllers/engine_controller.h index a6404e7fae..da82b6bb72 100644 --- a/firmware/controllers/engine_controller.h +++ b/firmware/controllers/engine_controller.h @@ -28,7 +28,6 @@ void setMockVBattVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockMapVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); // throttle body sensor void setMockThrottlePositionSensorVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); -void setMockThrottlePedalSensorVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockMafVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockIatVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/engine_controller_misc.cpp b/firmware/controllers/engine_controller_misc.cpp index 584b694fdf..ab8d7fecec 100644 --- a/firmware/controllers/engine_controller_misc.cpp +++ b/firmware/controllers/engine_controller_misc.cpp @@ -51,10 +51,6 @@ void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { setMockVoltage(engineConfiguration->afr.hwChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); } -void setMockThrottlePedalSensorVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { - setMockVoltage(engineConfiguration->throttlePedalPositionAdcChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); -} - void setMockThrottlePositionSensorVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { setMockVoltage(engineConfiguration->tps1_1AdcChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); } diff --git a/firmware/controllers/sensors/tps.cpp b/firmware/controllers/sensors/tps.cpp index 30b25d7f91..7106a2cb89 100644 --- a/firmware/controllers/sensors/tps.cpp +++ b/firmware/controllers/sensors/tps.cpp @@ -11,12 +11,6 @@ EXTERN_ENGINE; -/** - * set mock_pedal_position X - * See also directPwmValue - */ -percent_t mockPedalPosition = MOCK_UNDEFINED; - #if !EFI_PROD_CODE /** * this allows unit tests to simulate TPS position @@ -30,11 +24,6 @@ void setMockTpsValue(percent_t tpsPosition DECLARE_ENGINE_PARAMETER_SUFFIX) { } #endif /* EFI_PROD_CODE */ -// see also setMockThrottlePedalSensorVoltage -void setMockThrottlePedalPosition(percent_t value DECLARE_ENGINE_PARAMETER_SUFFIX) { - mockPedalPosition = value; -} - /** * We are using one instance for read and another for modification, this is how we get lock-free thread-safety * @@ -214,33 +203,6 @@ bool hasSecondThrottleBody(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return engineConfiguration->tps2_1AdcChannel != EFI_ADC_NONE; } -extern float canPedal; - -percent_t getPedalPosition(DECLARE_ENGINE_PARAMETER_SIGNATURE) { -#if EFI_CANBUS_SLAVE - return canPedal; -#endif - - - if (mockPedalPosition != MOCK_UNDEFINED) { - return mockPedalPosition; - } - - // Don't choke without a pedal set - if (CONFIG(throttlePedalPositionAdcChannel) == EFI_ADC_NONE) { - return 0; - } - - DISPLAY_TAG(PEDAL_SECTION); - DISPLAY_TEXT(Analog_MCU_reads); - - float voltage = getVoltageDivided("pPS", CONFIG(DISPLAY_CONFIG(throttlePedalPositionAdcChannel)) PASS_ENGINE_PARAMETER_SUFFIX); - percent_t result = interpolateMsg("pedal", engineConfiguration->throttlePedalUpVoltage, 0, engineConfiguration->throttlePedalWOTVoltage, 100, voltage); - - // this would put the value into the 0-100 range - return maxF(0, minF(100, result)); -} - static bool hasTpsSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return engineConfiguration->tps1_1AdcChannel != EFI_ADC_NONE; } diff --git a/firmware/controllers/sensors/tps.h b/firmware/controllers/sensors/tps.h index fe1e9f3505..a5756ed5f3 100644 --- a/firmware/controllers/sensors/tps.h +++ b/firmware/controllers/sensors/tps.h @@ -16,7 +16,6 @@ #define TPS_TS_CONVERSION 200 bool hasPedalPositionSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE); -percent_t getPedalPosition(DECLARE_ENGINE_PARAMETER_SIGNATURE); /** * Throttle Position Sensor * In case of dual TPS this function would return logical TPS position @@ -29,7 +28,6 @@ bool hasSecondThrottleBody(DECLARE_ENGINE_PARAMETER_SIGNATURE); percent_t getTpsValue(int index, float adc DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockTpsAdc(percent_t tpsPosition DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockTpsValue(percent_t tpsPosition DECLARE_ENGINE_PARAMETER_SUFFIX); -void setMockThrottlePedalPosition(percent_t value DECLARE_ENGINE_PARAMETER_SUFFIX); void grabTPSIsClosed(); void grabTPSIsWideOpen(); void grabPedalIsUp(); diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 3e4e372c56..cb8614d8a7 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -1177,8 +1177,6 @@ const command_f_s commandsF[] = { #if EFI_ENGINE_CONTROL #if EFI_ENABLE_MOCK_ADC {MOCK_IAT_COMMAND, setMockIatVoltage}, - {MOCK_PPS_POSITION_COMMAND, setMockThrottlePedalPosition}, - {MOCK_PPS_VOLTAGE_COMMAND, setMockThrottlePedalSensorVoltage}, {MOCK_TPS_COMMAND, setMockThrottlePositionSensorVoltage}, {MOCK_MAF_COMMAND, setMockMafVoltage}, {MOCK_AFR_COMMAND, setMockAfrVoltage}, diff --git a/firmware/hw_layer/stepper.cpp b/firmware/hw_layer/stepper.cpp index a28177d2e6..3e4e76803b 100644 --- a/firmware/hw_layer/stepper.cpp +++ b/firmware/hw_layer/stepper.cpp @@ -13,9 +13,9 @@ #if EFI_PROD_CODE || EFI_SIMULATOR #include "stepper.h" #include "pin_repository.h" -#include "tps.h" #include "engine_controller.h" #include "adc_inputs.h" +#include "sensor.h" EXTERN_ENGINE; @@ -55,10 +55,11 @@ void StepperMotor::ThreadTask() { bool isRunning = false; #endif /* EFI_SHAFT_POSITION_INPUT */ // now check if stepper motor re-initialization is requested - if the throttle pedal is pressed at startup - bool forceStepperParking = !isRunning && getTPS(PASS_ENGINE_PARAMETER_SIGNATURE) > STEPPER_PARKING_TPS; + auto tpsPos = Sensor::get(SensorType::DriverThrottleIntent).value_or(0); + bool forceStepperParking = !isRunning && tpsPos > STEPPER_PARKING_TPS; if (CONFIG(stepperForceParkingEveryRestart)) forceStepperParking = true; - scheduleMsg(logger, "Stepper: savedStepperPos=%d forceStepperParking=%d (tps=%.2f)", m_currentPosition, (forceStepperParking ? 1 : 0), getTPS(PASS_ENGINE_PARAMETER_SIGNATURE)); + scheduleMsg(logger, "Stepper: savedStepperPos=%d forceStepperParking=%d (tps=%.2f)", m_currentPosition, (forceStepperParking ? 1 : 0), tpsPos); if (m_currentPosition < 0 || forceStepperParking) { // reset saved value diff --git a/firmware/init/sensor/init_tps.cpp b/firmware/init/sensor/init_tps.cpp index 264495ceac..70f64fef28 100644 --- a/firmware/init/sensor/init_tps.cpp +++ b/firmware/init/sensor/init_tps.cpp @@ -62,6 +62,10 @@ void initTps() { } else { driverIntent.setProxiedSensor(SensorType::Tps1); } + + if (!driverIntent.Register()) { + firmwareError(CUSTOM_INVALID_TPS_SETTING, "Duplicate TPS registration for TPS sensor"); + } } void reconfigureTps() { diff --git a/unit_tests/tests/test_etb.cpp b/unit_tests/tests/test_etb.cpp index 178e3672c0..ce18b07361 100644 --- a/unit_tests/tests/test_etb.cpp +++ b/unit_tests/tests/test_etb.cpp @@ -9,7 +9,7 @@ #include "electronic_throttle.h" #include "dc_motor.h" #include "engine_controller.h" -#include "tps.h" +#include "sensor.h" class MockEtb : public IEtbController { public: @@ -75,16 +75,15 @@ TEST(etb, testTargetTpsIsFloatBug945) { engineConfiguration->throttlePedalPositionAdcChannel = EFI_ADC_0; - setMockThrottlePedalSensorVoltage(3 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::AcceleratorPedal, 50.0f); engine->etbControllers[0]->PeriodicTask(); ASSERT_NEAR(50, engine->engineState.targetFromTable, EPS4D); - setMockThrottlePedalSensorVoltage(3.05 PASS_ENGINE_PARAMETER_SUFFIX); - ASSERT_NEAR(50.8302, getPedalPosition(PASS_ENGINE_PARAMETER_SIGNATURE), EPS4D); + Sensor::setMockValue(SensorType::AcceleratorPedal, 50.8302f); engine->etbControllers[0]->PeriodicTask(); ASSERT_NEAR(50.8302, engine->engineState.targetFromTable, EPS4D); - setMockThrottlePedalSensorVoltage(3.1 PASS_ENGINE_PARAMETER_SUFFIX); + Sensor::setMockValue(SensorType::AcceleratorPedal, 51.6605f); engine->etbControllers[0]->PeriodicTask(); ASSERT_NEAR(51.6605, engine->engineState.targetFromTable, EPS4D); }