-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodTunerConstants.java
More file actions
177 lines (147 loc) · 8.89 KB
/
GoodTunerConstants.java
File metadata and controls
177 lines (147 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package frc.robot.drive;
import com.ctre.phoenix6.configs.Slot0Configs;
import com.ctre.phoenix6.mechanisms.swerve.SwerveDrivetrainConstants;
import com.ctre.phoenix6.mechanisms.swerve.SwerveModuleConstants;
import com.ctre.phoenix6.mechanisms.swerve.SwerveModuleConstantsFactory;
import com.ctre.phoenix6.mechanisms.swerve.SwerveModule.ClosedLoopOutputType;
import com.ctre.phoenix6.mechanisms.swerve.SwerveModuleConstants.SteerFeedbackType;
import edu.wpi.first.math.util.Units;
// Generated by the Tuner X Swerve Project Generator
// https://v6.docs.ctr-electronics.com/en/stable/docs/tuner/tuner-swerve/index.html
public class GoodTunerConstants {
// Both sets of gains need to be tuned to your individual robot.
// The steer motor uses any SwerveModule.SteerRequestType control request with the
// output type specified by SwerveModuleConstants.SteerMotorClosedLoopOutput
private static final Slot0Configs steerGains = new Slot0Configs()
.withKP(100).withKI(0).withKD(0.2)
.withKS(0).withKV(1.5).withKA(0);
// When using closed-loop control, the drive motor uses the control
// output type specified by SwerveModuleConstants.DriveMotorClosedLoopOutput
private static final Slot0Configs driveGains = new Slot0Configs()
.withKP(3).withKI(0).withKD(0)
.withKS(0).withKV(0).withKA(0);
// The closed-loop output type to use for the steer motors;
// This affects the PID/FF gains for the steer motors
private static final ClosedLoopOutputType steerClosedLoopOutput = ClosedLoopOutputType.Voltage;
// The closed-loop output type to use for the drive motors;
// This affects the PID/FF gains for the drive motors
private static final ClosedLoopOutputType driveClosedLoopOutput = ClosedLoopOutputType.Voltage;
// The stator current at which the wheels start to slip;
// This needs to be tuned to your individual robot
private static final double kSlipCurrentA = 80.0;
// Theoretical free speed (m/s) at 12v applied output;
// This needs to be tuned to your individual robot
public static final double kSpeedAt12VoltsMps = 4.5;
// Every 1 rotation of the azimuth results in kCoupleRatio drive motor turns;
// This may need to be tuned to your individual robot
private static final double kCoupleRatio = 3.5714285714285716;
private static final double kDriveGearRatio = 6.746031746031747;
private static final double kSteerGearRatio = 21.428571428571427;
private static final double kWheelRadiusInches = 2;
private static final boolean kSteerMotorReversed = true;
private static final boolean kInvertLeftSide = false;
private static final boolean kInvertRightSide = true;
private static final String kCANbusName = "DriveTrain";
private static final int kPigeonId = 18;
// These are only used for simulation
private static final double kSteerInertia = 0.00001;
private static final double kDriveInertia = 0.001;
// Simulated voltage necessary to overcome friction
private static final double kSteerFrictionVoltage = 0.25;
private static final double kDriveFrictionVoltage = 0.25;
private static final SwerveDrivetrainConstants DrivetrainConstants = new SwerveDrivetrainConstants()
.withPigeon2Id(kPigeonId)
.withCANbusName(kCANbusName);
private static final SwerveModuleConstantsFactory ConstantCreator = new SwerveModuleConstantsFactory()
.withDriveMotorGearRatio(kDriveGearRatio)
.withSteerMotorGearRatio(kSteerGearRatio)
.withWheelRadius(kWheelRadiusInches)
.withSlipCurrent(kSlipCurrentA)
.withSteerMotorGains(steerGains)
.withDriveMotorGains(driveGains)
.withSteerMotorClosedLoopOutput(steerClosedLoopOutput)
.withDriveMotorClosedLoopOutput(driveClosedLoopOutput)
.withSpeedAt12VoltsMps(kSpeedAt12VoltsMps)
.withSteerInertia(kSteerInertia)
.withDriveInertia(kDriveInertia)
.withSteerFrictionVoltage(kSteerFrictionVoltage)
.withDriveFrictionVoltage(kDriveFrictionVoltage)
.withFeedbackSource(SteerFeedbackType.FusedCANcoder)
.withCouplingGearRatio(kCoupleRatio)
.withSteerMotorInverted(kSteerMotorReversed);
static boolean isPractice = false;
static boolean hasInitialized = false;
public static void initCompetition() {
if (hasInitialized) return;
hasInitialized = true;
kFrontLeftEncoderOffset = kCompetitionFrontLeftEncoderOffset;
kFrontRightEncoderOffset = kCompetitionFrontRightEncoderOffset;
kBackLeftEncoderOffset = kCompetitionBackLeftEncoderOffset;
kBackRightEncoderOffset = kCompetitionBackRightEncoderOffset;
isPractice = false;
commonInit();
}
public static void initPractice() {
if (hasInitialized) return;
hasInitialized = true;
kFrontLeftEncoderOffset = kPracticeFrontLeftEncoderOffset;
kFrontRightEncoderOffset = kPracticeFrontRightEncoderOffset;
kBackLeftEncoderOffset = kPracticeBackLeftEncoderOffset;
kBackRightEncoderOffset = kPracticeBackRightEncoderOffset;
isPractice = true;
commonInit();
}
static void commonInit() {
FrontLeft = ConstantCreator.createModuleConstants(
kFrontLeftSteerMotorId, kFrontLeftDriveMotorId, kFrontLeftEncoderId, kFrontLeftEncoderOffset, Units.inchesToMeters(kFrontLeftXPosInches), Units.inchesToMeters(kFrontLeftYPosInches), kInvertLeftSide);
FrontRight = ConstantCreator.createModuleConstants(
kFrontRightSteerMotorId, kFrontRightDriveMotorId, kFrontRightEncoderId, kFrontRightEncoderOffset, Units.inchesToMeters(kFrontRightXPosInches), Units.inchesToMeters(kFrontRightYPosInches), kInvertRightSide);
BackLeft = ConstantCreator.createModuleConstants(
kBackLeftSteerMotorId, kBackLeftDriveMotorId, kBackLeftEncoderId, kBackLeftEncoderOffset, Units.inchesToMeters(kBackLeftXPosInches), Units.inchesToMeters(kBackLeftYPosInches), kInvertLeftSide);
BackRight = ConstantCreator.createModuleConstants(
kBackRightSteerMotorId, kBackRightDriveMotorId, kBackRightEncoderId, kBackRightEncoderOffset, Units.inchesToMeters(kBackRightXPosInches), Units.inchesToMeters(kBackRightYPosInches), kInvertRightSide);
DriveTrain = new CommandSwerveDrivetrain(DrivetrainConstants, FrontLeft,
FrontRight, BackLeft, BackRight);
}
// Front Left
private static final int kFrontLeftDriveMotorId = 10;
private static final int kFrontLeftSteerMotorId = 11;
private static final int kFrontLeftEncoderId = 19;
private static final double kPracticeFrontLeftEncoderOffset = 0.4111328125;
private static final double kCompetitionFrontLeftEncoderOffset = -0.399169921875;
private static double kFrontLeftEncoderOffset = kCompetitionFrontLeftEncoderOffset;
private static final double kFrontLeftXPosInches = 10.375;
private static final double kFrontLeftYPosInches = 10.375;
// Front Right
private static final int kFrontRightDriveMotorId = 12;
private static final int kFrontRightSteerMotorId = 13;
private static final int kFrontRightEncoderId = 20;
private static final double kPracticeFrontRightEncoderOffset = 0.340576171875;
private static final double kCompetitionFrontRightEncoderOffset = 0.34912109375;
private static double kFrontRightEncoderOffset = kCompetitionFrontRightEncoderOffset;
private static final double kFrontRightXPosInches = 10.375;
private static final double kFrontRightYPosInches = -10.375;
// Back Left
private static final int kBackLeftDriveMotorId = 14;
private static final int kBackLeftSteerMotorId = 15;
private static final int kBackLeftEncoderId = 21;
private static final double kPracticeBackLeftEncoderOffset = 0.40673828125;
private static final double kCompetitionBackLeftEncoderOffset = 0.414306640625;
private static double kBackLeftEncoderOffset = kCompetitionBackLeftEncoderOffset;
private static final double kBackLeftXPosInches = -10.375;
private static final double kBackLeftYPosInches = 10.375;
// Back Right
private static final int kBackRightDriveMotorId = 16;
private static final int kBackRightSteerMotorId = 17;
private static final int kBackRightEncoderId = 22;
private static final double kPracticeBackRightEncoderOffset = -0.0341796875;
private static final double kCompetitionBackRightEncoderOffset = -0.001708984375;
private static double kBackRightEncoderOffset = kCompetitionBackRightEncoderOffset;
private static final double kBackRightXPosInches = -10.375;
private static final double kBackRightYPosInches = -10.375;
private static SwerveModuleConstants FrontLeft;
private static SwerveModuleConstants FrontRight;
private static SwerveModuleConstants BackLeft;
private static SwerveModuleConstants BackRight;
public static CommandSwerveDrivetrain DriveTrain;
}