Skip to content

Commit 40df0f0

Browse files
Merge pull request #57 from Manchester-Central/constants-refactor
Constants refactor
2 parents 52ff316 + 5595d8e commit 40df0f0

File tree

11 files changed

+345
-160
lines changed

11 files changed

+345
-160
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package com.chaos131.can;
6+
7+
/** Constants used for CAN Bus initialization. */
8+
public final class CanConstants {
9+
10+
/** Stores our canbus names. */
11+
public static enum CanBusName {
12+
RIO("rio"),
13+
CTRE("ctre");
14+
15+
public final String name;
16+
17+
private CanBusName(String busName) {
18+
name = busName;
19+
}
20+
}
21+
22+
/**
23+
* This class will help quickly find references to all CanIds (besides from the generated CTRE
24+
* swerve template)
25+
*/
26+
public static enum CanId {
27+
// 0-19, reserved for TunerConstants
28+
29+
// 20s Climber
30+
ID_20(20),
31+
ID_21(21),
32+
ID_22(22),
33+
ID_23(23),
34+
ID_24(24),
35+
ID_25(25),
36+
ID_26(26),
37+
ID_27(27),
38+
ID_28(28),
39+
ID_29(29),
40+
41+
// 30s Intake
42+
ID_30(30),
43+
ID_31(31),
44+
ID_32(32),
45+
ID_33(33),
46+
ID_34(34),
47+
ID_35(35),
48+
ID_36(36),
49+
ID_37(37),
50+
ID_38(38),
51+
ID_39(39),
52+
53+
// 40s Launcher
54+
ID_40(40),
55+
ID_41(41),
56+
ID_42(42),
57+
ID_43(43),
58+
ID_44(44),
59+
ID_45(45),
60+
ID_46(46),
61+
ID_47(47),
62+
ID_48(48),
63+
ID_49(49),
64+
65+
// 50s
66+
ID_50(50),
67+
ID_51(51),
68+
ID_52(52),
69+
ID_53(53),
70+
ID_54(54),
71+
ID_55(55),
72+
ID_56(56),
73+
ID_57(57),
74+
ID_58(58),
75+
ID_59(59),
76+
77+
// 60s
78+
ID_60(60),
79+
ID_61(61),
80+
ID_62(62);
81+
82+
public final int id;
83+
84+
private CanId(int canIdNumber) {
85+
id = canIdNumber;
86+
}
87+
}
88+
}

src/main/java/com/chaos131/util/ChaosCanCoder.java renamed to src/main/java/com/chaos131/ctre/ChaosCanCoder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22
// Open Source Software; you can modify and/or share it under the terms of
33
// the WPILib BSD license file in the root directory of this project.
44

5-
package com.chaos131.util;
5+
package com.chaos131.ctre;
66

7+
import com.chaos131.can.CanConstants.CanBusName;
8+
import com.chaos131.can.CanConstants.CanId;
79
import com.ctre.phoenix6.configs.CANcoderConfiguration;
810
import com.ctre.phoenix6.hardware.CANcoder;
911

1012
/** A TalonFX wrapper with automatic simulation support and helper functions. */
1113
public class ChaosCanCoder extends CANcoder {
12-
public final CANcoderConfiguration Configuration = new CANcoderConfiguration();
14+
public final CANcoderConfiguration Configuration;
1315

1416
/** Creates the new TalonFX wrapper WITHOUT simulation support. */
15-
public ChaosCanCoder(int canId, String canBus) {
16-
super(canId, canBus);
17+
public ChaosCanCoder(CanId canId, CanBusName canBus, CANcoderConfiguration config) {
18+
super(canId.id, canBus.name);
19+
Configuration = config;
20+
}
21+
22+
/** Creates the new TalonFX wrapper WITHOUT simulation support. */
23+
public ChaosCanCoder(CanId canId, CanBusName canBus) {
24+
this(canId, canBus, new CANcoderConfiguration());
1725
}
1826

1927
/** Applies/burns the configuration to the motor. */
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package com.chaos131.ctre;
6+
7+
import com.chaos131.util.DashboardNumber;
8+
import com.ctre.phoenix6.configs.CANcoderConfiguration;
9+
import com.ctre.phoenix6.configs.MagnetSensorConfigs;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.function.BiConsumer;
13+
14+
/** This creates a class to easily tune CANcoderConfiguration for 1 or more CANcoders. */
15+
public class ChaosCanCoderTuner {
16+
private String m_name;
17+
private ChaosCanCoder m_canCoder;
18+
private List<DashboardNumber> m_tunables =
19+
new ArrayList<>(); // Keep in list to prevent any garbage collection
20+
21+
/**
22+
* Creates a tuner for modifying numeric values of TalonFxConfigs.
23+
*
24+
* @param name the name of the motor tuner
25+
* @param canCoder the list of talons to tune
26+
*/
27+
public ChaosCanCoderTuner(String name, ChaosCanCoder canCoder) {
28+
m_name = name;
29+
m_canCoder = canCoder;
30+
}
31+
32+
/**
33+
* Creates tunables for the MagnetSensorConfigs number values
34+
*
35+
* @param initialConfig the starting MagnetSensorConfigs values
36+
*/
37+
public void tunableMagnetSensor(MagnetSensorConfigs initialConfig) {
38+
tunable(
39+
"DiscontinuityPoint_rotations",
40+
initialConfig.AbsoluteSensorDiscontinuityPoint,
41+
(config, newValue) -> config.MagnetSensor.AbsoluteSensorDiscontinuityPoint = newValue);
42+
tunable(
43+
"Offset_rotations",
44+
initialConfig.MagnetOffset,
45+
(config, newValue) -> config.MagnetSensor.MagnetOffset = newValue);
46+
}
47+
48+
/**
49+
* Creates a tunable value for the TalonFxConfiguration and will apply/burn the value to the motor
50+
* when it changes.
51+
*
52+
* @param valueName the name of the value (e.g., "SupplyCurrentLimit")
53+
* @param initialValue the value to start at (is not applied by default)
54+
* @param onUpdate the function to update the configuration
55+
* @return the Dashboard number
56+
*/
57+
public DashboardNumber tunable(
58+
String valueName, double initialValue, BiConsumer<CANcoderConfiguration, Double> onUpdate) {
59+
DashboardNumber dsNumber =
60+
new DashboardNumber(
61+
"CANCoderConfig/" + m_name + "/" + valueName,
62+
initialValue,
63+
true,
64+
false,
65+
newValue -> {
66+
onUpdate.accept(m_canCoder.Configuration, newValue);
67+
m_canCoder.applyConfig();
68+
});
69+
m_tunables.add(dsNumber);
70+
return dsNumber;
71+
}
72+
}

src/main/java/com/chaos131/util/ChaosTalonFx.java renamed to src/main/java/com/chaos131/ctre/ChaosTalonFx.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Open Source Software; you can modify and/or share it under the terms of
33
// the WPILib BSD license file in the root directory of this project.
44

5-
package com.chaos131.util;
5+
package com.chaos131.ctre;
66

77
import static edu.wpi.first.units.Units.Kilograms;
88
import static edu.wpi.first.units.Units.Radians;
99
import static edu.wpi.first.units.Units.Volts;
1010

11+
import com.chaos131.can.CanConstants.CanBusName;
12+
import com.chaos131.can.CanConstants.CanId;
1113
import com.chaos131.pid.PIDFValue;
1214
import com.ctre.phoenix6.configs.Slot0Configs;
1315
import com.ctre.phoenix6.configs.TalonFXConfiguration;
@@ -23,7 +25,6 @@
2325
import edu.wpi.first.units.measure.Angle;
2426
import edu.wpi.first.units.measure.AngularVelocity;
2527
import edu.wpi.first.units.measure.Mass;
26-
import edu.wpi.first.units.measure.Velocity;
2728
import edu.wpi.first.wpilibj.RobotController;
2829
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
2930

@@ -38,16 +39,22 @@ public class ChaosTalonFx extends TalonFX {
3839
private final MotionMagicVoltage m_positionMotionMagicVoltage = new MotionMagicVoltage(0);
3940
private final DynamicMotionMagicVoltage m_positionDynamicMotionMagicVoltage =
4041
new DynamicMotionMagicVoltage(0, 0, 0);
41-
public final TalonFXConfiguration Configuration = new TalonFXConfiguration();
42+
public final TalonFXConfiguration Configuration;
4243

4344
/** Creates the new TalonFX wrapper WITHOUT simulation support. */
44-
public ChaosTalonFx(int canId, String canBus) {
45-
super(canId, canBus);
45+
public ChaosTalonFx(CanId canId, CanBusName canBus, TalonFXConfiguration config) {
46+
super(canId.id, canBus.name);
47+
Configuration = config;
4648
this.m_gearRatio = 0.0;
4749
m_motorSimModel = null;
4850
m_isMainSimMotor = false;
4951
}
5052

53+
/** Creates the new TalonFX wrapper WITHOUT simulation support. */
54+
public ChaosTalonFx(CanId canId, CanBusName canBus) {
55+
this(canId, canBus, new TalonFXConfiguration());
56+
}
57+
5158
/** Adds physical simulation support. */
5259
public void attachMotorSim(
5360
DCMotorSim dcMotorSim,
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package com.chaos131.ctre;
6+
7+
import com.chaos131.util.DashboardNumber;
8+
import com.ctre.phoenix6.configs.Slot0Configs;
9+
import com.ctre.phoenix6.configs.TalonFXConfiguration;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.function.BiConsumer;
13+
14+
/** This creates a class to easily tune TalonFXConfigs for 1 or more motors. */
15+
public class ChaosTalonFxTuner {
16+
private String m_name;
17+
private ChaosTalonFx[] m_talons;
18+
private List<DashboardNumber> m_tunables =
19+
new ArrayList<>(); // Keep in list to prevent any garbage collection
20+
21+
/**
22+
* Creates a tuner for modifying numeric values of TalonFxConfigs.
23+
*
24+
* @param name the name of the motor tuner
25+
* @param talons the list of talons to tune
26+
*/
27+
public ChaosTalonFxTuner(String name, ChaosTalonFx... talons) {
28+
m_name = name;
29+
m_talons = talons;
30+
}
31+
32+
/**
33+
* Creates tunables for the Slot0 number values
34+
*
35+
* @param initialConfig the starting Slot0 values
36+
*/
37+
public void tunableSlot0(Slot0Configs initialConfig) {
38+
tunable("Slot0_kP", initialConfig.kP, (config, newValue) -> config.Slot0.kP = newValue);
39+
tunable("Slot0_kI", initialConfig.kI, (config, newValue) -> config.Slot0.kI = newValue);
40+
tunable("Slot0_kD", initialConfig.kD, (config, newValue) -> config.Slot0.kD = newValue);
41+
tunable("Slot0_kG", initialConfig.kG, (config, newValue) -> config.Slot0.kG = newValue);
42+
tunable("Slot0_kS", initialConfig.kS, (config, newValue) -> config.Slot0.kS = newValue);
43+
tunable("Slot0_kV", initialConfig.kV, (config, newValue) -> config.Slot0.kV = newValue);
44+
tunable("Slot0_kA", initialConfig.kA, (config, newValue) -> config.Slot0.kA = newValue);
45+
}
46+
47+
/**
48+
* Creates a tunable value for the TalonFxConfiguration and will apply/burn the value to the motor
49+
* when it changes.
50+
*
51+
* @param valueName the name of the value (e.g., "SupplyCurrentLimit")
52+
* @param initialValue the value to start at (is not applied by default)
53+
* @param onUpdate the function to update the configuration
54+
* @return the Dashboard number
55+
*/
56+
public DashboardNumber tunable(
57+
String valueName, double initialValue, BiConsumer<TalonFXConfiguration, Double> onUpdate) {
58+
DashboardNumber dsNumber =
59+
new DashboardNumber(
60+
"TalonFxConfig/" + m_name + "/" + valueName,
61+
initialValue,
62+
true,
63+
false,
64+
newValue -> {
65+
for (ChaosTalonFx chaosTalonFx : m_talons) {
66+
onUpdate.accept(chaosTalonFx.Configuration, newValue);
67+
chaosTalonFx.applyConfig();
68+
}
69+
});
70+
m_tunables.add(dsNumber);
71+
return dsNumber;
72+
}
73+
}

src/main/java/com/chaos131/util/ChaosTalonFxs.java renamed to src/main/java/com/chaos131/ctre/ChaosTalonFxs.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Open Source Software; you can modify and/or share it under the terms of
33
// the WPILib BSD license file in the root directory of this project.
44

5-
package com.chaos131.util;
5+
package com.chaos131.ctre;
66

77
import static edu.wpi.first.units.Units.Radians;
88
import static edu.wpi.first.units.Units.Volts;
99

10+
import com.chaos131.can.CanConstants.CanBusName;
11+
import com.chaos131.can.CanConstants.CanId;
1012
import com.chaos131.pid.PIDFValue;
1113
import com.ctre.phoenix6.configs.Slot0Configs;
1214
import com.ctre.phoenix6.configs.TalonFXSConfiguration;
@@ -31,16 +33,22 @@ public class ChaosTalonFxs extends TalonFXS {
3133
private final MotionMagicVoltage m_positionMotionMagicVoltage = new MotionMagicVoltage(0);
3234
private final DynamicMotionMagicVoltage m_positionDynamicMotionMagicVoltage =
3335
new DynamicMotionMagicVoltage(0, 0, 0);
34-
public final TalonFXSConfiguration Configuration = new TalonFXSConfiguration();
36+
public final TalonFXSConfiguration Configuration;
3537

3638
/** Creates the new TalonFX wrapper WITHOUT simulation support. */
37-
public ChaosTalonFxs(int canId, String canBus) {
38-
super(canId, canBus);
39+
public ChaosTalonFxs(CanId canId, CanBusName canBus, TalonFXSConfiguration config) {
40+
super(canId.id, canBus.name);
41+
Configuration = config;
3942
this.m_gearRatio = 0.0;
4043
m_motorSimModel = null;
4144
m_isMainSimMotor = false;
4245
}
4346

47+
/** Creates the new TalonFX wrapper WITHOUT simulation support. */
48+
public ChaosTalonFxs(CanId canId, CanBusName canBus) {
49+
this(canId, canBus, new TalonFXSConfiguration());
50+
}
51+
4452
/** Adds physical simulation support. */
4553
public void attachMotorSim(
4654
DCMotorSim dcMotorSim,

0 commit comments

Comments
 (0)