blob: 01d9797cf30a128016ecd5a4065c97a1dca5a001 [file] [log] [blame]
Austin Schuh48d60c12017-02-04 21:58:58 -08001#!/usr/bin/python
2
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
14gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
15
16class VelocityIndexer(control_loop.ControlLoop):
17 def __init__(self, name='VelocityIndexer'):
18 super(VelocityIndexer, self).__init__(name)
19 # Stall Torque in N m
20 self.stall_torque = 0.71
21 # Stall Current in Amps
22 self.stall_current = 134
Brian Silverman052e69d2017-02-12 16:19:55 -080023 self.free_speed_rpm = 18730.0
24 # Free Speed in rotations/second.
25 self.free_speed = self.free_speed_rpm / 60.0
Austin Schuh48d60c12017-02-04 21:58:58 -080026 # Free Current in Amps
27 self.free_current = 0.7
28 # Moment of inertia of the indexer halves in kg m^2
29 # This is measured as Iyy in CAD (the moment of inertia around the Y axis).
30 # Inner part of indexer -> Iyy = 59500 lb * mm * mm
31 # Inner spins with 12 / 48 * 18 / 48 * 24 / 36 * 16 / 72
32 # Outer part of indexer -> Iyy = 210000 lb * mm * mm
33 # 1 775 pro -> 12 / 48 * 18 / 48 * 30 / 422
34
35 self.J_inner = 0.0269
36 self.J_outer = 0.0952
37 # Gear ratios for the inner and outer parts.
Ed Jordan8683f432017-02-12 00:13:26 +000038 self.G_inner = (12.0 / 48.0) * (18.0 / 36.0) * (12.0 / 84.0)
39 self.G_outer = (12.0 / 48.0) * (18.0 / 36.0) * (24.0 / 420.0)
Austin Schuh48d60c12017-02-04 21:58:58 -080040
41 # Motor inertia in kg * m^2
42 self.motor_inertia = 0.000006
43
44 # The output coordinate system is in radians for the inner part of the
45 # indexer.
46 # Compute the effective moment of inertia assuming all the mass is in that
47 # coordinate system.
48 self.J = (
49 self.J_inner * self.G_inner * self.G_inner +
50 self.J_outer * self.G_outer * self.G_outer) / (self.G_inner * self.G_inner) + \
51 self.motor_inertia * ((1.0 / self.G_inner) ** 2.0)
52 glog.debug('J is %f', self.J)
53 self.G = self.G_inner
54
55 # Resistance of the motor, divided by 2 to account for the 2 motors
56 self.R = 12.0 / self.stall_current
57 # Motor velocity constant
Brian Silverman052e69d2017-02-12 16:19:55 -080058 self.Kv = ((self.free_speed * 2.0 * numpy.pi) /
Austin Schuh48d60c12017-02-04 21:58:58 -080059 (12.0 - self.R * self.free_current))
60 # Torque constant
61 self.Kt = self.stall_torque / self.stall_current
62 # Control loop time step
63 self.dt = 0.005
64
65 # State feedback matrices
66 # [angular velocity]
67 self.A_continuous = numpy.matrix(
68 [[-self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
69 self.B_continuous = numpy.matrix(
70 [[self.Kt / (self.J * self.G * self.R)]])
71 self.C = numpy.matrix([[1]])
72 self.D = numpy.matrix([[0]])
73
74 self.A, self.B = self.ContinuousToDiscrete(
75 self.A_continuous, self.B_continuous, self.dt)
76
77 self.PlaceControllerPoles([.82])
78 glog.debug(repr(self.K))
79
80 self.PlaceObserverPoles([0.3])
81
82 self.U_max = numpy.matrix([[12.0]])
83 self.U_min = numpy.matrix([[-12.0]])
84
85 qff_vel = 8.0
86 self.Qff = numpy.matrix([[1.0 / (qff_vel ** 2.0)]])
87
88 self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
89 self.InitializeState()
90
91
92class Indexer(VelocityIndexer):
93 def __init__(self, name='Indexer'):
94 super(Indexer, self).__init__(name)
95
96 self.A_continuous_unaugmented = self.A_continuous
97 self.B_continuous_unaugmented = self.B_continuous
98
99 self.A_continuous = numpy.matrix(numpy.zeros((2, 2)))
100 self.A_continuous[1:2, 1:2] = self.A_continuous_unaugmented
101 self.A_continuous[0, 1] = 1
102
103 self.B_continuous = numpy.matrix(numpy.zeros((2, 1)))
104 self.B_continuous[1:2, 0] = self.B_continuous_unaugmented
105
106 # State feedback matrices
107 # [position, angular velocity]
108 self.C = numpy.matrix([[1, 0]])
109 self.D = numpy.matrix([[0]])
110
111 self.A, self.B = self.ContinuousToDiscrete(
112 self.A_continuous, self.B_continuous, self.dt)
113
114 self.rpl = .45
115 self.ipl = 0.07
116 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
117 self.rpl - 1j * self.ipl])
118
119 self.K_unaugmented = self.K
120 self.K = numpy.matrix(numpy.zeros((1, 2)))
121 self.K[0, 1:2] = self.K_unaugmented
122 self.Kff_unaugmented = self.Kff
123 self.Kff = numpy.matrix(numpy.zeros((1, 2)))
124 self.Kff[0, 1:2] = self.Kff_unaugmented
125
126 self.InitializeState()
127
128
129class IntegralIndexer(Indexer):
130 def __init__(self, name="IntegralIndexer"):
131 super(IntegralIndexer, self).__init__(name=name)
132
133 self.A_continuous_unaugmented = self.A_continuous
134 self.B_continuous_unaugmented = self.B_continuous
135
136 self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
137 self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented
138 self.A_continuous[0:2, 2] = self.B_continuous_unaugmented
139
140 self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
141 self.B_continuous[0:2, 0] = self.B_continuous_unaugmented
142
143 self.C_unaugmented = self.C
144 self.C = numpy.matrix(numpy.zeros((1, 3)))
145 self.C[0:1, 0:2] = self.C_unaugmented
146
147 self.A, self.B = self.ContinuousToDiscrete(
148 self.A_continuous, self.B_continuous, self.dt)
149
150 q_pos = 2.0
151 q_vel = 0.001
152 q_voltage = 10.0
153 self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0],
154 [0.0, (q_vel ** 2.0), 0.0],
155 [0.0, 0.0, (q_voltage ** 2.0)]])
156
157 r_pos = 0.001
158 self.R = numpy.matrix([[(r_pos ** 2.0)]])
159
160 self.KalmanGain, self.Q_steady = controls.kalman(
161 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
162 self.L = self.A * self.KalmanGain
163
164 self.K_unaugmented = self.K
165 self.K = numpy.matrix(numpy.zeros((1, 3)))
166 self.K[0, 0:2] = self.K_unaugmented
167 self.K[0, 2] = 1
168 self.Kff_unaugmented = self.Kff
169 self.Kff = numpy.matrix(numpy.zeros((1, 3)))
170 self.Kff[0, 0:2] = self.Kff_unaugmented
171
172 self.InitializeState()
173
174
175class ScenarioPlotter(object):
176 def __init__(self):
177 # Various lists for graphing things.
178 self.t = []
179 self.x = []
180 self.v = []
181 self.a = []
182 self.x_hat = []
183 self.u = []
184 self.offset = []
185
186 def run_test(self, indexer, goal, iterations=200, controller_indexer=None,
187 observer_indexer=None):
188 """Runs the indexer plant with an initial condition and goal.
189
190 Args:
191 indexer: Indexer object to use.
192 goal: goal state.
193 iterations: Number of timesteps to run the model for.
194 controller_indexer: Indexer object to get K from, or None if we should
195 use indexer.
196 observer_indexer: Indexer object to use for the observer, or None if we
197 should use the actual state.
198 """
199
200 if controller_indexer is None:
201 controller_indexer = indexer
202
203 vbat = 12.0
204
205 if self.t:
206 initial_t = self.t[-1] + indexer.dt
207 else:
208 initial_t = 0
209
210 for i in xrange(iterations):
211 X_hat = indexer.X
212
213 if observer_indexer is not None:
214 X_hat = observer_indexer.X_hat
215 self.x_hat.append(observer_indexer.X_hat[1, 0])
216
217 ff_U = controller_indexer.Kff * (goal - observer_indexer.A * goal)
218
219 U = controller_indexer.K * (goal - X_hat) + ff_U
220 U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
221 self.x.append(indexer.X[0, 0])
222
223
224 if self.v:
225 last_v = self.v[-1]
226 else:
227 last_v = 0
228
229 self.v.append(indexer.X[1, 0])
230 self.a.append((self.v[-1] - last_v) / indexer.dt)
231
232 if observer_indexer is not None:
233 observer_indexer.Y = indexer.Y
234 observer_indexer.CorrectObserver(U)
235 self.offset.append(observer_indexer.X_hat[2, 0])
236
237 applied_U = U.copy()
238 if i > 30:
239 applied_U += 2
240 indexer.Update(applied_U)
241
242 if observer_indexer is not None:
243 observer_indexer.PredictObserver(U)
244
245 self.t.append(initial_t + i * indexer.dt)
246 self.u.append(U[0, 0])
247
248 def Plot(self):
249 pylab.subplot(3, 1, 1)
250 pylab.plot(self.t, self.v, label='x')
251 pylab.plot(self.t, self.x_hat, label='x_hat')
252 pylab.legend()
253
254 pylab.subplot(3, 1, 2)
255 pylab.plot(self.t, self.u, label='u')
256 pylab.plot(self.t, self.offset, label='voltage_offset')
257 pylab.legend()
258
259 pylab.subplot(3, 1, 3)
260 pylab.plot(self.t, self.a, label='a')
261 pylab.legend()
262
263 pylab.show()
264
265
266def main(argv):
267 scenario_plotter = ScenarioPlotter()
268
269 indexer = Indexer()
270 indexer_controller = IntegralIndexer()
271 observer_indexer = IntegralIndexer()
272
273 initial_X = numpy.matrix([[0.0], [0.0]])
274 R = numpy.matrix([[0.0], [20.0], [0.0]])
275 scenario_plotter.run_test(indexer, goal=R, controller_indexer=indexer_controller,
276 observer_indexer=observer_indexer, iterations=200)
277
278 if FLAGS.plot:
279 scenario_plotter.Plot()
280
281 if len(argv) != 5:
282 glog.fatal('Expected .h file name and .cc file name')
283 else:
284 namespaces = ['y2017', 'control_loops', 'superstructure', 'indexer']
285 indexer = Indexer('Indexer')
286 loop_writer = control_loop.ControlLoopWriter('Indexer', [indexer],
287 namespaces=namespaces)
Brian Silverman052e69d2017-02-12 16:19:55 -0800288 loop_writer.AddConstant(control_loop.Constant(
289 'kFreeSpeed', '%f', indexer.free_speed))
290 loop_writer.AddConstant(control_loop.Constant(
291 'kOutputRatio', '%f', indexer.G))
Austin Schuh48d60c12017-02-04 21:58:58 -0800292 loop_writer.Write(argv[1], argv[2])
293
294 integral_indexer = IntegralIndexer('IntegralIndexer')
295 integral_loop_writer = control_loop.ControlLoopWriter(
296 'IntegralIndexer', [integral_indexer], namespaces=namespaces)
297 integral_loop_writer.Write(argv[3], argv[4])
298
299
300if __name__ == '__main__':
301 argv = FLAGS(sys.argv)
302 glog.init()
303 sys.exit(main(argv))