fix inverted vvt (#4464)

* fix inverted mode

* unit tests are great
This commit is contained in:
Matthew Kennedy 2022-08-20 17:12:32 -07:00 committed by GitHub
parent b449e85bae
commit 3e4965ea27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 8 deletions

View File

@ -82,6 +82,12 @@ static bool shouldInvertVvt(int camIndex) {
}
expected<percent_t> VvtController::getClosedLoop(angle_t target, angle_t observation) {
// User labels say "advance" and "retard"
// "advance" means that additional solenoid duty makes indicated VVT position more positive
// "retard" means that additional solenoid duty makes indicated VVT position more negative
bool isInverted = shouldInvertVvt(m_cam);
m_pid.setErrorAmplification(isInverted ? -1.0f : 1.0f);
float retVal = m_pid.getOutput(target, observation);
if (engineConfiguration->isVerboseAuxPid1) {
@ -93,14 +99,7 @@ expected<percent_t> VvtController::getClosedLoop(angle_t target, angle_t observa
m_pid.postState(engine->outputChannels.vvtStatus[index]);
#endif /* EFI_TUNER_STUDIO */
// User labels say "advance" and "retard"
// "advance" means that additional solenoid duty makes indicated VVT position more positive
// "retard" means that additional solenoid duty makes indicated VVT position more negative
if (shouldInvertVvt(m_cam)) {
return -retVal;
} else {
return retVal;
}
return retVal;
}
void VvtController::setOutput(expected<percent_t> outputValue) {

View File

@ -41,3 +41,34 @@ TEST(Vvt, openLoop) {
// No open loop for now
EXPECT_EQ(dut.getOpenLoop(10), 0);
}
TEST(Vvt, ClosedLoopNotInverted) {
EngineTestHelper eth(TEST_ENGINE);
VvtController dut;
dut.init(0, 0, 0, nullptr);
engineConfiguration->auxPid[0].pFactor = 1.5f;
engineConfiguration->auxPid[0].iFactor = 0;
engineConfiguration->auxPid[0].dFactor = 0;
engineConfiguration->auxPid[0].offset = 0;
// Target of 30 with position 20 should yield positive duty, P=1.5 means 15% duty for 10% error
EXPECT_EQ(dut.getClosedLoop(30, 20).value_or(0), 15);
}
TEST(Vvt, ClosedLoopInverted) {
EngineTestHelper eth(TEST_ENGINE);
VvtController dut;
dut.init(0, 0, 0, nullptr);
engineConfiguration->invertVvtControlIntake = true;
engineConfiguration->auxPid[0].pFactor = 1.5f;
engineConfiguration->auxPid[0].iFactor = 0;
engineConfiguration->auxPid[0].dFactor = 0;
engineConfiguration->auxPid[0].offset = 0;
// Target of -30 with position -20 should yield positive duty, P=1.5 means 15% duty for 10% error
EXPECT_EQ(dut.getClosedLoop(-30, -20).value_or(0), 15);
}