Set up control loops for y2024_swerve testing
Change-Id: Ic80503781a3b92ee711a1c8b2389fa194eacc31d
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/y2024_swerve/constants/BUILD b/y2024_swerve/constants/BUILD
index 42cff81..ae707f3 100644
--- a/y2024_swerve/constants/BUILD
+++ b/y2024_swerve/constants/BUILD
@@ -26,6 +26,7 @@
"common.jinja2",
"common.json",
"//y2024_swerve/constants/calib_files",
+ "//y2024_swerve/control_loops/drivetrain:rotation_json",
"//y2024_swerve/vision/maps",
],
parameters = {},
@@ -41,6 +42,7 @@
"common.jinja2",
"common.json",
"//y2024_swerve/constants/calib_files",
+ "//y2024_swerve/control_loops/drivetrain:rotation_json",
"//y2024_swerve/vision/maps",
],
parameters = {},
diff --git a/y2024_swerve/constants/common.json b/y2024_swerve/constants/common.json
index 393b7b9..300e5f6 100644
--- a/y2024_swerve/constants/common.json
+++ b/y2024_swerve/constants/common.json
@@ -3,5 +3,24 @@
{% set pi = 3.141592653589793 %}
"relative_encoder_scale": {{ 2.0 * pi / 4096 }},
"absolute_encoder_scale": {{ 2.0 * pi }}
+ },
+ "rotation": {
+ "zeroing_voltage": 3.0,
+ "operating_voltage": 12.0,
+ "zeroing_profile_params": {
+ "max_velocity": 0.5,
+ "max_acceleration": 3.0
+ },
+ "default_profile_params":{
+ "max_velocity": 12.0,
+ "max_acceleration": 55.0
+ },
+ "range": {
+ "lower_hard": -inf,
+ "upper_hard": inf,
+ "lower": -inf,
+ "upper": inf
+ },
+ "loop": {% include 'y2024_swerve/control_loops/drivetrain/integral_rotation_plant.json' %}
}
}
diff --git a/y2024_swerve/control_loops/drivetrain/BUILD b/y2024_swerve/control_loops/drivetrain/BUILD
new file mode 100644
index 0000000..544cf49
--- /dev/null
+++ b/y2024_swerve/control_loops/drivetrain/BUILD
@@ -0,0 +1,22 @@
+genrule(
+ name = "genrule_rotation",
+ outs = [
+ "rotation_plant.h",
+ "rotation_plant.cc",
+ "rotation_plant.json",
+ "integral_rotation_plant.h",
+ "integral_rotation_plant.cc",
+ "integral_rotation_plant.json",
+ ],
+ cmd = "$(location //y2024_swerve/control_loops/python:rotation) $(OUTS)",
+ target_compatible_with = ["@platforms//os:linux"],
+ tools = [
+ "//y2024_swerve/control_loops/python:rotation",
+ ],
+)
+
+filegroup(
+ name = "rotation_json",
+ srcs = ["integral_rotation_plant.json"],
+ visibility = ["//visibility:public"],
+)
diff --git a/y2024_swerve/control_loops/python/BUILD b/y2024_swerve/control_loops/python/BUILD
new file mode 100644
index 0000000..0a6e2c6
--- /dev/null
+++ b/y2024_swerve/control_loops/python/BUILD
@@ -0,0 +1,14 @@
+py_binary(
+ name = "rotation",
+ srcs = [
+ "rotation.py",
+ ],
+ target_compatible_with = ["@platforms//cpu:x86_64"],
+ visibility = ["//y2024_swerve:__subpackages__"],
+ deps = [
+ "//frc971/control_loops/python:angular_system_current",
+ "//frc971/control_loops/python:controls",
+ "@pip//glog",
+ "@pip//python_gflags",
+ ],
+)
diff --git a/y2024_swerve/control_loops/python/rotation.py b/y2024_swerve/control_loops/python/rotation.py
new file mode 100644
index 0000000..a75ed5b
--- /dev/null
+++ b/y2024_swerve/control_loops/python/rotation.py
@@ -0,0 +1,57 @@
+#!/usr/bin/python3
+
+from aos.util.trapezoid_profile import TrapezoidProfile
+from frc971.control_loops.python import control_loop
+from frc971.control_loops.python import angular_system_current
+from frc971.control_loops.python import controls
+import numpy
+import sys
+from matplotlib import pylab
+import gflags
+import glog
+import matplotlib
+
+FLAGS = gflags.FLAGS
+
+try:
+ gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
+except gflags.DuplicateFlagError:
+ pass
+
+kRotation = angular_system_current.AngularSystemCurrentParams(
+ name='Rotation',
+ motor=control_loop.KrakenFOC(),
+ G=9.0 / 24.0 * 14.0 / 72.0,
+ J=3.1 / 1000.0,
+ q_pos=0.05,
+ q_vel=20.0,
+ kalman_q_pos=0.12,
+ kalman_q_vel=2.0,
+ kalman_q_voltage=4.0,
+ kalman_r_position=0.05,
+ radius=25 * 0.0254,
+ wrap_point=2.0 * numpy.pi)
+
+
+def main(argv):
+ if FLAGS.plot:
+ R = numpy.matrix([[numpy.pi / 2.0], [0.0]])
+ angular_system_current.PlotKick(kRotation, R)
+ angular_system_current.PlotMotion(kRotation, R)
+ return
+
+ # Write the generated constants out to a file.
+ if len(argv) != 7:
+ glog.fatal(
+ 'Expected .h file name and .cc file name for the wrist and integral wrist.'
+ )
+ else:
+ namespaces = ['y2024_swerve', 'control_loops', 'drivetrain']
+ angular_system_current.WriteAngularSystemCurrent(
+ kRotation, argv[1:4], argv[4:7], namespaces)
+
+
+if __name__ == '__main__':
+ argv = FLAGS(sys.argv)
+ glog.init()
+ sys.exit(main(argv))