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

Commit

Permalink
refactor(config): Rename configurator to config applier.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenheroux committed May 13, 2024
1 parent f190f64 commit 2bdb76d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 122 deletions.
79 changes: 79 additions & 0 deletions src/main/java/frc/lib/config/ConfigApplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package frc.lib.config;

import com.ctre.phoenix6.StatusCode;
import com.ctre.phoenix6.configs.CANcoderConfiguration;
import com.ctre.phoenix6.configs.CANcoderConfigurator;
import com.ctre.phoenix6.configs.Pigeon2Configuration;
import com.ctre.phoenix6.configs.Pigeon2Configurator;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.configs.TalonFXConfigurator;
import java.util.function.Function;
import java.util.function.Supplier;

/** Applies configs. */
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 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) {
for (int i = 0; i < retries; i++) {
Result result = applier.get();

if (isSuccess.apply(result)) {
return true;
}
}

return false;
}

/**
* 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.
* @return true if successful.
*/
private static boolean apply(Supplier<StatusCode> applier) {
return apply(() -> applier.get(), StatusCode::isOK, 10);
}

/**
* Configures a CANcoder.
*
* @param configurator the CANcoder's configurator.
* @param config the config to apply.
*/
public static void configureCANcoder(
CANcoderConfigurator configurator, CANcoderConfiguration config) {
apply(() -> configurator.apply(config));
}

/**
* Configures a TalonFX.
*
* @param configurator the TalonFX's configurator.
* @param config the config to apply.
*/
public static void configureTalonFX(
TalonFXConfigurator configurator, TalonFXConfiguration config) {
apply(() -> configurator.apply(config));
}

/**
* Configures a Pigeon 2.
*
* @param configurator the Pigeon 2's configurator.
* @param config the config to apply.
*/
public static void configurePigeon2(
Pigeon2Configurator configurator, Pigeon2Configuration config) {
apply(() -> configurator.apply(config));
}

}
111 changes: 0 additions & 111 deletions src/main/java/frc/lib/config/Configurator.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.util.Units;
import frc.lib.CAN;
import frc.lib.config.Configurator;
import frc.lib.config.ConfigApplier;
import frc.lib.config.MechanismConfig;

/** Position controller using two TalonFXs and a CANcoder and an external PIDF for an arm. */
Expand Down Expand Up @@ -79,12 +79,12 @@ public void configure() {

ParentDevice.optimizeBusUtilizationForAll(leaderMotor, followerMotor, encoder);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import frc.lib.CAN;
import frc.lib.config.Configurator;
import frc.lib.config.ConfigApplier;
import frc.lib.config.MechanismConfig;

/** Creates a new position controller using a steer TalonFX and azimuth CANcoder. */
Expand Down Expand Up @@ -59,10 +59,10 @@ public void configure() {

ParentDevice.optimizeBusUtilizationForAll(steer, azimuth);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.ctre.phoenix6.hardware.ParentDevice;
import com.ctre.phoenix6.hardware.TalonFX;
import frc.lib.CAN;
import frc.lib.config.Configurator;
import frc.lib.config.ConfigApplier;
import frc.lib.config.MechanismConfig;

/** Velocity controller using TalonFX. */
Expand Down Expand Up @@ -40,7 +40,7 @@ public void configure() {

ParentDevice.optimizeBusUtilizationForAll(motor);

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

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/frc/lib/sensor/GyroscopeIOPigeon2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.ctre.phoenix6.configs.Pigeon2Configuration;
import com.ctre.phoenix6.hardware.Pigeon2;
import edu.wpi.first.math.util.Units;
import frc.lib.config.Configurator;
import frc.lib.config.ConfigApplier;

/** Pigeon 2 gyroscope. */
public class GyroscopeIOPigeon2 implements GyroscopeIO {
Expand All @@ -30,7 +30,7 @@ public GyroscopeIOPigeon2() {

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

@Override
Expand Down

0 comments on commit 2bdb76d

Please sign in to comment.