Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
refactor(config): Rewrite config applier API for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenheroux committed May 13, 2024
1 parent 2bdb76d commit b489e2b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 36 deletions.
79 changes: 56 additions & 23 deletions src/main/java/frc/lib/config/ConfigApplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import com.ctre.phoenix6.configs.Pigeon2Configurator;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.configs.TalonFXConfigurator;
import com.ctre.phoenix6.hardware.CANcoder;
import com.ctre.phoenix6.hardware.Pigeon2;
import com.ctre.phoenix6.hardware.TalonFX;
import edu.wpi.first.wpilibj.DriverStation;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -15,13 +19,15 @@ public class ConfigApplier {

/**
* Attempts to apply a config. Returns true if successful.
*
* @param applier a function that attempts to apply a config. Returns the result of the application.
*
* @param applier a function that attempts to apply a config. Returns the result of the
* application.
* @param isSuccess a function that returns true if the result of an application is a success.
* @param retries the number of unsuccessful attempts before failing.
* @return true if successful.
*/
private static <Result> boolean apply(Supplier<Result> applier, Function<Result, Boolean> isSuccess, int retries) {
private static <Result> boolean attempt(
Supplier<Result> applier, Function<Result, Boolean> isSuccess, int retries) {
for (int i = 0; i < retries; i++) {
Result result = applier.get();

Expand All @@ -35,45 +41,72 @@ private static <Result> boolean apply(Supplier<Result> applier, Function<Result,

/**
* Attempts to apply a Phoenix 6 config. Returns true if successful.
*
* @param applier a function that attempts to apply a config. Returns the result of the application.
*
* @param applier a function that attempts to apply a config. Returns the result of the
* application.
* @return true if successful.
*/
private static boolean apply(Supplier<StatusCode> applier) {
return apply(() -> applier.get(), StatusCode::isOK, 10);
private static boolean attempt(Supplier<StatusCode> applier) {
return attempt(() -> applier.get(), StatusCode::isOK, 10);
}

/**
* Configures a CANcoder.
* Attempts to apply a CANcoder config. Warns on failure.
*
* @param configurator the CANcoder's configurator.
* @param cancoder the CANcoder to configure.
* @param config the config to apply.
* @return true if successful.
*/
public static void configureCANcoder(
CANcoderConfigurator configurator, CANcoderConfiguration config) {
apply(() -> configurator.apply(config));
public static boolean applyCANcoderConfig(CANcoder cancoder, CANcoderConfiguration config) {
CANcoderConfigurator configurator = cancoder.getConfigurator();

boolean success = attempt(() -> configurator.apply(config));

if (!success) {
DriverStation.reportWarning(
"Failed to apply config for CANcoder ID: " + cancoder.getDeviceID(), false);
}

return success;
}

/**
* Configures a TalonFX.
* Attempts to apply a TalonFX config. Warns on failure.
*
* @param configurator the TalonFX's configurator.
* @param talonFX the TalonFX to configure.
* @param config the config to apply.
* @return true if successful.
*/
public static void configureTalonFX(
TalonFXConfigurator configurator, TalonFXConfiguration config) {
apply(() -> configurator.apply(config));
public static boolean applyTalonFXConfig(TalonFX talonFX, TalonFXConfiguration config) {
TalonFXConfigurator configurator = talonFX.getConfigurator();

boolean success = attempt(() -> configurator.apply(config));

if (!success) {
DriverStation.reportWarning(
"Failed to apply config for TalonFX ID: " + talonFX.getDeviceID(), false);
}

return success;
}

/**
* Configures a Pigeon 2.
* Attempts to apply a Pigeon 2 config. Warns on failure.
*
* @param configurator the Pigeon 2's configurator.
* @param pigeon2 the Pigeon 2 to configure.
* @param config the config to apply.
* @return true if successful.
*/
public static void configurePigeon2(
Pigeon2Configurator configurator, Pigeon2Configuration config) {
apply(() -> configurator.apply(config));
}
public static boolean applyPigeon2Config(Pigeon2 pigeon2, Pigeon2Configuration config) {
Pigeon2Configurator configurator = pigeon2.getConfigurator();

boolean success = attempt(() -> configurator.apply(config));

if (!success) {
DriverStation.reportWarning(
"Failed to apply config for Pigeon 2 ID: " + pigeon2.getDeviceID(), false);
}

return success;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ public void configure() {

ParentDevice.optimizeBusUtilizationForAll(leaderMotor, followerMotor, encoder);

ConfigApplier.configureTalonFX(
leaderMotor.getConfigurator(), config.motorConfig().createTalonFXConfig());
ConfigApplier.configureTalonFX(
followerMotor.getConfigurator(), config.motorConfig().createTalonFXConfig());
ConfigApplier.applyTalonFXConfig(leaderMotor, config.motorConfig().createTalonFXConfig());
ConfigApplier.applyTalonFXConfig(followerMotor, config.motorConfig().createTalonFXConfig());

ConfigApplier.configureCANcoder(
encoder.getConfigurator(), config.absoluteEncoderConfig().createCANcoderConfig());
ConfigApplier.applyCANcoderConfig(
encoder, config.absoluteEncoderConfig().createCANcoderConfig());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ public void configure() {

ParentDevice.optimizeBusUtilizationForAll(steer, azimuth);

ConfigApplier.configureTalonFX(
steer.getConfigurator(), config.motorConfig().createTalonFXConfig());
ConfigApplier.applyTalonFXConfig(steer, config.motorConfig().createTalonFXConfig());

ConfigApplier.configureCANcoder(
azimuth.getConfigurator(), config.absoluteEncoderConfig().createCANcoderConfig());
ConfigApplier.applyCANcoderConfig(
azimuth, config.absoluteEncoderConfig().createCANcoderConfig());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public void configure() {

ParentDevice.optimizeBusUtilizationForAll(motor);

ConfigApplier.configureTalonFX(
motor.getConfigurator(), config.motorConfig().createTalonFXConfig());
ConfigApplier.applyTalonFXConfig(motor, config.motorConfig().createTalonFXConfig());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/lib/sensor/GyroscopeIOPigeon2.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public GyroscopeIOPigeon2() {

@Override
public void configure() {
ConfigApplier.configurePigeon2(gyroscope.getConfigurator(), new Pigeon2Configuration());
ConfigApplier.applyPigeon2Config(gyroscope, new Pigeon2Configuration());
}

@Override
Expand Down

0 comments on commit b489e2b

Please sign in to comment.