Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,9 @@ public void close() {
*/
public synchronized SparkAbsoluteEncoder getAbsoluteEncoder(SparkAbsoluteEncoder.Type encoderType) {
if(absoluteEncoder == null) {
MockedEncoder absoluteEncoderImpl = new MockedEncoder(SimDevice.create("CANDutyCycle:" + name, port), 0, false, true) {
@Override
public double getVelocity() {
// A SparkAbsoluteEncoder returns a velocity in rps, not rpm.
return super.getVelocity() / 60.0;
}
};
MockedEncoder absoluteEncoderImpl = new MockedEncoder(
SimDevice.create("CANDutyCycle:" + name, port), 0, false,
true, true);
absoluteEncoder = Mocks.createMock(SparkAbsoluteEncoder.class, absoluteEncoderImpl, new REVLibErrorAnswer());
}
return absoluteEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MockedEncoder implements AbsoluteEncoder, AnalogInput, AutoCloseabl
protected final SimBoolean init;
protected final int countsPerRev;
protected final boolean absolute;
protected final boolean useRps;
protected double positionConversionFactor = 1.0;
protected double velocityConversionFactor = 1.0;
protected double positionOffset = 0.0;
Expand All @@ -41,11 +42,28 @@ public class MockedEncoder implements AbsoluteEncoder, AnalogInput, AutoCloseabl
* @param device The device to retrieve position and velocity data from
* @param countsPerRev The value that this.getCountsPerRevolution() should return
* @param analog Whether the encoder is an analog sensor
* @param absolute Whether the encoder is an absolute encoder.
* This flag caps the position to one rotation via. {@link MathUtil#inputModulus(double, double, double)},
* disables {@link #setPosition(double)}, and enables {@link #setZeroOffset(double)}.
* @param absolute Whether the encoder is an absolute encoder. This flag caps the position to
* one rotation via. {@link MathUtil#inputModulus(double, double, double)}, disables
* {@link #setPosition(double)}, and enables {@link #setZeroOffset(double)}.
Comment thread
CoolSpy3 marked this conversation as resolved.
*
* {@link #getVelocity()} will return a value in rpm.
*/
public MockedEncoder(SimDevice device, int countsPerRev, boolean analog, boolean absolute) {
public MockedEncoder(SimDevice device, int countsPerRev, boolean analog,
boolean absolute) {
this(device, countsPerRev, analog, absolute, false);
}

/**
* @param device The device to retrieve position and velocity data from
* @param countsPerRev The value that this.getCountsPerRevolution() should return
* @param analog Whether the encoder is an analog sensor
* @param absolute Whether the encoder is an absolute encoder. This flag caps the position to
* one rotation via. {@link MathUtil#inputModulus(double, double, double)}, disables
* {@link #setPosition(double)}, and enables {@link #setZeroOffset(double)}.
* @param useRps Whether getVelocity() should return rps instead of rpm.
*/
public MockedEncoder(SimDevice device, int countsPerRev, boolean analog,
boolean absolute, boolean useRps) {
this.device = device;
position = device.createDouble("position", Direction.kInput, 0); // Rotations
velocity = device.createDouble("velocity", Direction.kInput, 0); // Rotations per *second*
Expand All @@ -56,6 +74,7 @@ public MockedEncoder(SimDevice device, int countsPerRev, boolean analog, boolean
}
this.countsPerRev = countsPerRev;
this.absolute = absolute;
this.useRps = useRps;
init = device.createBoolean("init", Direction.kOutput, true);
}

Expand Down Expand Up @@ -105,7 +124,8 @@ public double getPosition() {

@Override
public double getVelocity() {
return velocity.get() * 60 * (inverted ? -1 : 1) * velocityConversionFactor;
return velocity.get() * (useRps ? 1 : 60) * (inverted ? -1 : 1)
* velocityConversionFactor;
}

@Override
Expand Down