diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 8fa6346163..54b4d9840d 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -185,6 +185,11 @@ bool EtbController::init(etb_function_e function, DcMotor *motor, pid_s *pidPara return false; } + // If no sensor is configured for this throttle, skip initialization. + if (!Sensor::hasSensor(functionToTpsSensorPrimary(function))) { + return false; + } + if (!Sensor::isRedundant(m_positionSensor)) { firmwareError( OBD_Throttle_Position_Sensor_Circuit_Malfunction, diff --git a/unit_tests/tests/test_etb.cpp b/unit_tests/tests/test_etb.cpp index 1b35469293..ba8bae29a1 100644 --- a/unit_tests/tests/test_etb.cpp +++ b/unit_tests/tests/test_etb.cpp @@ -174,6 +174,7 @@ TEST(etb, initializationNotRedundantTps) { Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); // Not redundant, should fail upon init + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, false); EXPECT_FATAL_ERROR(dut.init(ETB_Throttle1, nullptr, nullptr, nullptr, true)); @@ -186,11 +187,32 @@ TEST(etb, initializationNotRedundantPedal) { Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, false); // Normal redundant TPS + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); EXPECT_FATAL_ERROR(dut.init(ETB_Throttle1, nullptr, nullptr, nullptr, true)); } +TEST(etb, initializationNoPrimarySensor) { + Sensor::resetAllMocks(); + + EtbController dut; + + // Needs pedal for init + Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); + + // Redundant, but no primary configured + Sensor::setMockValue(SensorType::Tps1, 0.0f, true); + + EXPECT_FALSE(dut.init(ETB_Throttle1, nullptr, nullptr, nullptr, true)); + + // Now configure primary TPS + Sensor::setMockValue(SensorType::Tps1Primary, 0); + + // With primary TPS, should return true (ie, throttle was configured) + EXPECT_TRUE(dut.init(ETB_Throttle1, nullptr, nullptr, nullptr, true)); +} + TEST(etb, initializationNoThrottles) { // This tests the case where you don't want an ETB, and expect everything to go fine EtbController duts[2]; @@ -209,6 +231,7 @@ TEST(etb, initializationNoThrottles) { Sensor::resetMockValue(SensorType::AcceleratorPedal); // Not redundant TPS (aka cable throttle) + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, false); EXPECT_NO_FATAL_ERROR(doInitElectronicThrottle(PASS_ENGINE_PARAMETER_SIGNATURE)); @@ -251,6 +274,7 @@ TEST(etb, testSetpointOnlyPedal) { EXPECT_EQ(etb.getSetpoint(), unexpected); // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -296,6 +320,7 @@ TEST(etb, setpointIdle) { engineConfiguration->etbIdleThrottleRange = 0; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -353,6 +378,7 @@ TEST(etb, setpointRevLimit) { engineConfiguration->etbRevLimitRange = 750; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -482,6 +508,7 @@ TEST(etb, setOutputInvalid) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); // Redundant TPS & accelerator pedal required for init + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0, true); @@ -502,6 +529,7 @@ TEST(etb, setOutputValid) { StrictMock motor; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -522,6 +550,7 @@ TEST(etb, setOutputValid2) { StrictMock motor; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -542,6 +571,7 @@ TEST(etb, setOutputOutOfRangeHigh) { StrictMock motor; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -562,6 +592,7 @@ TEST(etb, setOutputOutOfRangeLow) { StrictMock motor; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -582,6 +613,7 @@ TEST(etb, setOutputPauseControl) { StrictMock motor; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -603,6 +635,7 @@ TEST(etb, setOutputLimpHome) { StrictMock motor; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -626,6 +659,7 @@ TEST(etb, closedLoopPid) { pid.minValue = -60; // Must have TPS & PPS initialized for ETB setup + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0.0f, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0.0f, true); @@ -651,6 +685,7 @@ TEST(etb, openLoopThrottle) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); // Redundant TPS & accelerator pedal required for init + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0, true); @@ -673,6 +708,7 @@ TEST(etb, openLoopNonThrottle) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); // Redundant TPS & accelerator pedal required for init + Sensor::setMockValue(SensorType::Tps1Primary, 0); Sensor::setMockValue(SensorType::Tps1, 0, true); Sensor::setMockValue(SensorType::AcceleratorPedal, 0, true);