mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
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:
committed by
Peter Johnson
parent
64ab6e51fe
commit
a834fff7b2
@@ -11,19 +11,20 @@ import edu.wpi.first.wpilibj.DigitalOutput;
|
||||
import edu.wpi.first.wpilibj.Timer;
|
||||
|
||||
/**
|
||||
* Simulates an encoder for testing purposes
|
||||
* Simulates an encoder for testing purposes.
|
||||
*
|
||||
* @author Ryan O'Meara
|
||||
*/
|
||||
public class FakeCounterSource {
|
||||
|
||||
private Thread m_task;
|
||||
private int m_count;
|
||||
private int m_mSec;
|
||||
private int m_milliSec;
|
||||
private DigitalOutput m_output;
|
||||
private boolean m_allocated;
|
||||
|
||||
/**
|
||||
* Thread object that allows emulation of an encoder
|
||||
* Thread object that allows emulation of an encoder.
|
||||
*/
|
||||
private class EncoderThread extends Thread {
|
||||
|
||||
@@ -37,19 +38,20 @@ public class FakeCounterSource {
|
||||
m_encoder.m_output.set(false);
|
||||
try {
|
||||
for (int i = 0; i < m_encoder.m_count; i++) {
|
||||
Thread.sleep(m_encoder.m_mSec);
|
||||
Thread.sleep(m_encoder.m_milliSec);
|
||||
m_encoder.m_output.set(true);
|
||||
Thread.sleep(m_encoder.m_mSec);
|
||||
Thread.sleep(m_encoder.m_milliSec);
|
||||
m_encoder.m_output.set(false);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fake encoder on a given port
|
||||
*$
|
||||
* Create a fake encoder on a given port.
|
||||
*
|
||||
* @param output the port to output the given signal to
|
||||
*/
|
||||
public FakeCounterSource(DigitalOutput output) {
|
||||
@@ -59,8 +61,8 @@ public class FakeCounterSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fake encoder on a given port
|
||||
*$
|
||||
* Create a fake encoder on a given port.
|
||||
*
|
||||
* @param port The port the encoder is supposed to be on
|
||||
*/
|
||||
public FakeCounterSource(int port) {
|
||||
@@ -70,7 +72,7 @@ public class FakeCounterSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy Object with minimum memory leak
|
||||
* Destroy Object with minimum memory leak.
|
||||
*/
|
||||
public void free() {
|
||||
m_task = null;
|
||||
@@ -82,36 +84,36 @@ public class FakeCounterSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Common initailization code
|
||||
* Common initailization code.
|
||||
*/
|
||||
private void initEncoder() {
|
||||
m_mSec = 1;
|
||||
m_milliSec = 1;
|
||||
m_task = new EncoderThread(this);
|
||||
m_output.set(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the thread execution task
|
||||
* Starts the thread execution task.
|
||||
*/
|
||||
public void start() {
|
||||
m_task.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the thread to complete
|
||||
* Waits for the thread to complete.
|
||||
*/
|
||||
public void complete() {
|
||||
try {
|
||||
m_task.join();
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
m_task = new EncoderThread(this);
|
||||
Timer.delay(.01);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts and completes a task set - does not return until thred has finished
|
||||
* its operations
|
||||
* Starts and completes a task set - does not return until thred has finished its operations.
|
||||
*/
|
||||
public void execute() {
|
||||
start();
|
||||
@@ -119,8 +121,8 @@ public class FakeCounterSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the count to run encoder
|
||||
*$
|
||||
* Sets the count to run encoder.
|
||||
*
|
||||
* @param count The count to emulate to the controller
|
||||
*/
|
||||
public void setCount(int count) {
|
||||
@@ -128,11 +130,11 @@ public class FakeCounterSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the rate to send pulses
|
||||
*$
|
||||
* @param mSec The rate to send out pulses at
|
||||
* Specify the rate to send pulses.
|
||||
*
|
||||
* @param milliSec The rate to send out pulses at
|
||||
*/
|
||||
public void setRate(int mSec) {
|
||||
m_mSec = mSec;
|
||||
public void setRate(int milliSec) {
|
||||
m_milliSec = milliSec;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,20 +11,22 @@ import edu.wpi.first.wpilibj.DigitalOutput;
|
||||
import edu.wpi.first.wpilibj.Timer;
|
||||
|
||||
/**
|
||||
* Emulates a quadrature encoder
|
||||
* Emulates a quadrature encoder.
|
||||
*
|
||||
* @author Ryan O'Meara
|
||||
*/
|
||||
public class FakeEncoderSource {
|
||||
|
||||
private Thread m_task;
|
||||
private int m_count;
|
||||
private int m_mSec;
|
||||
private int m_milliSec;
|
||||
private boolean m_forward;
|
||||
private DigitalOutput m_outputA, m_outputB;
|
||||
private final boolean allocatedOutputs;
|
||||
private final DigitalOutput m_outputA;
|
||||
private final DigitalOutput m_outputB;
|
||||
private final boolean m_allocatedOutputs;
|
||||
|
||||
/**
|
||||
* Thread object that allows emulation of a quadrature encoder
|
||||
* Thread object that allows emulation of a quadrature encoder.
|
||||
*/
|
||||
private class QuadEncoderThread extends Thread {
|
||||
|
||||
@@ -36,7 +38,8 @@ public class FakeEncoderSource {
|
||||
|
||||
public void run() {
|
||||
|
||||
DigitalOutput lead, lag;
|
||||
final DigitalOutput lead;
|
||||
final DigitalOutput lag;
|
||||
|
||||
m_encoder.m_outputA.set(false);
|
||||
m_encoder.m_outputB.set(false);
|
||||
@@ -52,46 +55,62 @@ public class FakeEncoderSource {
|
||||
try {
|
||||
for (int i = 0; i < m_encoder.m_count; i++) {
|
||||
lead.set(true);
|
||||
Thread.sleep(m_encoder.m_mSec);
|
||||
Thread.sleep(m_encoder.m_milliSec);
|
||||
lag.set(true);
|
||||
Thread.sleep(m_encoder.m_mSec);
|
||||
Thread.sleep(m_encoder.m_milliSec);
|
||||
lead.set(false);
|
||||
Thread.sleep(m_encoder.m_mSec);
|
||||
Thread.sleep(m_encoder.m_milliSec);
|
||||
lag.set(false);
|
||||
Thread.sleep(m_encoder.m_mSec);
|
||||
Thread.sleep(m_encoder.m_milliSec);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an encoder source using two ports.
|
||||
*
|
||||
* @param portA The A port
|
||||
* @param portB The B port
|
||||
*/
|
||||
public FakeEncoderSource(int portA, int portB) {
|
||||
m_outputA = new DigitalOutput(portA);
|
||||
m_outputB = new DigitalOutput(portB);
|
||||
allocatedOutputs = true;
|
||||
m_allocatedOutputs = true;
|
||||
initQuadEncoder();
|
||||
}
|
||||
|
||||
public FakeEncoderSource(DigitalOutput iA, DigitalOutput iB) {
|
||||
m_outputA = iA;
|
||||
m_outputB = iB;
|
||||
allocatedOutputs = false;
|
||||
/**
|
||||
* Creates the fake encoder using two digital outputs.
|
||||
*
|
||||
* @param outputA The A digital output
|
||||
* @param outputB The B digital output
|
||||
*/
|
||||
public FakeEncoderSource(DigitalOutput outputA, DigitalOutput outputB) {
|
||||
m_outputA = outputA;
|
||||
m_outputB = outputB;
|
||||
m_allocatedOutputs = false;
|
||||
initQuadEncoder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees the resource.
|
||||
*/
|
||||
public void free() {
|
||||
m_task = null;
|
||||
if (allocatedOutputs) {
|
||||
if (m_allocatedOutputs) {
|
||||
m_outputA.free();
|
||||
m_outputB.free();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Common initialization code
|
||||
* Common initialization code.
|
||||
*/
|
||||
private final void initQuadEncoder() {
|
||||
m_mSec = 1;
|
||||
m_milliSec = 1;
|
||||
m_forward = true;
|
||||
m_task = new QuadEncoderThread(this);
|
||||
m_outputA.set(false);
|
||||
@@ -99,26 +118,27 @@ public class FakeEncoderSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the thread
|
||||
* Starts the thread.
|
||||
*/
|
||||
public void start() {
|
||||
m_task.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for thread to end
|
||||
* Waits for thread to end.
|
||||
*/
|
||||
public void complete() {
|
||||
try {
|
||||
m_task.join();
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
m_task = new QuadEncoderThread(this);
|
||||
Timer.delay(.01);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs and waits for thread to end before returning
|
||||
* Runs and waits for thread to end before returning.
|
||||
*/
|
||||
public void execute() {
|
||||
start();
|
||||
@@ -126,17 +146,17 @@ public class FakeEncoderSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Rate of pulses to send
|
||||
*$
|
||||
* @param mSec Pulse Rate
|
||||
* Rate of pulses to send.
|
||||
*
|
||||
* @param milliSec Pulse Rate
|
||||
*/
|
||||
public void setRate(int mSec) {
|
||||
m_mSec = mSec;
|
||||
public void setRate(int milliSec) {
|
||||
m_milliSec = milliSec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the number of pulses to simulate
|
||||
*$
|
||||
* Set the number of pulses to simulate.
|
||||
*
|
||||
* @param count Pulse count
|
||||
*/
|
||||
public void setCount(int count) {
|
||||
@@ -144,8 +164,8 @@ public class FakeEncoderSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set which direction the encoder simulates motion in
|
||||
*$
|
||||
* Set which direction the encoder simulates motion in.
|
||||
*
|
||||
* @param isForward Whether to simulate forward motion
|
||||
*/
|
||||
public void setForward(boolean isForward) {
|
||||
@@ -153,8 +173,8 @@ public class FakeEncoderSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Accesses whether the encoder is simulating forward motion
|
||||
*$
|
||||
* Accesses whether the encoder is simulating forward motion.
|
||||
*
|
||||
* @return Whether the simulated motion is in the forward direction
|
||||
*/
|
||||
public boolean isForward() {
|
||||
|
||||
@@ -10,76 +10,86 @@ package edu.wpi.first.wpilibj.mockhardware;
|
||||
import edu.wpi.first.wpilibj.AnalogOutput;
|
||||
|
||||
/**
|
||||
* @author jonathanleitschuh
|
||||
* A fake source to provide output to a {@link edu.wpi.first.wpilibj.interfaces.Potentiometer}.
|
||||
*
|
||||
* @author jonathanleitschuh
|
||||
*/
|
||||
public class FakePotentiometerSource {
|
||||
private AnalogOutput output;
|
||||
private boolean m_init_output;
|
||||
private double potMaxAngle;
|
||||
private double potMaxVoltage = 5.0;
|
||||
private final double defaultPotMaxAngle;
|
||||
private AnalogOutput m_output;
|
||||
private boolean m_initOutput;
|
||||
private double m_potMaxAngle;
|
||||
private double m_potMaxVoltage = 5.0;
|
||||
private final double m_defaultPotMaxAngle;
|
||||
|
||||
/**
|
||||
* Constructs the fake source.
|
||||
*
|
||||
* @param output The analog port to output the signal on
|
||||
* @param defaultPotMaxAngle The default maximum angle the pot supports.
|
||||
*/
|
||||
public FakePotentiometerSource(AnalogOutput output, double defaultPotMaxAngle) {
|
||||
this.defaultPotMaxAngle = defaultPotMaxAngle;
|
||||
potMaxAngle = defaultPotMaxAngle;
|
||||
this.output = output;
|
||||
m_init_output = false;
|
||||
m_defaultPotMaxAngle = defaultPotMaxAngle;
|
||||
m_potMaxAngle = defaultPotMaxAngle;
|
||||
m_output = output;
|
||||
m_initOutput = false;
|
||||
}
|
||||
|
||||
public FakePotentiometerSource(int port, double defaultPotMaxAngle) {
|
||||
this(new AnalogOutput(port), defaultPotMaxAngle);
|
||||
m_init_output = true;
|
||||
m_initOutput = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum voltage output. If not the default is 5.0V
|
||||
*$
|
||||
* Sets the maximum voltage output. If not the default is 5.0V.
|
||||
*
|
||||
* @param voltage The voltage that indicates that the pot is at the max value.
|
||||
*/
|
||||
public void setMaxVoltage(double voltage) {
|
||||
potMaxVoltage = voltage;
|
||||
m_potMaxVoltage = voltage;
|
||||
}
|
||||
|
||||
public void setRange(double range) {
|
||||
potMaxAngle = range;
|
||||
m_potMaxAngle = range;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
potMaxAngle = defaultPotMaxAngle;
|
||||
output.setVoltage(0.0);
|
||||
m_potMaxAngle = m_defaultPotMaxAngle;
|
||||
m_output.setVoltage(0.0);
|
||||
}
|
||||
|
||||
public void setAngle(double angle) {
|
||||
output.setVoltage((potMaxVoltage / potMaxAngle) * angle);
|
||||
m_output.setVoltage((m_potMaxVoltage / m_potMaxAngle) * angle);
|
||||
}
|
||||
|
||||
public void setVoltage(double voltage) {
|
||||
output.setVoltage(voltage);
|
||||
m_output.setVoltage(voltage);
|
||||
}
|
||||
|
||||
public double getVoltage() {
|
||||
return output.getVoltage();
|
||||
return m_output.getVoltage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the currently set angle
|
||||
*$
|
||||
* Returns the currently set angle.
|
||||
*
|
||||
* @return the current angle
|
||||
*/
|
||||
public double getAngle() {
|
||||
double voltage = output.getVoltage();
|
||||
double voltage = m_output.getVoltage();
|
||||
if (voltage == 0) { // Removes divide by zero error
|
||||
return 0;
|
||||
}
|
||||
return voltage * (potMaxAngle / potMaxVoltage);
|
||||
return voltage * (m_potMaxAngle / m_potMaxVoltage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees the resouce.
|
||||
*/
|
||||
public void free() {
|
||||
if (m_init_output) {
|
||||
output.free();
|
||||
output = null;
|
||||
m_init_output = false;
|
||||
if (m_initOutput) {
|
||||
m_output.free();
|
||||
m_output = null;
|
||||
m_initOutput = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user