-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconstants.py
More file actions
293 lines (252 loc) · 10.8 KB
/
constants.py
File metadata and controls
293 lines (252 loc) · 10.8 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import math
from phoenix6 import CANBus, configs, signals, swerve, units
from wpimath.units import inchesToMeters, rotationsToRadians, degreesToRadians
import commands2.cmd as cmd
from wpimath.geometry import Pose2d, Transform2d
from robotpy_apriltag import AprilTagFieldLayout, AprilTagField
from pint import UnitRegistry
unit = UnitRegistry()
def makeCommand(func):
def cmdFn(*args, **kwargs):
return cmd.runOnce(lambda: func(*args, **kwargs))
return cmdFn
class Drive:
# module parameters
kWheelRadius = 2.0 * unit.inch
kWheelCircumference = kWheelRadius * 2 * math.pi / unit.turn
kEncoderResolution = 4096 # counts per rotation
kModuleMaxAngularVelocity = math.pi * unit.radian / unit.second
kModuleMaxAngularAcceleration = math.tau * unit.radian / unit.second / unit.second
kTurnRatio = 150.0 / 7.0
kDriveRatio = (50.0 / 14.0) * (17.0 / 27.0) * (45.0 / 15.0)
kDrive_p = 0.01
kDrive_i = 0
kDrive_d = 0
kDrive_v = 12.0 / (100.0 / kDriveRatio)
kTurn_p = 40
kTurn_i = 0
kTurn_d = 0
# drivetrain paramaters
kMaxSpeed = 3.0 * unit.meter / unit.second # 3 meters per second
kMaxAngularSpeed = math.pi * unit.radian / unit.second # 1/2 rotation per second
kChassisWidth = 28.0 * unit.inch
kChassisLength = 28.0 * unit.inch
kChassisRadius = (
math.sqrt(
kChassisWidth.m * kChassisWidth.m + kChassisLength.m * kChassisLength.m
)
* unit.inch
)
class Limelight:
kGyroId = 20
kLimelightHostnames = [ "limelight-wwdkd", "limelight-jonkler", "limelight-moist" ]
kAlignmentTargets = { id: AprilTagFieldLayout().loadField(AprilTagField.kDefaultField).getTagPose(id).toPose2d().transformBy(Transform2d(.5,0,math.pi)) for id in list(range(6,12))+list(range(17,23)) }
class precise:
move_p = 2
spin_p = 0.75
xy_tolerance = 0.01
theta_tolerance = 0.5
class Turntable:
# motor ID as set in the firmware
driveMotorId = 13
# PIDv values for motor speed controll
motorPID = {"p": 0.01, "i": 0, "d": 0, "v": 0.12}
class Spinner:
# motor ID as set in the firmware
driveMotorId = 14
# PIDv values for motor speed controll
motorPID = {"p": 0.01, "i": 0, "d": 0, "v": 0.12}
class TunerConstants:
"""
Generated by the Tuner X Swerve Project Generator
https://v6.docs.ctr-electronics.com/en/stable/docs/tuner/tuner-swerve/index.html
"""
# 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
_steer_gains = (
configs.Slot0Configs()
.with_k_p(100)
.with_k_i(0)
.with_k_d(0.5)
.with_k_s(0.1)
.with_k_v(2.66)
.with_k_a(0)
.with_static_feedforward_sign(signals.StaticFeedforwardSignValue.USE_CLOSED_LOOP_SIGN)
)
# When using closed-loop control, the drive motor uses the control
# output type specified by SwerveModuleConstants.DriveMotorClosedLoopOutput
_drive_gains = (
configs.Slot0Configs()
.with_k_p(0.1)
.with_k_i(0)
.with_k_d(0)
.with_k_s(0)
.with_k_v(0.124)
)
# The closed-loop output type to use for the steer motors;
# This affects the PID/FF gains for the steer motors
_steer_closed_loop_output = swerve.ClosedLoopOutputType.VOLTAGE
# The closed-loop output type to use for the drive motors;
# This affects the PID/FF gains for the drive motors
_drive_closed_loop_output = swerve.ClosedLoopOutputType.VOLTAGE
# The type of motor used for the drive motor
_drive_motor_type = swerve.DriveMotorArrangement.TALON_FX_INTEGRATED
# The type of motor used for the drive motor
_steer_motor_type = swerve.SteerMotorArrangement.TALON_FX_INTEGRATED
# The remote sensor feedback type to use for the steer motors;
# When not Pro-licensed, Fused*/Sync* automatically fall back to Remote*
_steer_feedback_type = swerve.SteerFeedbackType.FUSED_CANCODER
# The stator current at which the wheels start to slip;
# This needs to be tuned to your individual robot
_slip_current: units.ampere = 120.0
# Initial configs for the drive and steer motors and the azimuth encoder; these cannot be null.
# Some configs will be overwritten; check the `with_*_initial_configs()` API documentation.
_drive_initial_configs = configs.TalonFXConfiguration()
_steer_initial_configs = configs.TalonFXConfiguration().with_current_limits(
configs.CurrentLimitsConfigs()
# Swerve azimuth does not require much torque output, so we can set a relatively low
# stator current limit to help avoid brownouts without impacting performance.
.with_stator_current_limit(60).with_stator_current_limit_enable(True)
)
_encoder_initial_configs = configs.CANcoderConfiguration()
# Configs for the Pigeon 2; leave this None to skip applying Pigeon 2 configs
_pigeon_configs: configs.Pigeon2Configuration | None = None
# CAN bus that the devices are located on;
# All swerve devices must share the same CAN bus
canbus = CANBus("", "./logs/example.hoot")
# Theoretical free speed (m/s) at 12 V applied output;
# This needs to be tuned to your individual robot
speed_at_12_volts: units.meters_per_second = 4.73
# Every 1 rotation of the azimuth results in _couple_ratio drive motor turns;
# This may need to be tuned to your individual robot
_couple_ratio = 3.5714285714285716
_drive_gear_ratio = 6.746031746031747
_steer_gear_ratio = 21.428571428571427
_wheel_radius: units.meter = inchesToMeters(2)
_invert_left_side = False
_invert_right_side = True
_pigeon_id = 20
# These are only used for simulation
_steer_inertia: units.kilogram_square_meter = 0.01
_drive_inertia: units.kilogram_square_meter = 0.01
# Simulated voltage necessary to overcome friction
_steer_friction_voltage: units.volt = 0.2
_drive_friction_voltage: units.volt = 0.2
drivetrain_constants = (
swerve.SwerveDrivetrainConstants()
.with_can_bus_name(canbus.name)
.with_pigeon2_id(_pigeon_id)
.with_pigeon2_configs(_pigeon_configs)
)
_constants_creator: swerve.SwerveModuleConstantsFactory[configs.TalonFXConfiguration, configs.TalonFXConfiguration, configs.CANcoderConfiguration] = (
swerve.SwerveModuleConstantsFactory()
.with_drive_motor_gear_ratio(_drive_gear_ratio)
.with_steer_motor_gear_ratio(_steer_gear_ratio)
.with_coupling_gear_ratio(_couple_ratio)
.with_wheel_radius(_wheel_radius)
.with_steer_motor_gains(_steer_gains)
.with_drive_motor_gains(_drive_gains)
.with_steer_motor_closed_loop_output(_steer_closed_loop_output)
.with_drive_motor_closed_loop_output(_drive_closed_loop_output)
.with_slip_current(_slip_current)
.with_speed_at12_volts(speed_at_12_volts)
.with_drive_motor_type(_drive_motor_type)
.with_steer_motor_type(_steer_motor_type)
.with_feedback_source(_steer_feedback_type)
.with_drive_motor_initial_configs(_drive_initial_configs)
.with_steer_motor_initial_configs(_steer_initial_configs)
.with_encoder_initial_configs(_encoder_initial_configs)
.with_steer_inertia(_steer_inertia)
.with_drive_inertia(_drive_inertia)
.with_steer_friction_voltage(_steer_friction_voltage)
.with_drive_friction_voltage(_drive_friction_voltage)
)
# Front Left
_front_left_drive_motor_id = 3
_front_left_steer_motor_id = 4
_front_left_encoder_id = 10
_front_left_encoder_offset: units.rotation = 0.431884765625
_front_left_steer_motor_inverted = True
_front_left_encoder_inverted = False
_front_left_x_pos: units.meter = inchesToMeters(14)
_front_left_y_pos: units.meter = inchesToMeters(14)
# Front Right
_front_right_drive_motor_id = 7
_front_right_steer_motor_id = 8
_front_right_encoder_id = 12
_front_right_encoder_offset: units.rotation = -0.236572265625
_front_right_steer_motor_inverted = True
_front_right_encoder_inverted = False
_front_right_x_pos: units.meter = inchesToMeters(14)
_front_right_y_pos: units.meter = inchesToMeters(-14)
# Back Left
_back_left_drive_motor_id = 1
_back_left_steer_motor_id = 2
_back_left_encoder_id = 9
_back_left_encoder_offset: units.rotation = 0.14599609375
_back_left_steer_motor_inverted = True
_back_left_encoder_inverted = False
_back_left_x_pos: units.meter = inchesToMeters(-14)
_back_left_y_pos: units.meter = inchesToMeters(14)
# Back Right
_back_right_drive_motor_id = 5
_back_right_steer_motor_id = 6
_back_right_encoder_id = 11
_back_right_encoder_offset: units.rotation = 0.30517578125
_back_right_steer_motor_inverted = True
_back_right_encoder_inverted = False
_back_right_x_pos: units.meter = inchesToMeters(-14)
_back_right_y_pos: units.meter = inchesToMeters(-14)
front_left = _constants_creator.create_module_constants(
_front_left_steer_motor_id,
_front_left_drive_motor_id,
_front_left_encoder_id,
_front_left_encoder_offset,
_front_left_x_pos,
_front_left_y_pos,
_invert_left_side,
_front_left_steer_motor_inverted,
_front_left_encoder_inverted,
)
front_right = _constants_creator.create_module_constants(
_front_right_steer_motor_id,
_front_right_drive_motor_id,
_front_right_encoder_id,
_front_right_encoder_offset,
_front_right_x_pos,
_front_right_y_pos,
_invert_right_side,
_front_right_steer_motor_inverted,
_front_right_encoder_inverted,
)
back_left = _constants_creator.create_module_constants(
_back_left_steer_motor_id,
_back_left_drive_motor_id,
_back_left_encoder_id,
_back_left_encoder_offset,
_back_left_x_pos,
_back_left_y_pos,
_invert_left_side,
_back_left_steer_motor_inverted,
_back_left_encoder_inverted,
)
back_right = _constants_creator.create_module_constants(
_back_right_steer_motor_id,
_back_right_drive_motor_id,
_back_right_encoder_id,
_back_right_encoder_offset,
_back_right_x_pos,
_back_right_y_pos,
_invert_right_side,
_back_right_steer_motor_inverted,
_back_right_encoder_inverted,
)
class Global:
# dashboard port used by the driver controller
kDriverControllerPort = 0
# TODO: remove redundancy, these are already in TunerConstants
max_speed = TunerConstants.speed_at_12_volts # desired top speed
break_speed_mul = 0.5
max_angular_rate = rotationsToRadians(0.75) # 3/4 of a rotation per second max angular velocity
canivore = "0A58A9B5463847532020204B17130DFF"