This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(config): Use device-specific config appliers.
- Loading branch information
1 parent
b489e2b
commit 29d6355
Showing
11 changed files
with
306 additions
and
232 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
73 changes: 73 additions & 0 deletions
73
src/main/java/frc/lib/config/applier/CANcoderConfigApplier.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,73 @@ | ||
package frc.lib.config.applier; | ||
|
||
import com.ctre.phoenix6.configs.CANcoderConfiguration; | ||
import com.ctre.phoenix6.configs.CANcoderConfigurator; | ||
import com.ctre.phoenix6.configs.MagnetSensorConfigs; | ||
import com.ctre.phoenix6.hardware.CANcoder; | ||
import com.ctre.phoenix6.signals.SensorDirectionValue; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import frc.lib.config.AbsoluteEncoderConfig; | ||
|
||
/** Applies CANcoder configs. */ | ||
public class CANcoderConfigApplier extends ConfigApplier { | ||
|
||
/** | ||
* Reports to the user that a CANcoder failed configuration. | ||
* | ||
* @param cancoder the CANcoder. | ||
*/ | ||
private static void report(CANcoder cancoder) { | ||
DriverStation.reportWarning( | ||
"Failed to apply config to CANcoder with ID: " + cancoder.getDeviceID(), false); | ||
} | ||
|
||
/** | ||
* Applies a factory default config to a CANcoder. | ||
* | ||
* @param cancoder the CANcoder. | ||
*/ | ||
public static void applyFactoryDefault(CANcoder cancoder) { | ||
CANcoderConfiguration factoryDefaults = new CANcoderConfiguration(); | ||
|
||
CANcoderConfigurator configurator = cancoder.getConfigurator(); | ||
|
||
if (attempt(() -> configurator.apply(factoryDefaults)) == false) { | ||
report(cancoder); | ||
} | ||
} | ||
|
||
/** | ||
* Creates the magnet sensor configs for the absolute encoder config. | ||
* | ||
* @param absoluteEncoderConfig the absolute encoder config. | ||
* @return the created magnet sensor configs. | ||
*/ | ||
private static MagnetSensorConfigs createMagnetSensorConfigs( | ||
AbsoluteEncoderConfig absoluteEncoderConfig) { | ||
final MagnetSensorConfigs magnetSensorConfigs = new MagnetSensorConfigs(); | ||
|
||
magnetSensorConfigs.MagnetOffset = absoluteEncoderConfig.offset().getRotations(); | ||
magnetSensorConfigs.SensorDirection = | ||
absoluteEncoderConfig.ccwPositive() | ||
? SensorDirectionValue.CounterClockwise_Positive | ||
: SensorDirectionValue.Clockwise_Positive; | ||
|
||
return magnetSensorConfigs; | ||
} | ||
|
||
/** | ||
* Applies an absolute encoder config to a CANcoder. | ||
* | ||
* @param cancoder the CANcoder. | ||
* @param absoluteEncoderConfig the absolute encoder config. | ||
*/ | ||
public static void apply(CANcoder cancoder, AbsoluteEncoderConfig absoluteEncoderConfig) { | ||
MagnetSensorConfigs magnetSensorConfigs = createMagnetSensorConfigs(absoluteEncoderConfig); | ||
|
||
CANcoderConfigurator configurator = cancoder.getConfigurator(); | ||
|
||
if (attempt(() -> configurator.apply(magnetSensorConfigs)) == false) { | ||
report(cancoder); | ||
} | ||
} | ||
} |
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,42 @@ | ||
package frc.lib.config.applier; | ||
|
||
import com.ctre.phoenix6.StatusCode; | ||
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. | ||
*/ | ||
protected static <Result> boolean attempt( | ||
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. | ||
*/ | ||
protected static boolean attempt(Supplier<StatusCode> applier) { | ||
return attempt(() -> applier.get(), StatusCode::isOK, 10); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/frc/lib/config/applier/Pigeon2ConfigApplier.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,35 @@ | ||
package frc.lib.config.applier; | ||
|
||
import com.ctre.phoenix6.configs.Pigeon2Configuration; | ||
import com.ctre.phoenix6.configs.Pigeon2Configurator; | ||
import com.ctre.phoenix6.hardware.Pigeon2; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
|
||
/** Applies Pigeon 2 configs. */ | ||
public class Pigeon2ConfigApplier extends ConfigApplier { | ||
|
||
/** | ||
* Reports to the user that a Pigeon 2 failed configuration. | ||
* | ||
* @param pigeon2 the Pigeon 2. | ||
*/ | ||
private static void report(Pigeon2 pigeon2) { | ||
DriverStation.reportWarning( | ||
"Failed to apply config to Pigeon 2 with ID: " + pigeon2.getDeviceID(), false); | ||
} | ||
|
||
/** | ||
* Applies a factory default config to a Pigeon 2. | ||
* | ||
* @param pigeon2 the Pigeon 2. | ||
*/ | ||
public static void applyFactoryDefault(Pigeon2 pigeon2) { | ||
Pigeon2Configuration factoryDefaults = new Pigeon2Configuration(); | ||
|
||
Pigeon2Configurator configurator = pigeon2.getConfigurator(); | ||
|
||
if (attempt(() -> configurator.apply(factoryDefaults)) == false) { | ||
report(pigeon2); | ||
} | ||
} | ||
} |
Oops, something went wrong.