Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Add isConnected method to encoders #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/main/java/com/team766/hal/EncoderReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ enum Type {
*/
void reset();

/**
* Return true iff the encoder's readings are up-to-date.
*/
boolean isConnected();

/**
* Get the distance the robot has driven since the last reset.
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/team766/hal/mock/MockEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class MockEncoder implements EncoderReader {
private double distance = 0;
private double rate = 0;
private double distancePerPulse = 1;
private boolean isConnected = false;

public MockEncoder() {}

Expand All @@ -15,6 +16,11 @@ public void reset() {
distance = 0;
}

@Override
public boolean isConnected() {
return isConnected;
}

@Override
public double getDistance() {
return this.distance;
Expand All @@ -27,10 +33,16 @@ public double getRate() {

public void setDistance(final double distance_) {
this.distance = distance_;
this.isConnected = true;
}

public void setRate(final double rate_) {
this.rate = rate_;
this.isConnected = true;
}

public void disconnect() {
isConnected = false;
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/team766/hal/simulator/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public void reset() {
set(0);
}

@Override
public boolean isConnected() {
return true;
}

@Override
public double getDistance() {
int distance = (int) ProgramInterface.encoderChannels[channel].distance;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/team766/hal/wpilib/CANcoderEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public CANcoderEncoder(int deviceId, String canBus) {
cancoder = new CANcoder(deviceId, canBus);
}

@Override
public boolean isConnected() {
return cancoder.getPosition().getStatus().isOK();
}

@Override
public double getDistance() {
StatusSignal<Double> position = cancoder.getPosition();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/team766/hal/wpilib/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ public class Encoder extends edu.wpi.first.wpilibj.Encoder implements EncoderRea
public Encoder(final int channelA, final int channelB) {
super(channelA, channelB);
}

@Override
public boolean isConnected() {
return true;
}
}
Loading