mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Clean up PIDController interface in preparation for ProfiledPIDController
This commit is contained in:
committed by
Peter Johnson
parent
fdc098267e
commit
fc98a79dbb
@@ -50,8 +50,7 @@ class PIDInputOutputTest {
|
||||
@Test
|
||||
void continuousInputTest() {
|
||||
m_controller.setP(1);
|
||||
m_controller.setInputRange(-180, 180);
|
||||
m_controller.setContinuous(true);
|
||||
m_controller.enableContinuousInput(-180, 180);
|
||||
|
||||
assertTrue(m_controller.calculate(-179, 179) < 0.0);
|
||||
}
|
||||
|
||||
@@ -18,29 +18,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PIDToleranceTest {
|
||||
private PIDController m_pidController;
|
||||
private static final double m_setpoint = 50.0;
|
||||
private static final double m_tolerance = 10.0;
|
||||
private static final double m_range = 200;
|
||||
|
||||
private static class FakeInput {
|
||||
public double m_val;
|
||||
|
||||
FakeInput() {
|
||||
m_val = 0;
|
||||
}
|
||||
|
||||
public double getMeasurement() {
|
||||
return m_val;
|
||||
}
|
||||
}
|
||||
|
||||
private FakeInput m_inp;
|
||||
private static final double kSetpoint = 50.0;
|
||||
private static final double kTolerance = 10.0;
|
||||
private static final double kRange = 200;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
m_inp = new FakeInput();
|
||||
m_pidController = new PIDController(0.05, 0.0, 0.0);
|
||||
m_pidController.setInputRange(-m_range / 2, m_range / 2);
|
||||
m_pidController.setInputRange(-kRange / 2, kRange / 2);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@@ -51,60 +36,56 @@ class PIDToleranceTest {
|
||||
|
||||
@Test
|
||||
void initialToleranceTest() {
|
||||
assertFalse(m_pidController.atSetpoint());
|
||||
assertTrue(m_pidController.atSetpoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
void absoluteToleranceTest() {
|
||||
m_pidController.setAbsoluteTolerance(m_tolerance);
|
||||
m_pidController.setSetpoint(m_setpoint);
|
||||
for (int i = 0; i < 50; i++) {
|
||||
m_pidController.calculate(m_inp.getMeasurement());
|
||||
}
|
||||
m_pidController.setAbsoluteTolerance(kTolerance);
|
||||
m_pidController.setSetpoint(kSetpoint);
|
||||
|
||||
m_pidController.calculate(0.0);
|
||||
|
||||
assertFalse(m_pidController.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + m_pidController
|
||||
.getError());
|
||||
m_inp.m_val = m_setpoint + m_tolerance / 2;
|
||||
for (int i = 0; i < 50; i++) {
|
||||
m_pidController.calculate(m_inp.getMeasurement());
|
||||
}
|
||||
.getPositionError());
|
||||
|
||||
m_pidController.calculate(kSetpoint + kTolerance / 2);
|
||||
|
||||
assertTrue(m_pidController.atSetpoint(),
|
||||
"Error was not in tolerance when it should have been. Error was " + m_pidController
|
||||
.getError());
|
||||
m_inp.m_val = m_setpoint + 10 * m_tolerance;
|
||||
for (int i = 0; i < 50; i++) {
|
||||
m_pidController.calculate(m_inp.getMeasurement());
|
||||
}
|
||||
.getPositionError());
|
||||
|
||||
m_pidController.calculate(kSetpoint + 10 * kTolerance);
|
||||
|
||||
assertFalse(m_pidController.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + m_pidController
|
||||
.getError());
|
||||
.getPositionError());
|
||||
}
|
||||
|
||||
@Test
|
||||
void percentToleranceTest() {
|
||||
m_pidController.setPercentTolerance(m_tolerance);
|
||||
m_pidController.setSetpoint(m_setpoint);
|
||||
for (int i = 0; i < 50; i++) {
|
||||
m_pidController.calculate(m_inp.getMeasurement());
|
||||
}
|
||||
m_pidController.setPercentTolerance(kTolerance);
|
||||
m_pidController.setSetpoint(kSetpoint);
|
||||
|
||||
m_pidController.calculate(0.0);
|
||||
|
||||
assertFalse(m_pidController.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + m_pidController
|
||||
.getError());
|
||||
//half of percent tolerance away from setPoint
|
||||
m_inp.m_val = m_setpoint + m_tolerance / 200 * m_range;
|
||||
for (int i = 0; i < 50; i++) {
|
||||
m_pidController.calculate(m_inp.getMeasurement());
|
||||
}
|
||||
.getPositionError());
|
||||
|
||||
// Half of percent tolerance away from setpoint
|
||||
m_pidController.calculate(kSetpoint + (kTolerance / 2) / 100 * kRange);
|
||||
|
||||
assertTrue(m_pidController.atSetpoint(),
|
||||
"Error was not in tolerance when it should have been. Error was " + m_pidController
|
||||
.getError());
|
||||
//double percent tolerance away from setPoint
|
||||
m_inp.m_val = m_setpoint + m_tolerance / 50 * m_range;
|
||||
for (int i = 0; i < 50; i++) {
|
||||
m_pidController.calculate(m_inp.getMeasurement());
|
||||
}
|
||||
.getPositionError());
|
||||
|
||||
// Double percent tolerance away from setpoint
|
||||
m_pidController.calculate(kSetpoint + (kTolerance * 2) / 100 * kRange);
|
||||
|
||||
assertFalse(m_pidController.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + m_pidController
|
||||
.getError());
|
||||
.getPositionError());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user