mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
Remove percent tolerance from PID controller
It breaks the unit system badly; the tolerance member variable has different units depending on percent vs absolute. Absolute tolerance is a lot more natural than percent tolerance anyway.
This commit is contained in:
committed by
Peter Johnson
parent
0ca8d667d2
commit
ff8b8f0a8a
@@ -33,18 +33,6 @@ class PIDInputOutputTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputRangeTest() {
|
||||
m_controller.setP(1);
|
||||
m_controller.setOutputRange(-1000, 1000);
|
||||
m_controller.setInputRange(-50, 50);
|
||||
|
||||
assertAll(
|
||||
() -> assertEquals(-100, m_controller.calculate(100, 0), 1e-5),
|
||||
() -> assertEquals(50, m_controller.calculate(0, 100), 1e-5)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void continuousInputTest() {
|
||||
m_controller.setP(1);
|
||||
|
||||
@@ -7,83 +7,48 @@
|
||||
|
||||
package edu.wpi.first.wpilibj.controller;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PIDToleranceTest {
|
||||
private PIDController m_pidController;
|
||||
private static final double kSetpoint = 50.0;
|
||||
private static final double kTolerance = 10.0;
|
||||
private static final double kRange = 200;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
m_pidController = new PIDController(0.05, 0.0, 0.0);
|
||||
m_pidController.setInputRange(-kRange / 2, kRange / 2);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
m_pidController.close();
|
||||
m_pidController = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialToleranceTest() {
|
||||
assertTrue(m_pidController.atSetpoint());
|
||||
var controller = new PIDController(0.05, 0.0, 0.0);
|
||||
controller.enableContinuousInput(-kRange / 2, kRange / 2);
|
||||
|
||||
assertTrue(controller.atSetpoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
void absoluteToleranceTest() {
|
||||
m_pidController.setAbsoluteTolerance(kTolerance);
|
||||
m_pidController.setSetpoint(kSetpoint);
|
||||
var controller = new PIDController(0.05, 0.0, 0.0);
|
||||
controller.enableContinuousInput(-kRange / 2, kRange / 2);
|
||||
|
||||
m_pidController.calculate(0.0);
|
||||
controller.setTolerance(kTolerance);
|
||||
controller.setSetpoint(kSetpoint);
|
||||
|
||||
assertFalse(m_pidController.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + m_pidController
|
||||
controller.calculate(0.0);
|
||||
|
||||
assertFalse(controller.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + controller
|
||||
.getPositionError());
|
||||
|
||||
m_pidController.calculate(kSetpoint + kTolerance / 2);
|
||||
controller.calculate(kSetpoint + kTolerance / 2);
|
||||
|
||||
assertTrue(m_pidController.atSetpoint(),
|
||||
"Error was not in tolerance when it should have been. Error was " + m_pidController
|
||||
assertTrue(controller.atSetpoint(),
|
||||
"Error was not in tolerance when it should have been. Error was " + controller
|
||||
.getPositionError());
|
||||
|
||||
m_pidController.calculate(kSetpoint + 10 * kTolerance);
|
||||
controller.calculate(kSetpoint + 10 * kTolerance);
|
||||
|
||||
assertFalse(m_pidController.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + m_pidController
|
||||
.getPositionError());
|
||||
}
|
||||
|
||||
@Test
|
||||
void percentToleranceTest() {
|
||||
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
|
||||
.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
|
||||
.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
|
||||
assertFalse(controller.atSetpoint(),
|
||||
"Error was in tolerance when it should not have been. Error was " + controller
|
||||
.getPositionError());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user