blob: 02c1ada9c3a30cbd20263164af7730e1b9e2cd34 [file] [log] [blame]
Comran Morshed2ae094e2016-01-23 20:43:20 +00001#!/usr/bin/python
2
Philipp Schrader1a25ee42016-02-11 07:02:03 +00003from aos.common.util.trapezoid_profile import TrapezoidProfile
Comran Morshed2ae094e2016-01-23 20:43:20 +00004from frc971.control_loops.python import control_loop
5from frc971.control_loops.python import controls
Comran Morshed2ae094e2016-01-23 20:43:20 +00006import numpy
7import sys
Comran Morshed2ae094e2016-01-23 20:43:20 +00008from matplotlib import pylab
9import gflags
10import glog
11
12FLAGS = gflags.FLAGS
13
14try:
15 gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
16except gflags.DuplicateFlagError:
17 pass
18
19class Intake(control_loop.ControlLoop):
Austin Schuh07cb5852016-01-31 00:58:46 -080020 def __init__(self, name="Intake"):
Comran Morshed2ae094e2016-01-23 20:43:20 +000021 super(Intake, self).__init__(name)
22 # TODO(constants): Update all of these & retune poles.
23 # Stall Torque in N m
Austin Schuh07cb5852016-01-31 00:58:46 -080024 self.stall_torque = 0.71
Comran Morshed2ae094e2016-01-23 20:43:20 +000025 # Stall Current in Amps
Austin Schuh07cb5852016-01-31 00:58:46 -080026 self.stall_current = 134
Comran Morshed2ae094e2016-01-23 20:43:20 +000027 # Free Speed in RPM
Austin Schuh07cb5852016-01-31 00:58:46 -080028 self.free_speed = 18730
Comran Morshed2ae094e2016-01-23 20:43:20 +000029 # Free Current in Amps
Austin Schuh07cb5852016-01-31 00:58:46 -080030 self.free_current = 0.7
Comran Morshed2ae094e2016-01-23 20:43:20 +000031
32 # Resistance of the motor
33 self.R = 12.0 / self.stall_current
34 # Motor velocity constant
35 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
36 (12.0 - self.R * self.free_current))
37 # Torque constant
38 self.Kt = self.stall_torque / self.stall_current
39 # Gear ratio
Austin Schuh07cb5852016-01-31 00:58:46 -080040 self.G = (56.0 / 12.0) * (54.0 / 14.0) * (64.0 / 18.0) * (48.0 / 16.0)
Comran Morshed2ae094e2016-01-23 20:43:20 +000041
Comran Morshedb6a22362016-03-05 14:14:32 +000042 # Moment of inertia, measured in CAD.
43 # Extra mass to compensate for friction is added on.
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070044 self.J = 0.34 + 0.40
Comran Morshed2ae094e2016-01-23 20:43:20 +000045
46 # Control loop time step
47 self.dt = 0.005
48
49 # State is [position, velocity]
50 # Input is [Voltage]
51
52 C1 = self.G * self.G * self.Kt / (self.R * self.J * self.Kv)
53 C2 = self.Kt * self.G / (self.J * self.R)
54
55 self.A_continuous = numpy.matrix(
56 [[0, 1],
57 [0, -C1]])
58
59 # Start with the unmodified input
60 self.B_continuous = numpy.matrix(
61 [[0],
62 [C2]])
63
64 self.C = numpy.matrix([[1, 0]])
65 self.D = numpy.matrix([[0]])
66
67 self.A, self.B = self.ContinuousToDiscrete(
68 self.A_continuous, self.B_continuous, self.dt)
69
70 controllability = controls.ctrb(self.A, self.B)
71
Austin Schuha88c4072016-02-06 14:31:03 -080072 glog.debug("Free speed is %f", self.free_speed * numpy.pi * 2.0 / 60.0 / self.G)
Comran Morshed2ae094e2016-01-23 20:43:20 +000073
Austin Schuh07cb5852016-01-31 00:58:46 -080074 q_pos = 0.20
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070075 q_vel = 5.0
Comran Morshed2ae094e2016-01-23 20:43:20 +000076 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0],
77 [0.0, (1.0 / (q_vel ** 2.0))]])
78
79 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0))]])
80 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
81
Austin Schuh2fc10fa2016-02-08 00:44:34 -080082 q_pos_ff = 0.005
83 q_vel_ff = 1.0
84 self.Qff = numpy.matrix([[(1.0 / (q_pos_ff ** 2.0)), 0.0],
85 [0.0, (1.0 / (q_vel_ff ** 2.0))]])
86
87 self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
88
Austin Schuha88c4072016-02-06 14:31:03 -080089 glog.debug('K %s', repr(self.K))
90 glog.debug('Poles are %s',
91 repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
Comran Morshed2ae094e2016-01-23 20:43:20 +000092
93 self.rpl = 0.30
94 self.ipl = 0.10
95 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
96 self.rpl - 1j * self.ipl])
97
Austin Schuha88c4072016-02-06 14:31:03 -080098 glog.debug('L is %s', repr(self.L))
Comran Morshed2ae094e2016-01-23 20:43:20 +000099
Austin Schuh1aa5ee92016-02-28 21:57:45 -0800100 q_pos = 0.10
101 q_vel = 1.65
Comran Morshed2ae094e2016-01-23 20:43:20 +0000102 self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0],
103 [0.0, (q_vel ** 2.0)]])
104
105 r_volts = 0.025
106 self.R = numpy.matrix([[(r_volts ** 2.0)]])
107
108 self.KalmanGain, self.Q_steady = controls.kalman(
109 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
110
Austin Schuha88c4072016-02-06 14:31:03 -0800111 glog.debug('Kal %s', repr(self.KalmanGain))
Comran Morshed2ae094e2016-01-23 20:43:20 +0000112 self.L = self.A * self.KalmanGain
Austin Schuha88c4072016-02-06 14:31:03 -0800113 glog.debug('KalL is %s', repr(self.L))
Comran Morshed2ae094e2016-01-23 20:43:20 +0000114
115 # The box formed by U_min and U_max must encompass all possible values,
116 # or else Austin's code gets angry.
117 self.U_max = numpy.matrix([[12.0]])
118 self.U_min = numpy.matrix([[-12.0]])
119
120 self.InitializeState()
121
122class IntegralIntake(Intake):
Austin Schuh07cb5852016-01-31 00:58:46 -0800123 def __init__(self, name="IntegralIntake"):
124 super(IntegralIntake, self).__init__(name=name)
Comran Morshed2ae094e2016-01-23 20:43:20 +0000125
126 self.A_continuous_unaugmented = self.A_continuous
127 self.B_continuous_unaugmented = self.B_continuous
128
129 self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
130 self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented
131 self.A_continuous[0:2, 2] = self.B_continuous_unaugmented
132
133 self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
134 self.B_continuous[0:2, 0] = self.B_continuous_unaugmented
135
136 self.C_unaugmented = self.C
137 self.C = numpy.matrix(numpy.zeros((1, 3)))
138 self.C[0:1, 0:2] = self.C_unaugmented
139
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800140 self.A, self.B = self.ContinuousToDiscrete(
141 self.A_continuous, self.B_continuous, self.dt)
Comran Morshed2ae094e2016-01-23 20:43:20 +0000142
Austin Schuh1aa5ee92016-02-28 21:57:45 -0800143 q_pos = 0.12
144 q_vel = 2.00
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800145 q_voltage = 4.0
Comran Morshed2ae094e2016-01-23 20:43:20 +0000146 self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0],
147 [0.0, (q_vel ** 2.0), 0.0],
148 [0.0, 0.0, (q_voltage ** 2.0)]])
149
150 r_pos = 0.05
151 self.R = numpy.matrix([[(r_pos ** 2.0)]])
152
153 self.KalmanGain, self.Q_steady = controls.kalman(
154 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
155 self.L = self.A * self.KalmanGain
156
157 self.K_unaugmented = self.K
158 self.K = numpy.matrix(numpy.zeros((1, 3)))
159 self.K[0, 0:2] = self.K_unaugmented
160 self.K[0, 2] = 1
161
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800162 self.Kff = numpy.concatenate((self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1)
163
Comran Morshed2ae094e2016-01-23 20:43:20 +0000164 self.InitializeState()
Austin Schuh07cb5852016-01-31 00:58:46 -0800165
Comran Morshed2ae094e2016-01-23 20:43:20 +0000166class ScenarioPlotter(object):
167 def __init__(self):
168 # Various lists for graphing things.
169 self.t = []
170 self.x = []
171 self.v = []
172 self.a = []
173 self.x_hat = []
174 self.u = []
Austin Schuh07cb5852016-01-31 00:58:46 -0800175 self.offset = []
Comran Morshed2ae094e2016-01-23 20:43:20 +0000176
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800177 def run_test(self, intake, end_goal,
178 controller_intake,
179 observer_intake=None,
180 iterations=200):
Comran Morshed2ae094e2016-01-23 20:43:20 +0000181 """Runs the intake plant with an initial condition and goal.
182
Comran Morshed2ae094e2016-01-23 20:43:20 +0000183 Args:
184 intake: intake object to use.
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800185 end_goal: end_goal state.
Comran Morshed2ae094e2016-01-23 20:43:20 +0000186 controller_intake: Intake object to get K from, or None if we should
187 use intake.
188 observer_intake: Intake object to use for the observer, or None if we should
189 use the actual state.
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800190 iterations: Number of timesteps to run the model for.
Comran Morshed2ae094e2016-01-23 20:43:20 +0000191 """
192
193 if controller_intake is None:
194 controller_intake = intake
195
196 vbat = 12.0
197
198 if self.t:
199 initial_t = self.t[-1] + intake.dt
200 else:
201 initial_t = 0
202
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800203 goal = numpy.concatenate((intake.X, numpy.matrix(numpy.zeros((1, 1)))), axis=0)
204
Philipp Schrader1a25ee42016-02-11 07:02:03 +0000205 profile = TrapezoidProfile(intake.dt)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800206 profile.set_maximum_acceleration(70.0)
207 profile.set_maximum_velocity(10.0)
208 profile.SetGoal(goal[0, 0])
209
210 U_last = numpy.matrix(numpy.zeros((1, 1)))
Comran Morshed2ae094e2016-01-23 20:43:20 +0000211 for i in xrange(iterations):
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800212 observer_intake.Y = intake.Y
213 observer_intake.CorrectObserver(U_last)
Comran Morshed2ae094e2016-01-23 20:43:20 +0000214
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800215 self.offset.append(observer_intake.X_hat[2, 0])
216 self.x_hat.append(observer_intake.X_hat[0, 0])
Comran Morshed2ae094e2016-01-23 20:43:20 +0000217
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800218 next_goal = numpy.concatenate(
219 (profile.Update(end_goal[0, 0], end_goal[1, 0]),
220 numpy.matrix(numpy.zeros((1, 1)))),
221 axis=0)
222
223 ff_U = controller_intake.Kff * (next_goal - observer_intake.A * goal)
224
225 U_uncapped = controller_intake.K * (goal - observer_intake.X_hat) + ff_U
226 U = U_uncapped.copy()
Comran Morshed2ae094e2016-01-23 20:43:20 +0000227 U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
228 self.x.append(intake.X[0, 0])
229
230 if self.v:
231 last_v = self.v[-1]
232 else:
233 last_v = 0
234
235 self.v.append(intake.X[1, 0])
236 self.a.append((self.v[-1] - last_v) / intake.dt)
237
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800238 offset = 0.0
239 if i > 100:
240 offset = 2.0
241 intake.Update(U + offset)
Comran Morshed2ae094e2016-01-23 20:43:20 +0000242
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800243 observer_intake.PredictObserver(U)
Comran Morshed2ae094e2016-01-23 20:43:20 +0000244
245 self.t.append(initial_t + i * intake.dt)
246 self.u.append(U[0, 0])
247
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800248 ff_U -= U_uncapped - U
249 goal = controller_intake.A * goal + controller_intake.B * ff_U
250
251 if U[0, 0] != U_uncapped[0, 0]:
252 profile.MoveCurrentState(
253 numpy.matrix([[goal[0, 0]], [goal[1, 0]]]))
254
255 glog.debug('Time: %f', self.t[-1])
256 glog.debug('goal_error %s', repr(end_goal - goal))
257 glog.debug('error %s', repr(observer_intake.X_hat - end_goal))
Comran Morshed2ae094e2016-01-23 20:43:20 +0000258
259 def Plot(self):
260 pylab.subplot(3, 1, 1)
261 pylab.plot(self.t, self.x, label='x')
262 pylab.plot(self.t, self.x_hat, label='x_hat')
263 pylab.legend()
264
265 pylab.subplot(3, 1, 2)
266 pylab.plot(self.t, self.u, label='u')
Austin Schuh07cb5852016-01-31 00:58:46 -0800267 pylab.plot(self.t, self.offset, label='voltage_offset')
268 pylab.legend()
Comran Morshed2ae094e2016-01-23 20:43:20 +0000269
270 pylab.subplot(3, 1, 3)
271 pylab.plot(self.t, self.a, label='a')
Comran Morshed2ae094e2016-01-23 20:43:20 +0000272 pylab.legend()
Austin Schuh07cb5852016-01-31 00:58:46 -0800273
Comran Morshed2ae094e2016-01-23 20:43:20 +0000274 pylab.show()
275
276
277def main(argv):
278 argv = FLAGS(argv)
Austin Schuha88c4072016-02-06 14:31:03 -0800279 glog.init()
Comran Morshed2ae094e2016-01-23 20:43:20 +0000280
Comran Morshed2ae094e2016-01-23 20:43:20 +0000281 scenario_plotter = ScenarioPlotter()
282
Austin Schuh07cb5852016-01-31 00:58:46 -0800283 intake = Intake()
284 intake_controller = IntegralIntake()
285 observer_intake = IntegralIntake()
Comran Morshed2ae094e2016-01-23 20:43:20 +0000286
287 # Test moving the intake with constant separation.
288 initial_X = numpy.matrix([[0.0], [0.0]])
Austin Schuh07cb5852016-01-31 00:58:46 -0800289 R = numpy.matrix([[numpy.pi/2.0], [0.0], [0.0]])
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800290 scenario_plotter.run_test(intake, end_goal=R,
291 controller_intake=intake_controller,
Comran Morshed2ae094e2016-01-23 20:43:20 +0000292 observer_intake=observer_intake, iterations=200)
293
294 if FLAGS.plot:
295 scenario_plotter.Plot()
296
297 # Write the generated constants out to a file.
298 if len(argv) != 5:
299 glog.fatal('Expected .h file name and .cc file name for the intake and integral intake.')
300 else:
301 namespaces = ['y2016', 'control_loops', 'superstructure']
302 intake = Intake("Intake")
303 loop_writer = control_loop.ControlLoopWriter('Intake', [intake],
304 namespaces=namespaces)
305 loop_writer.Write(argv[1], argv[2])
306
Austin Schuh07cb5852016-01-31 00:58:46 -0800307 integral_intake = IntegralIntake("IntegralIntake")
Comran Morshed2ae094e2016-01-23 20:43:20 +0000308 integral_loop_writer = control_loop.ControlLoopWriter("IntegralIntake", [integral_intake],
Austin Schuh07cb5852016-01-31 00:58:46 -0800309 namespaces=namespaces)
Comran Morshed2ae094e2016-01-23 20:43:20 +0000310 integral_loop_writer.Write(argv[3], argv[4])
311
312if __name__ == '__main__':
313 sys.exit(main(sys.argv))