-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement DTCANCoder.java
#16
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/org/victorrobotics/frc/dtlib/sensor/encoder/DTAbsoluteEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.victorrobotics.frc.dtlib.sensor.encoder; | ||
|
||
import org.victorrobotics.frc.dtlib.network.DTSendable; | ||
|
||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.util.sendable.SendableBuilder; | ||
|
||
public interface DTAbsoluteEncoder extends DTSendable { | ||
Object getEncoderImpl(); | ||
|
||
Rotation2d getPosition(); | ||
|
||
Rotation2d getAbsolutePosition(); | ||
|
||
Rotation2d getVelocity(); | ||
|
||
String getFirmwareVersion(); | ||
|
||
DTAbsoluteEncoderFaults getFaults(); | ||
|
||
boolean isInverted(); | ||
|
||
void setRange(boolean signed); | ||
|
||
void setInverted(boolean invert); | ||
|
||
void setPosition(Rotation2d position); | ||
|
||
void setZeroPosition(Rotation2d position); | ||
|
||
void zeroPosition(); | ||
|
||
@Override | ||
void close(); | ||
|
||
@Override | ||
default void initSendable(SendableBuilder builder) { | ||
builder.addDoubleProperty("Position", limitRate(() -> getPosition().getDegrees(), UPDATE_RATE_FAST_HZ), | ||
d -> setPosition(Rotation2d.fromDegrees(d))); | ||
builder.addDoubleProperty("Absolute", limitRate(() -> getAbsolutePosition().getDegrees(), UPDATE_RATE_FAST_HZ), | ||
null); | ||
|
||
builder.addBooleanProperty("Inverted", limitRate(this::isInverted, UPDATE_RATE_SLOW_HZ), this::setInverted); | ||
builder.addStringProperty("Firmware", limitRate(this::getFirmwareVersion, UPDATE_RATE_FAST_HZ), null); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/victorrobotics/frc/dtlib/sensor/encoder/DTAbsoluteEncoderFaults.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.victorrobotics.frc.dtlib.sensor.encoder; | ||
|
||
public interface DTAbsoluteEncoderFaults { | ||
boolean lowVoltage(); | ||
|
||
boolean hardwareFailure(); | ||
|
||
boolean hasAnyFault(); | ||
|
||
boolean other(); | ||
} |
131 changes: 131 additions & 0 deletions
131
src/main/java/org/victorrobotics/frc/dtlib/sensor/encoder/ctre/DTCANCoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.victorrobotics.frc.dtlib.sensor.encoder.ctre; | ||
|
||
import org.victorrobotics.frc.dtlib.sensor.encoder.DTAbsoluteEncoder; | ||
import org.victorrobotics.frc.dtlib.sensor.encoder.DTAbsoluteEncoderFaults; | ||
|
||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.util.sendable.SendableBuilder; | ||
import edu.wpi.first.wpilibj.Encoder; | ||
import edu.wpi.first.wpilibj.simulation.EncoderSim; | ||
|
||
import com.ctre.phoenix.sensors.AbsoluteSensorRange; | ||
import com.ctre.phoenix.sensors.CANCoder; | ||
import com.ctre.phoenix.sensors.CANCoderFaults; | ||
|
||
public class DTCANCoder implements DTAbsoluteEncoder { | ||
private final CANCoder internal; | ||
|
||
private String firmwareVersion; | ||
|
||
public DTCANCoder(int canID) { | ||
this(canID, ""); | ||
} | ||
|
||
public DTCANCoder(int canID, String canBus) { | ||
internal = new CANCoder(canID, canBus); | ||
} | ||
|
||
@Override | ||
public CANCoder getEncoderImpl() { | ||
return internal; | ||
} | ||
|
||
public void configFactoryDefault() { | ||
internal.configFactoryDefault(); | ||
} | ||
|
||
@Override | ||
public Rotation2d getPosition() { | ||
return Rotation2d.fromDegrees(internal.getPosition()); | ||
} | ||
|
||
@Override | ||
public Rotation2d getAbsolutePosition() { | ||
return Rotation2d.fromDegrees(internal.getAbsolutePosition()); | ||
} | ||
|
||
public boolean isInverted() { | ||
return internal.configGetSensorDirection(); | ||
} | ||
|
||
@Override | ||
public void setRange(boolean signed) { | ||
internal.configAbsoluteSensorRange( | ||
signed ? AbsoluteSensorRange.Signed_PlusMinus180 : AbsoluteSensorRange.Unsigned_0_to_360); | ||
} | ||
|
||
@Override | ||
public void setInverted(boolean invert) { | ||
internal.configSensorDirection(invert); | ||
} | ||
|
||
@Override | ||
public void setPosition(Rotation2d position) { | ||
internal.setPosition(position.getDegrees() % 360); | ||
} | ||
|
||
@Override | ||
public void setZeroPosition(Rotation2d position) { | ||
double currentPos = internal.getAbsolutePosition(); | ||
internal.setPosition((currentPos - position.getDegrees()) % 360); | ||
} | ||
|
||
@Override | ||
public void zeroPosition() { | ||
internal.setPosition(0); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// nothing here | ||
} | ||
|
||
@Override | ||
public Rotation2d getVelocity() { | ||
return Rotation2d.fromDegrees(internal.getVelocity()); | ||
} | ||
|
||
@Override | ||
public String getFirmwareVersion() { | ||
if (firmwareVersion == null) { | ||
int version = internal.getFirmwareVersion(); | ||
firmwareVersion = (version >> 8) + "." + (version & 0xFF); | ||
} | ||
return firmwareVersion; | ||
} | ||
|
||
@Override | ||
public DTAbsoluteEncoderFaults getFaults() { | ||
CANCoderFaults faults = new CANCoderFaults(); | ||
internal.getFaults(faults); | ||
return new DTCANCoderFaults(faults); | ||
} | ||
|
||
public static class DTCANCoderFaults implements DTAbsoluteEncoderFaults { | ||
private final CANCoderFaults internal; | ||
|
||
DTCANCoderFaults(CANCoderFaults internal) { | ||
this.internal = internal; | ||
} | ||
|
||
@Override | ||
public boolean lowVoltage() { | ||
return internal.UnderVoltage; | ||
} | ||
|
||
@Override | ||
public boolean hardwareFailure() { | ||
return internal.HardwareFault || internal.MagnetTooWeak; | ||
} | ||
|
||
@Override | ||
public boolean hasAnyFault() { | ||
return internal.hasAnyFault(); | ||
} | ||
|
||
@Override | ||
public boolean other() { | ||
return internal.APIError || internal.ResetDuringEn; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one of the constructors calls
setPositionToAbsolute
, is that intentional?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I recall that the cancoder can be configured to return values from -180 to 180, or 0 to 360, and it seems like we might want different zero positions depending on the application.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the constructor, and that is a good addition