1212import edu .wpi .first .wpilibj2 .command .Command ;
1313import edu .wpi .first .wpilibj2 .command .SubsystemBase ;
1414import org .littletonrobotics .junction .Logger ;
15- import org .littletonrobotics .junction .networktables .LoggedDashboardBoolean ;
1615import org .littletonrobotics .junction .networktables .LoggedDashboardNumber ;
1716
1817import java .util .Arrays ;
1918import java .util .function .DoubleConsumer ;
2019import java .util .function .DoubleSupplier ;
2120import java .util .function .Supplier ;
2221
22+ /**
23+ * A command for characterizing the wheel radius of each module of a swerve drive system by calculating the radius of each drive wheel based on rotational data and robot yaw during operation.
24+ */
2325public class WheelRadiusCharacterizationCommand extends Command {
2426 private static final LoggedDashboardNumber CHARACTERIZATION_SPEED = new LoggedDashboardNumber ("WheelRadiusCharacterization/SpeedRadiansPerSecond" , 1 );
2527 private static final LoggedDashboardNumber ROTATION_RATE_LIMIT = new LoggedDashboardNumber ("WheelRadiusCharacterization/RotationRateLimit" , 1 );
26- private static final LoggedDashboardBoolean SHOULD_MOVE_CLOCKWISE = new LoggedDashboardBoolean ("WheelRadiusCharacterization/ShouldMoveClockwise" , false );
2728
28- private final double [] wheelDistancesFromCenterMeters ;
29+ private final double []
30+ wheelDistancesFromCenterMeters ,
31+ driveWheelRadii ;
2932 private final Supplier <double []> wheelPositionsRadiansSupplier ;
3033 private final DoubleSupplier gyroYawRadiansSupplier ;
3134 private final DoubleConsumer runWheelRadiusCharacterization ;
@@ -34,25 +37,97 @@ public class WheelRadiusCharacterizationCommand extends Command {
3437 private double startingGyroYawRadians ;
3538 private double accumulatedGyroYawRadians ;
3639 private double [] startingWheelPositions ;
37- private double driveWheelsRadius ;
3840
39- public WheelRadiusCharacterizationCommand (Translation2d [] wheelDistancesFromCenterMeters , Supplier <double []> wheelPositionsRadiansSupplier ,
40- DoubleSupplier gyroYawRadiansSupplier , DoubleConsumer runWheelRadiusCharacterization , SubsystemBase requirement
41- ) {
42- this .wheelDistancesFromCenterMeters = Arrays .stream (wheelDistancesFromCenterMeters ).mapToDouble (Translation2d ::getNorm ).toArray ();
43- this .wheelPositionsRadiansSupplier = wheelPositionsRadiansSupplier ;
44- this .gyroYawRadiansSupplier = gyroYawRadiansSupplier ;
45- this .runWheelRadiusCharacterization = runWheelRadiusCharacterization ;
46- addRequirements (requirement );
41+ /**
42+ * Creates a new WheelRadiusCharacterizationCommand.
43+ *
44+ * @param wheelDistanceFromCenterMeters an array of the distances of each wheel from the center of the robot in meters
45+ * @param wheelPositionsRadiansSupplier a supplier that returns the current position of each wheel in radians
46+ * @param gyroYawRadiansSupplier a supplier that returns the current yaw of the robot in radians
47+ * @param runWheelRadiusCharacterization a consumer that runs the swerve drive with a given speed
48+ * @param requirement the subsystem that this command requires
49+ */
50+ public WheelRadiusCharacterizationCommand (Translation2d wheelDistanceFromCenterMeters ,
51+ Supplier <double []> wheelPositionsRadiansSupplier ,
52+ DoubleSupplier gyroYawRadiansSupplier ,
53+ DoubleConsumer runWheelRadiusCharacterization ,
54+ SubsystemBase requirement ) {
55+ this (
56+ new Translation2d []{wheelDistanceFromCenterMeters , wheelDistanceFromCenterMeters , wheelDistanceFromCenterMeters , wheelDistanceFromCenterMeters },
57+ wheelPositionsRadiansSupplier ,
58+ gyroYawRadiansSupplier ,
59+ runWheelRadiusCharacterization ,
60+ requirement
61+ );
62+ }
63+
64+ /**
65+ * Creates a new WheelRadiusCharacterizationCommand.
66+ *
67+ * @param wheelDistancesFromCenterMeters an array of the distances of each wheel from the center of the robot in meters
68+ * @param wheelPositionsRadiansSupplier a supplier that returns the current position of each wheel in radians
69+ * @param gyroYawRadiansSupplier a supplier that returns the current yaw of the robot in radians
70+ * @param runWheelRadiusCharacterization a consumer that runs the swerve drive with a given speed
71+ * @param requirement the subsystem that this command requires
72+ */
73+ public WheelRadiusCharacterizationCommand (
74+ Translation2d [] wheelDistancesFromCenterMeters ,
75+ Supplier <double []> wheelPositionsRadiansSupplier ,
76+ DoubleSupplier gyroYawRadiansSupplier ,
77+ DoubleConsumer runWheelRadiusCharacterization ,
78+ SubsystemBase requirement ) {
79+ this (
80+ Arrays .stream (wheelDistancesFromCenterMeters ).mapToDouble (Translation2d ::getNorm ).toArray (),
81+ wheelPositionsRadiansSupplier ,
82+ gyroYawRadiansSupplier ,
83+ runWheelRadiusCharacterization ,
84+ requirement
85+ );
86+ }
87+
88+ /**
89+ * Creates a new WheelRadiusCharacterizationCommand.
90+ *
91+ * @param wheelDistanceFromCenterMeters the distance of the wheels from the center of the robot in meters
92+ * @param wheelPositionsRadiansSupplier a supplier that returns the current position of each wheel in radians
93+ * @param gyroYawRadiansSupplier a supplier that returns the current yaw of the robot in radians
94+ * @param runWheelRadiusCharacterization a consumer that runs the swerve drive with a given speed
95+ * @param requirement the subsystem that this command requires
96+ */
97+ public WheelRadiusCharacterizationCommand (
98+ double wheelDistanceFromCenterMeters ,
99+ Supplier <double []> wheelPositionsRadiansSupplier ,
100+ DoubleSupplier gyroYawRadiansSupplier ,
101+ DoubleConsumer runWheelRadiusCharacterization ,
102+ SubsystemBase requirement ) {
103+ this (
104+ new double []{wheelDistanceFromCenterMeters , wheelDistanceFromCenterMeters , wheelDistanceFromCenterMeters , wheelDistanceFromCenterMeters },
105+ wheelPositionsRadiansSupplier ,
106+ gyroYawRadiansSupplier ,
107+ runWheelRadiusCharacterization ,
108+ requirement
109+ );
47110 }
48111
49- public WheelRadiusCharacterizationCommand (double [] wheelDistancesFromCenterMeters , Supplier <double []> wheelPositionsRadiansSupplier ,
50- DoubleSupplier gyroYawRadiansSupplier , DoubleConsumer runWheelRadiusCharacterization , SubsystemBase requirement
51- ) {
112+ /**
113+ * Creates a new WheelRadiusCharacterizationCommand.
114+ *
115+ * @param wheelDistancesFromCenterMeters an array of the distances of each wheel from the center of the robot in meters
116+ * @param wheelPositionsRadiansSupplier a supplier that returns the current position of each wheel in radians
117+ * @param gyroYawRadiansSupplier a supplier that returns the current yaw of the robot in radians
118+ * @param runWheelRadiusCharacterization a consumer that runs the swerve drive with a given speed
119+ * @param requirement the subsystem that this command requires
120+ */
121+ public WheelRadiusCharacterizationCommand (double [] wheelDistancesFromCenterMeters ,
122+ Supplier <double []> wheelPositionsRadiansSupplier ,
123+ DoubleSupplier gyroYawRadiansSupplier ,
124+ DoubleConsumer runWheelRadiusCharacterization ,
125+ SubsystemBase requirement ) {
52126 this .wheelDistancesFromCenterMeters = wheelDistancesFromCenterMeters ;
53127 this .wheelPositionsRadiansSupplier = wheelPositionsRadiansSupplier ;
54128 this .gyroYawRadiansSupplier = gyroYawRadiansSupplier ;
55129 this .runWheelRadiusCharacterization = runWheelRadiusCharacterization ;
130+ this .driveWheelRadii = new double [wheelDistancesFromCenterMeters .length ];
56131 addRequirements (requirement );
57132 }
58133
@@ -66,10 +141,10 @@ public void execute() {
66141 driveMotors ();
67142
68143 accumulatedGyroYawRadians = getAccumulatedGyroYaw ();
69- driveWheelsRadius = calculateDriveWheelRadius ();
144+ calculateDriveWheelRadius ();
70145
71146 Logger .recordOutput ("WheelRadiusCharacterization/AccumulatedGyroYawRadians" , accumulatedGyroYawRadians );
72- Logger . recordOutput ( "RadiusCharacterization/DriveWheelRadius" , driveWheelsRadius );
147+ logWheelRadii ( );
73148 }
74149
75150 @ Override
@@ -87,34 +162,36 @@ private void configureStartingStats() {
87162 }
88163
89164 private void driveMotors () {
90- runWheelRadiusCharacterization .accept (rotationSlewRateLimiter .calculate (getRotationDirection () * CHARACTERIZATION_SPEED .get ()));
165+ runWheelRadiusCharacterization .accept (rotationSlewRateLimiter .calculate (CHARACTERIZATION_SPEED .get ()));
91166 }
92167
93168 private double getAccumulatedGyroYaw () {
94169 return Math .abs (startingGyroYawRadians - gyroYawRadiansSupplier .getAsDouble ());
95170 }
96171
97- private double calculateDriveWheelRadius () {
98- driveWheelsRadius = 0 ;
172+ private void calculateDriveWheelRadius () {
99173 final double [] wheelPositionsRadians = wheelPositionsRadiansSupplier .get ();
100174
101175 for (int i = 0 ; i < 4 ; i ++) {
102176 final double accumulatedWheelRadians = Math .abs (wheelPositionsRadians [i ] - startingWheelPositions [i ]);
103- driveWheelsRadius + = (accumulatedGyroYawRadians * wheelDistancesFromCenterMeters [i ]) / accumulatedWheelRadians ;
177+ driveWheelRadii [ i ] = (accumulatedGyroYawRadians * wheelDistancesFromCenterMeters [i ]) / accumulatedWheelRadians ;
104178 Logger .recordOutput ("RadiusCharacterization/AccumulatedWheelRadians" + i , accumulatedWheelRadians );
105179 }
106-
107- return driveWheelsRadius /= 4 ;
108180 }
109181
110182 private void printResults () {
111- if (accumulatedGyroYawRadians <= Math .PI * 2.0 )
183+ if (accumulatedGyroYawRadians <= Math .PI * 2.0 ) {
112184 System .out .println ("Not enough data for characterization" );
113- else
114- System .out .println ("Drive Wheel Radius: " + driveWheelsRadius + " meters" );
185+ return ;
186+ }
187+ for (int i = 0 ; i < driveWheelRadii .length ; i ++)
188+ System .out .println ("Drive Wheel Radius for Module " + i + ": " + driveWheelRadii [i ] + " meters" );
189+ System .out .println ("Average Drive Wheel Radius: " + Arrays .stream (driveWheelRadii ).average ().getAsDouble () + " meters" );
115190 }
116191
117- private int getRotationDirection () {
118- return SHOULD_MOVE_CLOCKWISE .get () ? -1 : 1 ;
192+ private void logWheelRadii () {
193+ for (int i = 0 ; i < driveWheelRadii .length ; i ++)
194+ Logger .recordOutput ("RadiusCharacterization/DriveWheelRadiusModule" + i , driveWheelRadii [i ]);
195+ Logger .recordOutput ("RadiusCharacterization/AverageDriveWheelRadius" , Arrays .stream (driveWheelRadii ).average ().getAsDouble ());
119196 }
120197}
0 commit comments