blob: 99bbdbacfe4e6454f851689bda7238e547c7acd3 [file] [log] [blame]
Austin Schuhc8ca2442013-02-23 12:29:33 -08001#!/usr/bin/python
2
Austin Schuh3c542312013-02-24 01:53:50 -08003import control_loop
Austin Schuh0d9467a2014-02-15 22:36:45 -08004import controls
Austin Schuhc8ca2442013-02-23 12:29:33 -08005import numpy
Austin Schuhc8ca2442013-02-23 12:29:33 -08006import sys
Austin Schuhc8ca2442013-02-23 12:29:33 -08007from matplotlib import pylab
Austin Schuhc8ca2442013-02-23 12:29:33 -08008
Austin Schuh3bb9a442014-02-02 16:01:45 -08009class Claw(control_loop.ControlLoop):
10 def __init__(self, name="RawClaw"):
11 super(Claw, self).__init__(name)
Austin Schuhc8ca2442013-02-23 12:29:33 -080012 # Stall Torque in N m
James Kuszmaule1755b32014-02-13 06:27:48 -080013 self.stall_torque = 2.42
Austin Schuhc8ca2442013-02-23 12:29:33 -080014 # Stall Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080015 self.stall_current = 133
James Kuszmaul92797402014-02-17 14:08:49 -080016 # Free Speed in RPM
17 self.free_speed = 5500.0
Austin Schuh3c542312013-02-24 01:53:50 -080018 # Free Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080019 self.free_current = 2.7
Austin Schuh3bb9a442014-02-02 16:01:45 -080020 # Moment of inertia of the claw in kg m^2
James Kuszmaul92797402014-02-17 14:08:49 -080021 # measured from CAD
22 self.J_top = 0.3
23 self.J_bottom = 0.9
Austin Schuhc8ca2442013-02-23 12:29:33 -080024 # Resistance of the motor
James Kuszmaul92797402014-02-17 14:08:49 -080025 self.R = 12.0 / self.stall_current
Austin Schuhc8ca2442013-02-23 12:29:33 -080026 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080027 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
28 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080029 # Torque constant
30 self.Kt = self.stall_torque / self.stall_current
31 # Gear ratio
James Kuszmaule1755b32014-02-13 06:27:48 -080032 self.G = 14.0 / 48.0 * 18.0 / 32.0 * 18.0 / 66.0 * 12.0 / 60.0
Austin Schuhc8ca2442013-02-23 12:29:33 -080033 # Control loop time step
34 self.dt = 0.01
35
Austin Schuh0d9467a2014-02-15 22:36:45 -080036 # State is [bottom position, top - bottom position,
37 # bottom velocity, top - bottom velocity]
James Kuszmaul92797402014-02-17 14:08:49 -080038 # Input is [bottom power, top - bottom power]
39 # Motor time constants. difference_bottom refers to the constant for how the
40 # bottom velocity affects the difference of the top and bottom velocities.
41 self.common_motor_constant = -self.Kt / self.Kv / (self.G * self.G * self.R)
42 self.bottom_bottom = self.common_motor_constant / self.J_bottom
43 self.difference_bottom = self.common_motor_constant * (1 / self.J_bottom
44 - 1 / self.J_top)
45 self.difference_difference = self.common_motor_constant / self.J_top
Austin Schuhc8ca2442013-02-23 12:29:33 -080046 # State feedback matrices
47 self.A_continuous = numpy.matrix(
Austin Schuh0d9467a2014-02-15 22:36:45 -080048 [[0, 0, 1, 0],
49 [0, 0, 0, 1],
James Kuszmaul92797402014-02-17 14:08:49 -080050 [0, 0, self.bottom_bottom, 0],
51 [0, 0, self.difference_bottom, self.difference_difference]])
Austin Schuh0d9467a2014-02-15 22:36:45 -080052
James Kuszmaul92797402014-02-17 14:08:49 -080053 self.motor_feedforward = self.Kt / (self.G * self.R)
54 self.motor_feedforward_bottom = self.motor_feedforward / self.J_top
55 self.motor_feedforward_difference = self.motor_feedforward / self.J_bottom
56 self.motor_feedforward_difference_bottom = (
57 self.motor_feedforward * (1 / self.J_bottom - 1 / self.J_top))
Austin Schuhc8ca2442013-02-23 12:29:33 -080058 self.B_continuous = numpy.matrix(
Austin Schuh0d9467a2014-02-15 22:36:45 -080059 [[0, 0],
60 [0, 0],
James Kuszmaul92797402014-02-17 14:08:49 -080061 [self.motor_feedforward_bottom, 0],
62 [self.motor_feedforward_difference_bottom,
63 self.motor_feedforward_difference]])
Austin Schuh0d9467a2014-02-15 22:36:45 -080064 self.C = numpy.matrix([[1, 0, 0, 0],
65 [1, 1, 0, 0]])
66 self.D = numpy.matrix([[0, 0],
67 [0, 0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080068
Austin Schuhc1f68892013-03-16 17:06:27 -070069 self.A, self.B = self.ContinuousToDiscrete(
70 self.A_continuous, self.B_continuous, self.dt)
Austin Schuhc8ca2442013-02-23 12:29:33 -080071
Austin Schuh0d9467a2014-02-15 22:36:45 -080072 #controlability = controls.ctrb(self.A, self.B);
73 #print "Rank of controlability matrix.", numpy.linalg.matrix_rank(controlability)
74
Austin Schuhcda86af2014-02-16 16:16:39 -080075 self.Q = numpy.matrix([[(1.0 / (0.10 ** 2.0)), 0.0, 0.0, 0.0],
76 [0.0, (1.0 / (0.03 ** 2.0)), 0.0, 0.0],
77 [0.0, 0.0, 0.2, 0.0],
78 [0.0, 0.0, 0.0, 2.00]])
79
80 self.R = numpy.matrix([[(1.0 / (20.0 ** 2.0)), 0.0],
81 [0.0, (1.0 / (20.0 ** 2.0))]])
82 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
83
84 print "K unaugmented"
85 print self.K
Austin Schuhc8ca2442013-02-23 12:29:33 -080086
87 self.rpl = .05
88 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080089 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
Austin Schuh0d9467a2014-02-15 22:36:45 -080090 self.rpl - 1j * self.ipl,
91 self.rpl + 1j * self.ipl,
Austin Schuh3c542312013-02-24 01:53:50 -080092 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080093
James Kuszmaul92797402014-02-17 14:08:49 -080094 # The box formed by U_min and U_max must encompass all possible values,
95 # or else Austin's code gets angry.
Austin Schuhcda86af2014-02-16 16:16:39 -080096 self.U_max = numpy.matrix([[12.0], [24.0]])
97 self.U_min = numpy.matrix([[-12.0], [-24.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080098
Austin Schuhc1f68892013-03-16 17:06:27 -070099 self.InitializeState()
100
101
Austin Schuh3bb9a442014-02-02 16:01:45 -0800102class ClawDeltaU(Claw):
103 def __init__(self, name="Claw"):
104 super(ClawDeltaU, self).__init__(name)
Austin Schuhc1f68892013-03-16 17:06:27 -0700105 A_unaugmented = self.A
106 B_unaugmented = self.B
Austin Schuh0d9467a2014-02-15 22:36:45 -0800107 C_unaugmented = self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700108
Austin Schuh0d9467a2014-02-15 22:36:45 -0800109 self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0],
110 [0.0, 0.0, 0.0, 0.0, 0.0],
111 [0.0, 0.0, 0.0, 0.0, 0.0],
112 [0.0, 0.0, 0.0, 0.0, 0.0],
113 [0.0, 0.0, 0.0, 0.0, 1.0]])
114 self.A[0:4, 0:4] = A_unaugmented
115 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Austin Schuhc1f68892013-03-16 17:06:27 -0700116
Austin Schuh0d9467a2014-02-15 22:36:45 -0800117 self.B = numpy.matrix([[0.0, 0.0],
118 [0.0, 0.0],
119 [0.0, 0.0],
120 [0.0, 0.0],
121 [1.0, 0.0]])
122 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Austin Schuhc1f68892013-03-16 17:06:27 -0700123
Austin Schuh0d9467a2014-02-15 22:36:45 -0800124 self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])),
125 axis=1)
126 self.D = numpy.matrix([[0.0, 0.0],
127 [0.0, 0.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700128
Austin Schuh0d9467a2014-02-15 22:36:45 -0800129 #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80])
130 self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0],
131 [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0],
132 [0.0, 0.0, 0.01, 0.0, 0.0],
133 [0.0, 0.0, 0.0, 0.08, 0.0],
134 [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]])
135
136 self.R = numpy.matrix([[0.000001, 0.0],
137 [0.0, 1.0 / (10.0 ** 2.0)]])
138 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
139
140 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
141 [50.0, 0.0, 10.0, 0.0, 1.0]])
142 #self.K = numpy.matrix([[50.0, -100.0, 0, -10, 0],
143 # [50.0, 100.0, 0, 10, 0]])
144
145 controlability = controls.ctrb(self.A, self.B);
146 print "Rank of augmented controlability matrix.", numpy.linalg.matrix_rank(controlability)
Austin Schuhc1f68892013-03-16 17:06:27 -0700147
148 print "K"
149 print self.K
150 print "Placed controller poles are"
151 print numpy.linalg.eig(self.A - self.B * self.K)[0]
Austin Schuh0d9467a2014-02-15 22:36:45 -0800152 print [numpy.abs(x) for x in numpy.linalg.eig(self.A - self.B * self.K)[0]]
Austin Schuhc1f68892013-03-16 17:06:27 -0700153
154 self.rpl = .05
155 self.ipl = 0.008
Austin Schuh0d9467a2014-02-15 22:36:45 -0800156 self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09,
Brian Silverman23a67ca2013-03-16 23:48:50 -0700157 self.rpl - 1j * self.ipl, 0.90])
Austin Schuh0d9467a2014-02-15 22:36:45 -0800158 #print "A is"
159 #print self.A
160 #print "L is"
161 #print self.L
162 #print "C is"
163 #print self.C
164 #print "A - LC is"
165 #print self.A - self.L * self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700166
Austin Schuh0d9467a2014-02-15 22:36:45 -0800167 #print "Placed observer poles are"
168 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
169
170 self.U_max = numpy.matrix([[12.0], [12.0]])
171 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700172
173 self.InitializeState()
174
175
Austin Schuhcda86af2014-02-16 16:16:39 -0800176def FullSeparationPriority(claw, U):
177 bottom_u = U[0, 0]
178 top_u = U[1, 0] + bottom_u
179
180 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
181 if bottom_u > claw.U_max[0, 0]:
182 #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0]
183 top_u -= bottom_u - claw.U_max[0, 0]
184 if top_u < claw.U_min[1, 0]:
185 top_u = claw.U_min[1, 0]
186
187 bottom_u = claw.U_max[0, 0]
188 if top_u > claw.U_max[1, 0]:
189 bottom_u -= top_u - claw.U_max[1, 0]
190 if bottom_u < claw.U_min[0, 0]:
191 bottom_u = claw.U_min[0, 0]
192
193 top_u = claw.U_max[1, 0]
194 if top_u < claw.U_min[1, 0]:
195 bottom_u -= top_u - claw.U_min[1, 0]
196 if bottom_u > claw.U_max[0, 0]:
197 bottom_u = claw.U_max[0, 0]
198
199 top_u = claw.U_min[1, 0]
200 if bottom_u < claw.U_min[0, 0]:
201 top_u -= bottom_u - claw.U_min[0, 0]
202 if top_u > claw.U_max[1, 0]:
203 top_u = claw.U_max[1, 0]
204
205 bottom_u = claw.U_min[0, 0]
206
207 return numpy.matrix([[bottom_u], [top_u - bottom_u]])
208
209def AverageUFix(claw, U):
210 bottom_u = U[0, 0]
211 top_u = U[1, 0] + bottom_u
212
213 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
James Kuszmaul92797402014-02-17 14:08:49 -0800214 if (bottom_u > claw.U_max[0, 0] or top_u > claw.U_max[0, 0] or
215 top_u < claw.U_min[0, 0] or bottom_u < claw.U_min[0, 0]):
Austin Schuhcda86af2014-02-16 16:16:39 -0800216 scalar = 12.0 / max(numpy.abs(top_u), numpy.abs(bottom_u))
217 top_u *= scalar
218 bottom_u *= scalar
219
220 return numpy.matrix([[bottom_u], [top_u - bottom_u]])
221
Austin Schuh0d9467a2014-02-15 22:36:45 -0800222def ClipDeltaU(claw, U):
223 delta_u = U[0, 0]
224 top_u = U[1, 0]
225 old_bottom_u = claw.X[4, 0]
226
227 # TODO(austin): Preserve the difference between the top and bottom power.
228 new_unclipped_bottom_u = old_bottom_u + delta_u
229
230 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
231 if new_unclipped_bottom_u > claw.U_max[0, 0]:
232 #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0]
233 top_u -= new_unclipped_bottom_u - claw.U_max[0, 0]
234 new_unclipped_bottom_u = claw.U_max[0, 0]
235 if top_u > claw.U_max[1, 0]:
236 new_unclipped_bottom_u -= top_u - claw.U_max[1, 0]
237 top_u = claw.U_max[1, 0]
238 if top_u < claw.U_min[1, 0]:
239 new_unclipped_bottom_u -= top_u - claw.U_min[1, 0]
240 top_u = claw.U_min[1, 0]
241 if new_unclipped_bottom_u < claw.U_min[0, 0]:
242 top_u -= new_unclipped_bottom_u - claw.U_min[0, 0]
243 new_unclipped_bottom_u = claw.U_min[0, 0]
244
245 new_bottom_u = numpy.clip(new_unclipped_bottom_u, claw.U_min[0, 0], claw.U_max[0, 0])
246 new_top_u = numpy.clip(top_u, claw.U_min[1, 0], claw.U_max[1, 0])
247
248 return numpy.matrix([[new_bottom_u - old_bottom_u], [new_top_u]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700249
Austin Schuhc8ca2442013-02-23 12:29:33 -0800250def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -0800251 # Simulate the response of the system to a step input.
Austin Schuh0d9467a2014-02-15 22:36:45 -0800252 #claw = ClawDeltaU()
253 #simulated_x = []
254 #for _ in xrange(100):
255 # claw.Update(numpy.matrix([[12.0]]))
256 # simulated_x.append(claw.X[0, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800257
Austin Schuh0d9467a2014-02-15 22:36:45 -0800258 #pylab.plot(range(100), simulated_x)
259 #pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800260
Austin Schuh3c542312013-02-24 01:53:50 -0800261 # Simulate the closed loop response of the system to a step input.
Austin Schuhcda86af2014-02-16 16:16:39 -0800262 claw = Claw("TopClaw")
263 t = []
Austin Schuh0d9467a2014-02-15 22:36:45 -0800264 close_loop_x_bottom = []
265 close_loop_x_sep = []
266 close_loop_u_bottom = []
267 close_loop_u_top = []
Austin Schuhcda86af2014-02-16 16:16:39 -0800268 R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
269 claw.X[0, 0] = 0
270 for i in xrange(100):
271 #print "Error is", (R - claw.X_hat)
272 U = claw.K * (R - claw.X_hat)
273 #U = numpy.clip(claw.K * (R - claw.X_hat), claw.U_min, claw.U_max)
274 #U = FullSeparationPriority(claw, U)
275 U = AverageUFix(claw, U)
276 #U = claw.K * (R - claw.X_hat)
277 #U = ClipDeltaU(claw, U)
278 claw.UpdateObserver(U)
279 claw.Update(U)
280 close_loop_x_bottom.append(claw.X[0, 0] * 10)
281 close_loop_u_bottom.append(U[0, 0])
282 close_loop_x_sep.append(claw.X[1, 0] * 10)
283 close_loop_u_top.append(U[1, 0] + U[0, 0])
284 t.append(0.01 * i)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800285
Austin Schuhcda86af2014-02-16 16:16:39 -0800286 pylab.plot(t, close_loop_x_bottom, label='x bottom')
287 pylab.plot(t, close_loop_x_sep, label='seperation')
288 pylab.plot(t, close_loop_u_bottom, label='u bottom')
289 pylab.plot(t, close_loop_u_top, label='u top')
Austin Schuh0d9467a2014-02-15 22:36:45 -0800290 pylab.legend()
Austin Schuhfa033692013-02-24 01:00:55 -0800291 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800292
Austin Schuh3c542312013-02-24 01:53:50 -0800293 # Write the generated constants out to a file.
Austin Schuhcda86af2014-02-16 16:16:39 -0800294 if len(argv) != 3:
295 print "Expected .h file name and .cc file name for the claw."
Austin Schuhc8ca2442013-02-23 12:29:33 -0800296 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800297 claw = Claw("Claw")
298 loop_writer = control_loop.ControlLoopWriter("Claw", [claw])
Austin Schuh683a0d02013-03-02 01:51:31 -0800299 if argv[1][-3:] == '.cc':
Austin Schuhcda86af2014-02-16 16:16:39 -0800300 loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800301 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800302 loop_writer.Write(argv[1], argv[2])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800303
304if __name__ == '__main__':
305 sys.exit(main(sys.argv))