blob: c77d1345b493f69bf6898e6a9d2cb1013e85efe3 [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
Ravago Jones5127ccc2022-07-31 16:32:45 -0700121 self.KalmanGain, self.Q_steady = controls.kalman(A=self.A,
122 B=self.B,
123 C=self.C,
124 Q=self.Q,
125 R=self.R)
Austin Schuh48d60c12017-02-04 21:58:58 -0800126
Tyler Chatow6738c362019-02-16 14:12:30 -0800127 glog.debug('Kal %s', repr(self.KalmanGain))
128 self.L = self.A * self.KalmanGain
129 glog.debug('KalL is %s', repr(self.L))
Austin Schuh48d60c12017-02-04 21:58:58 -0800130
Tyler Chatow6738c362019-02-16 14:12:30 -0800131 # The box formed by U_min and U_max must encompass all possible values,
132 # or else Austin's code gets angry.
133 self.U_max = numpy.matrix([[12.0]])
134 self.U_min = numpy.matrix([[-12.0]])
135
136 self.InitializeState()
137
Austin Schuh48d60c12017-02-04 21:58:58 -0800138
139class IntegralHood(Hood):
Austin Schuh48d60c12017-02-04 21:58:58 -0800140
Tyler Chatow6738c362019-02-16 14:12:30 -0800141 def __init__(self, name='IntegralHood'):
142 super(IntegralHood, self).__init__(name=name)
Austin Schuh48d60c12017-02-04 21:58:58 -0800143
Tyler Chatow6738c362019-02-16 14:12:30 -0800144 self.A_continuous_unaugmented = self.A_continuous
145 self.B_continuous_unaugmented = self.B_continuous
Austin Schuh48d60c12017-02-04 21:58:58 -0800146
Tyler Chatow6738c362019-02-16 14:12:30 -0800147 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
Tyler Chatow6738c362019-02-16 14:12:30 -0800151 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
Tyler Chatow6738c362019-02-16 14:12:30 -0800154 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
Tyler Chatow6738c362019-02-16 14:12:30 -0800158 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
159 self.B_continuous, self.dt)
Austin Schuh48d60c12017-02-04 21:58:58 -0800160
Tyler Chatow6738c362019-02-16 14:12:30 -0800161 q_pos = 0.01
162 q_vel = 4.0
163 q_voltage = 4.0
164 self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0],
165 [0.0, (q_vel**2.0), 0.0],
166 [0.0, 0.0, (q_voltage**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800167
Tyler Chatow6738c362019-02-16 14:12:30 -0800168 r_pos = 0.001
169 self.R = numpy.matrix([[(r_pos**2.0)]])
Austin Schuh48d60c12017-02-04 21:58:58 -0800170
Ravago Jones5127ccc2022-07-31 16:32:45 -0700171 self.KalmanGain, self.Q_steady = controls.kalman(A=self.A,
172 B=self.B,
173 C=self.C,
174 Q=self.Q,
175 R=self.R)
Tyler Chatow6738c362019-02-16 14:12:30 -0800176 self.L = self.A * self.KalmanGain
Austin Schuh48d60c12017-02-04 21:58:58 -0800177
Tyler Chatow6738c362019-02-16 14:12:30 -0800178 self.K_unaugmented = self.K
179 self.K = numpy.matrix(numpy.zeros((1, 3)))
180 self.K[0, 0:2] = self.K_unaugmented
181 self.K[0, 2] = 1
Austin Schuh48d60c12017-02-04 21:58:58 -0800182
Tyler Chatow6738c362019-02-16 14:12:30 -0800183 self.Kff = numpy.concatenate(
184 (self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1)
185
186 self.InitializeState()
187
Austin Schuh48d60c12017-02-04 21:58:58 -0800188
189class ScenarioPlotter(object):
Austin Schuh48d60c12017-02-04 21:58:58 -0800190
Tyler Chatow6738c362019-02-16 14:12:30 -0800191 def __init__(self):
192 # Various lists for graphing things.
193 self.t = []
194 self.x = []
195 self.v = []
196 self.v_hat = []
197 self.a = []
198 self.x_hat = []
199 self.u = []
200 self.offset = []
Austin Schuh48d60c12017-02-04 21:58:58 -0800201
Tyler Chatow6738c362019-02-16 14:12:30 -0800202 def run_test(self,
203 hood,
204 end_goal,
205 controller_hood,
206 observer_hood=None,
207 iterations=200):
208 """Runs the hood plant with an initial condition and goal.
Austin Schuh48d60c12017-02-04 21:58:58 -0800209
Tyler Chatow6738c362019-02-16 14:12:30 -0800210 Args:
211 hood: hood object to use.
212 end_goal: end_goal state.
213 controller_hood: Hood object to get K from, or None if we should
214 use hood.
215 observer_hood: Hood object to use for the observer, or None if we should
216 use the actual state.
217 iterations: Number of timesteps to run the model for.
218 """
Austin Schuh48d60c12017-02-04 21:58:58 -0800219
Tyler Chatow6738c362019-02-16 14:12:30 -0800220 if controller_hood is None:
221 controller_hood = hood
Austin Schuh48d60c12017-02-04 21:58:58 -0800222
Tyler Chatow6738c362019-02-16 14:12:30 -0800223 vbat = 12.0
Austin Schuh48d60c12017-02-04 21:58:58 -0800224
Tyler Chatow6738c362019-02-16 14:12:30 -0800225 if self.t:
226 initial_t = self.t[-1] + hood.dt
227 else:
228 initial_t = 0
Austin Schuh48d60c12017-02-04 21:58:58 -0800229
Tyler Chatow6738c362019-02-16 14:12:30 -0800230 goal = numpy.concatenate((hood.X, numpy.matrix(numpy.zeros((1, 1)))),
231 axis=0)
Austin Schuh48d60c12017-02-04 21:58:58 -0800232
Tyler Chatow6738c362019-02-16 14:12:30 -0800233 profile = TrapezoidProfile(hood.dt)
234 profile.set_maximum_acceleration(10.0)
235 profile.set_maximum_velocity(1.0)
236 profile.SetGoal(goal[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800237
Tyler Chatow6738c362019-02-16 14:12:30 -0800238 U_last = numpy.matrix(numpy.zeros((1, 1)))
Austin Schuh5ea48472021-02-02 20:46:41 -0800239 for i in range(iterations):
Tyler Chatow6738c362019-02-16 14:12:30 -0800240 observer_hood.Y = hood.Y
241 observer_hood.CorrectObserver(U_last)
Austin Schuh48d60c12017-02-04 21:58:58 -0800242
Tyler Chatow6738c362019-02-16 14:12:30 -0800243 self.offset.append(observer_hood.X_hat[2, 0])
244 self.x_hat.append(observer_hood.X_hat[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800245
Tyler Chatow6738c362019-02-16 14:12:30 -0800246 next_goal = numpy.concatenate(
247 (profile.Update(end_goal[0, 0], end_goal[1, 0]),
248 numpy.matrix(numpy.zeros((1, 1)))),
249 axis=0)
Austin Schuh48d60c12017-02-04 21:58:58 -0800250
Tyler Chatow6738c362019-02-16 14:12:30 -0800251 ff_U = controller_hood.Kff * (next_goal - observer_hood.A * goal)
Austin Schuh48d60c12017-02-04 21:58:58 -0800252
Ravago Jones5127ccc2022-07-31 16:32:45 -0700253 U_uncapped = controller_hood.K * (goal -
254 observer_hood.X_hat) + ff_U
Tyler Chatow6738c362019-02-16 14:12:30 -0800255 U = U_uncapped.copy()
256 U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
257 self.x.append(hood.X[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800258
Tyler Chatow6738c362019-02-16 14:12:30 -0800259 if self.v:
260 last_v = self.v[-1]
261 else:
262 last_v = 0
Austin Schuh48d60c12017-02-04 21:58:58 -0800263
Tyler Chatow6738c362019-02-16 14:12:30 -0800264 self.v.append(hood.X[1, 0])
265 self.a.append((self.v[-1] - last_v) / hood.dt)
266 self.v_hat.append(observer_hood.X_hat[1, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800267
Tyler Chatow6738c362019-02-16 14:12:30 -0800268 offset = 0.0
269 if i > 100:
270 offset = 2.0
271 hood.Update(U + offset)
Austin Schuh48d60c12017-02-04 21:58:58 -0800272
Tyler Chatow6738c362019-02-16 14:12:30 -0800273 observer_hood.PredictObserver(U)
Austin Schuh48d60c12017-02-04 21:58:58 -0800274
Tyler Chatow6738c362019-02-16 14:12:30 -0800275 self.t.append(initial_t + i * hood.dt)
276 self.u.append(U[0, 0])
Austin Schuh48d60c12017-02-04 21:58:58 -0800277
Tyler Chatow6738c362019-02-16 14:12:30 -0800278 ff_U -= U_uncapped - U
279 goal = controller_hood.A * goal + controller_hood.B * ff_U
Austin Schuh48d60c12017-02-04 21:58:58 -0800280
Tyler Chatow6738c362019-02-16 14:12:30 -0800281 if U[0, 0] != U_uncapped[0, 0]:
282 profile.MoveCurrentState(
283 numpy.matrix([[goal[0, 0]], [goal[1, 0]]]))
Austin Schuh48d60c12017-02-04 21:58:58 -0800284
Tyler Chatow6738c362019-02-16 14:12:30 -0800285 glog.debug('Time: %f', self.t[-1])
286 glog.debug('goal_error %s', repr(end_goal - goal))
287 glog.debug('error %s', repr(observer_hood.X_hat - end_goal))
Austin Schuh48d60c12017-02-04 21:58:58 -0800288
Tyler Chatow6738c362019-02-16 14:12:30 -0800289 def Plot(self):
290 pylab.subplot(3, 1, 1)
291 pylab.plot(self.t, self.x, label='x')
292 pylab.plot(self.t, self.x_hat, label='x_hat')
293 pylab.plot(self.t, self.v, label='v')
294 pylab.plot(self.t, self.v_hat, label='v_hat')
295 pylab.legend()
Austin Schuh48d60c12017-02-04 21:58:58 -0800296
Tyler Chatow6738c362019-02-16 14:12:30 -0800297 pylab.subplot(3, 1, 2)
298 pylab.plot(self.t, self.u, label='u')
299 pylab.plot(self.t, self.offset, label='voltage_offset')
300 pylab.legend()
Austin Schuh48d60c12017-02-04 21:58:58 -0800301
Tyler Chatow6738c362019-02-16 14:12:30 -0800302 pylab.subplot(3, 1, 3)
303 pylab.plot(self.t, self.a, label='a')
304 pylab.legend()
305
306 pylab.show()
Austin Schuh48d60c12017-02-04 21:58:58 -0800307
308
309def main(argv):
310
Tyler Chatow6738c362019-02-16 14:12:30 -0800311 scenario_plotter = ScenarioPlotter()
Austin Schuh48d60c12017-02-04 21:58:58 -0800312
Tyler Chatow6738c362019-02-16 14:12:30 -0800313 hood = Hood()
314 hood_controller = IntegralHood()
315 observer_hood = IntegralHood()
Austin Schuh48d60c12017-02-04 21:58:58 -0800316
Tyler Chatow6738c362019-02-16 14:12:30 -0800317 # Test moving the hood with constant separation.
318 initial_X = numpy.matrix([[0.0], [0.0]])
319 R = numpy.matrix([[numpy.pi / 4.0], [0.0], [0.0]])
Ravago Jones5127ccc2022-07-31 16:32:45 -0700320 scenario_plotter.run_test(hood,
321 end_goal=R,
322 controller_hood=hood_controller,
323 observer_hood=observer_hood,
324 iterations=200)
Austin Schuh48d60c12017-02-04 21:58:58 -0800325
Tyler Chatow6738c362019-02-16 14:12:30 -0800326 if FLAGS.plot:
327 scenario_plotter.Plot()
Austin Schuh48d60c12017-02-04 21:58:58 -0800328
Tyler Chatow6738c362019-02-16 14:12:30 -0800329 # Write the generated constants out to a file.
330 if len(argv) != 5:
331 glog.fatal(
332 'Expected .h file name and .cc file name for the hood and integral hood.'
333 )
334 else:
335 namespaces = ['y2017', 'control_loops', 'superstructure', 'hood']
336 hood = Hood('Hood')
Ravago Jones5127ccc2022-07-31 16:32:45 -0700337 loop_writer = control_loop.ControlLoopWriter('Hood', [hood],
338 namespaces=namespaces)
Tyler Chatow6738c362019-02-16 14:12:30 -0800339 loop_writer.AddConstant(
340 control_loop.Constant('kFreeSpeed', '%f', hood.free_speed))
341 loop_writer.AddConstant(
342 control_loop.Constant('kOutputRatio', '%f', hood.G))
343 loop_writer.Write(argv[1], argv[2])
Austin Schuh48d60c12017-02-04 21:58:58 -0800344
Tyler Chatow6738c362019-02-16 14:12:30 -0800345 integral_hood = IntegralHood('IntegralHood')
346 integral_loop_writer = control_loop.ControlLoopWriter(
347 'IntegralHood', [integral_hood], namespaces=namespaces)
348 integral_loop_writer.AddConstant(
Ravago Jones5127ccc2022-07-31 16:32:45 -0700349 control_loop.Constant('kLastReduction', '%f',
350 integral_hood.last_G))
Tyler Chatow6738c362019-02-16 14:12:30 -0800351 integral_loop_writer.Write(argv[3], argv[4])
Austin Schuh48d60c12017-02-04 21:58:58 -0800352
353
354if __name__ == '__main__':
Tyler Chatow6738c362019-02-16 14:12:30 -0800355 argv = FLAGS(sys.argv)
356 glog.init()
357 sys.exit(main(argv))