diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index c92863908f..b044fb7d91 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -595,7 +595,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ #if EFI_SHAFT_POSITION_INPUT // 248 - tsOutputChannels->vvtPosition = engine->triggerCentral.getVVTPosition(); + tsOutputChannels->vvtPosition = engine->triggerCentral.getVVTPosition(0, 0); #endif // 252 diff --git a/firmware/controllers/actuators/vvt.cpp b/firmware/controllers/actuators/vvt.cpp index 860564785e..6e6dc41c4f 100644 --- a/firmware/controllers/actuators/vvt.cpp +++ b/firmware/controllers/actuators/vvt.cpp @@ -27,9 +27,14 @@ static fsio8_Map3D_u8t vvtTable2("vvt#2"); static Logging *logger; -void VvtController::init(int index, const ValueProvider3D* targetMap) { +void VvtController::init(int index, int bankIndex, int camIndex, const ValueProvider3D* targetMap) { this->index = index; - m_pid.initPidClass(&CONFIG(auxPid[index])); + m_bank = bankIndex; + m_cam = camIndex; + + // Use the same settings for the Nth cam in every bank (ie, all exhaust cams use the same PID) + m_pid.initPidClass(&CONFIG(auxPid[camIndex])); + m_targetMap = targetMap; } @@ -47,7 +52,7 @@ void VvtController::PeriodicTask() { } expected VvtController::observePlant() const { - return engine->triggerCentral.getVVTPosition(); + return engine->triggerCentral.getVVTPosition(m_bank, m_cam); } expected VvtController::getSetpoint() const { @@ -138,10 +143,10 @@ void initAuxPid(Logging *sharedLogger) { for (int i = 0;i < CAM_INPUTS_COUNT;i++) { INJECT_ENGINE_REFERENCE(&instances[i]); - // TODO: this is wrong for cams 3/4 - int indexInBank = i % CAMS_PER_BANK; - auto targetMap = indexInBank == 0 ? &vvtTable1 : &vvtTable2; - instances[i].init(i, targetMap); + int camIndex = i % CAMS_PER_BANK; + int bankIndex = i / CAMS_PER_BANK; + auto targetMap = camIndex == 0 ? &vvtTable1 : &vvtTable2; + instances[i].init(i, bankIndex, camIndex, targetMap); } startVvtControlPins(); diff --git a/firmware/controllers/actuators/vvt.h b/firmware/controllers/actuators/vvt.h index 6f9943dfb3..75f531db55 100644 --- a/firmware/controllers/actuators/vvt.h +++ b/firmware/controllers/actuators/vvt.h @@ -24,7 +24,7 @@ class VvtController : public PeriodicTimerController, public ClosedLoopControlle public: DECLARE_ENGINE_PTR; - void init(int index, const ValueProvider3D* targetMap); + void init(int index, int bankIndex, int camIndex, const ValueProvider3D* targetMap); // PeriodicTimerController implementation int getPeriodMs() override; @@ -43,6 +43,12 @@ private: const ValueProvider3D* m_targetMap = nullptr; int index = 0; + // Bank index, 0 or 1 + uint8_t m_bank = 0; + + // Cam index, 0 = intake, 1 = exhaust + uint8_t m_cam = 0; + public: // todo: encapsulate or inject these SimplePwm m_pwm; diff --git a/firmware/controllers/can/can_verbose.cpp b/firmware/controllers/can/can_verbose.cpp index 39674d021d..726111dbe4 100644 --- a/firmware/controllers/can/can_verbose.cpp +++ b/firmware/controllers/can/can_verbose.cpp @@ -120,7 +120,7 @@ struct Sensors2 { static void populateFrame(Sensors2& msg) { msg.afr = Sensor::get(SensorType::Lambda1).value_or(0) * 14.7f; msg.oilPressure = Sensor::get(SensorType::OilPressure).value_or(-1); - msg.vvtPos = engine->triggerCentral.getVVTPosition(); + msg.vvtPos = engine->triggerCentral.getVVTPosition(0, 0); msg.vbatt = Sensor::get(SensorType::BatteryVoltage).value_or(0); } diff --git a/firmware/controllers/core/fsio_impl.cpp b/firmware/controllers/core/fsio_impl.cpp index f8ca7bd0a6..8b599c1400 100644 --- a/firmware/controllers/core/fsio_impl.cpp +++ b/firmware/controllers/core/fsio_impl.cpp @@ -138,8 +138,9 @@ FsioResult getEngineValue(le_action_e action DECLARE_ENGINE_PARAMETER_SUFFIX) { return Sensor::get(SensorType::Map).value_or(0); #if EFI_SHAFT_POSITION_INPUT case LE_METHOD_INTAKE_VVT: + return engine->triggerCentral.getVVTPosition(0, 0); case LE_METHOD_EXHAUST_VVT: - return engine->triggerCentral.getVVTPosition(); + return engine->triggerCentral.getVVTPosition(0, 1); #endif case LE_METHOD_TIME_SINCE_TRIGGER_EVENT: return engine->triggerCentral.getTimeSinceTriggerEvent(getTimeNowNt()); diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index ad576353b2..77a72fe5a8 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -79,8 +79,8 @@ EXTERN_ENGINE; static Logging *logger; -angle_t TriggerCentral::getVVTPosition() { - return vvtPosition[0][0]; +angle_t TriggerCentral::getVVTPosition(uint8_t bankIndex, uint8_t camIndex) { + return vvtPosition[bankIndex][camIndex]; } #define miataNbIndex (0) @@ -89,7 +89,8 @@ static bool vvtWithRealDecoder(vvt_mode_e vvtMode) { return vvtMode == VVT_MIATA_NB2 || vvtMode == VVT_BOSCH_QUICK_START || vvtMode == VVT_FORD_ST170 - || vvtMode == VVT_4_1; + || vvtMode == VVT_4_1 + || vvtMode == VVT_BARRA_3_PLUS_1; } void hwHandleVvtCamSignal(trigger_value_e front, efitick_t nowNt, int index DECLARE_ENGINE_PARAMETER_SUFFIX) { diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index c0087df243..76aea14e83 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -57,7 +57,7 @@ public: TriggerNoiseFilter noiseFilter; trigger_type_e vvtTriggerType[CAMS_PER_BANK]; - angle_t getVVTPosition(); + angle_t getVVTPosition(uint8_t bankIndex, uint8_t camIndex); #if EFI_UNIT_TEST // latest VVT event position (could be not synchronization event) diff --git a/unit_tests/tests/test_vvt.cpp b/unit_tests/tests/test_vvt.cpp index 02d502b5b5..f8bdf07422 100644 --- a/unit_tests/tests/test_vvt.cpp +++ b/unit_tests/tests/test_vvt.cpp @@ -19,7 +19,7 @@ TEST(Vvt, setpoint) { VvtController dut; INJECT_ENGINE_REFERENCE(&dut); - dut.init(0, &targetMap); + dut.init(0, 0, 0, &targetMap); // Test dut EXPECT_EQ(20, dut.getSetpoint().value_or(0)); @@ -32,7 +32,7 @@ TEST(Vvt, observePlant) { VvtController dut; INJECT_ENGINE_REFERENCE(&dut); - dut.init(0, nullptr); + dut.init(0, 0, 0, nullptr); EXPECT_EQ(23, dut.observePlant().value_or(0)); } diff --git a/unit_tests/tests/trigger/test_cam_vvt_input.cpp b/unit_tests/tests/trigger/test_cam_vvt_input.cpp index ad736cd9bc..6b1b8c34e6 100644 --- a/unit_tests/tests/trigger/test_cam_vvt_input.cpp +++ b/unit_tests/tests/trigger/test_cam_vvt_input.cpp @@ -114,7 +114,7 @@ TEST(trigger, testCamInput) { // asserting that error code has cleared ASSERT_EQ(0, unitTestWarningCodeState.recentWarnings.getCount()) << "warningCounter#testCamInput #3"; - ASSERT_NEAR(720 - 181, engine->triggerCentral.getVVTPosition(), EPS3D); + ASSERT_NEAR(720 - 181, engine->triggerCentral.getVVTPosition(0, 0), EPS3D); } TEST(sensors, testNB2CamInput) { @@ -153,14 +153,14 @@ TEST(sensors, testNB2CamInput) { // this second important front would give us first real VVT gap duration hwHandleVvtCamSignal(TV_RISE, getTimeNowNt(), 0 PASS_ENGINE_PARAMETER_SUFFIX); - ASSERT_FLOAT_EQ(0, engine->triggerCentral.getVVTPosition()); + ASSERT_FLOAT_EQ(0, engine->triggerCentral.getVVTPosition(0, 0)); ASSERT_EQ(totalRevolutionCountBeforeVvtSync, engine->triggerCentral.triggerState.getTotalRevolutionCounter()); eth.moveTimeForwardUs(MS2US(130)); // this third important front would give us first comparison between two real gaps hwHandleVvtCamSignal(TV_RISE, getTimeNowNt(), 0 PASS_ENGINE_PARAMETER_SUFFIX); - ASSERT_NEAR(-67.6 - 720 - 720, engine->triggerCentral.getVVTPosition(), EPS3D); + ASSERT_NEAR(-67.6 - 720 - 720, engine->triggerCentral.getVVTPosition(0, 0), EPS3D); // actually position based on VVT! ASSERT_EQ(totalRevolutionCountBeforeVvtSync + 2, engine->triggerCentral.triggerState.getTotalRevolutionCounter()); } diff --git a/unit_tests/tests/trigger/test_quad_cam.cpp b/unit_tests/tests/trigger/test_quad_cam.cpp index 1a7f2a09d0..9b188b99cc 100644 --- a/unit_tests/tests/trigger/test_quad_cam.cpp +++ b/unit_tests/tests/trigger/test_quad_cam.cpp @@ -53,14 +53,14 @@ TEST(trigger, testQuadCam) { // this second important front would give us first real VVT gap duration hwHandleVvtCamSignal(TV_RISE, getTimeNowNt(), secondCam PASS_ENGINE_PARAMETER_SUFFIX); - ASSERT_FLOAT_EQ(0, engine->triggerCentral.getVVTPosition()); + ASSERT_FLOAT_EQ(0, engine->triggerCentral.getVVTPosition(0, 0)); ASSERT_EQ(totalRevolutionCountBeforeVvtSync, engine->triggerCentral.triggerState.getTotalRevolutionCounter()); eth.moveTimeForwardUs(MS2US(130 / d)); // this third important front would give us first comparison between two real gaps hwHandleVvtCamSignal(TV_RISE, getTimeNowNt(), secondCam PASS_ENGINE_PARAMETER_SUFFIX); - ASSERT_NEAR(-67.6 - 720 - 720 + 160.2, engine->triggerCentral.vvtPosition[0][secondCam], EPS3D); + ASSERT_NEAR(-67.6 - 720 - 720 + 160.2, engine->triggerCentral.getVVTPosition(0, 1), EPS3D); // actually position based on VVT! ASSERT_EQ(totalRevolutionCountBeforeVvtSync, engine->triggerCentral.triggerState.getTotalRevolutionCounter()); @@ -78,14 +78,14 @@ TEST(trigger, testQuadCam) { // this second important front would give us first real VVT gap duration hwHandleVvtCamSignal(TV_RISE, getTimeNowNt(), secondCamSecondBank PASS_ENGINE_PARAMETER_SUFFIX); - ASSERT_FLOAT_EQ(0, engine->triggerCentral.getVVTPosition()); + ASSERT_FLOAT_EQ(0, engine->triggerCentral.getVVTPosition(0, 0)); ASSERT_EQ(totalRevolutionCountBeforeVvtSync, engine->triggerCentral.triggerState.getTotalRevolutionCounter()); eth.moveTimeForwardUs(MS2US(130 / d)); // this third important front would give us first comparison between two real gaps hwHandleVvtCamSignal(TV_RISE, getTimeNowNt(), secondCamSecondBank PASS_ENGINE_PARAMETER_SUFFIX); - ASSERT_NEAR(-2571.4, engine->triggerCentral.vvtPosition[secondBank][secondCam], EPS3D); + ASSERT_NEAR(-2571.4, engine->triggerCentral.getVVTPosition(secondBank, secondCam), EPS3D); // actually position based on VVT! ASSERT_EQ(totalRevolutionCountBeforeVvtSync, engine->triggerCentral.triggerState.getTotalRevolutionCounter());