blob: d4c3fe6710cbe5b1408b965f5e8a74b55b67a8bb [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Austin Schuh48d60c12017-02-04 21:58:58 -08002
3from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
5import numpy
6import sys
7from matplotlib import pylab
8
9import gflags
10import glog
11
12FLAGS = gflags.FLAGS
13
Austin Schuh82a66dc2017-03-04 15:06:44 -080014try:
Ravago Jones5127ccc2022-07-31 16:32:45 -070015 gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
Austin Schuh82a66dc2017-03-04 15:06:44 -080016except gflags.DuplicateFlagError:
Ravago Jones5127ccc2022-07-31 16:32:45 -070017 pass
Austin Schuh48d60c12017-02-04 21:58:58 -080018
Austin Schuhcd3237a2017-02-18 14:19:26 -080019gflags.DEFINE_bool('stall', False, 'If true, stall the indexer.')
20
Ravago Jones5127ccc2022-07-31 16:32:45 -070021
Austin Schuh48d60c12017-02-04 21:58:58 -080022class VelocityIndexer(control_loop.ControlLoop):
Austin Schuh48d60c12017-02-04 21:58:58 -080023
Ravago Jones5127ccc2022-07-31 16:32:45 -070024 def __init__(self, name='VelocityIndexer'):
25 super(VelocityIndexer, self).__init__(name)
26 # Stall Torque in N m
27 self.stall_torque = 0.71
28 # Stall Current in Amps
29 self.stall_current = 134
30 self.free_speed_rpm = 18730.0
31 # Free Speed in rotations/second.
32 self.free_speed = self.free_speed_rpm / 60.0
33 # Free Current in Amps
34 self.free_current = 0.7
35 # Moment of inertia of the indexer halves in kg m^2
36 # This is measured as Iyy in CAD (the moment of inertia around the Y axis).
37 # Inner part of indexer -> Iyy = 59500 lb * mm * mm
38 # Inner spins with 12 / 48 * 18 / 48 * 24 / 36 * 16 / 72
39 # Outer part of indexer -> Iyy = 210000 lb * mm * mm
40 # 1 775 pro -> 12 / 48 * 18 / 48 * 30 / 422
Austin Schuh48d60c12017-02-04 21:58:58 -080041
Ravago Jones5127ccc2022-07-31 16:32:45 -070042 self.J_inner = 0.0269
43 self.J_outer = 0.0952
44 # Gear ratios for the inner and outer parts.
45 self.G_inner = (12.0 / 48.0) * (20.0 / 34.0) * (18.0 / 36.0) * (12.0 /
46 84.0)
47 self.G_outer = (12.0 / 48.0) * (20.0 / 34.0) * (18.0 / 36.0) * (24.0 /
48 420.0)
Austin Schuh48d60c12017-02-04 21:58:58 -080049
Ravago Jones5127ccc2022-07-31 16:32:45 -070050 # Motor inertia in kg m^2
51 self.motor_inertia = 0.00001187
Austin Schuh48d60c12017-02-04 21:58:58 -080052
Ravago Jones5127ccc2022-07-31 16:32:45 -070053 # The output coordinate system is in radians for the inner part of the
54 # indexer.
55 # Compute the effective moment of inertia assuming all the mass is in that
56 # coordinate system.
57 self.J = (
58 self.J_inner * self.G_inner * self.G_inner +
59 self.J_outer * self.G_outer * self.G_outer) / (self.G_inner * self.G_inner) + \
60 self.motor_inertia * ((1.0 / self.G_inner) ** 2.0)
61 glog.debug('Indexer J is %f', self.J)
62 self.G = self.G_inner
Austin Schuh48d60c12017-02-04 21:58:58 -080063
Ravago Jones5127ccc2022-07-31 16:32:45 -070064 # Resistance of the motor, divided by 2 to account for the 2 motors
65 self.resistance = 12.0 / self.stall_current
66 # Motor velocity constant
67 self.Kv = ((self.free_speed * 2.0 * numpy.pi) /
68 (12.0 - self.resistance * self.free_current))
69 # Torque constant
70 self.Kt = self.stall_torque / self.stall_current
71 # Control loop time step
72 self.dt = 0.005
Austin Schuh48d60c12017-02-04 21:58:58 -080073
Ravago Jones5127ccc2022-07-31 16:32:45 -070074 # State feedback matrices
75 # [angular velocity]
76 self.A_continuous = numpy.matrix([[
77 -self.Kt / self.Kv / (self.J * self.G * self.G * self.resistance)
78 ]])
79 self.B_continuous = numpy.matrix(
80 [[self.Kt / (self.J * self.G * self.resistance)]])
81 self.C = numpy.matrix([[1]])
82 self.D = numpy.matrix([[0]])
Austin Schuh48d60c12017-02-04 21:58:58 -080083
Ravago Jones5127ccc2022-07-31 16:32:45 -070084 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
85 self.B_continuous, self.dt)
Austin Schuh48d60c12017-02-04 21:58:58 -080086
Ravago Jones5127ccc2022-07-31 16:32:45 -070087 self.PlaceControllerPoles([.75])
Austin Schuh48d60c12017-02-04 21:58:58 -080088
Ravago Jones5127ccc2022-07-31 16:32:45 -070089 self.PlaceObserverPoles([0.3])
Austin Schuh48d60c12017-02-04 21:58:58 -080090
Ravago Jones5127ccc2022-07-31 16:32:45 -070091 self.U_max = numpy.matrix([[12.0]])
92 self.U_min = numpy.matrix([[-12.0]])
Austin Schuh48d60c12017-02-04 21:58:58 -080093
Ravago Jones5127ccc2022-07-31 16:32:45 -070094 qff_vel = 8.0
95 self.Qff = numpy.matrix([[1.0 / (qff_vel**2.0)]])
96
97 self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
98 self.InitializeState()
Austin Schuh48d60c12017-02-04 21:58:58 -080099
100
101class Indexer(VelocityIndexer):
Austin Schuh48d60c12017-02-04 21:58:58 -0800102
Ravago Jones5127ccc2022-07-31 16:32:45 -0700103 def __init__(self, name='Indexer'):
104 super(Indexer, self).__init__(name)
Austin Schuh48d60c12017-02-04 21:58:58 -0800105
Ravago Jones5127ccc2022-07-31 16:32:45 -0700106 self.A_continuous_unaugmented = self.A_continuous
107 self.B_continuous_unaugmented = self.B_continuous
Austin Schuh48d60c12017-02-04 21:58:58 -0800108
Ravago Jones5127ccc2022-07-31 16:32:45 -0700109 self.A_continuous = numpy.matrix(numpy.zeros((2, 2)))
110 self.A_continuous[1:2, 1:2] = self.A_continuous_unaugmented
111 self.A_continuous[0, 1] = 1
Austin Schuh48d60c12017-02-04 21:58:58 -0800112
Ravago Jones5127ccc2022-07-31 16:32:45 -0700113 self.B_continuous = numpy.matrix(numpy.zeros((2, 1)))
114 self.B_continuous[1:2, 0] = self.B_continuous_unaugmented
Austin Schuh48d60c12017-02-04 21:58:58 -0800115
Ravago Jones5127ccc2022-07-31 16:32:45 -0700116 # State feedback matrices
117 # [position, angular velocity]
118 self.C = numpy.matrix([[1, 0]])
119 self.D = numpy.matrix([[0]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800120
Ravago Jones5127ccc2022-07-31 16:32:45 -0700121 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
122 self.B_continuous, self.dt)
Austin Schuh48d60c12017-02-04 21:58:58 -0800123
Ravago Jones5127ccc2022-07-31 16:32:45 -0700124 self.rpl = .45
125 self.ipl = 0.07
126 self.PlaceObserverPoles(
127 [self.rpl + 1j * self.ipl, self.rpl - 1j * self.ipl])
Austin Schuh48d60c12017-02-04 21:58:58 -0800128
Ravago Jones5127ccc2022-07-31 16:32:45 -0700129 self.K_unaugmented = self.K
130 self.K = numpy.matrix(numpy.zeros((1, 2)))
131 self.K[0, 1:2] = self.K_unaugmented
132 self.Kff_unaugmented = self.Kff
133 self.Kff = numpy.matrix(numpy.zeros((1, 2)))
134 self.Kff[0, 1:2] = self.Kff_unaugmented
135
136 self.InitializeState()
Austin Schuh48d60c12017-02-04 21:58:58 -0800137
138
139class IntegralIndexer(Indexer):
Austin Schuh48d60c12017-02-04 21:58:58 -0800140
Ravago Jones5127ccc2022-07-31 16:32:45 -0700141 def __init__(self, name="IntegralIndexer", voltage_error_noise=None):
142 super(IntegralIndexer, self).__init__(name=name)
Austin Schuh48d60c12017-02-04 21:58:58 -0800143
Ravago Jones5127ccc2022-07-31 16:32:45 -0700144 self.A_continuous_unaugmented = self.A_continuous
145 self.B_continuous_unaugmented = self.B_continuous
Austin Schuh48d60c12017-02-04 21:58:58 -0800146
Ravago Jones5127ccc2022-07-31 16:32:45 -0700147 self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
148 self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented
149 self.A_continuous[0:2, 2] = self.B_continuous_unaugmented
Austin Schuh48d60c12017-02-04 21:58:58 -0800150
Ravago Jones5127ccc2022-07-31 16:32:45 -0700151 self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
152 self.B_continuous[0:2, 0] = self.B_continuous_unaugmented
Austin Schuh48d60c12017-02-04 21:58:58 -0800153
Ravago Jones5127ccc2022-07-31 16:32:45 -0700154 self.C_unaugmented = self.C
155 self.C = numpy.matrix(numpy.zeros((1, 3)))
156 self.C[0:1, 0:2] = self.C_unaugmented
Austin Schuh48d60c12017-02-04 21:58:58 -0800157
Ravago Jones5127ccc2022-07-31 16:32:45 -0700158 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
159 self.B_continuous, self.dt)
Austin Schuhcd3237a2017-02-18 14:19:26 -0800160
Ravago Jones5127ccc2022-07-31 16:32:45 -0700161 q_pos = 0.01
162 q_vel = 2.0
163 q_voltage = 0.6
164 if voltage_error_noise is not None:
165 q_voltage = voltage_error_noise
Austin Schuh48d60c12017-02-04 21:58:58 -0800166
Ravago Jones5127ccc2022-07-31 16:32:45 -0700167 self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0],
168 [0.0, (q_vel**2.0), 0.0],
169 [0.0, 0.0, (q_voltage**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800170
Ravago Jones5127ccc2022-07-31 16:32:45 -0700171 r_pos = 0.001
172 self.R = numpy.matrix([[(r_pos**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800173
Ravago Jones5127ccc2022-07-31 16:32:45 -0700174 self.KalmanGain, self.Q_steady = controls.kalman(A=self.A,
175 B=self.B,
176 C=self.C,
177 Q=self.Q,
178 R=self.R)
179 self.L = self.A * self.KalmanGain
Austin Schuh48d60c12017-02-04 21:58:58 -0800180
Ravago Jones5127ccc2022-07-31 16:32:45 -0700181 self.K_unaugmented = self.K
182 self.K = numpy.matrix(numpy.zeros((1, 3)))
183 self.K[0, 0:2] = self.K_unaugmented
184 self.K[0, 2] = 1
185 self.Kff_unaugmented = self.Kff
186 self.Kff = numpy.matrix(numpy.zeros((1, 3)))
187 self.Kff[0, 0:2] = self.Kff_unaugmented
188
189 self.InitializeState()
Austin Schuh48d60c12017-02-04 21:58:58 -0800190
191
192class ScenarioPlotter(object):
Austin Schuh48d60c12017-02-04 21:58:58 -0800193
Ravago Jones5127ccc2022-07-31 16:32:45 -0700194 def __init__(self):
195 # Various lists for graphing things.
196 self.t = []
197 self.x = []
198 self.v = []
199 self.a = []
200 self.stall_ratio = []
201 self.x_hat = []
202 self.u = []
203 self.offset = []
204
205 def run_test(self,
206 indexer,
207 goal,
208 iterations=200,
209 controller_indexer=None,
210 observer_indexer=None):
211 """Runs the indexer plant with an initial condition and goal.
Austin Schuh48d60c12017-02-04 21:58:58 -0800212
213 Args:
214 indexer: Indexer object to use.
215 goal: goal state.
216 iterations: Number of timesteps to run the model for.
217 controller_indexer: Indexer object to get K from, or None if we should
218 use indexer.
219 observer_indexer: Indexer object to use for the observer, or None if we
220 should use the actual state.
221 """
222
Ravago Jones5127ccc2022-07-31 16:32:45 -0700223 if controller_indexer is None:
224 controller_indexer = indexer
Austin Schuh48d60c12017-02-04 21:58:58 -0800225
Ravago Jones5127ccc2022-07-31 16:32:45 -0700226 vbat = 12.0
Austin Schuh48d60c12017-02-04 21:58:58 -0800227
Ravago Jones5127ccc2022-07-31 16:32:45 -0700228 if self.t:
229 initial_t = self.t[-1] + indexer.dt
Austin Schuhcd3237a2017-02-18 14:19:26 -0800230 else:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700231 initial_t = 0
Austin Schuhcd3237a2017-02-18 14:19:26 -0800232
Ravago Jones5127ccc2022-07-31 16:32:45 -0700233 for i in range(iterations):
234 X_hat = indexer.X
Austin Schuhcd3237a2017-02-18 14:19:26 -0800235
Ravago Jones5127ccc2022-07-31 16:32:45 -0700236 if observer_indexer is not None:
237 X_hat = observer_indexer.X_hat
238 observer_indexer.Y = indexer.Y
239 observer_indexer.CorrectObserver(numpy.matrix([[0.0]]))
240 self.x_hat.append(observer_indexer.X_hat[1, 0])
241 self.offset.append(observer_indexer.X_hat[2, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800242
Ravago Jones5127ccc2022-07-31 16:32:45 -0700243 ff_U = controller_indexer.Kff * (goal - observer_indexer.A * goal)
Austin Schuh48d60c12017-02-04 21:58:58 -0800244
Ravago Jones5127ccc2022-07-31 16:32:45 -0700245 U = controller_indexer.K * (goal - X_hat) + ff_U
246 U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
247 self.x.append(indexer.X[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800248
Ravago Jones5127ccc2022-07-31 16:32:45 -0700249 if self.v:
250 last_v = self.v[-1]
251 else:
252 last_v = 0
Austin Schuh48d60c12017-02-04 21:58:58 -0800253
Ravago Jones5127ccc2022-07-31 16:32:45 -0700254 self.v.append(indexer.X[1, 0])
255 self.a.append((self.v[-1] - last_v) / indexer.dt)
Austin Schuh48d60c12017-02-04 21:58:58 -0800256
Ravago Jones5127ccc2022-07-31 16:32:45 -0700257 applied_U = U.copy()
258 if i >= 40:
259 applied_U -= 2
260
261 if FLAGS.stall and i >= 40:
262 indexer.X[1, 0] = 0.0
263 else:
264 indexer.Update(applied_U)
265
266 if observer_indexer is not None:
267 clipped_u = U[0, 0]
268 clip_u_value = 3.0
269 if clipped_u < 0:
270 clipped_u = min(clipped_u, -clip_u_value)
271 else:
272 clipped_u = max(clipped_u, clip_u_value)
273
274 self.stall_ratio.append(10 * (-self.offset[-1] / clipped_u))
275
276 observer_indexer.PredictObserver(U)
277
278 self.t.append(initial_t + i * indexer.dt)
279 self.u.append(U[0, 0])
280
281 def Plot(self):
282 pylab.subplot(3, 1, 1)
283 pylab.plot(self.t, self.v, label='x')
284 pylab.plot(self.t, self.x_hat, label='x_hat')
285 pylab.legend()
286
287 pylab.subplot(3, 1, 2)
288 pylab.plot(self.t, self.u, label='u')
289 pylab.plot(self.t, self.offset, label='voltage_offset')
290 pylab.plot(self.t, self.stall_ratio, label='stall_ratio')
291 pylab.plot(self.t,
292 [10.0 if x > 6.0 else 0.0 for x in self.stall_ratio],
293 label='is_stalled')
294 pylab.legend()
295
296 pylab.subplot(3, 1, 3)
297 pylab.plot(self.t, self.a, label='a')
298 pylab.legend()
299
300 pylab.show()
Austin Schuh48d60c12017-02-04 21:58:58 -0800301
302
303def main(argv):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700304 scenario_plotter = ScenarioPlotter()
Austin Schuh48d60c12017-02-04 21:58:58 -0800305
Ravago Jones5127ccc2022-07-31 16:32:45 -0700306 indexer = Indexer()
307 indexer_controller = IntegralIndexer()
308 observer_indexer = IntegralIndexer()
Austin Schuh48d60c12017-02-04 21:58:58 -0800309
Ravago Jones5127ccc2022-07-31 16:32:45 -0700310 initial_X = numpy.matrix([[0.0], [0.0]])
311 R = numpy.matrix([[0.0], [20.0], [0.0]])
312 scenario_plotter.run_test(indexer,
313 goal=R,
314 controller_indexer=indexer_controller,
315 observer_indexer=observer_indexer,
316 iterations=200)
Austin Schuh48d60c12017-02-04 21:58:58 -0800317
Ravago Jones5127ccc2022-07-31 16:32:45 -0700318 if FLAGS.plot:
319 scenario_plotter.Plot()
Austin Schuh48d60c12017-02-04 21:58:58 -0800320
Ravago Jones5127ccc2022-07-31 16:32:45 -0700321 scenario_plotter = ScenarioPlotter()
Austin Schuhcd3237a2017-02-18 14:19:26 -0800322
Ravago Jones5127ccc2022-07-31 16:32:45 -0700323 indexer = Indexer()
324 indexer_controller = IntegralIndexer(voltage_error_noise=1.5)
325 observer_indexer = IntegralIndexer(voltage_error_noise=1.5)
Austin Schuhcd3237a2017-02-18 14:19:26 -0800326
Ravago Jones5127ccc2022-07-31 16:32:45 -0700327 initial_X = numpy.matrix([[0.0], [0.0]])
328 R = numpy.matrix([[0.0], [20.0], [0.0]])
329 scenario_plotter.run_test(indexer,
330 goal=R,
331 controller_indexer=indexer_controller,
332 observer_indexer=observer_indexer,
333 iterations=200)
Austin Schuhcd3237a2017-02-18 14:19:26 -0800334
Ravago Jones5127ccc2022-07-31 16:32:45 -0700335 if FLAGS.plot:
336 scenario_plotter.Plot()
Austin Schuhcd3237a2017-02-18 14:19:26 -0800337
Ravago Jones5127ccc2022-07-31 16:32:45 -0700338 if len(argv) != 7:
339 glog.fatal('Expected .h file name and .cc file names')
340 else:
341 namespaces = ['y2017', 'control_loops', 'superstructure', 'indexer']
342 indexer = Indexer('Indexer')
343 loop_writer = control_loop.ControlLoopWriter('Indexer', [indexer],
344 namespaces=namespaces)
345 loop_writer.AddConstant(
346 control_loop.Constant('kFreeSpeed', '%f', indexer.free_speed))
347 loop_writer.AddConstant(
348 control_loop.Constant('kOutputRatio', '%f', indexer.G))
349 loop_writer.Write(argv[1], argv[2])
Austin Schuh48d60c12017-02-04 21:58:58 -0800350
Ravago Jones5127ccc2022-07-31 16:32:45 -0700351 integral_indexer = IntegralIndexer('IntegralIndexer')
352 integral_loop_writer = control_loop.ControlLoopWriter(
353 'IntegralIndexer', [integral_indexer], namespaces=namespaces)
354 integral_loop_writer.Write(argv[3], argv[4])
Austin Schuh48d60c12017-02-04 21:58:58 -0800355
Ravago Jones5127ccc2022-07-31 16:32:45 -0700356 stuck_integral_indexer = IntegralIndexer('StuckIntegralIndexer',
357 voltage_error_noise=1.5)
358 stuck_integral_loop_writer = control_loop.ControlLoopWriter(
359 'StuckIntegralIndexer', [stuck_integral_indexer],
360 namespaces=namespaces)
361 stuck_integral_loop_writer.Write(argv[5], argv[6])
Austin Schuhcd3237a2017-02-18 14:19:26 -0800362
Austin Schuh48d60c12017-02-04 21:58:58 -0800363
364if __name__ == '__main__':
Ravago Jones5127ccc2022-07-31 16:32:45 -0700365 argv = FLAGS(sys.argv)
366 glog.init()
367 sys.exit(main(argv))