def test_absolute_tolerance(pid_controller: PIDController): setpoint = 50 tolerance = 10 pid_controller.setTolerance(tolerance) pid_controller.setSetpoint(setpoint) pid_controller.calculate(0) assert not pid_controller.atSetpoint(), ( "Error was in tolerance when it should not have been. Error was %f" % pid_controller.getPositionError()) measurement = setpoint + tolerance / 2 pid_controller.calculate(measurement) assert pid_controller.atSetpoint(), ( "Error was not in tolerance when it should have been. Error was %f" % pid_controller.getPositionError()) measurement = setpoint + 10 * tolerance pid_controller.calculate(measurement) assert not pid_controller.atSetpoint(), ( "Error was in tolerance when it should not have been. Error was %f" % pid_controller.getPositionError())
def test_pidcontroller_calculate_rate7(input, setpoint, expected_error, expected_output): pid = PIDController(0.1, 0, 0.075) pid.enableContinuousInput(-180.0, 180.0) pid.setIntegratorRange(-1, 1) pid.setSetpoint(setpoint) out = pid.calculate(input) assert pid.getPositionError() == pytest.approx(expected_error, 0.01) # assert out == pytest.approx(expected_output, 0.01) assert out != 0