blob: c405bb20c03f52ddb9ed7337ba32c6e1c745be18 [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Austin Schuh48d60c12017-02-04 21:58:58 -08002
John Park33858a32018-09-28 23:05:48 -07003from aos.util.trapezoid_profile import TrapezoidProfile
Austin Schuh48d60c12017-02-04 21:58:58 -08004from frc971.control_loops.python import control_loop
5from frc971.control_loops.python import controls
6import numpy
7import sys
Austin Schuh48d60c12017-02-04 21:58:58 -08008from matplotlib import pylab
9import gflags
10import glog
11
12FLAGS = gflags.FLAGS
13
14try:
Tyler Chatow6738c362019-02-16 14:12:30 -080015 gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
Austin Schuh48d60c12017-02-04 21:58:58 -080016except gflags.DuplicateFlagError:
Tyler Chatow6738c362019-02-16 14:12:30 -080017 pass
18
Austin Schuh48d60c12017-02-04 21:58:58 -080019
20class Hood(control_loop.ControlLoop):
Austin Schuh48d60c12017-02-04 21:58:58 -080021
Tyler Chatow6738c362019-02-16 14:12:30 -080022 def __init__(self, name='Hood'):
23 super(Hood, self).__init__(name)
24 # Stall Torque in N m
25 self.stall_torque = 0.43
26 # Stall Current in Amps
27 self.stall_current = 53.0
28 self.free_speed_rpm = 13180.0
29 # Free Speed in rotations/second.
30 self.free_speed = self.free_speed_rpm / 60
31 # Free Current in Amps
32 self.free_current = 1.8
Austin Schuh48d60c12017-02-04 21:58:58 -080033
Tyler Chatow6738c362019-02-16 14:12:30 -080034 # Resistance of the motor
35 self.R = 12.0 / self.stall_current
36 # Motor velocity constant
37 self.Kv = ((self.free_speed * 2.0 * numpy.pi) /
38 (12.0 - self.R * self.free_current))
39 # Torque constant
40 self.Kt = self.stall_torque / self.stall_current
41 # First axle gear ratio off the motor
42 self.G1 = (12.0 / 60.0)
43 # Second axle gear ratio off the motor
44 self.G2 = self.G1 * (14.0 / 36.0)
45 # Third axle gear ratio off the motor
46 self.G3 = self.G2 * (14.0 / 36.0)
47 # The last gear reduction (encoder -> hood angle)
48 self.last_G = (20.0 / 345.0)
49 # Gear ratio
50 self.G = (12.0 / 60.0) * (14.0 / 36.0) * (14.0 / 36.0) * self.last_G
Austin Schuh48d60c12017-02-04 21:58:58 -080051
Tyler Chatow6738c362019-02-16 14:12:30 -080052 # 36 tooth gear inertia in kg * m^2
53 self.big_gear_inertia = 0.5 * 0.039 * ((36.0 / 40.0 * 0.025)**2)
Austin Schuh48d60c12017-02-04 21:58:58 -080054
Tyler Chatow6738c362019-02-16 14:12:30 -080055 # Motor inertia in kg * m^2
56 self.motor_inertia = 0.000006
57 glog.debug(self.big_gear_inertia)
Austin Schuh48d60c12017-02-04 21:58:58 -080058
Tyler Chatow6738c362019-02-16 14:12:30 -080059 # Moment of inertia, measured in CAD.
60 # Extra mass to compensate for friction is added on.
61 self.J = 0.08 + 2.3 + \
62 self.big_gear_inertia * ((self.G1 / self.G) ** 2) + \
63 self.big_gear_inertia * ((self.G2 / self.G) ** 2) + \
64 self.motor_inertia * ((1.0 / self.G) ** 2.0)
65 glog.debug('J effective %f', self.J)
Austin Schuh48d60c12017-02-04 21:58:58 -080066
Tyler Chatow6738c362019-02-16 14:12:30 -080067 # Control loop time step
68 self.dt = 0.005
Austin Schuh48d60c12017-02-04 21:58:58 -080069
Tyler Chatow6738c362019-02-16 14:12:30 -080070 # State is [position, velocity]
71 # Input is [Voltage]
Austin Schuh48d60c12017-02-04 21:58:58 -080072
Tyler Chatow6738c362019-02-16 14:12:30 -080073 C1 = self.Kt / (self.R * self.J * self.Kv * self.G * self.G)
74 C2 = self.Kt / (self.J * self.R * self.G)
Austin Schuh48d60c12017-02-04 21:58:58 -080075
Tyler Chatow6738c362019-02-16 14:12:30 -080076 self.A_continuous = numpy.matrix([[0, 1], [0, -C1]])
Austin Schuh48d60c12017-02-04 21:58:58 -080077
Tyler Chatow6738c362019-02-16 14:12:30 -080078 # Start with the unmodified input
79 self.B_continuous = numpy.matrix([[0], [C2]])
Austin Schuh48d60c12017-02-04 21:58:58 -080080
Tyler Chatow6738c362019-02-16 14:12:30 -080081 self.C = numpy.matrix([[1, 0]])
82 self.D = numpy.matrix([[0]])
Austin Schuh48d60c12017-02-04 21:58:58 -080083
Tyler Chatow6738c362019-02-16 14:12:30 -080084 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
85 self.B_continuous, self.dt)
Austin Schuh48d60c12017-02-04 21:58:58 -080086
Tyler Chatow6738c362019-02-16 14:12:30 -080087 controllability = controls.ctrb(self.A, self.B)
Austin Schuh48d60c12017-02-04 21:58:58 -080088
Tyler Chatow6738c362019-02-16 14:12:30 -080089 glog.debug('Free speed is %f',
90 -self.B_continuous[1, 0] / self.A_continuous[1, 1] * 12.0)
91 glog.debug(repr(self.A_continuous))
Austin Schuh48d60c12017-02-04 21:58:58 -080092
Tyler Chatow6738c362019-02-16 14:12:30 -080093 # Calculate the LQR controller gain
94 q_pos = 0.015
95 q_vel = 0.40
96 self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0],
97 [0.0, (1.0 / (q_vel**2.0))]])
Austin Schuh48d60c12017-02-04 21:58:58 -080098
Tyler Chatow6738c362019-02-16 14:12:30 -080099 self.R = numpy.matrix([[(5.0 / (12.0**2.0))]])
100 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Austin Schuh48d60c12017-02-04 21:58:58 -0800101
Tyler Chatow6738c362019-02-16 14:12:30 -0800102 # Calculate the feed forwards gain.
103 q_pos_ff = 0.005
104 q_vel_ff = 1.0
105 self.Qff = numpy.matrix([[(1.0 / (q_pos_ff**2.0)), 0.0],
106 [0.0, (1.0 / (q_vel_ff**2.0))]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800107
Tyler Chatow6738c362019-02-16 14:12:30 -0800108 self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
Austin Schuh48d60c12017-02-04 21:58:58 -0800109
Tyler Chatow6738c362019-02-16 14:12:30 -0800110 glog.debug('K %s', repr(self.K))
111 glog.debug('Poles are %s',
112 repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
Austin Schuh48d60c12017-02-04 21:58:58 -0800113
Tyler Chatow6738c362019-02-16 14:12:30 -0800114 q_pos = 0.10
115 q_vel = 1.65
116 self.Q = numpy.matrix([[(q_pos**2.0), 0.0], [0.0, (q_vel**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800117
Tyler Chatow6738c362019-02-16 14:12:30 -0800118 r_volts = 0.025
119 self.R = numpy.matrix([[(r_volts**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800120
Tyler Chatow6738c362019-02-16 14:12:30 -0800121 self.KalmanGain, self.Q_steady = controls.kalman(
122 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
Austin Schuh48d60c12017-02-04 21:58:58 -0800123
Tyler Chatow6738c362019-02-16 14:12:30 -0800124 glog.debug('Kal %s', repr(self.KalmanGain))
125 self.L = self.A * self.KalmanGain
126 glog.debug('KalL is %s', repr(self.L))
Austin Schuh48d60c12017-02-04 21:58:58 -0800127
Tyler Chatow6738c362019-02-16 14:12:30 -0800128 # The box formed by U_min and U_max must encompass all possible values,
129 # or else Austin's code gets angry.
130 self.U_max = numpy.matrix([[12.0]])
131 self.U_min = numpy.matrix([[-12.0]])
132
133 self.InitializeState()
134
Austin Schuh48d60c12017-02-04 21:58:58 -0800135
136class IntegralHood(Hood):
Austin Schuh48d60c12017-02-04 21:58:58 -0800137
Tyler Chatow6738c362019-02-16 14:12:30 -0800138 def __init__(self, name='IntegralHood'):
139 super(IntegralHood, self).__init__(name=name)
Austin Schuh48d60c12017-02-04 21:58:58 -0800140
Tyler Chatow6738c362019-02-16 14:12:30 -0800141 self.A_continuous_unaugmented = self.A_continuous
142 self.B_continuous_unaugmented = self.B_continuous
Austin Schuh48d60c12017-02-04 21:58:58 -0800143
Tyler Chatow6738c362019-02-16 14:12:30 -0800144 self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
145 self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented
146 self.A_continuous[0:2, 2] = self.B_continuous_unaugmented
Austin Schuh48d60c12017-02-04 21:58:58 -0800147
Tyler Chatow6738c362019-02-16 14:12:30 -0800148 self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
149 self.B_continuous[0:2, 0] = self.B_continuous_unaugmented
Austin Schuh48d60c12017-02-04 21:58:58 -0800150
Tyler Chatow6738c362019-02-16 14:12:30 -0800151 self.C_unaugmented = self.C
152 self.C = numpy.matrix(numpy.zeros((1, 3)))
153 self.C[0:1, 0:2] = self.C_unaugmented
Austin Schuh48d60c12017-02-04 21:58:58 -0800154
Tyler Chatow6738c362019-02-16 14:12:30 -0800155 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
156 self.B_continuous, self.dt)
Austin Schuh48d60c12017-02-04 21:58:58 -0800157
Tyler Chatow6738c362019-02-16 14:12:30 -0800158 q_pos = 0.01
159 q_vel = 4.0
160 q_voltage = 4.0
161 self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0],
162 [0.0, (q_vel**2.0), 0.0],
163 [0.0, 0.0, (q_voltage**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800164
Tyler Chatow6738c362019-02-16 14:12:30 -0800165 r_pos = 0.001
166 self.R = numpy.matrix([[(r_pos**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800167
Tyler Chatow6738c362019-02-16 14:12:30 -0800168 self.KalmanGain, self.Q_steady = controls.kalman(
169 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
170 self.L = self.A * self.KalmanGain
Austin Schuh48d60c12017-02-04 21:58:58 -0800171
Tyler Chatow6738c362019-02-16 14:12:30 -0800172 self.K_unaugmented = self.K
173 self.K = numpy.matrix(numpy.zeros((1, 3)))
174 self.K[0, 0:2] = self.K_unaugmented
175 self.K[0, 2] = 1
Austin Schuh48d60c12017-02-04 21:58:58 -0800176
Tyler Chatow6738c362019-02-16 14:12:30 -0800177 self.Kff = numpy.concatenate(
178 (self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1)
179
180 self.InitializeState()
181
Austin Schuh48d60c12017-02-04 21:58:58 -0800182
183class ScenarioPlotter(object):
Austin Schuh48d60c12017-02-04 21:58:58 -0800184
Tyler Chatow6738c362019-02-16 14:12:30 -0800185 def __init__(self):
186 # Various lists for graphing things.
187 self.t = []
188 self.x = []
189 self.v = []
190 self.v_hat = []
191 self.a = []
192 self.x_hat = []
193 self.u = []
194 self.offset = []
Austin Schuh48d60c12017-02-04 21:58:58 -0800195
Tyler Chatow6738c362019-02-16 14:12:30 -0800196 def run_test(self,
197 hood,
198 end_goal,
199 controller_hood,
200 observer_hood=None,
201 iterations=200):
202 """Runs the hood plant with an initial condition and goal.
Austin Schuh48d60c12017-02-04 21:58:58 -0800203
Tyler Chatow6738c362019-02-16 14:12:30 -0800204 Args:
205 hood: hood object to use.
206 end_goal: end_goal state.
207 controller_hood: Hood object to get K from, or None if we should
208 use hood.
209 observer_hood: Hood object to use for the observer, or None if we should
210 use the actual state.
211 iterations: Number of timesteps to run the model for.
212 """
Austin Schuh48d60c12017-02-04 21:58:58 -0800213
Tyler Chatow6738c362019-02-16 14:12:30 -0800214 if controller_hood is None:
215 controller_hood = hood
Austin Schuh48d60c12017-02-04 21:58:58 -0800216
Tyler Chatow6738c362019-02-16 14:12:30 -0800217 vbat = 12.0
Austin Schuh48d60c12017-02-04 21:58:58 -0800218
Tyler Chatow6738c362019-02-16 14:12:30 -0800219 if self.t:
220 initial_t = self.t[-1] + hood.dt
221 else:
222 initial_t = 0
Austin Schuh48d60c12017-02-04 21:58:58 -0800223
Tyler Chatow6738c362019-02-16 14:12:30 -0800224 goal = numpy.concatenate((hood.X, numpy.matrix(numpy.zeros((1, 1)))),
225 axis=0)
Austin Schuh48d60c12017-02-04 21:58:58 -0800226
Tyler Chatow6738c362019-02-16 14:12:30 -0800227 profile = TrapezoidProfile(hood.dt)
228 profile.set_maximum_acceleration(10.0)
229 profile.set_maximum_velocity(1.0)
230 profile.SetGoal(goal[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800231
Tyler Chatow6738c362019-02-16 14:12:30 -0800232 U_last = numpy.matrix(numpy.zeros((1, 1)))
Austin Schuh5ea48472021-02-02 20:46:41 -0800233 for i in range(iterations):
Tyler Chatow6738c362019-02-16 14:12:30 -0800234 observer_hood.Y = hood.Y
235 observer_hood.CorrectObserver(U_last)
Austin Schuh48d60c12017-02-04 21:58:58 -0800236
Tyler Chatow6738c362019-02-16 14:12:30 -0800237 self.offset.append(observer_hood.X_hat[2, 0])
238 self.x_hat.append(observer_hood.X_hat[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800239
Tyler Chatow6738c362019-02-16 14:12:30 -0800240 next_goal = numpy.concatenate(
241 (profile.Update(end_goal[0, 0], end_goal[1, 0]),
242 numpy.matrix(numpy.zeros((1, 1)))),
243 axis=0)
Austin Schuh48d60c12017-02-04 21:58:58 -0800244
Tyler Chatow6738c362019-02-16 14:12:30 -0800245 ff_U = controller_hood.Kff * (next_goal - observer_hood.A * goal)
Austin Schuh48d60c12017-02-04 21:58:58 -0800246
Tyler Chatow6738c362019-02-16 14:12:30 -0800247 U_uncapped = controller_hood.K * (goal - observer_hood.X_hat) + ff_U
248 U = U_uncapped.copy()
249 U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
250 self.x.append(hood.X[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800251
Tyler Chatow6738c362019-02-16 14:12:30 -0800252 if self.v:
253 last_v = self.v[-1]
254 else:
255 last_v = 0
Austin Schuh48d60c12017-02-04 21:58:58 -0800256
Tyler Chatow6738c362019-02-16 14:12:30 -0800257 self.v.append(hood.X[1, 0])
258 self.a.append((self.v[-1] - last_v) / hood.dt)
259 self.v_hat.append(observer_hood.X_hat[1, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800260
Tyler Chatow6738c362019-02-16 14:12:30 -0800261 offset = 0.0
262 if i > 100:
263 offset = 2.0
264 hood.Update(U + offset)
Austin Schuh48d60c12017-02-04 21:58:58 -0800265
Tyler Chatow6738c362019-02-16 14:12:30 -0800266 observer_hood.PredictObserver(U)
Austin Schuh48d60c12017-02-04 21:58:58 -0800267
Tyler Chatow6738c362019-02-16 14:12:30 -0800268 self.t.append(initial_t + i * hood.dt)
269 self.u.append(U[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800270
Tyler Chatow6738c362019-02-16 14:12:30 -0800271 ff_U -= U_uncapped - U
272 goal = controller_hood.A * goal + controller_hood.B * ff_U
Austin Schuh48d60c12017-02-04 21:58:58 -0800273
Tyler Chatow6738c362019-02-16 14:12:30 -0800274 if U[0, 0] != U_uncapped[0, 0]:
275 profile.MoveCurrentState(
276 numpy.matrix([[goal[0, 0]], [goal[1, 0]]]))
Austin Schuh48d60c12017-02-04 21:58:58 -0800277
Tyler Chatow6738c362019-02-16 14:12:30 -0800278 glog.debug('Time: %f', self.t[-1])
279 glog.debug('goal_error %s', repr(end_goal - goal))
280 glog.debug('error %s', repr(observer_hood.X_hat - end_goal))
Austin Schuh48d60c12017-02-04 21:58:58 -0800281
Tyler Chatow6738c362019-02-16 14:12:30 -0800282 def Plot(self):
283 pylab.subplot(3, 1, 1)
284 pylab.plot(self.t, self.x, label='x')
285 pylab.plot(self.t, self.x_hat, label='x_hat')
286 pylab.plot(self.t, self.v, label='v')
287 pylab.plot(self.t, self.v_hat, label='v_hat')
288 pylab.legend()
Austin Schuh48d60c12017-02-04 21:58:58 -0800289
Tyler Chatow6738c362019-02-16 14:12:30 -0800290 pylab.subplot(3, 1, 2)
291 pylab.plot(self.t, self.u, label='u')
292 pylab.plot(self.t, self.offset, label='voltage_offset')
293 pylab.legend()
Austin Schuh48d60c12017-02-04 21:58:58 -0800294
Tyler Chatow6738c362019-02-16 14:12:30 -0800295 pylab.subplot(3, 1, 3)
296 pylab.plot(self.t, self.a, label='a')
297 pylab.legend()
298
299 pylab.show()
Austin Schuh48d60c12017-02-04 21:58:58 -0800300
301
302def main(argv):
303
Tyler Chatow6738c362019-02-16 14:12:30 -0800304 scenario_plotter = ScenarioPlotter()
Austin Schuh48d60c12017-02-04 21:58:58 -0800305
Tyler Chatow6738c362019-02-16 14:12:30 -0800306 hood = Hood()
307 hood_controller = IntegralHood()
308 observer_hood = IntegralHood()
Austin Schuh48d60c12017-02-04 21:58:58 -0800309
Tyler Chatow6738c362019-02-16 14:12:30 -0800310 # Test moving the hood with constant separation.
311 initial_X = numpy.matrix([[0.0], [0.0]])
312 R = numpy.matrix([[numpy.pi / 4.0], [0.0], [0.0]])
313 scenario_plotter.run_test(
314 hood,
315 end_goal=R,
316 controller_hood=hood_controller,
317 observer_hood=observer_hood,
318 iterations=200)
Austin Schuh48d60c12017-02-04 21:58:58 -0800319
Tyler Chatow6738c362019-02-16 14:12:30 -0800320 if FLAGS.plot:
321 scenario_plotter.Plot()
Austin Schuh48d60c12017-02-04 21:58:58 -0800322
Tyler Chatow6738c362019-02-16 14:12:30 -0800323 # Write the generated constants out to a file.
324 if len(argv) != 5:
325 glog.fatal(
326 'Expected .h file name and .cc file name for the hood and integral hood.'
327 )
328 else:
329 namespaces = ['y2017', 'control_loops', 'superstructure', 'hood']
330 hood = Hood('Hood')
331 loop_writer = control_loop.ControlLoopWriter(
332 'Hood', [hood], namespaces=namespaces)
333 loop_writer.AddConstant(
334 control_loop.Constant('kFreeSpeed', '%f', hood.free_speed))
335 loop_writer.AddConstant(
336 control_loop.Constant('kOutputRatio', '%f', hood.G))
337 loop_writer.Write(argv[1], argv[2])
Austin Schuh48d60c12017-02-04 21:58:58 -0800338
Tyler Chatow6738c362019-02-16 14:12:30 -0800339 integral_hood = IntegralHood('IntegralHood')
340 integral_loop_writer = control_loop.ControlLoopWriter(
341 'IntegralHood', [integral_hood], namespaces=namespaces)
342 integral_loop_writer.AddConstant(
343 control_loop.Constant('kLastReduction', '%f', integral_hood.last_G))
344 integral_loop_writer.Write(argv[3], argv[4])
Austin Schuh48d60c12017-02-04 21:58:58 -0800345
346
347if __name__ == '__main__':
Tyler Chatow6738c362019-02-16 14:12:30 -0800348 argv = FLAGS(sys.argv)
349 glog.init()
350 sys.exit(main(argv))