Skip to content

Commit 4078c12

Browse files
Merge pull request #60 from Manchester-Central/cleanup+improvements
Cleanup+improvements
2 parents 9d8803a + 4910fb4 commit 4078c12

20 files changed

+398
-55
lines changed

release-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"major_version": 2026,
3-
"minor_version": 0
3+
"minor_version": 1
44
}

src/main/java/com/chaos131/ctre/ChaosCanCoderTuner.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ public ChaosCanCoderTuner(String name, ChaosCanCoder canCoder) {
2929
m_canCoder = canCoder;
3030
}
3131

32+
/** Creates tunables for the MagnetSensorConfigs number values using the cancoder's config */
33+
public ChaosCanCoderTuner withMagnetSensor() {
34+
return withMagnetSensor(m_canCoder.Configuration.MagnetSensor);
35+
}
36+
3237
/**
3338
* Creates tunables for the MagnetSensorConfigs number values
3439
*
3540
* @param initialConfig the starting MagnetSensorConfigs values
3641
*/
37-
public void tunableMagnetSensor(MagnetSensorConfigs initialConfig) {
42+
public ChaosCanCoderTuner withMagnetSensor(MagnetSensorConfigs initialConfig) {
3843
tunable(
3944
"DiscontinuityPoint_rotations",
4045
initialConfig.AbsoluteSensorDiscontinuityPoint,
@@ -43,6 +48,7 @@ public void tunableMagnetSensor(MagnetSensorConfigs initialConfig) {
4348
"Offset_rotations",
4449
initialConfig.MagnetOffset,
4550
(config, newValue) -> config.MagnetSensor.MagnetOffset = newValue);
51+
return this;
4652
}
4753

4854
/**
@@ -58,9 +64,8 @@ public DashboardNumber tunable(
5864
String valueName, double initialValue, BiConsumer<CANcoderConfiguration, Double> onUpdate) {
5965
DashboardNumber dsNumber =
6066
new DashboardNumber(
61-
"CANCoderConfig/" + m_name + "/" + valueName,
67+
m_name + "/" + valueName,
6268
initialValue,
63-
true,
6469
false,
6570
newValue -> {
6671
onUpdate.accept(m_canCoder.Configuration, newValue);

src/main/java/com/chaos131/ctre/ChaosTalonFx.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.chaos131.can.CanConstants.CanBusName;
1212
import com.chaos131.can.CanConstants.CanId;
1313
import com.chaos131.pid.PIDFValue;
14+
import com.chaos131.util.PeriodicTasks;
1415
import com.ctre.phoenix6.configs.Slot0Configs;
1516
import com.ctre.phoenix6.configs.TalonFXConfiguration;
1617
import com.ctre.phoenix6.controls.DynamicMotionMagicVoltage;
@@ -25,6 +26,7 @@
2526
import edu.wpi.first.units.measure.Angle;
2627
import edu.wpi.first.units.measure.AngularVelocity;
2728
import edu.wpi.first.units.measure.Mass;
29+
import edu.wpi.first.wpilibj.RobotBase;
2830
import edu.wpi.first.wpilibj.RobotController;
2931
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
3032

@@ -68,13 +70,22 @@ public void attachMotorSim(
6870
var talonFXSim = this.getSimState();
6971
talonFXSim.Orientation = orientation;
7072
talonFXSim.setMotorType(motorType);
73+
74+
if (RobotBase.isSimulation()) {
75+
PeriodicTasks.getInstance().addTask(this::simulationPeriodic);
76+
}
7177
}
7278

7379
/** Adds a CanCoder for syncing simulation values. */
7480
public void attachCanCoderSim(ChaosCanCoder canCoder) {
7581
m_attachedCanCoder = canCoder;
7682
}
7783

84+
/**
85+
* Sets the starting angle of the motor in the sim.
86+
*
87+
* @param simAngle the initial angle
88+
*/
7889
public void setSimAngle(Angle simAngle) {
7990
if (m_motorSimModel == null) {
8091
// Skip sim updates for motors without models
@@ -88,12 +99,7 @@ public void setSimAngle(Angle simAngle) {
8899
* Tells the motor to handle updating the sim state. Copied/inspired from:
89100
* https://v6.docs.ctr-electronics.com/en/2024/docs/api-reference/simulation/simulation-intro.html
90101
*/
91-
public void simulationPeriodic() {
92-
if (m_motorSimModel == null) {
93-
// Skip sim updates for motors without models
94-
return;
95-
}
96-
102+
private void simulationPeriodic() {
97103
TalonFXSimState talonFxSim = getSimState();
98104

99105
// set the supply voltage of the TalonFX

src/main/java/com/chaos131/ctre/ChaosTalonFxTuner.java

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package com.chaos131.ctre;
66

77
import com.chaos131.util.DashboardNumber;
8+
import com.ctre.phoenix6.configs.CurrentLimitsConfigs;
9+
import com.ctre.phoenix6.configs.FeedbackConfigs;
810
import com.ctre.phoenix6.configs.Slot0Configs;
911
import com.ctre.phoenix6.configs.TalonFXConfiguration;
1012
import java.util.ArrayList;
@@ -29,19 +31,97 @@ public ChaosTalonFxTuner(String name, ChaosTalonFx... talons) {
2931
m_talons = talons;
3032
}
3133

34+
/** Creates tunables for the Slot0 number values using the first motor's config */
35+
public ChaosTalonFxTuner withSlot0() {
36+
return withSlot0(m_talons[0].Configuration.Slot0);
37+
}
38+
3239
/**
3340
* Creates tunables for the Slot0 number values
3441
*
3542
* @param initialConfig the starting Slot0 values
3643
*/
37-
public void tunableSlot0(Slot0Configs initialConfig) {
44+
public ChaosTalonFxTuner withSlot0(Slot0Configs initialConfig) {
3845
tunable("Slot0_kP", initialConfig.kP, (config, newValue) -> config.Slot0.kP = newValue);
3946
tunable("Slot0_kI", initialConfig.kI, (config, newValue) -> config.Slot0.kI = newValue);
4047
tunable("Slot0_kD", initialConfig.kD, (config, newValue) -> config.Slot0.kD = newValue);
4148
tunable("Slot0_kG", initialConfig.kG, (config, newValue) -> config.Slot0.kG = newValue);
4249
tunable("Slot0_kS", initialConfig.kS, (config, newValue) -> config.Slot0.kS = newValue);
4350
tunable("Slot0_kV", initialConfig.kV, (config, newValue) -> config.Slot0.kV = newValue);
4451
tunable("Slot0_kA", initialConfig.kA, (config, newValue) -> config.Slot0.kA = newValue);
52+
return this;
53+
}
54+
55+
/** Creates tunables for the CurrentLimitsConfigs number values using the first motor's config */
56+
public ChaosTalonFxTuner withCurrentLimits() {
57+
return withCurrentLimits(m_talons[0].Configuration.CurrentLimits);
58+
}
59+
60+
/**
61+
* Creates tunables for the CurrentLimitsConfigs number values
62+
*
63+
* @param initialConfig the starting CurrentLimitsConfigs values
64+
*/
65+
public ChaosTalonFxTuner withCurrentLimits(CurrentLimitsConfigs initialConfig) {
66+
tunable(
67+
"CurrentLimit_Stator",
68+
initialConfig.StatorCurrentLimit,
69+
(config, newValue) -> config.CurrentLimits.StatorCurrentLimit = newValue);
70+
tunable(
71+
"CurrentLimit_Supply",
72+
initialConfig.SupplyCurrentLimit,
73+
(config, newValue) -> config.CurrentLimits.SupplyCurrentLimit = newValue);
74+
tunable(
75+
"CurrentLimit_SupplyLower_Limit",
76+
initialConfig.SupplyCurrentLowerLimit,
77+
(config, newValue) -> config.CurrentLimits.SupplyCurrentLowerLimit = newValue);
78+
tunable(
79+
"CurrentLimit_SupplyLower_Time",
80+
initialConfig.SupplyCurrentLowerTime,
81+
(config, newValue) -> config.CurrentLimits.SupplyCurrentLowerTime = newValue);
82+
return this;
83+
}
84+
85+
/** Creates tunables for the FeedbackConfigs number values using the first motor's config */
86+
public ChaosTalonFxTuner withFeedbackValues() {
87+
return withFeedbackValues(m_talons[0].Configuration.Feedback);
88+
}
89+
90+
/**
91+
* Creates tunables for the FeedbackConfigs number values
92+
*
93+
* @param initialConfig the starting FeedbackConfigs values
94+
*/
95+
public ChaosTalonFxTuner withFeedbackValues(FeedbackConfigs initialConfig) {
96+
tunable(
97+
"Feedback_RotorToSensorRatio",
98+
initialConfig.RotorToSensorRatio,
99+
(config, newValue) -> config.Feedback.RotorToSensorRatio = newValue);
100+
tunable(
101+
"Feedback_SensorToMechanismRatio",
102+
initialConfig.SensorToMechanismRatio,
103+
(config, newValue) -> config.Feedback.SensorToMechanismRatio = newValue);
104+
return this;
105+
}
106+
107+
/**
108+
* Adds all tunable values (currently Slot0, CurrentLimits, and FeedbackValues) using the first
109+
* motor's config
110+
*/
111+
public ChaosTalonFxTuner withAllConfigs() {
112+
return withAllConfigs(m_talons[0].Configuration);
113+
}
114+
115+
/**
116+
* Adds all tunable values (currently Slot0, CurrentLimits, and FeedbackValues)
117+
*
118+
* @param initialConfig the starting config
119+
*/
120+
public ChaosTalonFxTuner withAllConfigs(TalonFXConfiguration initialConfig) {
121+
withSlot0(initialConfig.Slot0);
122+
withCurrentLimits(initialConfig.CurrentLimits);
123+
withFeedbackValues(initialConfig.Feedback);
124+
return this;
45125
}
46126

47127
/**
@@ -57,9 +137,8 @@ public DashboardNumber tunable(
57137
String valueName, double initialValue, BiConsumer<TalonFXConfiguration, Double> onUpdate) {
58138
DashboardNumber dsNumber =
59139
new DashboardNumber(
60-
"TalonFxConfig/" + m_name + "/" + valueName,
140+
m_name + "/" + valueName,
61141
initialValue,
62-
true,
63142
false,
64143
newValue -> {
65144
for (ChaosTalonFx chaosTalonFx : m_talons) {

src/main/java/com/chaos131/ctre/ChaosTalonFxs.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.chaos131.can.CanConstants.CanBusName;
1111
import com.chaos131.can.CanConstants.CanId;
1212
import com.chaos131.pid.PIDFValue;
13+
import com.chaos131.util.PeriodicTasks;
1314
import com.ctre.phoenix6.configs.Slot0Configs;
1415
import com.ctre.phoenix6.configs.TalonFXSConfiguration;
1516
import com.ctre.phoenix6.controls.DynamicMotionMagicVoltage;
@@ -20,6 +21,7 @@
2021
import com.ctre.phoenix6.sim.ChassisReference;
2122
import com.ctre.phoenix6.sim.TalonFXSSimState;
2223
import edu.wpi.first.units.measure.Angle;
24+
import edu.wpi.first.wpilibj.RobotBase;
2325
import edu.wpi.first.wpilibj.RobotController;
2426
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
2527

@@ -60,6 +62,10 @@ public void attachMotorSim(
6062
m_isMainSimMotor = isMainSimMotor;
6163
var talonFXSim = this.getSimState();
6264
talonFXSim.MotorOrientation = orientation;
65+
66+
if (RobotBase.isSimulation()) {
67+
PeriodicTasks.getInstance().addTask(this::simulationPeriodic);
68+
}
6369
}
6470

6571
/** Adds a CanCoder for syncing simulation values. */
@@ -80,12 +86,7 @@ public void setSimAngle(Angle simAngle) {
8086
* Tells the motor to handle updating the sim state. Copied/inspired from:
8187
* https://v6.docs.ctr-electronics.com/en/2024/docs/api-reference/simulation/simulation-intro.html
8288
*/
83-
public void simulationPeriodic() {
84-
if (m_motorSimModel == null) {
85-
// Skip sim updates for motors without models
86-
return;
87-
}
88-
89+
private void simulationPeriodic() {
8990
TalonFXSSimState talonFxSim = getSimState();
9091

9192
// set the supply voltage of the TalonFX

src/main/java/com/chaos131/ctre/ChaosTalonFxsTuner.java

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package com.chaos131.ctre;
66

77
import com.chaos131.util.DashboardNumber;
8+
import com.ctre.phoenix6.configs.CurrentLimitsConfigs;
9+
import com.ctre.phoenix6.configs.ExternalFeedbackConfigs;
810
import com.ctre.phoenix6.configs.Slot0Configs;
911
import com.ctre.phoenix6.configs.TalonFXSConfiguration;
1012
import java.util.ArrayList;
@@ -29,19 +31,99 @@ public ChaosTalonFxsTuner(String name, ChaosTalonFxs... talons) {
2931
m_talons = talons;
3032
}
3133

34+
/** Creates tunables for the Slot0 number values using the first motor's config */
35+
public ChaosTalonFxsTuner withSlot0() {
36+
return withSlot0(m_talons[0].Configuration.Slot0);
37+
}
38+
3239
/**
3340
* Creates tunables for the Slot0 number values
3441
*
3542
* @param initialConfig the starting Slot0 values
3643
*/
37-
public void tunableSlot0(Slot0Configs initialConfig) {
44+
public ChaosTalonFxsTuner withSlot0(Slot0Configs initialConfig) {
3845
tunable("Slot0_kP", initialConfig.kP, (config, newValue) -> config.Slot0.kP = newValue);
3946
tunable("Slot0_kI", initialConfig.kI, (config, newValue) -> config.Slot0.kI = newValue);
4047
tunable("Slot0_kD", initialConfig.kD, (config, newValue) -> config.Slot0.kD = newValue);
4148
tunable("Slot0_kG", initialConfig.kG, (config, newValue) -> config.Slot0.kG = newValue);
4249
tunable("Slot0_kS", initialConfig.kS, (config, newValue) -> config.Slot0.kS = newValue);
4350
tunable("Slot0_kV", initialConfig.kV, (config, newValue) -> config.Slot0.kV = newValue);
4451
tunable("Slot0_kA", initialConfig.kA, (config, newValue) -> config.Slot0.kA = newValue);
52+
return this;
53+
}
54+
55+
/** Creates tunables for the CurrentLimitsConfigs number values using the first motor's config */
56+
public ChaosTalonFxsTuner withCurrentLimits() {
57+
return withCurrentLimits(m_talons[0].Configuration.CurrentLimits);
58+
}
59+
60+
/**
61+
* Creates tunables for the CurrentLimitsConfigs number values
62+
*
63+
* @param initialConfig the starting CurrentLimitsConfigs values
64+
*/
65+
public ChaosTalonFxsTuner withCurrentLimits(CurrentLimitsConfigs initialConfig) {
66+
tunable(
67+
"CurrentLimit_Stator",
68+
initialConfig.StatorCurrentLimit,
69+
(config, newValue) -> config.CurrentLimits.StatorCurrentLimit = newValue);
70+
tunable(
71+
"CurrentLimit_Supply",
72+
initialConfig.SupplyCurrentLimit,
73+
(config, newValue) -> config.CurrentLimits.SupplyCurrentLimit = newValue);
74+
tunable(
75+
"CurrentLimit_SupplyLower_Limit",
76+
initialConfig.SupplyCurrentLowerLimit,
77+
(config, newValue) -> config.CurrentLimits.SupplyCurrentLowerLimit = newValue);
78+
tunable(
79+
"CurrentLimit_SupplyLower_Time",
80+
initialConfig.SupplyCurrentLowerTime,
81+
(config, newValue) -> config.CurrentLimits.SupplyCurrentLowerTime = newValue);
82+
return this;
83+
}
84+
85+
/**
86+
* Creates tunables for the ExternalFeedbackConfigs number values using the first motor's config
87+
*/
88+
public ChaosTalonFxsTuner withFeedbackValues() {
89+
return withFeedbackValues(m_talons[0].Configuration.ExternalFeedback);
90+
}
91+
92+
/**
93+
* Creates tunables for the ExternalFeedbackConfigs number values
94+
*
95+
* @param initialConfig the starting ExternalFeedbackConfigs values
96+
*/
97+
public ChaosTalonFxsTuner withFeedbackValues(ExternalFeedbackConfigs initialConfig) {
98+
tunable(
99+
"Feedback_RotorToSensorRatio",
100+
initialConfig.RotorToSensorRatio,
101+
(config, newValue) -> config.ExternalFeedback.RotorToSensorRatio = newValue);
102+
tunable(
103+
"Feedback_SensorToMechanismRatio",
104+
initialConfig.SensorToMechanismRatio,
105+
(config, newValue) -> config.ExternalFeedback.SensorToMechanismRatio = newValue);
106+
return this;
107+
}
108+
109+
/**
110+
* Adds all tunable values (currently Slot0, CurrentLimits, and FeedbackValues) using the first
111+
* motor's config
112+
*/
113+
public ChaosTalonFxsTuner withAllConfigs() {
114+
return withAllConfigs(m_talons[0].Configuration);
115+
}
116+
117+
/**
118+
* Adds all tunable values (currently Slot0, CurrentLimits, and FeedbackValues)
119+
*
120+
* @param initialConfig the starting config
121+
*/
122+
public ChaosTalonFxsTuner withAllConfigs(TalonFXSConfiguration initialConfig) {
123+
withSlot0(initialConfig.Slot0);
124+
withCurrentLimits(initialConfig.CurrentLimits);
125+
withFeedbackValues(initialConfig.ExternalFeedback);
126+
return this;
45127
}
46128

47129
/**
@@ -57,9 +139,8 @@ public DashboardNumber tunable(
57139
String valueName, double initialValue, BiConsumer<TalonFXSConfiguration, Double> onUpdate) {
58140
DashboardNumber dsNumber =
59141
new DashboardNumber(
60-
"TalonFxConfig/" + m_name + "/" + valueName,
142+
m_name + "/" + valueName,
61143
initialValue,
62-
true,
63144
false,
64145
newValue -> {
65146
for (ChaosTalonFxs chaosTalonFxs : m_talons) {

src/main/java/com/chaos131/pid/LoggedPIDTuner.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
* A Logged Dashboard number specific for PID Values. This is useful for tracking PID values during
88
* testing, but should be replaced with a basic PID structure once settled. Extends the PIDTuner to
99
* make it easier to swap between the two.
10+
*
11+
* @deprecated We use {@link com.chaos131.ctre.ChaosTalonFxTuner} for CTRE PID tuning and WPILib's
12+
* PIDController already supports dashboard tuning.
1013
*/
14+
@Deprecated(since = "2026.1", forRemoval = true)
1115
public class LoggedPIDTuner extends PIDTuner {
1216
/** The P, I, D, and F values broken into their own logged values */
1317
private LoggedNetworkNumber m_p, m_i, m_d, m_f;

0 commit comments

Comments
 (0)