Applies Google Styleguide to Java parts of the library (#23)

This was partially applied to simulation but
simulation is a bit of a mess and has a lot of duplicated code.
This commit is contained in:
Jonathan Leitschuh
2016-05-20 12:07:40 -04:00
committed by Peter Johnson
parent 64ab6e51fe
commit a834fff7b2
266 changed files with 15574 additions and 14718 deletions

View File

@@ -11,45 +11,46 @@ import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.AnalogOutput;
/**
* @author jonathanleitschuh
* A fixture that connects an {@link AnalogInput} and an {@link AnalogOutput}.
*
* @author jonathanleitschuh
*/
public abstract class AnalogCrossConnectFixture implements ITestFixture {
private boolean initialized = false;
private boolean m_initialized = false;
private AnalogInput input;
private AnalogOutput output;
private AnalogInput m_input;
private AnalogOutput m_output;
abstract protected AnalogInput giveAnalogInput();
protected abstract AnalogInput giveAnalogInput();
abstract protected AnalogOutput giveAnalogOutput();
protected abstract AnalogOutput giveAnalogOutput();
private void initialize() {
synchronized (this) {
if (!initialized) {
input = giveAnalogInput();
output = giveAnalogOutput();
initialized = true;
if (!m_initialized) {
m_input = giveAnalogInput();
m_output = giveAnalogOutput();
m_initialized = true;
}
}
}
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#setup()
*/
@Override
public boolean setup() {
initialize();
output.setVoltage(0);
m_output.setVoltage(0);
return true;
}
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#reset()
*/
@Override
@@ -60,27 +61,27 @@ public abstract class AnalogCrossConnectFixture implements ITestFixture {
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#teardown()
*/
@Override
public boolean teardown() {
input.free();
output.free();
m_input.free();
m_output.free();
return true;
}
/**
* @return Analog Output
* Analog Output.
*/
final public AnalogOutput getOutput() {
public final AnalogOutput getOutput() {
initialize();
return output;
return m_output;
}
final public AnalogInput getInput() {
public final AnalogInput getInput() {
initialize();
return input;
return m_input;
}

View File

@@ -18,18 +18,19 @@ import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.mockhardware.FakePotentiometerSource;
/**
* @author jonathanleitschuh
* A fixture that wraps a {@link CANJaguar}.
*
* @author jonathanleitschuh
*/
public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJaguar> implements
ITestFixture {
private static final Logger logger = Logger.getLogger(CANMotorEncoderFixture.class.getName());
public static final double RELAY_POWER_UP_TIME = .75;
private FakePotentiometerSource potSource;
private DigitalOutput forwardLimit;
private DigitalOutput reverseLimit;
private Relay powerCycler;
private boolean initialized = false;
private FakePotentiometerSource m_potSource;
private DigitalOutput m_forwardLimit;
private DigitalOutput m_reverseLimit;
private Relay m_powerCycler;
private boolean m_initialized = false;
protected abstract FakePotentiometerSource giveFakePotentiometerSource();
@@ -39,24 +40,25 @@ public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJagu
protected abstract Relay givePowerCycleRelay();
public CANMotorEncoderFixture() {}
public CANMotorEncoderFixture() {
}
private void initialize() {
synchronized (this) {
if (!initialized) {
initialized = true;// This ensures it is only initialized once
if (!m_initialized) {
m_initialized = true;// This ensures it is only initialized once
powerCycler = givePowerCycleRelay();
powerCycler.setDirection(Direction.kForward);
m_powerCycler = givePowerCycleRelay();
m_powerCycler.setDirection(Direction.kForward);
logger.fine("Turning on the power!");
powerCycler.set(Value.kForward);
forwardLimit = giveFakeForwardLimit();
reverseLimit = giveFakeReverseLimit();
forwardLimit.set(false);
reverseLimit.set(false);
potSource = giveFakePotentiometerSource();
m_powerCycler.set(Value.kForward);
m_forwardLimit = giveFakeForwardLimit();
m_reverseLimit = giveFakeReverseLimit();
m_forwardLimit.set(false);
m_reverseLimit.set(false);
m_potSource = giveFakePotentiometerSource();
Timer.delay(RELAY_POWER_UP_TIME); // Delay so the relay has time to boot
// up
// up
}
}
}
@@ -71,34 +73,37 @@ public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJagu
@Override
public boolean reset() {
initialize();
potSource.reset();
forwardLimit.set(false);
reverseLimit.set(false);
m_potSource.reset();
m_forwardLimit.set(false);
m_reverseLimit.set(false);
getMotor().setPercentMode(); // Get the Jaguar into a mode where setting the
// speed means stop
// speed means stop
return super.reset();
}
@Override
public boolean teardown() {
boolean wasNull = false;
if (potSource != null) {
potSource.free();
potSource = null;
} else
if (m_potSource != null) {
m_potSource.free();
m_potSource = null;
} else {
wasNull = true;
if (forwardLimit != null) {
forwardLimit.set(false);
forwardLimit.free();
forwardLimit = null;
} else
}
if (m_forwardLimit != null) {
m_forwardLimit.set(false);
m_forwardLimit.free();
m_forwardLimit = null;
} else {
wasNull = true;
if (reverseLimit != null) {
reverseLimit.set(false);
reverseLimit.free();
reverseLimit = null;
} else
}
if (m_reverseLimit != null) {
m_reverseLimit.set(false);
m_reverseLimit.free();
m_reverseLimit = null;
} else {
wasNull = true;
}
boolean superTornDown = false;
try {
superTornDown = super.teardown();
@@ -107,14 +112,16 @@ public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJagu
if (getMotor() != null) {
getMotor().disableControl();
getMotor().free();
} else
} else {
wasNull = true;
}
} finally {
if (powerCycler != null) {
powerCycler.free();
powerCycler = null;
} else
if (m_powerCycler != null) {
m_powerCycler.free();
m_powerCycler = null;
} else {
wasNull = true;
}
}
}
if (wasNull) {
@@ -126,19 +133,22 @@ public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJagu
public FakePotentiometerSource getFakePot() {
initialize();
return potSource;
return m_potSource;
}
public DigitalOutput getForwardLimit() {
initialize();
return forwardLimit;
return m_forwardLimit;
}
public DigitalOutput getReverseLimit() {
initialize();
return reverseLimit;
return m_reverseLimit;
}
/**
* Prints the current status of the fixture.
*/
public String printStatus() {
StringBuilder status = new StringBuilder("CAN Motor Encoder Status: ");
if (getMotor() != null) {
@@ -152,13 +162,13 @@ public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJagu
} else {
status.append("\t" + "CANJaguar Motor = null" + "\n");
}
if (forwardLimit != null) {
status.append("\tForward Limit Output = " + forwardLimit + "\n");
if (m_forwardLimit != null) {
status.append("\tForward Limit Output = " + m_forwardLimit + "\n");
} else {
status.append("\tForward Limit Output = null" + "\n");
}
if (reverseLimit != null) {
status.append("\tReverse Limit Output = " + reverseLimit + "\n");
if (m_reverseLimit != null) {
status.append("\tReverse Limit Output = " + m_reverseLimit + "\n");
} else {
status.append("\tReverse Limit Output = null" + "\n");
}
@@ -166,6 +176,11 @@ public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJagu
return status.toString();
}
/**
* Browns out the fixture for a specific ammount of time.
*
* @param seconds The number of seconds to brown out for.
*/
public void brownOut(double seconds) {
initialize();
powerOff();
@@ -175,12 +190,12 @@ public abstract class CANMotorEncoderFixture extends MotorEncoderFixture<CANJagu
public void powerOff() {
initialize();
powerCycler.set(Value.kOff);
m_powerCycler.set(Value.kOff);
}
public void powerOn() {
initialize();
powerCycler.set(Value.kForward);
m_powerCycler.set(Value.kForward);
}
}

View File

@@ -13,37 +13,54 @@ import java.util.logging.Logger;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DigitalOutput;
/**
* Connects a digital input to a digital output.
*
* @author Jonathan Leitschuh
*/
public class DIOCrossConnectFixture implements ITestFixture {
private static final Logger logger = Logger.getLogger(DIOCrossConnectFixture.class.getName());
private final DigitalInput input;
private final DigitalOutput output;
private final DigitalInput m_input;
private final DigitalOutput m_output;
private boolean m_allocated;
/**
* Constructs using two pre-allocated digital objects.
*
* @param input The input
* @param output The output.
*/
public DIOCrossConnectFixture(DigitalInput input, DigitalOutput output) {
assert input != null;
assert output != null;
this.input = input;
this.output = output;
m_input = input;
m_output = output;
m_allocated = false;
}
/**
* Constructs a {@link DIOCrossConnectFixture} using the ports of the digital objects.
*
* @param input The port of the {@link DigitalInput}
* @param output The port of the {@link DigitalOutput}
*/
public DIOCrossConnectFixture(Integer input, Integer output) {
assert input != null;
assert output != null;
assert !input.equals(output);
this.input = new DigitalInput(input);
this.output = new DigitalOutput(output);
m_input = new DigitalInput(input);
m_output = new DigitalOutput(output);
m_allocated = true;
}
public DigitalInput getInput() {
return input;
return m_input;
}
public DigitalOutput getOutput() {
return output;
return m_output;
}
@Override
@@ -54,11 +71,11 @@ public class DIOCrossConnectFixture implements ITestFixture {
@Override
public boolean reset() {
try {
input.cancelInterrupts();
} catch (IllegalStateException e) {
m_input.cancelInterrupts();
} catch (IllegalStateException ex) {
// This will happen if the interrupt has not been allocated for this test.
}
output.set(false);
m_output.set(false);
return true;
}
@@ -66,8 +83,8 @@ public class DIOCrossConnectFixture implements ITestFixture {
public boolean teardown() {
logger.log(Level.FINE, "Begining teardown");
if (m_allocated) {
input.free();
output.free();
m_input.free();
m_output.free();
m_allocated = false;
}
return true;

View File

@@ -14,66 +14,66 @@ import edu.wpi.first.wpilibj.Counter;
import edu.wpi.first.wpilibj.mockhardware.FakeCounterSource;
/**
* @author jonathanleitschuh
* A fixture that can test the {@link Counter} using a {@link DIOCrossConnectFixture}.
*
* @author jonathanleitschuh
*/
public class FakeCounterFixture implements ITestFixture {
private static final Logger logger = Logger.getLogger(FakeEncoderFixture.class.getName());
private final DIOCrossConnectFixture dio;
private final DIOCrossConnectFixture m_dio;
private boolean m_allocated;
private final FakeCounterSource source;
private final Counter counter;
private final FakeCounterSource m_source;
private final Counter m_counter;
/**
* Constructs a FakeCounterFixture.
*$
* @param dio A previously allocated DIOCrossConnectFixture (must be freed
* outside this class)
*
* @param dio A previously allocated DIOCrossConnectFixture (must be freed outside this class)
*/
public FakeCounterFixture(DIOCrossConnectFixture dio) {
this.dio = dio;
m_dio = dio;
m_allocated = false;
source = new FakeCounterSource(dio.getOutput());
counter = new Counter(dio.getInput());
m_source = new FakeCounterSource(dio.getOutput());
m_counter = new Counter(dio.getInput());
}
/**
* Constructs a FakeCounterFixture using two port numbers
*$
* @param input the input port
* Constructs a FakeCounterFixture using two port numbers.
*
* @param input the input port
* @param output the output port
*/
public FakeCounterFixture(int input, int output) {
this.dio = new DIOCrossConnectFixture(input, output);
m_dio = new DIOCrossConnectFixture(input, output);
m_allocated = true;
source = new FakeCounterSource(dio.getOutput());
counter = new Counter(dio.getInput());
m_source = new FakeCounterSource(m_dio.getOutput());
m_counter = new Counter(m_dio.getInput());
}
/**
* Retrieves the FakeCouterSource for use
*$
* Retrieves the FakeCouterSource for use.
*
* @return the FakeCounterSource
*/
public FakeCounterSource getFakeCounterSource() {
return source;
return m_source;
}
/**
* Gets the Counter for use
*$
* Gets the Counter for use.
*
* @return the Counter
*/
public Counter getCounter() {
return counter;
return m_counter;
}
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#setup()
*/
@Override
@@ -84,32 +84,31 @@ public class FakeCounterFixture implements ITestFixture {
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#reset()
*/
@Override
public boolean reset() {
counter.reset();
m_counter.reset();
return true;
}
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#teardown()
*/
@Override
public boolean teardown() {
logger.log(Level.FINE, "Begining teardown");
counter.free();
source.free();
m_counter.free();
m_source.free();
if (m_allocated) { // Only tear down the dio if this class allocated it
dio.teardown();
m_dio.teardown();
m_allocated = false;
}
return true;
}
}

View File

@@ -7,15 +7,16 @@
package edu.wpi.first.wpilibj.fixtures;
import java.util.logging.Level;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.mockhardware.FakeEncoderSource;
/**
* @author jonathanleitschuh
* An encoder that uses two {@link DIOCrossConnectFixture DIOCrossConnectFixtures} to test the
* {@link Encoder}.
*
* @author jonathanleitschuh
*/
public class FakeEncoderFixture implements ITestFixture {
private static final Logger logger = Logger.getLogger(FakeEncoderFixture.class.getName());
@@ -25,33 +26,25 @@ public class FakeEncoderFixture implements ITestFixture {
private boolean m_allocated;
private final FakeEncoderSource m_source;
private int m_sourcePort[] = new int[2];
private int[] m_sourcePort = new int[2];
private final Encoder m_encoder;
private int m_encoderPort[] = new int[2];
private int[] m_encoderPort = new int[2];
/**
* Constructs a FakeEncoderFixture from two DIOCrossConnectFixture
*$
* @param dio1
* @param dio2
* Constructs a FakeEncoderFixture from two DIOCrossConnectFixture.
*/
public FakeEncoderFixture(DIOCrossConnectFixture dio1, DIOCrossConnectFixture dio2) {
assert dio1 != null;
assert dio2 != null;
this.m_dio1 = dio1;
this.m_dio2 = dio2;
m_dio1 = dio1;
m_dio2 = dio2;
m_allocated = false;
m_source = new FakeEncoderSource(dio1.getOutput(), dio2.getOutput());
m_encoder = new Encoder(dio1.getInput(), dio2.getInput());
}
/**
* Construcst a FakeEncoderFixture from a set of Digital I/O ports
*$
* @param inputA
* @param outputA
* @param inputB
* @param outputB
* Construcst a FakeEncoderFixture from a set of Digital I/O ports.
*/
public FakeEncoderFixture(int inputA, int outputA, int inputB, int outputB) {
assert outputA != outputB;
@@ -60,8 +53,8 @@ public class FakeEncoderFixture implements ITestFixture {
assert outputB != inputA;
assert outputB != inputB;
assert inputA != inputB;
this.m_dio1 = new DIOCrossConnectFixture(inputA, outputA);
this.m_dio2 = new DIOCrossConnectFixture(inputB, outputB);
m_dio1 = new DIOCrossConnectFixture(inputA, outputA);
m_dio2 = new DIOCrossConnectFixture(inputB, outputB);
m_allocated = true;
m_sourcePort[0] = outputA;
m_sourcePort[1] = outputB;
@@ -81,7 +74,7 @@ public class FakeEncoderFixture implements ITestFixture {
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#setup()
*/
@Override
@@ -91,7 +84,7 @@ public class FakeEncoderFixture implements ITestFixture {
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#reset()
*/
@Override
@@ -104,7 +97,7 @@ public class FakeEncoderFixture implements ITestFixture {
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#teardown()
*/
@Override

View File

@@ -8,48 +8,45 @@
package edu.wpi.first.wpilibj.fixtures;
import java.lang.reflect.ParameterizedType;
import java.util.logging.Logger;
import java.util.Random;
import java.util.logging.Logger;
import edu.wpi.first.wpilibj.PIDSource;
import edu.wpi.first.wpilibj.PIDSourceType;
import edu.wpi.first.wpilibj.test.TestBench;
/**
* Represents a physically connected Motor and Encoder to allow for unit tests
* on these different pairs<br>
* Designed to allow the user to easily setup and tear down the fixture to allow
* for reuse. This class should be explicitly instantiated in the TestBed class
* to allow any test to access this fixture. This allows tests to be mailable so
* that you can easily reconfigure the physical testbed without breaking the
* tests.
* Represents a physically connected Motor and Encoder to allow for unit tests on these different
* pairs<br> Designed to allow the user to easily setup and tear down the fixture to allow for
* reuse. This class should be explicitly instantiated in the TestBed class to allow any test to
* access this fixture. This allows tests to be mailable so that you can easily reconfigure the
* physical testbed without breaking the tests.
*/
public abstract class FilterNoiseFixture<T extends PIDSource> implements ITestFixture {
private static final Logger logger = Logger.getLogger(FilterNoiseFixture.class.getName());
private boolean initialized = false;
private boolean tornDown = false;
protected T filter;
private NoiseGenerator data;
private boolean m_initialized = false;
private boolean m_tornDown = false;
protected T m_filter;
private NoiseGenerator m_data;
/**
* Where the implementer of this class should pass the filter constructor
*$
* @return
* Where the implementer of this class should pass the filter constructor.
*/
abstract protected T giveFilter(PIDSource source);
protected abstract T giveFilter(PIDSource source);
final private void initialize() {
private void initialize() {
synchronized (this) {
if (!initialized) {
initialized = true; // This ensures it is only initialized once
if (!m_initialized) {
m_initialized = true; // This ensures it is only initialized once
data = new NoiseGenerator(TestBench.kStdDev) {
m_data = new NoiseGenerator(TestBench.kStdDev) {
@Override
@SuppressWarnings("ParameterName")
public double getData(double t) {
return 100.0 * Math.sin(2.0 * Math.PI * t);
}
};
filter = giveFilter(data);
m_filter = giveFilter(m_data);
}
}
}
@@ -61,33 +58,33 @@ public abstract class FilterNoiseFixture<T extends PIDSource> implements ITestFi
}
/**
* Gets the filter for this Object
*$
* Gets the filter for this Object.
*
* @return the filter this object refers too
*/
public T getFilter() {
initialize();
return filter;
return m_filter;
}
/**
* Gets the noise generator for this object
*$
* Gets the noise generator for this object.
*
* @return the noise generator that this object refers too
*/
public NoiseGenerator getNoiseGenerator() {
initialize();
return data;
return m_data;
}
/**
* Retrieves the name of the filter that this object refers to
*$
* Retrieves the name of the filter that this object refers to.
*
* @return The simple name of the filter {@link Class#getSimpleName()}
*/
public String getType() {
initialize();
return filter.getClass().getSimpleName();
return m_filter.getClass().getSimpleName();
}
// test here?
@@ -108,61 +105,55 @@ public abstract class FilterNoiseFixture<T extends PIDSource> implements ITestFi
// Get the generic type as a class
@SuppressWarnings("unchecked")
Class<T> class1 =
(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
string.append(class1.getSimpleName());
string.append(">");
return string.toString();
}
/**
* Adds Gaussian white noise to a function returning data. The noise will have
* the standard deviation provided in the constructor.
* Adds Gaussian white noise to a function returning data. The noise will have the standard
* deviation provided in the constructor.
*/
public abstract class NoiseGenerator implements PIDSource {
private double noise = 0.0;
private double m_noise = 0.0;
// Make sure first call to pidGet() uses count == 0
private double count = -TestBench.kFilterStep;
private double m_count = -TestBench.kFilterStep;
private double stdDev;
private Random gen = new Random();
private double m_stdDev;
private Random m_gen = new Random();
NoiseGenerator(double stdDev) {
this.stdDev = stdDev;
m_stdDev = stdDev;
}
abstract public double getData(double t);
@SuppressWarnings("ParameterName")
public abstract double getData(double t);
/**
* {@inheritDoc}
*/
@Override
public void setPIDSourceType(PIDSourceType pidSource) {}
public void setPIDSourceType(PIDSourceType pidSource) {
}
/**
* {@inheritDoc}
*/
@Override
public PIDSourceType getPIDSourceType() {
return PIDSourceType.kDisplacement;
}
public double get() {
return getData(count) + noise;
return getData(m_count) + m_noise;
}
/**
* {@inheritDoc}
*/
@Override
public double pidGet() {
noise = gen.nextGaussian() * stdDev;
count += TestBench.kFilterStep;
return getData(count) + noise;
m_noise = m_gen.nextGaussian() * m_stdDev;
m_count += TestBench.kFilterStep;
return getData(m_count) + m_noise;
}
public void reset() {
count = -TestBench.kFilterStep;
m_count = -TestBench.kFilterStep;
}
}
}

View File

@@ -15,52 +15,49 @@ import edu.wpi.first.wpilibj.PIDSourceType;
import edu.wpi.first.wpilibj.test.TestBench;
/**
* Represents a filterphysically connected Motor and Encoder to allow for unit tests
* on these different pairs<br>
* Designed to allow the user to easily setup and tear down the fixture to allow
* for reuse. This class should be explicitly instantiated in the TestBed class
* to allow any test to access this fixture. This allows tests to be mailable so
* that you can easily reconfigure the physical testbed without breaking the
* tests.
* Represents a filterphysically connected Motor and Encoder to allow for unit tests on these
* different pairs<br> Designed to allow the user to easily setup and tear down the fixture to allow
* for reuse. This class should be explicitly instantiated in the TestBed class to allow any test to
* access this fixture. This allows tests to be mailable so that you can easily reconfigure the
* physical testbed without breaking the tests.
*/
public abstract class FilterOutputFixture<T extends PIDSource> implements ITestFixture {
private static final Logger logger = Logger.getLogger(FilterOutputFixture.class.getName());
private boolean initialized = false;
private boolean tornDown = false;
protected T filter;
private DataWrapper data;
private double expectedOutput;
private boolean m_initialized = false;
private boolean m_tornDown = false;
protected T m_filter;
private DataWrapper m_data;
private double m_expectedOutput;
public FilterOutputFixture(double expectedOutput) {
this.expectedOutput = expectedOutput;
m_expectedOutput = expectedOutput;
}
/**
* Get expected output of fixture
* Get expected output of fixture.
*/
public double getExpectedOutput() {
return expectedOutput;
return m_expectedOutput;
}
/**
* Where the implementer of this class should pass the filter constructor
*$
* @return
* Where the implementer of this class should pass the filter constructor.
*/
abstract protected T giveFilter(PIDSource source);
protected abstract T giveFilter(PIDSource source);
final private void initialize() {
private void initialize() {
synchronized (this) {
if (!initialized) {
initialized = true; // This ensures it is only initialized once
if (!m_initialized) {
m_initialized = true; // This ensures it is only initialized once
data = new DataWrapper() {
m_data = new DataWrapper() {
@Override
@SuppressWarnings("ParameterName")
public double getData(double t) {
return 100.0 * Math.sin(2.0 * Math.PI * t) + 20.0 * Math.cos(50.0 * Math.PI * t);
}
};
filter = giveFilter(data);
m_filter = giveFilter(m_data);
}
}
}
@@ -72,38 +69,38 @@ public abstract class FilterOutputFixture<T extends PIDSource> implements ITestF
}
/**
* Gets the filter for this Object
*$
* Gets the filter for this Object.
*
* @return the filter this object refers too
*/
public T getFilter() {
initialize();
return filter;
return m_filter;
}
/**
* Gets the data wrapper for this object
*$
* Gets the data wrapper for this object.
*
* @return the data wrapper that this object refers too
*/
public DataWrapper getDataWrapper() {
initialize();
return data;
return m_data;
}
/**
* Retrieves the name of the filter that this object refers to
*$
* Retrieves the name of the filter that this object refers to.
*
* @return The simple name of the filter {@link Class#getSimpleName()}
*/
public String getType() {
initialize();
return filter.getClass().getSimpleName();
return m_filter.getClass().getSimpleName();
}
@Override
public boolean reset() {
data.reset();
m_data.reset();
return true;
}
@@ -118,7 +115,8 @@ public abstract class FilterOutputFixture<T extends PIDSource> implements ITestF
// Get the generic type as a class
@SuppressWarnings("unchecked")
Class<T> class1 =
(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
string.append(class1.getSimpleName());
string.append(">");
return string.toString();
@@ -126,35 +124,31 @@ public abstract class FilterOutputFixture<T extends PIDSource> implements ITestF
public abstract class DataWrapper implements PIDSource {
// Make sure first call to pidGet() uses count == 0
private double count = -TestBench.kFilterStep;
private double m_count = -TestBench.kFilterStep;
abstract public double getData(double t);
@SuppressWarnings("ParameterName")
public abstract double getData(double t);
/**
* {@inheritDoc}
*/
@Override
public void setPIDSourceType(PIDSourceType pidSource) {}
public void setPIDSourceType(PIDSourceType pidSource) {
}
/**
* {@inheritDoc}
*/
@Override
public PIDSourceType getPIDSourceType() {
return PIDSourceType.kDisplacement;
}
/**
* {@inheritDoc}
*/
@Override
public double pidGet() {
count += TestBench.kFilterStep;
return getData(count);
m_count += TestBench.kFilterStep;
return getData(m_count);
}
public void reset() {
count = -TestBench.kFilterStep;
m_count = -TestBench.kFilterStep;
}
}
}

View File

@@ -10,44 +10,40 @@ package edu.wpi.first.wpilibj.fixtures;
import edu.wpi.first.wpilibj.test.TestBench;
/**
* Master interface for all test fixtures. This ensures that all test fixtures
* have setup and teardown methods, to ensure that the tests run properly.
*$
* Test fixtures should be modeled around the content of a test, rather than the
* actual physical layout of the testing board. They should obtain references to
* hardware from the {@link TestBench} class, which is a singleton. Testing
* Fixtures are responsible for ensuring that the hardware is in an appropriate
* state for the start of a test, and ensuring that future tests will not be
* affected by the results of a test.
*$
* Master interface for all test fixtures. This ensures that all test fixtures have setup and
* teardown methods, to ensure that the tests run properly. Test fixtures should be modeled around
* the content of a test, rather than the actual physical layout of the testing board. They should
* obtain references to hardware from the {@link TestBench} class, which is a singleton. Testing
* Fixtures are responsible for ensuring that the hardware is in an appropriate state for the start
* of a test, and ensuring that future tests will not be affected by the results of a test.
*
* @author Fredric Silberberg
*/
public interface ITestFixture {
/**
* Performs any required setup for this fixture, ensuring that all fixture
* elements are ready for testing.
*$
* Performs any required setup for this fixture, ensuring that all fixture elements are ready for
* testing.
*
* @return True if the fixture is ready for testing
*/
boolean setup();
/**
* Resets the fixture back to test start state. This should be called by the
* test class in the test setup method to ensure that the hardware is in the
* default state. This differs from {@link ITestFixture#setup()} as that is
* called once, before the class is constructed, so it may need to start
* sensors. This method should not have to start anything, just reset sensors
* and ensure that motors are stopped.
*$
* Resets the fixture back to test start state. This should be called by the test class in the
* test setup method to ensure that the hardware is in the default state. This differs from {@link
* ITestFixture#setup()} as that is called once, before the class is constructed, so it may need
* to start sensors. This method should not have to start anything, just reset sensors and ensure
* that motors are stopped.
*
* @return True if the fixture is ready for testing
*/
boolean reset();
/**
* Performs any required teardown after use of the fixture, ensuring that
* future tests will not run into conflicts.
*$
* Performs any required teardown after use of the fixture, ensuring that future tests will not
* run into conflicts.
*
* @return True if the teardown succeeded
*/
boolean teardown();

View File

@@ -19,74 +19,74 @@ import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.test.TestBench;
/**
* Represents a physically connected Motor and Encoder to allow for unit tests
* on these different pairs<br>
* Designed to allow the user to easily setup and tear down the fixture to allow
* for reuse. This class should be explicitly instantiated in the TestBed class
* to allow any test to access this fixture. This allows tests to be mailable so
* that you can easily reconfigure the physical testbed without breaking the
* tests.
* Represents a physically connected Motor and Encoder to allow for unit tests on these different
* pairs<br> Designed to allow the user to easily setup and tear down the fixture to allow for
* reuse. This class should be explicitly instantiated in the TestBed class to allow any test to
* access this fixture. This allows tests to be mailable so that you can easily reconfigure the
* physical testbed without breaking the tests.
*
* @author Jonathan Leitschuh
*
*/
public abstract class MotorEncoderFixture<T extends SpeedController> implements ITestFixture {
private static final Logger logger = Logger.getLogger(MotorEncoderFixture.class.getName());
private boolean initialized = false;
private boolean tornDown = false;
protected T motor;
private Encoder encoder;
private final Counter counters[] = new Counter[2];
protected DigitalInput aSource; // Stored so it can be freed at tear down
protected DigitalInput bSource;
private boolean m_initialized = false;
private boolean m_tornDown = false;
protected T m_motor;
private Encoder m_encoder;
private final Counter[] m_counters = new Counter[2];
protected DigitalInput m_alphaSource; // Stored so it can be freed at tear down
protected DigitalInput m_betaSource;
/**
* Default constructor for a MotorEncoderFixture
* Default constructor for a MotorEncoderFixture.
*/
public MotorEncoderFixture() {}
public MotorEncoderFixture() {
}
abstract public int getPDPChannel();
public abstract int getPDPChannel();
/**
* Where the implementer of this class should pass the speed controller
* Constructor should only be called from outside this class if the Speed
* controller is not also an implementation of PWM interface
*$
* Where the implementer of this class should pass the speed controller Constructor should only be
* called from outside this class if the Speed controller is not also an implementation of PWM
* interface.
*
* @return SpeedController
*/
abstract protected T giveSpeedController();
protected abstract T giveSpeedController();
/**
* Where the implementer of this class should pass one of the digital inputs<br>
* CONSTRUCTOR SHOULD NOT BE CALLED FROM OUTSIDE THIS CLASS!
*$
* Where the implementer of this class should pass one of the digital inputs.
*
* <p>CONSTRUCTOR SHOULD NOT BE CALLED FROM OUTSIDE THIS CLASS!
*
* @return DigitalInput
*/
abstract protected DigitalInput giveDigitalInputA();
protected abstract DigitalInput giveDigitalInputA();
/**
* Where the implementer fo this class should pass the other digital input<br>
* CONSTRUCTOR SHOULD NOT BE CALLED FROM OUTSIDE THIS CLASS!
*$
* Where the implementer fo this class should pass the other digital input.
*
* <p>CONSTRUCTOR SHOULD NOT BE CALLED FROM OUTSIDE THIS CLASS!
*
* @return Input B to be used when this class is instantiated
*/
abstract protected DigitalInput giveDigitalInputB();
protected abstract DigitalInput giveDigitalInputB();
final private void initialize() {
private final void initialize() {
synchronized (this) {
if (!initialized) {
initialized = true; // This ensures it is only initialized once
if (!m_initialized) {
m_initialized = true; // This ensures it is only initialized once
aSource = giveDigitalInputA();
bSource = giveDigitalInputB();
m_alphaSource = giveDigitalInputA();
m_betaSource = giveDigitalInputB();
encoder = new Encoder(aSource, bSource);
counters[0] = new Counter(aSource);
counters[1] = new Counter(bSource);
m_encoder = new Encoder(m_alphaSource, m_betaSource);
m_counters[0] = new Counter(m_alphaSource);
m_counters[1] = new Counter(m_betaSource);
logger.fine("Creating the speed controller!");
motor = giveSpeedController(); // CANJaguar throws an exception if it
// doesn't get the message
m_motor = giveSpeedController(); // CANJaguar throws an exception if it
// doesn't get the message
}
}
}
@@ -98,70 +98,68 @@ public abstract class MotorEncoderFixture<T extends SpeedController> implements
}
/**
* Gets the motor for this Object
*$
* Gets the motor for this Object.
*
* @return the motor this object refers too
*/
public T getMotor() {
initialize();
return motor;
return m_motor;
}
/**
* Gets the encoder for this object
*$
* Gets the encoder for this object.
*
* @return the encoder that this object refers too
*/
public Encoder getEncoder() {
initialize();
return encoder;
return m_encoder;
}
public Counter[] getCounters() {
initialize();
return counters;
return m_counters;
}
/**
* Retrieves the name of the motor that this object refers to
*$
* Retrieves the name of the motor that this object refers to.
*
* @return The simple name of the motor {@link Class#getSimpleName()}
*/
public String getType() {
initialize();
return motor.getClass().getSimpleName();
return m_motor.getClass().getSimpleName();
}
/**
* Checks to see if the speed of the motor is within some range of a given
* value. This is used instead of equals() because doubles can have
* inaccuracies.
*$
* @param value The value to compare against
* Checks to see if the speed of the motor is within some range of a given value. This is used
* instead of equals() because doubles can have inaccuracies.
*
* @param value The value to compare against
* @param accuracy The accuracy range to check against to see if the
* @return true if the range of values between motors speed accuracy contains
* the 'value'.
* @return true if the range of values between motors speed accuracy contains the 'value'.
*/
public boolean isMotorSpeedWithinRange(double value, double accuracy) {
initialize();
return Math.abs((Math.abs(motor.get()) - Math.abs(value))) < Math.abs(accuracy);
return Math.abs((Math.abs(m_motor.get()) - Math.abs(value))) < Math.abs(accuracy);
}
@Override
public boolean reset() {
initialize();
boolean wasReset = true;
motor.setInverted(false);
motor.set(0);
m_motor.setInverted(false);
m_motor.set(0);
Timer.delay(TestBench.MOTOR_STOP_TIME); // DEFINED IN THE TestBench
encoder.reset();
for (Counter c : counters) {
m_encoder.reset();
for (Counter c : m_counters) {
c.reset();
}
wasReset = wasReset && motor.get() == 0;
wasReset = wasReset && encoder.get() == 0;
for (Counter c : counters) {
boolean wasReset = true;
wasReset = wasReset && m_motor.get() == 0;
wasReset = wasReset && m_encoder.get() == 0;
for (Counter c : m_counters) {
wasReset = wasReset && c.get() == 0;
}
@@ -169,54 +167,59 @@ public abstract class MotorEncoderFixture<T extends SpeedController> implements
}
/**
* Safely tears down the MotorEncoder Fixture in a way that makes sure that
* even if an object fails to initialize the rest of the fixture can still be
* torn down and the resources deallocated
* Safely tears down the MotorEncoder Fixture in a way that makes sure that even if an object
* fails to initialize the rest of the fixture can still be torn down and the resources
* deallocated.
*/
@Override
public boolean teardown() {
String type;
if (motor != null) {
if (m_motor != null) {
type = getType();
} else {
type = "null";
}
if (!tornDown) {
if (!m_tornDown) {
boolean wasNull = false;
if (motor instanceof PWM && motor != null) {
((PWM) motor).free();
motor = null;
} else if (motor == null)
if (m_motor instanceof PWM && m_motor != null) {
((PWM) m_motor).free();
m_motor = null;
} else if (m_motor == null) {
wasNull = true;
if (encoder != null) {
encoder.free();
encoder = null;
} else
}
if (m_encoder != null) {
m_encoder.free();
m_encoder = null;
} else {
wasNull = true;
if (counters[0] != null) {
counters[0].free();
counters[0] = null;
} else
}
if (m_counters[0] != null) {
m_counters[0].free();
m_counters[0] = null;
} else {
wasNull = true;
if (counters[1] != null) {
counters[1].free();
counters[1] = null;
} else
}
if (m_counters[1] != null) {
m_counters[1].free();
m_counters[1] = null;
} else {
wasNull = true;
if (aSource != null) {
aSource.free();
aSource = null;
} else
}
if (m_alphaSource != null) {
m_alphaSource.free();
m_alphaSource = null;
} else {
wasNull = true;
if (bSource != null) {
bSource.free();
bSource = null;
} else
}
if (m_betaSource != null) {
m_betaSource.free();
m_betaSource = null;
} else {
wasNull = true;
}
tornDown = true;
m_tornDown = true;
if (wasNull) {
throw new NullPointerException("MotorEncoderFixture had null params at teardown");
@@ -234,7 +237,8 @@ public abstract class MotorEncoderFixture<T extends SpeedController> implements
// Get the generic type as a class
@SuppressWarnings("unchecked")
Class<T> class1 =
(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
string.append(class1.getSimpleName());
string.append(">");
return string.toString();

View File

@@ -9,22 +9,19 @@ package edu.wpi.first.wpilibj.fixtures;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.Relay.Direction;
import edu.wpi.first.wpilibj.Relay.Value;
/**
* A connection between a {@link Relay} and two {@link DigitalInput DigitalInputs}.
* @author jonathanleitschuh
*
*/
public abstract class RelayCrossConnectFixture implements ITestFixture {
private DigitalInput inputOne;
private DigitalInput inputTwo;
private Relay relay;
private boolean initialized = false;
private boolean freed = false;
private DigitalInput m_inputOne;
private DigitalInput m_inputTwo;
private Relay m_relay;
private boolean m_initialized = false;
private boolean m_freed = false;
protected abstract Relay giveRelay();
@@ -35,33 +32,33 @@ public abstract class RelayCrossConnectFixture implements ITestFixture {
private void initialize() {
synchronized (this) {
if (!initialized) {
relay = giveRelay();
inputOne = giveInputOne();
inputTwo = giveInputTwo();
initialized = true;
if (!m_initialized) {
m_relay = giveRelay();
m_inputOne = giveInputOne();
m_inputTwo = giveInputTwo();
m_initialized = true;
}
}
}
public Relay getRelay() {
initialize();
return relay;
return m_relay;
}
public DigitalInput getInputOne() {
initialize();
return inputOne;
return m_inputOne;
}
public DigitalInput getInputTwo() {
initialize();
return inputTwo;
return m_inputTwo;
}
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#setup()
*/
@Override
@@ -72,7 +69,7 @@ public abstract class RelayCrossConnectFixture implements ITestFixture {
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#reset()
*/
@Override
@@ -83,16 +80,16 @@ public abstract class RelayCrossConnectFixture implements ITestFixture {
/*
* (non-Javadoc)
*$
*
* @see edu.wpi.first.wpilibj.fixtures.ITestFixture#teardown()
*/
@Override
public boolean teardown() {
if (!freed) {
relay.free();
inputOne.free();
inputTwo.free();
freed = true;
if (!m_freed) {
m_relay.free();
m_inputOne.free();
m_inputTwo.free();
m_freed = true;
} else {
throw new RuntimeException("You attempted to free the "
+ RelayCrossConnectFixture.class.getSimpleName() + " multiple times");

View File

@@ -9,17 +9,13 @@ package edu.wpi.first.wpilibj.fixtures;
/**
* This is an example of how to use the {@link ITestFixture} interface to create
* test fixtures for a test.
*$
* This is an example of how to use the {@link ITestFixture} interface to create test fixtures for a
* test.
*
* @author Fredric Silberberg
*$
*/
public class SampleFixture implements ITestFixture {
/**
* {@inheritDoc}
*/
@Override
public boolean setup() {
/*
@@ -42,9 +38,6 @@ public class SampleFixture implements ITestFixture {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean teardown() {
/*

View File

@@ -14,21 +14,20 @@ import edu.wpi.first.wpilibj.Servo;
import edu.wpi.first.wpilibj.Timer;
/**
* A class to represent the a physical Camera with two servos (tilt and pan)
* designed to test to see if the gyroscope is operating normally.
* A class to represent the a physical Camera with two servos (tilt and pan) designed to test to see
* if the gyroscope is operating normally.
*
* @author Jonathan Leitschuh
*
*/
public abstract class TiltPanCameraFixture implements ITestFixture {
public static final Logger logger = Logger.getLogger(TiltPanCameraFixture.class.getName());
public static final double RESET_TIME = 2.0;
private AnalogGyro gyro;
private AnalogGyro gyroParam;
private Servo tilt;
private Servo pan;
private boolean initialized = false;
private AnalogGyro m_gyro;
private AnalogGyro m_gyroParam;
private Servo m_tilt;
private Servo m_pan;
private boolean m_initialized = false;
protected abstract AnalogGyro giveGyro();
@@ -40,24 +39,25 @@ public abstract class TiltPanCameraFixture implements ITestFixture {
protected abstract Servo givePan();
/**
* Constructs the TiltPanCamera
* Constructs the TiltPanCamera.
*/
public TiltPanCameraFixture() {}
public TiltPanCameraFixture() {
}
@Override
public boolean setup() {
boolean wasSetup = false;
if (!initialized) {
initialized = true;
tilt = giveTilt();
tilt.set(0);
pan = givePan();
pan.set(0);
if (!m_initialized) {
m_initialized = true;
m_tilt = giveTilt();
m_tilt.set(0);
m_pan = givePan();
m_pan.set(0);
Timer.delay(RESET_TIME);
logger.fine("Initializing the gyro");
gyro = giveGyro();
gyro.reset();
m_gyro = giveGyro();
m_gyro.reset();
wasSetup = true;
}
return wasSetup;
@@ -65,50 +65,52 @@ public abstract class TiltPanCameraFixture implements ITestFixture {
@Override
public boolean reset() {
if(gyro != null)
gyro.reset();
if (m_gyro != null) {
m_gyro.reset();
}
return true;
}
public Servo getTilt() {
return tilt;
return m_tilt;
}
public Servo getPan() {
return pan;
return m_pan;
}
public AnalogGyro getGyro() {
return gyro;
return m_gyro;
}
public AnalogGyro getGyroParam() {
return gyroParam;
return m_gyroParam;
}
// Do not call unless all other instances of Gyro have been deallocated
public void setupGyroParam(int center, double offset) {
gyroParam = giveGyroParam(center, offset);
gyroParam.reset();
m_gyroParam = giveGyroParam(center, offset);
m_gyroParam.reset();
}
public void freeGyro() {
gyro.free();
gyro = null;
m_gyro.free();
m_gyro = null;
}
@Override
public boolean teardown() {
tilt.free();
tilt = null;
pan.free();
pan = null;
if(gyro !=null){//in case not freed during gyro tests
gyro.free();
gyro = null;
m_tilt.free();
m_tilt = null;
m_pan.free();
m_pan = null;
if (m_gyro != null) { //in case not freed during gyro tests
m_gyro.free();
m_gyro = null;
}
if(gyroParam != null){//in case gyro tests failed before getting to this point
gyroParam.free();
gyroParam = null;
if (m_gyroParam != null) { //in case gyro tests failed before getting to this point
m_gyroParam.free();
m_gyroParam = null;
}
return true;
}