Add roll AngularSystem code gen to 2023
Controller hasn't been tuned, FYI.
Change-Id: I0e5ddb594552a020b58060c4edea96ff675bf677
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/frc971/control_loops/python/angular_system.py b/frc971/control_loops/python/angular_system.py
index 6772e71..c6ffb50 100755
--- a/frc971/control_loops/python/angular_system.py
+++ b/frc971/control_loops/python/angular_system.py
@@ -120,11 +120,15 @@
self.R = numpy.matrix([[(self.params.kalman_r_position**2.0)]])
- self.KalmanGain, self.Q_steady = controls.kalman(A=self.A,
- B=self.B,
- C=self.C,
- Q=self.Q,
- R=self.R)
+ # From testing, these continuous Q and R's appear to be good approximations of Q and R.
+ self.Q_continuous = self.Q / self.dt
+ self.R_continuous = self.R * self.dt
+
+ self.KalmanGain, self.P_steady_state = controls.kalman(A=self.A,
+ B=self.B,
+ C=self.C,
+ Q=self.Q,
+ R=self.R)
glog.debug('Kal %s', repr(self.KalmanGain))
@@ -165,11 +169,15 @@
self.R = numpy.matrix([[(self.params.kalman_r_position**2.0)]])
- self.KalmanGain, self.Q_steady = controls.kalman(A=self.A,
- B=self.B,
- C=self.C,
- Q=self.Q,
- R=self.R)
+ # From testing, these continuous Q and R's appear to be good approximations of Q and R.
+ self.Q_continuous = self.Q / self.dt
+ self.R_continuous = self.R * self.dt
+
+ self.KalmanGain, self.P_steady_state = controls.kalman(A=self.A,
+ B=self.B,
+ C=self.C,
+ Q=self.Q,
+ R=self.R)
self.K_unaugmented = self.K
self.K = numpy.matrix(numpy.zeros((1, 3)))
@@ -409,7 +417,12 @@
max_acceleration=max_acceleration)
-def WriteAngularSystem(params, plant_files, controller_files, year_namespaces):
+def WriteAngularSystem(params,
+ plant_files,
+ controller_files,
+ year_namespaces,
+ plant_type='StateFeedbackPlant',
+ observer_type='StateFeedbackObserver'):
"""Writes out the constants for a angular system to a file.
Args:
@@ -440,7 +453,9 @@
loop_writer = control_loop.ControlLoopWriter(name,
angular_systems,
- namespaces=year_namespaces)
+ namespaces=year_namespaces,
+ plant_type=plant_type,
+ observer_type=observer_type)
loop_writer.AddConstant(
control_loop.Constant('kOutputRatio', '%f', angular_systems[0].G))
loop_writer.AddConstant(
@@ -451,5 +466,7 @@
integral_loop_writer = control_loop.ControlLoopWriter(
'Integral' + name,
integral_angular_systems,
- namespaces=year_namespaces)
+ namespaces=year_namespaces,
+ plant_type=plant_type,
+ observer_type=observer_type)
integral_loop_writer.Write(controller_files[0], controller_files[1])
diff --git a/y2023/control_loops/python/BUILD b/y2023/control_loops/python/BUILD
index 7825675..f94772d 100644
--- a/y2023/control_loops/python/BUILD
+++ b/y2023/control_loops/python/BUILD
@@ -90,3 +90,19 @@
visibility = ["//visibility:public"],
deps = ["//y2023/control_loops:python_init"],
)
+
+py_binary(
+ name = "roll",
+ srcs = [
+ "roll.py",
+ ],
+ legacy_create_init = False,
+ target_compatible_with = ["@platforms//cpu:x86_64"],
+ deps = [
+ ":python_init",
+ "//frc971/control_loops/python:angular_system",
+ "//frc971/control_loops/python:controls",
+ "@pip//glog",
+ "@pip//python_gflags",
+ ],
+)
diff --git a/y2023/control_loops/python/roll.py b/y2023/control_loops/python/roll.py
new file mode 100644
index 0000000..7e342b2
--- /dev/null
+++ b/y2023/control_loops/python/roll.py
@@ -0,0 +1,67 @@
+#!/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
+from frc971.control_loops.python import controls
+import numpy
+import sys
+from matplotlib import pylab
+import gflags
+import glog
+
+FLAGS = gflags.FLAGS
+
+try:
+ gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
+except gflags.DuplicateFlagError:
+ pass
+
+gflags.DEFINE_bool('hybrid', False, 'If true, make it hybrid.')
+
+kRoll = angular_system.AngularSystemParams(
+ name='Roll',
+ motor=control_loop.BAG(),
+ G=18.0 / 48.0 * 1.0 / 36.0,
+ # 598.006 in^2 lb
+ J=0.175,
+ q_pos=0.40,
+ 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=13 * 0.0254)
+
+
+def main(argv):
+ if FLAGS.plot:
+ R = numpy.matrix([[numpy.pi / 2.0], [0.0]])
+ angular_system.PlotKick(kRoll, R)
+ angular_system.PlotMotion(kRoll, R)
+
+ # Write the generated constants out to a file.
+ if len(argv) != 5:
+ glog.fatal(
+ 'Expected .h file name and .cc file name for the intake and integral intake.'
+ )
+ else:
+ namespaces = ['y2023', 'control_loops', 'superstructure', 'roll']
+ if FLAGS.hybrid:
+ kRoll.name = 'HybridRoll'
+ angular_system.WriteAngularSystem(
+ kRoll,
+ argv[1:3],
+ argv[3:5],
+ namespaces,
+ plant_type='StateFeedbackHybridPlant',
+ observer_type='HybridKalman')
+ else:
+ angular_system.WriteAngularSystem(kRoll, argv[1:3], argv[3:5],
+ namespaces)
+
+
+if __name__ == '__main__':
+ argv = FLAGS(sys.argv)
+ glog.init()
+ sys.exit(main(argv))
diff --git a/y2023/control_loops/superstructure/roll/BUILD b/y2023/control_loops/superstructure/roll/BUILD
new file mode 100644
index 0000000..f1cca65
--- /dev/null
+++ b/y2023/control_loops/superstructure/roll/BUILD
@@ -0,0 +1,53 @@
+package(default_visibility = ["//y2023:__subpackages__"])
+
+genrule(
+ name = "genrule_roll",
+ outs = [
+ "roll_plant.h",
+ "roll_plant.cc",
+ "integral_roll_plant.h",
+ "integral_roll_plant.cc",
+ ],
+ cmd = "$(location //y2023/control_loops/python:roll) $(OUTS)",
+ target_compatible_with = ["@platforms//os:linux"],
+ tools = [
+ "//y2023/control_loops/python:roll",
+ ],
+)
+
+genrule(
+ name = "genrule_hybrid_roll",
+ outs = [
+ "hybrid_roll_plant.h",
+ "hybrid_roll_plant.cc",
+ "integral_hybrid_roll_plant.h",
+ "integral_hybrid_roll_plant.cc",
+ ],
+ cmd = "$(location //y2023/control_loops/python:roll) --hybrid $(OUTS)",
+ target_compatible_with = ["@platforms//os:linux"],
+ tools = [
+ "//y2023/control_loops/python:roll",
+ ],
+)
+
+cc_library(
+ name = "roll_plants",
+ srcs = [
+ "hybrid_roll_plant.cc",
+ "integral_hybrid_roll_plant.cc",
+ "integral_roll_plant.cc",
+ "roll_plant.cc",
+ ],
+ hdrs = [
+ "hybrid_roll_plant.h",
+ "integral_hybrid_roll_plant.h",
+ "integral_roll_plant.h",
+ "roll_plant.h",
+ ],
+ target_compatible_with = ["@platforms//os:linux"],
+ visibility = ["//visibility:public"],
+ deps = [
+ "//frc971/control_loops:hybrid_state_feedback_loop",
+ "//frc971/control_loops:state_feedback_loop",
+ ],
+)