test(pid): add regression test for derivative-on-measurement sign (#27153)

PIDTest never exercised the D path with a non-zero gain, which let the
sign error fixed in #27140 slip through. Add a D-only test that asserts
the output opposes a known feedback ramp.

Fails on main, passes with #27140.
This commit is contained in:
Jacob Dahl
2026-04-20 21:07:47 -08:00
committed by GitHub
parent 1a689214cd
commit 68083e6ba8
+21
View File
@@ -128,3 +128,24 @@ TEST(PIDTest, InteralOpenLoop)
EXPECT_FLOAT_EQ(pid.update(0.f, 0.1f, true), 0.f);
EXPECT_FLOAT_EQ(pid.update(0.f, 0.1f, true), -.01f);
}
TEST(PIDTest, DerivativeOnlyDampsFeedbackMotion)
{
// Derivative-on-measurement: the D term must oppose feedback motion so the
// output damps the plant rather than reinforcing its movement.
PID pid;
pid.setOutputLimit(100.f);
pid.setGains(0.f, 0.f, 1.f);
pid.setSetpoint(0.f);
// First sample seeds _last_feedback; derivative contribution is zero.
EXPECT_FLOAT_EQ(pid.update(0.f, 0.1f, false), 0.f);
// Feedback rises by 1.0 over dt = 0.1 -> d(feedback)/dt = +10.
// Expected output = -Kd * d(feedback)/dt = -10.
EXPECT_FLOAT_EQ(pid.update(1.f, 0.1f, false), -10.f);
// Feedback falls by 1.0 over dt = 0.1 -> d(feedback)/dt = -10.
// Expected output = +10 (still damping).
EXPECT_FLOAT_EQ(pid.update(0.f, 0.1f, false), 10.f);
}