7
7
import com .ctre .phoenix6 .configs .Pigeon2Configurator ;
8
8
import com .ctre .phoenix6 .configs .TalonFXConfiguration ;
9
9
import com .ctre .phoenix6 .configs .TalonFXConfigurator ;
10
+ import com .ctre .phoenix6 .hardware .CANcoder ;
11
+ import com .ctre .phoenix6 .hardware .Pigeon2 ;
12
+ import com .ctre .phoenix6 .hardware .TalonFX ;
13
+ import edu .wpi .first .wpilibj .DriverStation ;
10
14
import java .util .function .Function ;
11
15
import java .util .function .Supplier ;
12
16
@@ -15,13 +19,15 @@ public class ConfigApplier {
15
19
16
20
/**
17
21
* Attempts to apply a config. Returns true if successful.
18
- *
19
- * @param applier a function that attempts to apply a config. Returns the result of the application.
22
+ *
23
+ * @param applier a function that attempts to apply a config. Returns the result of the
24
+ * application.
20
25
* @param isSuccess a function that returns true if the result of an application is a success.
21
26
* @param retries the number of unsuccessful attempts before failing.
22
27
* @return true if successful.
23
28
*/
24
- private static <Result > boolean apply (Supplier <Result > applier , Function <Result , Boolean > isSuccess , int retries ) {
29
+ private static <Result > boolean attempt (
30
+ Supplier <Result > applier , Function <Result , Boolean > isSuccess , int retries ) {
25
31
for (int i = 0 ; i < retries ; i ++) {
26
32
Result result = applier .get ();
27
33
@@ -35,45 +41,72 @@ private static <Result> boolean apply(Supplier<Result> applier, Function<Result,
35
41
36
42
/**
37
43
* Attempts to apply a Phoenix 6 config. Returns true if successful.
38
- *
39
- * @param applier a function that attempts to apply a config. Returns the result of the application.
44
+ *
45
+ * @param applier a function that attempts to apply a config. Returns the result of the
46
+ * application.
40
47
* @return true if successful.
41
48
*/
42
- private static boolean apply (Supplier <StatusCode > applier ) {
43
- return apply (() -> applier .get (), StatusCode ::isOK , 10 );
49
+ private static boolean attempt (Supplier <StatusCode > applier ) {
50
+ return attempt (() -> applier .get (), StatusCode ::isOK , 10 );
44
51
}
45
52
46
53
/**
47
- * Configures a CANcoder.
54
+ * Attempts to apply a CANcoder config. Warns on failure .
48
55
*
49
- * @param configurator the CANcoder's configurator .
56
+ * @param cancoder the CANcoder to configure .
50
57
* @param config the config to apply.
58
+ * @return true if successful.
51
59
*/
52
- public static void configureCANcoder (
53
- CANcoderConfigurator configurator , CANcoderConfiguration config ) {
54
- apply (() -> configurator .apply (config ));
60
+ public static boolean applyCANcoderConfig (CANcoder cancoder , CANcoderConfiguration config ) {
61
+ CANcoderConfigurator configurator = cancoder .getConfigurator ();
62
+
63
+ boolean success = attempt (() -> configurator .apply (config ));
64
+
65
+ if (!success ) {
66
+ DriverStation .reportWarning (
67
+ "Failed to apply config for CANcoder ID: " + cancoder .getDeviceID (), false );
68
+ }
69
+
70
+ return success ;
55
71
}
56
72
57
73
/**
58
- * Configures a TalonFX.
74
+ * Attempts to apply a TalonFX config. Warns on failure .
59
75
*
60
- * @param configurator the TalonFX's configurator .
76
+ * @param talonFX the TalonFX to configure .
61
77
* @param config the config to apply.
78
+ * @return true if successful.
62
79
*/
63
- public static void configureTalonFX (
64
- TalonFXConfigurator configurator , TalonFXConfiguration config ) {
65
- apply (() -> configurator .apply (config ));
80
+ public static boolean applyTalonFXConfig (TalonFX talonFX , TalonFXConfiguration config ) {
81
+ TalonFXConfigurator configurator = talonFX .getConfigurator ();
82
+
83
+ boolean success = attempt (() -> configurator .apply (config ));
84
+
85
+ if (!success ) {
86
+ DriverStation .reportWarning (
87
+ "Failed to apply config for TalonFX ID: " + talonFX .getDeviceID (), false );
88
+ }
89
+
90
+ return success ;
66
91
}
67
92
68
93
/**
69
- * Configures a Pigeon 2.
94
+ * Attempts to apply a Pigeon 2 config. Warns on failure .
70
95
*
71
- * @param configurator the Pigeon 2's configurator .
96
+ * @param pigeon2 the Pigeon 2 to configure .
72
97
* @param config the config to apply.
98
+ * @return true if successful.
73
99
*/
74
- public static void configurePigeon2 (
75
- Pigeon2Configurator configurator , Pigeon2Configuration config ) {
76
- apply (() -> configurator .apply (config ));
77
- }
100
+ public static boolean applyPigeon2Config (Pigeon2 pigeon2 , Pigeon2Configuration config ) {
101
+ Pigeon2Configurator configurator = pigeon2 .getConfigurator ();
78
102
103
+ boolean success = attempt (() -> configurator .apply (config ));
104
+
105
+ if (!success ) {
106
+ DriverStation .reportWarning (
107
+ "Failed to apply config for Pigeon 2 ID: " + pigeon2 .getDeviceID (), false );
108
+ }
109
+
110
+ return success ;
111
+ }
79
112
}
0 commit comments