2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-06-03 10:07:08 -04:00
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.wpilibj.fixtures.FakeEncoderFixture;
|
|
|
|
|
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
|
|
|
|
|
import edu.wpi.first.wpilibj.test.TestBench;
|
2018-05-24 00:31:04 -04:00
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.logging.Logger;
|
2014-06-03 10:07:08 -04:00
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.junit.runners.Parameterized;
|
|
|
|
|
import org.junit.runners.Parameterized.Parameters;
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Test to see if the FPGA properly recognizes a mock Encoder input. */
|
2014-06-03 10:07:08 -04:00
|
|
|
@RunWith(Parameterized.class)
|
|
|
|
|
public class EncoderTest extends AbstractComsSetup {
|
2015-06-25 15:07:55 -04:00
|
|
|
private static final Logger logger = Logger.getLogger(EncoderTest.class.getName());
|
|
|
|
|
private static FakeEncoderFixture encoder = null;
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
private final boolean m_flip; // Does this test need to flip the inputs
|
|
|
|
|
private final int m_inputA;
|
|
|
|
|
private final int m_inputB;
|
|
|
|
|
private final int m_outputA;
|
|
|
|
|
private final int m_outputB;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Logger getClassLogger() {
|
|
|
|
|
return logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Test data generator. This method is called the the JUnit parametrized test runner and returns a
|
|
|
|
|
* Collection of Arrays. For each Array in the Collection, each array element corresponds to a
|
2016-05-20 12:07:40 -04:00
|
|
|
* parameter in the constructor.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
@Parameters
|
|
|
|
|
public static Collection<Integer[]> generateData() {
|
2021-06-15 23:06:03 -07:00
|
|
|
return TestBench.getEncoderDIOCrossConnectCollection();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Constructs a parametrized Encoder Test.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param inputA The port number for inputA
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param outputA The port number for outputA
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param inputB The port number for inputB
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param outputB The port number for outputB
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param flip whether or not these set of values require the encoder to be reversed (0 or 1)
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public EncoderTest(int inputA, int outputA, int inputB, int outputB, int flip) {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_inputA = inputA;
|
|
|
|
|
m_inputB = inputB;
|
|
|
|
|
m_outputA = outputA;
|
|
|
|
|
m_outputB = outputB;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// If the encoder from a previous test is allocated then we must free its
|
|
|
|
|
// members
|
2016-05-20 12:07:40 -04:00
|
|
|
if (encoder != null) {
|
2015-06-25 15:07:55 -04:00
|
|
|
encoder.teardown();
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
|
|
|
|
m_flip = flip == 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
encoder = new FakeEncoderFixture(inputA, outputA, inputB, outputB);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
2019-08-01 01:19:48 -04:00
|
|
|
public static void tearDownAfterClass() {
|
2015-06-25 15:07:55 -04:00
|
|
|
encoder.teardown();
|
|
|
|
|
encoder = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Sets up the test and verifies that the test was reset to the default state. */
|
2015-06-25 15:07:55 -04:00
|
|
|
@Before
|
2019-08-01 01:19:48 -04:00
|
|
|
public void setUp() {
|
2015-06-25 15:07:55 -04:00
|
|
|
encoder.setup();
|
|
|
|
|
testDefaultState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
2019-08-01 01:19:48 -04:00
|
|
|
public void tearDown() {
|
2015-06-25 15:07:55 -04:00
|
|
|
encoder.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Tests to see if Encoders initialize to zero. */
|
2015-06-25 15:07:55 -04:00
|
|
|
@Test
|
|
|
|
|
public void testDefaultState() {
|
|
|
|
|
int value = encoder.getEncoder().get();
|
|
|
|
|
assertTrue(errorMessage(0, value), value == 0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Tests to see if Encoders can count up successfully. */
|
2015-06-25 15:07:55 -04:00
|
|
|
@Test
|
|
|
|
|
public void testCountUp() {
|
|
|
|
|
int goal = 100;
|
|
|
|
|
encoder.getFakeEncoderSource().setCount(goal);
|
2016-05-20 12:07:40 -04:00
|
|
|
encoder.getFakeEncoderSource().setForward(m_flip);
|
2015-06-25 15:07:55 -04:00
|
|
|
encoder.getFakeEncoderSource().execute();
|
|
|
|
|
int value = encoder.getEncoder().get();
|
|
|
|
|
assertTrue(errorMessage(goal, value), value == goal);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Tests to see if Encoders can count down successfully. */
|
2015-06-25 15:07:55 -04:00
|
|
|
@Test
|
|
|
|
|
public void testCountDown() {
|
|
|
|
|
int goal = -100;
|
|
|
|
|
encoder.getFakeEncoderSource().setCount(goal); // Goal has to be positive
|
2016-05-20 12:07:40 -04:00
|
|
|
encoder.getFakeEncoderSource().setForward(!m_flip);
|
2015-06-25 15:07:55 -04:00
|
|
|
encoder.getFakeEncoderSource().execute();
|
|
|
|
|
int value = encoder.getEncoder().get();
|
|
|
|
|
assertTrue(errorMessage(goal, value), value == goal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Creates a simple message with the error that was encountered for the Encoders.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param goal The goal that was trying to be reached
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param trueValue The actual value that was reached by the test
|
2016-05-20 12:07:40 -04:00
|
|
|
* @return A fully constructed message with data about where and why the the test failed.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
private String errorMessage(int goal, int trueValue) {
|
2020-12-29 22:45:16 -08:00
|
|
|
return "Encoder ({In,Out}): {"
|
|
|
|
|
+ m_inputA
|
|
|
|
|
+ ", "
|
|
|
|
|
+ m_outputA
|
|
|
|
|
+ "},{"
|
|
|
|
|
+ m_inputB
|
|
|
|
|
+ ", "
|
|
|
|
|
+ m_outputB
|
|
|
|
|
+ "} Returned: "
|
|
|
|
|
+ trueValue
|
|
|
|
|
+ ", Wanted: "
|
|
|
|
|
+ goal;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-06-03 10:07:08 -04:00
|
|
|
}
|