Skip to content

Commit 400693b

Browse files
authored
Merge pull request #103 from DeepBlueRobotics/fix-error-when-accessing-SparkBase.getAbsoluteEncoder().getVelocity()
Fix-error-when-accessing-SparkBase.getAbsoluteEncoder().getVelocity()
2 parents b0ff5b7 + 3672212 commit 400693b

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

src/main/java/org/carlmontrobotics/lib199/sim/MockSparkBase.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,9 @@ public void close() {
196196
*/
197197
public synchronized SparkAbsoluteEncoder getAbsoluteEncoder(SparkAbsoluteEncoder.Type encoderType) {
198198
if(absoluteEncoder == null) {
199-
MockedEncoder absoluteEncoderImpl = new MockedEncoder(SimDevice.create("CANDutyCycle:" + name, port), 0, false, true) {
200-
@Override
201-
public double getVelocity() {
202-
// A SparkAbsoluteEncoder returns a velocity in rps, not rpm.
203-
return super.getVelocity() / 60.0;
204-
}
205-
};
199+
MockedEncoder absoluteEncoderImpl = new MockedEncoder(
200+
SimDevice.create("CANDutyCycle:" + name, port), 0, false,
201+
true, true);
206202
absoluteEncoder = Mocks.createMock(SparkAbsoluteEncoder.class, absoluteEncoderImpl, new REVLibErrorAnswer());
207203
}
208204
return absoluteEncoder;

src/main/java/org/carlmontrobotics/lib199/sim/MockedEncoder.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class MockedEncoder implements AbsoluteEncoder, AnalogInput, AutoCloseabl
3232
protected final SimBoolean init;
3333
protected final int countsPerRev;
3434
protected final boolean absolute;
35+
protected final boolean useRps;
3536
protected double positionConversionFactor = 1.0;
3637
protected double velocityConversionFactor = 1.0;
3738
protected double positionOffset = 0.0;
@@ -41,11 +42,28 @@ public class MockedEncoder implements AbsoluteEncoder, AnalogInput, AutoCloseabl
4142
* @param device The device to retrieve position and velocity data from
4243
* @param countsPerRev The value that this.getCountsPerRevolution() should return
4344
* @param analog Whether the encoder is an analog sensor
44-
* @param absolute Whether the encoder is an absolute encoder.
45-
* This flag caps the position to one rotation via. {@link MathUtil#inputModulus(double, double, double)},
46-
* disables {@link #setPosition(double)}, and enables {@link #setZeroOffset(double)}.
45+
* @param absolute Whether the encoder is an absolute encoder. This flag caps the position to
46+
* one rotation via. {@link MathUtil#inputModulus(double, double, double)}, disables
47+
* {@link #setPosition(double)}, and enables {@link #setZeroOffset(double)}.
48+
*
49+
* {@link #getVelocity()} will return a value in rpm.
4750
*/
48-
public MockedEncoder(SimDevice device, int countsPerRev, boolean analog, boolean absolute) {
51+
public MockedEncoder(SimDevice device, int countsPerRev, boolean analog,
52+
boolean absolute) {
53+
this(device, countsPerRev, analog, absolute, false);
54+
}
55+
56+
/**
57+
* @param device The device to retrieve position and velocity data from
58+
* @param countsPerRev The value that this.getCountsPerRevolution() should return
59+
* @param analog Whether the encoder is an analog sensor
60+
* @param absolute Whether the encoder is an absolute encoder. This flag caps the position to
61+
* one rotation via. {@link MathUtil#inputModulus(double, double, double)}, disables
62+
* {@link #setPosition(double)}, and enables {@link #setZeroOffset(double)}.
63+
* @param useRps Whether getVelocity() should return rps instead of rpm.
64+
*/
65+
public MockedEncoder(SimDevice device, int countsPerRev, boolean analog,
66+
boolean absolute, boolean useRps) {
4967
this.device = device;
5068
position = device.createDouble("position", Direction.kInput, 0); // Rotations
5169
velocity = device.createDouble("velocity", Direction.kInput, 0); // Rotations per *second*
@@ -56,6 +74,7 @@ public MockedEncoder(SimDevice device, int countsPerRev, boolean analog, boolean
5674
}
5775
this.countsPerRev = countsPerRev;
5876
this.absolute = absolute;
77+
this.useRps = useRps;
5978
init = device.createBoolean("init", Direction.kOutput, true);
6079
}
6180

@@ -105,7 +124,8 @@ public double getPosition() {
105124

106125
@Override
107126
public double getVelocity() {
108-
return velocity.get() * 60 * (inverted ? -1 : 1) * velocityConversionFactor;
127+
return velocity.get() * (useRps ? 1 : 60) * (inverted ? -1 : 1)
128+
* velocityConversionFactor;
109129
}
110130

111131
@Override

0 commit comments

Comments
 (0)