blob: 6be2604ae3f78422ae31c887cc0b2976a62f9701 [file] [log] [blame]
justinT21446e4f62024-06-16 22:36:10 -07001load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")
Nikolai Sohmers8412fe52024-06-09 18:01:18 -07002load("//aos:config.bzl", "aos_config")
James Kuszmauld938d332024-05-15 20:47:19 -07003load("//aos/flatbuffers:generate.bzl", "static_flatbuffer")
4
Nikolai Sohmers69ecb912024-06-08 14:06:22 -07005package(default_visibility = ["//visibility:public"])
6
7static_flatbuffer(
James Kuszmauldf6b1682024-07-06 13:53:10 -07008 name = "swerve_zeroing_fbs",
9 srcs = ["swerve_zeroing.fbs"],
10 visibility = ["//visibility:public"],
11 deps = [
12 "//frc971/zeroing:constants_fbs",
13 ],
14)
15
16static_flatbuffer(
James Kuszmaule6cd20f2024-10-29 22:21:57 -070017 name = "swerve_drivetrain_joystick_goal_fbs",
18 srcs = ["swerve_drivetrain_joystick_goal.fbs"],
19)
20
21static_flatbuffer(
Nikolai Sohmers69ecb912024-06-08 14:06:22 -070022 name = "swerve_drivetrain_goal_fbs",
23 srcs = ["swerve_drivetrain_goal.fbs"],
James Kuszmaule6cd20f2024-10-29 22:21:57 -070024 deps = [
25 ":swerve_drivetrain_joystick_goal_fbs",
26 "//frc971/math:matrix_fbs",
27 ],
Nikolai Sohmers69ecb912024-06-08 14:06:22 -070028)
29
30static_flatbuffer(
31 name = "swerve_drivetrain_status_fbs",
32 srcs = ["swerve_drivetrain_status.fbs"],
33 deps = ["//frc971/control_loops:profiled_subsystem_fbs"],
34)
35
James Kuszmauld938d332024-05-15 20:47:19 -070036static_flatbuffer(
37 name = "swerve_drivetrain_output_fbs",
38 srcs = ["swerve_drivetrain_output.fbs"],
James Kuszmauld938d332024-05-15 20:47:19 -070039 deps = ["//frc971/control_loops:can_talonfx_fbs"],
40)
41
42static_flatbuffer(
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070043 name = "swerve_drivetrain_can_position_fbs",
44 srcs = ["swerve_drivetrain_can_position.fbs"],
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070045 deps = ["//frc971/control_loops:can_talonfx_fbs"],
46)
47
48static_flatbuffer(
James Kuszmauld938d332024-05-15 20:47:19 -070049 name = "swerve_drivetrain_position_fbs",
50 srcs = ["swerve_drivetrain_position.fbs"],
James Kuszmauld938d332024-05-15 20:47:19 -070051 deps = ["//frc971/control_loops:control_loops_fbs"],
52)
53
Austin Schuh999a19e2024-05-04 14:52:39 -070054py_binary(
55 name = "simulation",
56 srcs = [
57 "simulation.py",
58 ],
59 deps = [
60 "//frc971/control_loops/python:controls",
61 "@pip//matplotlib",
62 "@pip//numpy",
63 "@pip//pygobject",
64 "@pip//sympy",
65 ],
66)
Nikolai Sohmers8412fe52024-06-09 18:01:18 -070067
68cc_library(
69 name = "swerve_control_loops",
70 srcs = ["swerve_control_loops.cc"],
71 hdrs = ["swerve_control_loops.h"],
72 target_compatible_with = ["@platforms//os:linux"],
73 deps = [
74 ":swerve_drivetrain_can_position_fbs",
75 ":swerve_drivetrain_goal_fbs",
76 ":swerve_drivetrain_output_fbs",
77 ":swerve_drivetrain_position_fbs",
78 ":swerve_drivetrain_status_fbs",
Nikolai Sohmers308c8992024-06-22 15:11:05 -070079 ":swerve_zeroing_fbs",
Nikolai Sohmers8412fe52024-06-09 18:01:18 -070080 "//frc971/control_loops:control_loop",
Nikolai Sohmers308c8992024-06-22 15:11:05 -070081 "//frc971/control_loops:static_zeroing_single_dof_profiled_subsystem",
82 "//frc971/zeroing:continuous_absolute_encoder",
Nikolai Sohmers8412fe52024-06-09 18:01:18 -070083 ],
84)
85
86cc_test(
87 name = "swerve_control_test",
88 srcs = ["swerve_control_test.cc"],
89 data = [
90 ":aos_config",
Nikolai Sohmers308c8992024-06-22 15:11:05 -070091 "//frc971/control_loops/swerve/test_module:rotation_json",
Nikolai Sohmers8412fe52024-06-09 18:01:18 -070092 ],
93 target_compatible_with = ["@platforms//os:linux"],
94 deps = [
95 ":swerve_control_loops",
96 ":swerve_drivetrain_can_position_fbs",
97 ":swerve_drivetrain_goal_fbs",
98 ":swerve_drivetrain_output_fbs",
99 ":swerve_drivetrain_position_fbs",
100 ":swerve_drivetrain_status_fbs",
101 "//aos/events:shm_event_loop",
102 "//aos/testing:googletest",
103 "//frc971/control_loops:control_loop_test",
104 "//frc971/control_loops:state_feedback_loop",
105 "//frc971/control_loops:team_number_test_environment",
106 ],
107)
108
109aos_config(
110 name = "aos_config",
111 src = "swerve.json",
112 flatbuffers = [
113 ":swerve_drivetrain_goal_fbs",
114 ":swerve_drivetrain_output_fbs",
115 ":swerve_drivetrain_position_fbs",
116 ":swerve_drivetrain_can_position_fbs",
117 ":swerve_drivetrain_status_fbs",
118 ],
119 target_compatible_with = ["@platforms//os:linux"],
120 deps = ["//frc971/input:aos_config"],
121)
justinT21446e4f62024-06-16 22:36:10 -0700122
123cc_library(
124 name = "motors",
125 hdrs = [
126 "motors.h",
127 ],
128 deps = [
129 "@symengine",
130 ],
131)
132
133cc_binary(
134 name = "generate_physics",
135 srcs = [
136 "generate_physics.cc",
137 ],
138 deps = [
139 ":motors",
140 "//aos:init",
141 "//aos/util:file",
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700142 "@com_google_absl//absl/log",
143 "@com_google_absl//absl/log:check",
justinT21446e4f62024-06-16 22:36:10 -0700144 "@com_google_absl//absl/strings",
145 "@com_google_absl//absl/strings:str_format",
146 "@symengine",
147 ],
148)
149
150run_binary(
Austin Schuh27694fa2024-07-20 16:29:49 -0700151 name = "dynamics_codegen_cc",
justinT21446e4f62024-06-16 22:36:10 -0700152 outs = [
153 "dynamics.cc",
154 "dynamics.h",
justinT21942892b2024-07-02 22:33:50 -0700155 "dynamics.py",
Austin Schuha9550c02024-10-19 13:48:10 -0700156 "dynamics_constants.py",
justinT21446e4f62024-06-16 22:36:10 -0700157 ],
158 args = [
159 "--output_base=$(BINDIR)/",
160 "--cc_output_path=$(location :dynamics.cc)",
161 "--h_output_path=$(location :dynamics.h)",
Austin Schuh0f881092024-06-28 15:36:48 -0700162 "--casadi_py_output_path=$(location :dynamics.py)",
Austin Schuha9550c02024-10-19 13:48:10 -0700163 "--constants_output_path=$(location :dynamics_constants.py)",
justinT21446e4f62024-06-16 22:36:10 -0700164 ],
165 tool = ":generate_physics",
166)
167
Austin Schuh27694fa2024-07-20 16:29:49 -0700168run_binary(
169 name = "dynamics_codegen_nocaster",
170 outs = [
171 "nocaster_dynamics.py",
172 ],
173 args = [
174 "--caster=0.0",
175 "--output_base=$(BINDIR)/",
176 "--casadi_py_output_path=$(location :nocaster_dynamics.py)",
177 ],
178 tool = ":generate_physics",
179)
180
181run_binary(
182 name = "dynamics_codegen_bigcaster",
183 outs = [
184 "bigcaster_dynamics.py",
185 ],
186 args = [
187 "--caster=0.05",
188 "--output_base=$(BINDIR)/",
189 "--casadi_py_output_path=$(location :bigcaster_dynamics.py)",
190 ],
191 tool = ":generate_physics",
192)
193
justinT21446e4f62024-06-16 22:36:10 -0700194cc_library(
justinT2107d41ed2024-07-31 21:12:31 -0700195 name = "eigen_dynamics",
justinT21446e4f62024-06-16 22:36:10 -0700196 srcs = ["dynamics.cc"],
197 hdrs = ["dynamics.h"],
198 deps = [
199 "@org_tuxfamily_eigen//:eigen",
200 ],
201)
justinT21942892b2024-07-02 22:33:50 -0700202
Austin Schuh6ea789e2024-07-27 13:45:53 -0700203py_library(
204 name = "dynamics",
justinT21942892b2024-07-02 22:33:50 -0700205 srcs = [
Austin Schuh6ea789e2024-07-27 13:45:53 -0700206 "bigcaster_dynamics.py",
207 "dynamics.py",
208 "nocaster_dynamics.py",
justinT21942892b2024-07-02 22:33:50 -0700209 ],
210 deps = [
Austin Schuh6ea789e2024-07-27 13:45:53 -0700211 "@pip//casadi",
justinT21942892b2024-07-02 22:33:50 -0700212 ],
213)
Austin Schuh2a1abec2024-07-10 20:31:16 -0700214
Austin Schuh76534f32024-09-02 13:52:45 -0700215py_library(
216 name = "jax_dynamics",
217 srcs = [
Austin Schuha9550c02024-10-19 13:48:10 -0700218 "dynamics_constants.py",
Austin Schuh76534f32024-09-02 13:52:45 -0700219 "jax_dynamics.py",
220 ],
221 deps = [
Austin Schuh76534f32024-09-02 13:52:45 -0700222 "//frc971/control_loops/python:controls",
223 "@pip//jax",
224 ],
225)
226
Austin Schuh2a1abec2024-07-10 20:31:16 -0700227py_test(
Austin Schuh76534f32024-09-02 13:52:45 -0700228 name = "physics_test_cpu",
Austin Schuh2a1abec2024-07-10 20:31:16 -0700229 srcs = [
Austin Schuh2a1abec2024-07-10 20:31:16 -0700230 "physics_test.py",
231 ],
James Kuszmaulef14ab42024-09-14 15:05:24 -0700232 data = [":cpp_dynamics.so"],
Austin Schuh76534f32024-09-02 13:52:45 -0700233 env = {
234 "JAX_PLATFORMS": "cpu",
235 },
236 main = "physics_test.py",
Austin Schuh2a1abec2024-07-10 20:31:16 -0700237 target_compatible_with = ["@platforms//cpu:x86_64"],
238 deps = [
Austin Schuh5dac2292024-10-19 13:56:58 -0700239 ":casadi_velocity_mpc_lib",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700240 ":dynamics",
Austin Schuh76534f32024-09-02 13:52:45 -0700241 ":jax_dynamics",
242 ":physics_test_utils",
Austin Schuh5dac2292024-10-19 13:56:58 -0700243 "@pip//absl_py",
Austin Schuh76534f32024-09-02 13:52:45 -0700244 "@pip//casadi",
Austin Schuh5dac2292024-10-19 13:56:58 -0700245 "@pip//matplotlib",
Austin Schuh76534f32024-09-02 13:52:45 -0700246 "@pip//numpy",
Austin Schuh5dac2292024-10-19 13:56:58 -0700247 "@pip//pygobject",
Austin Schuh76534f32024-09-02 13:52:45 -0700248 "@pip//scipy",
249 ],
250)
251
252py_test(
253 name = "physics_test_gpu",
254 srcs = [
255 "physics_test.py",
256 ],
James Kuszmaulef14ab42024-09-14 15:05:24 -0700257 data = [":cpp_dynamics.so"],
Austin Schuh76534f32024-09-02 13:52:45 -0700258 env = {
259 "JAX_PLATFORMS": "cuda",
260 },
261 main = "physics_test.py",
262 target_compatible_with = ["@platforms//cpu:x86_64"],
263 deps = [
Austin Schuh5dac2292024-10-19 13:56:58 -0700264 ":casadi_velocity_mpc_lib",
Austin Schuh76534f32024-09-02 13:52:45 -0700265 ":dynamics",
266 ":jax_dynamics",
justinT2107d41ed2024-07-31 21:12:31 -0700267 ":physics_test_utils",
Austin Schuh5dac2292024-10-19 13:56:58 -0700268 "@pip//absl_py",
269 "@pip//casadi",
270 "@pip//matplotlib",
271 "@pip//numpy",
272 "@pip//pygobject",
273 "@pip//scipy",
274 ],
275)
276
277py_binary(
278 name = "casadi_velocity_mpc_lib",
279 srcs = [
280 "casadi_velocity_mpc_lib.py",
281 ],
282 deps = [
283 ":dynamics",
Austin Schuh2a1abec2024-07-10 20:31:16 -0700284 "@pip//casadi",
285 "@pip//numpy",
286 ],
287)
justinT2107d41ed2024-07-31 21:12:31 -0700288
Austin Schuh6ea789e2024-07-27 13:45:53 -0700289py_binary(
290 name = "casadi_velocity_mpc",
justinT2107d41ed2024-07-31 21:12:31 -0700291 srcs = [
Austin Schuh6ea789e2024-07-27 13:45:53 -0700292 "casadi_velocity_mpc.py",
justinT2107d41ed2024-07-31 21:12:31 -0700293 ],
294 deps = [
Austin Schuh5dac2292024-10-19 13:56:58 -0700295 ":casadi_velocity_mpc_lib",
Austin Schuh9289e6e2024-09-02 13:10:12 -0700296 "@pip//absl_py",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700297 "@pip//matplotlib",
298 "@pip//numpy",
299 "@pip//pygobject",
300 "@pip//scipy",
justinT2107d41ed2024-07-31 21:12:31 -0700301 ],
302)
303
Austin Schuh9f57e5b2024-10-26 17:27:36 -0700304py_binary(
305 name = "experience_collector",
306 srcs = [
307 "experience_collector.py",
308 ],
309 deps = [
310 ":casadi_velocity_mpc_lib",
311 ":jax_dynamics",
312 "//frc971/control_loops/swerve/velocity_controller:physics",
313 "@pip//absl_py",
314 "@pip//matplotlib",
315 "@pip//numpy",
316 "@pip//pygobject",
317 "@pip//scipy",
318 "@pip//tensorflow",
319 ],
320)
321
322py_binary(
323 name = "multi_experience_collector",
324 srcs = ["multi_experience_collector.py"],
325 data = [":experience_collector"],
326 deps = [
327 "@pip//absl_py",
328 ],
329)
330
justinT2107d41ed2024-07-31 21:12:31 -0700331py_library(
332 name = "physics_test_utils",
333 srcs = [
334 "physics_test_utils.py",
335 ],
336 deps = [
337 ":dynamics",
338 "@pip//casadi",
339 "@pip//numpy",
340 ],
341)
342
343py_binary(
344 name = "physics_debug",
345 srcs = [
346 "physics_debug.py",
347 ],
348 deps = [
349 ":physics_test_utils",
350 "@pip//matplotlib",
351 "@pip//pygobject",
352 "@pip//scipy",
353 ],
354)
Austin Schuh7fa65212024-08-18 17:20:58 -0700355
356py_binary(
357 name = "debug_fatrop",
358 srcs = [
359 "debug_fatrop.py",
360 ],
361 deps = [
362 "@pip//casadi",
363 "@pip//matplotlib",
364 "@pip//numpy",
365 "@pip//pygobject",
366 "@pip//scipy",
367 ],
368)
justinT21cc6eed02024-08-26 00:15:57 -0700369
370py_binary(
371 name = "smooth_function_graph",
372 srcs = [
373 "smooth_function_graph.py",
374 ],
375 deps = [
376 "@pip//matplotlib",
377 "@pip//numpy",
378 "@pip//pygobject",
379 "@pip//scipy",
380 ],
381)
Austin Schuh3aa49ab2024-09-02 13:14:26 -0700382
383py_binary(
384 name = "multi_casadi_velocity_mpc",
385 srcs = ["multi_casadi_velocity_mpc.py"],
386 data = [":casadi_velocity_mpc"],
387 deps = [
388 ":dynamics",
389 "@pip//absl_py",
390 "@pip//matplotlib",
391 "@pip//numpy",
392 "@pip//pygobject",
393 ],
394)
James Kuszmaulef14ab42024-09-14 15:05:24 -0700395
396cc_binary(
397 name = "cpp_dynamics.so",
398 # Just use the python dynamics directly if you want them; this is just for testing.
399 testonly = True,
400 srcs = ["dynamics_python_bindings.cc"],
401 linkshared = True,
402 target_compatible_with = ["@platforms//os:linux"],
403 deps = [
404 ":eigen_dynamics",
405 "//third_party/python",
406 "@com_google_absl//absl/log",
407 "@com_google_absl//absl/log:check",
408 ],
409)
James Kuszmaul43504bf2024-10-08 23:28:11 -0700410
411cc_library(
James Kuszmaul886675c2024-10-09 20:32:00 -0700412 name = "linearization_utils",
413 hdrs = ["linearization_utils.h"],
414)
415
416cc_library(
417 name = "linearized_controller",
418 hdrs = ["linearized_controller.h"],
419 deps = [
420 ":eigen_dynamics",
421 ":linearization_utils",
422 "//frc971/control_loops:c2d",
423 "//frc971/control_loops:dlqr",
424 "//frc971/control_loops:jacobian",
425 "@com_google_absl//absl/log",
426 "@com_google_absl//absl/log:check",
427 ],
428)
429
430cc_test(
431 name = "linearized_controller_test",
432 srcs = ["linearized_controller_test.cc"],
433 deps = [
434 ":linearized_controller",
435 "//aos/testing:googletest",
436 ],
437)
438
439cc_library(
James Kuszmaul43504bf2024-10-08 23:28:11 -0700440 name = "auto_diff_jacobian",
441 hdrs = ["auto_diff_jacobian.h"],
442 target_compatible_with = ["@platforms//os:linux"],
443 deps = [
444 "@com_google_ceres_solver//:ceres",
445 ],
446)
447
448cc_test(
449 name = "auto_diff_jacobian_test",
450 srcs = ["auto_diff_jacobian_test.cc"],
451 deps = [
452 ":auto_diff_jacobian",
453 "//aos/testing:googletest",
454 ],
455)
James Kuszmaulde44eb52024-10-08 23:39:24 -0700456
457cc_library(
458 name = "simplified_dynamics",
459 hdrs = ["simplified_dynamics.h"],
460 deps = [
461 ":auto_diff_jacobian",
462 ":eigen_dynamics",
463 ":motors",
464 "//aos/util:math",
465 "@com_google_absl//absl/log",
466 "@com_google_absl//absl/log:check",
467 ],
468)
469
470cc_test(
471 name = "simplified_dynamics_test",
472 srcs = ["simplified_dynamics_test.cc"],
473 deps = [
474 ":simplified_dynamics",
475 "//aos/testing:googletest",
476 "//aos/time",
477 "//frc971/control_loops:jacobian",
478 "@com_google_absl//absl/log",
479 ],
480)
James Kuszmaul29a64902024-10-09 07:56:18 -0700481
482cc_library(
483 name = "inverse_kinematics",
484 hdrs = ["inverse_kinematics.h"],
485 deps = [
486 ":simplified_dynamics",
487 "//aos/util:math",
488 ],
489)
490
491cc_test(
492 name = "inverse_kinematics_test",
493 srcs = ["inverse_kinematics_test.cc"],
494 deps = [
495 ":inverse_kinematics",
496 "//aos/testing:googletest",
497 ],
498)