Create/support "continuous" control loops
This supplies a wrap_point argument to the control loops code that
makes it so that you can have a system that spins infinitely (as the
swerve modules do) and still control them.
TODO: I observed some idiosyncracies in wrapping behavior during
testing; this likely requires additional tests to be written to validate
that we handle wrapping correctly.
Change-Id: Id4b9065de2b3334c0e8097b28a32916c47a54258
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/frc971/control_loops/python/BUILD b/frc971/control_loops/python/BUILD
index 7f458b5..0e0cdb6 100644
--- a/frc971/control_loops/python/BUILD
+++ b/frc971/control_loops/python/BUILD
@@ -262,6 +262,22 @@
)
py_binary(
+ name = "wrapped_subsystem_test",
+ srcs = [
+ "wrapped_subsystem_test.py",
+ ],
+ legacy_create_init = False,
+ target_compatible_with = ["@platforms//cpu:x86_64"],
+ deps = [
+ ":angular_system",
+ ":controls",
+ ":python_init",
+ "@pip//glog",
+ "@pip//python_gflags",
+ ],
+)
+
+py_binary(
name = "static_zeroing_single_dof_profiled_subsystem_test",
srcs = [
"static_zeroing_single_dof_profiled_subsystem_test.py",
diff --git a/frc971/control_loops/python/angular_system.py b/frc971/control_loops/python/angular_system.py
index 16773fa..9c5484c 100755
--- a/frc971/control_loops/python/angular_system.py
+++ b/frc971/control_loops/python/angular_system.py
@@ -24,7 +24,8 @@
radius=None,
dt=0.00505,
enable_voltage_error=True,
- delayed_u=0):
+ delayed_u=0,
+ wrap_point=0.0):
"""Constructs an AngularSystemParams object.
Args:
@@ -46,6 +47,7 @@
self.dt = dt
self.enable_voltage_error = enable_voltage_error
self.delayed_u = delayed_u
+ self.wrap_point = wrap_point
class AngularSystem(control_loop.ControlLoop):
@@ -144,6 +146,8 @@
self.InitializeState()
+ self.wrap_point = numpy.matrix([[self.params.wrap_point]])
+
class IntegralAngularSystem(AngularSystem):
@@ -195,6 +199,8 @@
self.InitializeState()
+ self.wrap_point = numpy.matrix([[self.params.wrap_point]])
+
def RunTest(plant,
end_goal,
diff --git a/frc971/control_loops/python/angular_system_current.py b/frc971/control_loops/python/angular_system_current.py
index 585f54a..0365a19 100755
--- a/frc971/control_loops/python/angular_system_current.py
+++ b/frc971/control_loops/python/angular_system_current.py
@@ -22,7 +22,8 @@
kalman_q_voltage,
kalman_r_position,
radius=None,
- dt=0.00505):
+ dt=0.00505,
+ wrap_point=0.0):
"""Constructs an AngularSystemCurrentParams object.
Args:
@@ -38,6 +39,8 @@
kalman_r_position: float, std deviation of the position measurement
radius: float, radius of the mechanism in meters
dt: float, length of the control loop period in seconds
+ wrap_point: float, point at which the position will wrap (if non-zero)
+ Will typically be 2 * pi if set.
"""
self.name = name
self.motor = motor
@@ -51,6 +54,7 @@
self.kalman_r_position = kalman_r_position
self.radius = radius
self.dt = dt
+ self.wrap_point = wrap_point
# An angular system that uses current control instead of voltage
@@ -160,6 +164,8 @@
self.delayed_u = 1
+ self.wrap_point = numpy.matrix([[self.params.wrap_point]])
+
self.InitializeState()
@@ -216,6 +222,8 @@
self.InitializeState()
+ self.wrap_point = numpy.matrix([[self.params.wrap_point]])
+
def RunTest(plant,
end_goal,
diff --git a/frc971/control_loops/python/control_loop.py b/frc971/control_loops/python/control_loop.py
index a6fcd3e..ec44092 100644
--- a/frc971/control_loops/python/control_loop.py
+++ b/frc971/control_loops/python/control_loop.py
@@ -353,6 +353,7 @@
self.X_hat = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
self.last_U = numpy.matrix(
numpy.zeros((self.B.shape[1], max(1, self.delayed_u))))
+ self.wrap_point = numpy.matrix(numpy.zeros(self.Y.shape))
def PlaceControllerPoles(self, poles):
"""Places the controller poles.
@@ -451,7 +452,8 @@
"u_max": MatrixToJson(self.U_max),
"u_limit_coefficient": MatrixToJson(self.U_limit_coefficient),
"u_limit_constant": MatrixToJson(self.U_limit_constant),
- "delayed_u": self.delayed_u
+ "delayed_u": self.delayed_u,
+ "wrap_point": MatrixToJson(self.wrap_point)
}
if plant_coefficient_type.startswith('StateFeedbackPlant'):
result["a"] = MatrixToJson(self.A)
@@ -502,11 +504,13 @@
if plant_coefficient_type.startswith('StateFeedbackPlant'):
ans.append(self._DumpMatrix('A', self.A, scalar_type))
ans.append(self._DumpMatrix('B', self.B, scalar_type))
+ ans.append(
+ self._DumpMatrix('wrap_point', self.wrap_point, scalar_type))
ans.append(' const std::chrono::nanoseconds dt(%d);\n' %
(self.dt * 1e9))
ans.append(
' return %s'
- '(A, B, C, D, U_max, U_min, U_limit_coefficient, U_limit_constant, dt, %s);\n'
+ '(A, B, C, D, U_max, U_min, U_limit_coefficient, U_limit_constant, dt, %s, wrap_point);\n'
% (plant_coefficient_type, delayed_u_string))
elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
ans.append(
diff --git a/frc971/control_loops/python/polydrivetrain.py b/frc971/control_loops/python/polydrivetrain.py
index a529378..02a61f1 100644
--- a/frc971/control_loops/python/polydrivetrain.py
+++ b/frc971/control_loops/python/polydrivetrain.py
@@ -170,6 +170,7 @@
self.U_max = self._drivetrain.U_max
self.U_min = self._drivetrain.U_min
+ self.wrap_point = numpy.matrix(numpy.zeros((2, 1)))
@property
def robot_radius_l(self):
diff --git a/frc971/control_loops/python/wrapped_subsystem_test.py b/frc971/control_loops/python/wrapped_subsystem_test.py
new file mode 100644
index 0000000..0a68103
--- /dev/null
+++ b/frc971/control_loops/python/wrapped_subsystem_test.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python3
+
+# Generates profiled subsystem for use in
+# static_zeroing_single_dof_profiled_subsystem_test
+
+from frc971.control_loops.python import control_loop
+from frc971.control_loops.python import angular_system
+import numpy
+import sys
+import gflags
+import glog
+
+FLAGS = gflags.FLAGS
+
+try:
+ gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
+except gflags.DuplicateFlagError:
+ pass
+
+kWrappedSystem = angular_system.AngularSystemParams(
+ name='TestWrappedSystem',
+ motor=control_loop.Vex775Pro(),
+ G=(1.0 / 35.0) * (20.0 / 40.0),
+ radius=16.0 * 0.25 / (2.0 * numpy.pi) * 0.0254,
+ J=5.4,
+ q_pos=0.015,
+ q_vel=0.3,
+ kalman_q_pos=0.12,
+ kalman_q_vel=2.00,
+ kalman_q_voltage=40.0,
+ kalman_r_position=0.05,
+ wrap_point=2.0 * numpy.pi)
+
+
+def main(argv):
+ if FLAGS.plot:
+ R = numpy.matrix([[0.1], [0.0]])
+ angular_system.PlotMotion(kWrappedSystem, R)
+
+ # Write the generated constants out to a file.
+ if len(argv) != 7:
+ glog.fatal(
+ 'Expected .h, .cc, and .json filenames and .json file name for the \
+ static_zeroing_single_dof_profiled_subsystem_test and integral \
+ static_zeroing_single_dof_profiled_subsystem_test.')
+ else:
+ namespaces = ['frc971', 'control_loops']
+ angular_system.WriteAngularSystem(kWrappedSystem, argv[1:4], argv[4:7],
+ namespaces)
+
+
+if __name__ == '__main__':
+ argv = FLAGS(sys.argv)
+ glog.init()
+ sys.exit(main(argv))