2014-06-02 18:05:15 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2014-06-02 18:05:15 -04:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* the project. */
|
2014-06-02 18:05:15 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-06-02 18:05:15 -04:00
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.junit.runners.Parameterized;
|
|
|
|
|
import org.junit.runners.Parameterized.Parameters;
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2017-08-28 00:32:53 -07:00
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
2014-06-02 18:05:15 -04:00
|
|
|
import edu.wpi.first.wpilibj.fixtures.MotorEncoderFixture;
|
2017-12-04 23:28:33 -08:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilderImpl;
|
2014-06-02 18:05:15 -04:00
|
|
|
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
|
|
|
|
|
import edu.wpi.first.wpilibj.test.TestBench;
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
import static org.hamcrest.CoreMatchers.is;
|
|
|
|
|
import static org.hamcrest.CoreMatchers.not;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
2014-06-02 18:05:15 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Test that covers the {@link PIDController}.
|
2014-06-02 18:05:15 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@RunWith(Parameterized.class)
|
|
|
|
|
public class PIDTest extends AbstractComsSetup {
|
2015-06-25 15:07:55 -04:00
|
|
|
private static final Logger logger = Logger.getLogger(PIDTest.class.getName());
|
2016-05-20 12:07:40 -04:00
|
|
|
private NetworkTable m_table;
|
2017-12-04 23:28:33 -08:00
|
|
|
private SendableBuilderImpl m_builder;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
private static final double absoluteTolerance = 50;
|
2016-01-28 14:25:39 -05:00
|
|
|
private static final double outputRange = 0.25;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
private PIDController m_controller = null;
|
2015-06-25 15:07:55 -04:00
|
|
|
private static MotorEncoderFixture me = null;
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings({"MemberName", "EmptyLineSeparator", "MultipleVariableDeclarations"})
|
2015-06-25 15:07:55 -04:00
|
|
|
private final Double k_p, k_i, k_d;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Logger getClassLogger() {
|
|
|
|
|
return logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings({"ParameterName", "JavadocMethod"})
|
2015-06-25 15:07:55 -04:00
|
|
|
public PIDTest(Double p, Double i, Double d, MotorEncoderFixture mef) {
|
|
|
|
|
logger.fine("Constructor with: " + mef.getType());
|
2016-05-20 12:07:40 -04:00
|
|
|
if (PIDTest.me != null && !PIDTest.me.equals(mef)) {
|
2015-06-25 15:07:55 -04:00
|
|
|
PIDTest.me.teardown();
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
PIDTest.me = mef;
|
|
|
|
|
this.k_p = p;
|
|
|
|
|
this.k_i = i;
|
|
|
|
|
this.k_d = d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Parameters
|
|
|
|
|
public static Collection<Object[]> generateData() {
|
|
|
|
|
// logger.fine("Loading the MotorList");
|
|
|
|
|
Collection<Object[]> data = new ArrayList<Object[]>();
|
|
|
|
|
double kp = 0.001;
|
|
|
|
|
double ki = 0.0005;
|
|
|
|
|
double kd = 0.0;
|
|
|
|
|
for (int i = 0; i < 1; i++) {
|
2016-05-20 12:07:40 -04:00
|
|
|
data.addAll(Arrays.asList(new Object[][]{
|
2015-06-25 15:07:55 -04:00
|
|
|
{kp, ki, kd, TestBench.getInstance().getTalonPair()},
|
|
|
|
|
{kp, ki, kd, TestBench.getInstance().getVictorPair()},
|
|
|
|
|
{kp, ki, kd, TestBench.getInstance().getJaguarPair()}}));
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
2016-05-20 12:07:40 -04:00
|
|
|
public static void setUpBeforeClass() throws Exception {
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDownAfterClass() throws Exception {
|
|
|
|
|
logger.fine("TearDownAfterClass: " + me.getType());
|
|
|
|
|
me.teardown();
|
|
|
|
|
me = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
|
logger.fine("Setup: " + me.getType());
|
|
|
|
|
me.setup();
|
2017-08-28 00:32:53 -07:00
|
|
|
m_table = NetworkTableInstance.getDefault().getTable("TEST_PID");
|
2017-12-04 23:28:33 -08:00
|
|
|
m_builder = new SendableBuilderImpl();
|
|
|
|
|
m_builder.setTable(m_table);
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller = new PIDController(k_p, k_i, k_d, me.getEncoder(), me.getMotor());
|
2017-12-04 23:28:33 -08:00
|
|
|
m_controller.initSendable(m_builder);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
|
logger.fine("Teardown: " + me.getType());
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.disable();
|
2018-05-22 23:33:17 -07:00
|
|
|
m_controller.close();
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller = null;
|
2015-06-25 15:07:55 -04:00
|
|
|
me.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupAbsoluteTolerance() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.setAbsoluteTolerance(absoluteTolerance);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupOutputRange() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.setOutputRange(-outputRange, outputRange);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInitialSettings() {
|
|
|
|
|
setupAbsoluteTolerance();
|
|
|
|
|
setupOutputRange();
|
|
|
|
|
double setpoint = 2500.0;
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.setSetpoint(setpoint);
|
2017-07-01 01:05:33 -04:00
|
|
|
assertFalse("PID did not begin disabled", m_controller.isEnabled());
|
2016-05-20 12:07:40 -04:00
|
|
|
assertEquals("PID.getError() did not start at " + setpoint, setpoint,
|
|
|
|
|
m_controller.getError(), 0);
|
2017-12-04 23:28:33 -08:00
|
|
|
m_builder.updateTable();
|
2017-08-28 00:32:53 -07:00
|
|
|
assertEquals(k_p, m_table.getEntry("p").getDouble(9999999), 0);
|
|
|
|
|
assertEquals(k_i, m_table.getEntry("i").getDouble(9999999), 0);
|
|
|
|
|
assertEquals(k_d, m_table.getEntry("d").getDouble(9999999), 0);
|
|
|
|
|
assertEquals(setpoint, m_table.getEntry("setpoint").getDouble(9999999), 0);
|
|
|
|
|
assertFalse(m_table.getEntry("enabled").getBoolean(true));
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testRestartAfterEnable() {
|
|
|
|
|
setupAbsoluteTolerance();
|
|
|
|
|
setupOutputRange();
|
|
|
|
|
double setpoint = 2500.0;
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.setSetpoint(setpoint);
|
|
|
|
|
m_controller.enable();
|
2017-12-04 23:28:33 -08:00
|
|
|
m_builder.updateTable();
|
2017-08-28 00:32:53 -07:00
|
|
|
assertTrue(m_table.getEntry("enabled").getBoolean(false));
|
2017-07-01 01:05:33 -04:00
|
|
|
assertTrue(m_controller.isEnabled());
|
2015-06-25 15:07:55 -04:00
|
|
|
assertThat(0.0, is(not(me.getMotor().get())));
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.reset();
|
2017-12-04 23:28:33 -08:00
|
|
|
m_builder.updateTable();
|
2017-08-28 00:32:53 -07:00
|
|
|
assertFalse(m_table.getEntry("enabled").getBoolean(true));
|
2017-07-01 01:05:33 -04:00
|
|
|
assertFalse(m_controller.isEnabled());
|
2015-06-25 15:07:55 -04:00
|
|
|
assertEquals(0, me.getMotor().get(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testSetSetpoint() {
|
|
|
|
|
setupAbsoluteTolerance();
|
|
|
|
|
setupOutputRange();
|
|
|
|
|
Double setpoint = 2500.0;
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.disable();
|
|
|
|
|
m_controller.setSetpoint(setpoint);
|
|
|
|
|
m_controller.enable();
|
|
|
|
|
assertEquals("Did not correctly set set-point", setpoint, new Double(m_controller
|
|
|
|
|
.getSetpoint()));
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(timeout = 10000)
|
|
|
|
|
public void testRotateToTarget() {
|
|
|
|
|
setupAbsoluteTolerance();
|
|
|
|
|
setupOutputRange();
|
2016-01-28 14:25:39 -05:00
|
|
|
double setpoint = 1000.0;
|
2016-05-20 12:07:40 -04:00
|
|
|
assertEquals(pidData() + "did not start at 0", 0, m_controller.get(), 0);
|
|
|
|
|
m_controller.setSetpoint(setpoint);
|
2015-06-25 15:07:55 -04:00
|
|
|
assertEquals(pidData() + "did not have an error of " + setpoint, setpoint,
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.getError(), 0);
|
|
|
|
|
m_controller.enable();
|
2015-06-25 15:07:55 -04:00
|
|
|
Timer.delay(5);
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.disable();
|
|
|
|
|
assertTrue(pidData() + "Was not on Target. Controller Error: " + m_controller.getError(),
|
|
|
|
|
m_controller.onTarget());
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String pidData() {
|
2016-05-20 12:07:40 -04:00
|
|
|
return me.getType() + " PID {P:" + m_controller.getP() + " I:" + m_controller.getI() + " D:"
|
|
|
|
|
+ m_controller.getD() + "} ";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RuntimeException.class)
|
|
|
|
|
public void testOnTargetNoToleranceSet() {
|
|
|
|
|
setupOutputRange();
|
2016-05-20 12:07:40 -04:00
|
|
|
m_controller.onTarget();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-06-02 18:05:15 -04:00
|
|
|
}
|