blob: c8e6424ed9b0771f3fee6e6693daf411b3aaf131 [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 Schuhfe6d37e2014-03-24 09:45:01 -07005import polytope
6import polydrivetrain
Austin Schuhc8ca2442013-02-23 12:29:33 -08007import numpy
Austin Schuhc8ca2442013-02-23 12:29:33 -08008import sys
Austin Schuhc8ca2442013-02-23 12:29:33 -08009from matplotlib import pylab
Austin Schuhc8ca2442013-02-23 12:29:33 -080010
Austin Schuh3bb9a442014-02-02 16:01:45 -080011class Claw(control_loop.ControlLoop):
12 def __init__(self, name="RawClaw"):
13 super(Claw, self).__init__(name)
Austin Schuhc8ca2442013-02-23 12:29:33 -080014 # Stall Torque in N m
James Kuszmaule1755b32014-02-13 06:27:48 -080015 self.stall_torque = 2.42
Austin Schuhc8ca2442013-02-23 12:29:33 -080016 # Stall Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080017 self.stall_current = 133
James Kuszmaul92797402014-02-17 14:08:49 -080018 # Free Speed in RPM
19 self.free_speed = 5500.0
Austin Schuh3c542312013-02-24 01:53:50 -080020 # Free Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080021 self.free_current = 2.7
Austin Schuh3bb9a442014-02-02 16:01:45 -080022 # Moment of inertia of the claw in kg m^2
Brian Silverman6dd2c532014-03-29 23:34:39 -070023 self.J_top = 2.8
24 self.J_bottom = 3.0
James Kuszmaul3ac190a2014-03-26 20:11:04 -070025
Austin Schuhc8ca2442013-02-23 12:29:33 -080026 # Resistance of the motor
James Kuszmaul92797402014-02-17 14:08:49 -080027 self.R = 12.0 / self.stall_current
Austin Schuhc8ca2442013-02-23 12:29:33 -080028 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080029 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
30 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080031 # Torque constant
32 self.Kt = self.stall_torque / self.stall_current
33 # Gear ratio
James Kuszmaule1755b32014-02-13 06:27:48 -080034 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 -080035 # Control loop time step
36 self.dt = 0.01
37
James Kuszmaule2afbe42014-02-17 22:29:59 -080038 # State is [bottom position, bottom velocity, top - bottom position,
39 # top - bottom velocity]
40 # Input is [bottom power, top power - bottom power * J_top / J_bottom]
James Kuszmaul92797402014-02-17 14:08:49 -080041 # Motor time constants. difference_bottom refers to the constant for how the
42 # bottom velocity affects the difference of the top and bottom velocities.
43 self.common_motor_constant = -self.Kt / self.Kv / (self.G * self.G * self.R)
44 self.bottom_bottom = self.common_motor_constant / self.J_bottom
James Kuszmaulc02a39a2014-02-18 15:45:16 -080045 self.difference_bottom = -self.common_motor_constant * (1 / self.J_bottom
James Kuszmaul92797402014-02-17 14:08:49 -080046 - 1 / self.J_top)
47 self.difference_difference = self.common_motor_constant / self.J_top
Austin Schuhc8ca2442013-02-23 12:29:33 -080048 # State feedback matrices
James Kuszmaule2afbe42014-02-17 22:29:59 -080049
Austin Schuhc8ca2442013-02-23 12:29:33 -080050 self.A_continuous = numpy.matrix(
James Kuszmaulc02a39a2014-02-18 15:45:16 -080051 [[0, 0, 1, 0],
52 [0, 0, 0, 1],
James Kuszmaul92797402014-02-17 14:08:49 -080053 [0, 0, self.bottom_bottom, 0],
54 [0, 0, self.difference_bottom, self.difference_difference]])
Austin Schuh0d9467a2014-02-15 22:36:45 -080055
James Kuszmaule2afbe42014-02-17 22:29:59 -080056 self.A_bottom_cont = numpy.matrix(
57 [[0, 1],
58 [0, self.bottom_bottom]])
59
60 self.A_diff_cont = numpy.matrix(
61 [[0, 1],
62 [0, self.difference_difference]])
63
James Kuszmaul92797402014-02-17 14:08:49 -080064 self.motor_feedforward = self.Kt / (self.G * self.R)
James Kuszmaulf2ed6e62014-02-17 17:52:07 -080065 self.motor_feedforward_bottom = self.motor_feedforward / self.J_bottom
66 self.motor_feedforward_difference = self.motor_feedforward / self.J_top
James Kuszmaul92797402014-02-17 14:08:49 -080067 self.motor_feedforward_difference_bottom = (
68 self.motor_feedforward * (1 / self.J_bottom - 1 / self.J_top))
Austin Schuhc8ca2442013-02-23 12:29:33 -080069 self.B_continuous = numpy.matrix(
Austin Schuh0d9467a2014-02-15 22:36:45 -080070 [[0, 0],
James Kuszmaule2afbe42014-02-17 22:29:59 -080071 [0, 0],
James Kuszmaulc02a39a2014-02-18 15:45:16 -080072 [self.motor_feedforward_bottom, 0],
Austin Schuh170fe252014-02-22 15:52:01 -080073 [-self.motor_feedforward_bottom, self.motor_feedforward_difference]])
James Kuszmaule2afbe42014-02-17 22:29:59 -080074
James Kuszmaulc02a39a2014-02-18 15:45:16 -080075 print "Cont X_ss", self.motor_feedforward / -self.common_motor_constant
76
James Kuszmaule2afbe42014-02-17 22:29:59 -080077 self.B_bottom_cont = numpy.matrix(
78 [[0],
79 [self.motor_feedforward_bottom]])
80
81 self.B_diff_cont = numpy.matrix(
82 [[0],
83 [self.motor_feedforward_difference]])
84
Austin Schuh0d9467a2014-02-15 22:36:45 -080085 self.C = numpy.matrix([[1, 0, 0, 0],
James Kuszmaulc02a39a2014-02-18 15:45:16 -080086 [1, 1, 0, 0]])
Austin Schuh0d9467a2014-02-15 22:36:45 -080087 self.D = numpy.matrix([[0, 0],
88 [0, 0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080089
Austin Schuhc1f68892013-03-16 17:06:27 -070090 self.A, self.B = self.ContinuousToDiscrete(
91 self.A_continuous, self.B_continuous, self.dt)
Austin Schuhc8ca2442013-02-23 12:29:33 -080092
James Kuszmaule2afbe42014-02-17 22:29:59 -080093 self.A_bottom, self.B_bottom = controls.c2d(
94 self.A_bottom_cont, self.B_bottom_cont, self.dt)
95 self.A_diff, self.B_diff = controls.c2d(
96 self.A_diff_cont, self.B_diff_cont, self.dt)
97
Brian Silverman6dd2c532014-03-29 23:34:39 -070098 self.K_bottom = controls.dplace(self.A_bottom, self.B_bottom, [.75 + 0.1j, .75 - 0.1j])
99 self.K_diff = controls.dplace(self.A_diff, self.B_diff, [.75 + 0.1j, .75 - 0.1j])
Austin Schuh2758ade2014-02-22 16:58:21 -0800100
101 print "K_diff", self.K_diff
102 print "K_bottom", self.K_bottom
103
James Kuszmaulc02a39a2014-02-18 15:45:16 -0800104 print "A"
105 print self.A
106 print "B"
107 print self.B
James Kuszmaule2afbe42014-02-17 22:29:59 -0800108
James Kuszmaulc02a39a2014-02-18 15:45:16 -0800109
Austin Schuhcda86af2014-02-16 16:16:39 -0800110 self.Q = numpy.matrix([[(1.0 / (0.10 ** 2.0)), 0.0, 0.0, 0.0],
Austin Schuh01c652b2014-02-21 23:13:42 -0800111 [0.0, (1.0 / (0.06 ** 2.0)), 0.0, 0.0],
112 [0.0, 0.0, 0.10, 0.0],
113 [0.0, 0.0, 0.0, 0.1]])
Austin Schuhcda86af2014-02-16 16:16:39 -0800114
James Kuszmaulf2ed6e62014-02-17 17:52:07 -0800115 self.R = numpy.matrix([[(1.0 / (40.0 ** 2.0)), 0.0],
116 [0.0, (1.0 / (5.0 ** 2.0))]])
Austin Schuh170fe252014-02-22 15:52:01 -0800117 #self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
James Kuszmaule2afbe42014-02-17 22:29:59 -0800118
Austin Schuh2758ade2014-02-22 16:58:21 -0800119 self.K = numpy.matrix([[self.K_bottom[0, 0], 0.0, self.K_bottom[0, 1], 0.0],
120 [0.0, self.K_diff[0, 0], 0.0, self.K_diff[0, 1]]])
Austin Schuh170fe252014-02-22 15:52:01 -0800121
122 # Compute the feed forwards aceleration term.
123 self.K[1, 0] = -self.B[1, 0] * self.K[0, 0] / self.B[1, 1]
124
James Kuszmaulc02a39a2014-02-18 15:45:16 -0800125 lstsq_A = numpy.identity(2)
Austin Schuh170fe252014-02-22 15:52:01 -0800126 lstsq_A[0, :] = self.B[1, :]
127 lstsq_A[1, :] = self.B[3, :]
James Kuszmaulc02a39a2014-02-18 15:45:16 -0800128 print "System of Equations coefficients:"
Austin Schuh170fe252014-02-22 15:52:01 -0800129 print lstsq_A
James Kuszmaulc02a39a2014-02-18 15:45:16 -0800130 print "det", numpy.linalg.det(lstsq_A)
Austin Schuh170fe252014-02-22 15:52:01 -0800131
James Kuszmaulc02a39a2014-02-18 15:45:16 -0800132 out_x = numpy.linalg.lstsq(
133 lstsq_A,
134 numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]))[0]
135 self.K[1, 2] = -lstsq_A[0, 0] * (self.K[0, 2] - out_x[0]) / lstsq_A[0, 1] + out_x[1]
Austin Schuhcda86af2014-02-16 16:16:39 -0800136
137 print "K unaugmented"
138 print self.K
James Kuszmaule2afbe42014-02-17 22:29:59 -0800139 print "B * K unaugmented"
140 print self.B * self.K
141 F = self.A - self.B * self.K
James Kuszmaule2afbe42014-02-17 22:29:59 -0800142 print "A - B * K unaugmented"
143 print F
144 print "eigenvalues"
145 print numpy.linalg.eig(F)[0]
Austin Schuhc8ca2442013-02-23 12:29:33 -0800146
Brian Silverman6dd2c532014-03-29 23:34:39 -0700147 self.rpl = .05
148 self.ipl = 0.010
Austin Schuh3c542312013-02-24 01:53:50 -0800149 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
Austin Schuh0d9467a2014-02-15 22:36:45 -0800150 self.rpl + 1j * self.ipl,
James Kuszmaule2afbe42014-02-17 22:29:59 -0800151 self.rpl - 1j * self.ipl,
Austin Schuh3c542312013-02-24 01:53:50 -0800152 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800153
James Kuszmaul92797402014-02-17 14:08:49 -0800154 # The box formed by U_min and U_max must encompass all possible values,
155 # or else Austin's code gets angry.
James Kuszmauld536a402014-02-18 22:32:12 -0800156 self.U_max = numpy.matrix([[12.0], [12.0]])
157 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800158
Austin Schuh2758ade2014-02-22 16:58:21 -0800159 # Compute the steady state velocities for a given applied voltage.
160 # The top and bottom of the claw should spin at the same rate if the
161 # physics is right.
162 X_ss = numpy.matrix([[0], [0], [0.0], [0]])
163
164 U = numpy.matrix([[1.0],[1.0]])
165 A = self.A
166 B = self.B
167 #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0]
168 X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0]
169 #X_ss[3, 0] = X_ss[3, 0] * A[3, 3] + X_ss[2, 0] * A[3, 2] + B[3, 0] * U[0, 0] + B[3, 1] * U[1, 0]
170 #X_ss[3, 0] * (1 - A[3, 3]) = X_ss[2, 0] * A[3, 2] + B[3, 0] * U[0, 0] + B[3, 1] * U[1, 0]
171 X_ss[3, 0] = 1 / (1 - A[3, 3]) * (X_ss[2, 0] * A[3, 2] + B[3, 1] * U[1, 0] + B[3, 0] * U[0, 0])
172 #X_ss[3, 0] = 1 / (1 - A[3, 3]) / (1 - A[2, 2]) * B[2, 0] * U[0, 0] * A[3, 2] + B[3, 0] * U[0, 0] + B[3, 1] * U[1, 0]
173 X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0]
174 X_ss[1, 0] = A[1, 2] * X_ss[2, 0] + A[1, 3] * X_ss[3, 0] + B[1, 0] * U[0, 0] + B[1, 1] * U[1, 0]
175
176 print "X_ss", X_ss
177
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700178 self.U_poly = polytope.HPolytope(
179 numpy.matrix([[1, 0],
180 [-1, 0],
181 [0, 1],
182 [0, -1]]),
183 numpy.matrix([[12],
184 [12],
185 [12],
186 [12]]))
187
Austin Schuhc1f68892013-03-16 17:06:27 -0700188 self.InitializeState()
189
190
Austin Schuh3bb9a442014-02-02 16:01:45 -0800191class ClawDeltaU(Claw):
192 def __init__(self, name="Claw"):
193 super(ClawDeltaU, self).__init__(name)
Austin Schuhc1f68892013-03-16 17:06:27 -0700194 A_unaugmented = self.A
195 B_unaugmented = self.B
Austin Schuh0d9467a2014-02-15 22:36:45 -0800196 C_unaugmented = self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700197
Austin Schuh0d9467a2014-02-15 22:36:45 -0800198 self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0],
199 [0.0, 0.0, 0.0, 0.0, 0.0],
200 [0.0, 0.0, 0.0, 0.0, 0.0],
201 [0.0, 0.0, 0.0, 0.0, 0.0],
202 [0.0, 0.0, 0.0, 0.0, 1.0]])
203 self.A[0:4, 0:4] = A_unaugmented
204 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Austin Schuhc1f68892013-03-16 17:06:27 -0700205
Austin Schuh0d9467a2014-02-15 22:36:45 -0800206 self.B = numpy.matrix([[0.0, 0.0],
207 [0.0, 0.0],
208 [0.0, 0.0],
209 [0.0, 0.0],
210 [1.0, 0.0]])
211 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Austin Schuhc1f68892013-03-16 17:06:27 -0700212
Austin Schuh0d9467a2014-02-15 22:36:45 -0800213 self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])),
214 axis=1)
215 self.D = numpy.matrix([[0.0, 0.0],
216 [0.0, 0.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700217
Austin Schuh0d9467a2014-02-15 22:36:45 -0800218 #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80])
219 self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0],
220 [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0],
221 [0.0, 0.0, 0.01, 0.0, 0.0],
222 [0.0, 0.0, 0.0, 0.08, 0.0],
223 [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]])
224
225 self.R = numpy.matrix([[0.000001, 0.0],
226 [0.0, 1.0 / (10.0 ** 2.0)]])
227 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
228
229 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
230 [50.0, 0.0, 10.0, 0.0, 1.0]])
231 #self.K = numpy.matrix([[50.0, -100.0, 0, -10, 0],
232 # [50.0, 100.0, 0, 10, 0]])
233
234 controlability = controls.ctrb(self.A, self.B);
235 print "Rank of augmented controlability matrix.", numpy.linalg.matrix_rank(controlability)
Austin Schuhc1f68892013-03-16 17:06:27 -0700236
237 print "K"
238 print self.K
239 print "Placed controller poles are"
240 print numpy.linalg.eig(self.A - self.B * self.K)[0]
Austin Schuh0d9467a2014-02-15 22:36:45 -0800241 print [numpy.abs(x) for x in numpy.linalg.eig(self.A - self.B * self.K)[0]]
Austin Schuhc1f68892013-03-16 17:06:27 -0700242
243 self.rpl = .05
244 self.ipl = 0.008
Austin Schuh0d9467a2014-02-15 22:36:45 -0800245 self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09,
Brian Silverman23a67ca2013-03-16 23:48:50 -0700246 self.rpl - 1j * self.ipl, 0.90])
Austin Schuh0d9467a2014-02-15 22:36:45 -0800247 #print "A is"
248 #print self.A
249 #print "L is"
250 #print self.L
251 #print "C is"
252 #print self.C
253 #print "A - LC is"
254 #print self.A - self.L * self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700255
Austin Schuh0d9467a2014-02-15 22:36:45 -0800256 #print "Placed observer poles are"
257 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
258
259 self.U_max = numpy.matrix([[12.0], [12.0]])
260 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700261
262 self.InitializeState()
263
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700264def ScaleU(claw, U, K, error):
265 """Clips U as necessary.
266
267 Args:
268 claw: claw object containing moments of inertia and U limits.
269 U: Input matrix to clip as necessary.
270 """
Brian Silverman6dd2c532014-03-29 23:34:39 -0700271
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700272 bottom_u = U[0, 0]
273 top_u = U[1, 0]
274
275 if (bottom_u > claw.U_max[0, 0] or bottom_u < claw.U_min[0, 0] or
276 top_u > claw.U_max[0, 0] or top_u < claw.U_min[0, 0]):
277
278 position_K = K[:, 0:2]
279 velocity_K = K[:, 2:]
280 position_error = error[0:2, 0]
281 velocity_error = error[2:, 0]
282
283 # H * U <= k
284 # U = UPos + UVel
285 # H * (UPos + UVel) <= k
286 # H * UPos <= k - H * UVel
287 #
288 # Now, we can do a coordinate transformation and say the following.
289 #
290 # UPos = position_K * position_error
291 # (H * position_K) * position_error <= k - H * UVel
292 #
293 # Add in the constraint that 0 <= t <= 1
294 # Now, there are 2 ways this can go. Either we have a region, or we don't
295 # have a region. If we have a region, then pick the largest t and go for it.
296 # If we don't have a region, we need to pick a good comprimise.
297
298 # First prototype! -> Only cap the position power, leave the velocity power active.
299
300 #u_velocity = velocity_K * velocity_error
301 #u_position = position_K * position_error
302 #scalar = min(1.0, claw.U_max[0, 0] / max(numpy.abs(u_position[0, 0]), numpy.abs(u_position[1, 0])))
303 #return u_velocity + scalar * u_position
304
305 pos_poly = polytope.HPolytope(
306 claw.U_poly.H * position_K,
307 claw.U_poly.k - claw.U_poly.H * velocity_K * velocity_error)
308
Brian Silverman6dd2c532014-03-29 23:34:39 -0700309 P = position_error
310 #K = numpy.matrix([[position_error[1, 0], -position_error[0, 0]]])
Brian Silvermanf05948b2014-03-30 00:24:36 -0700311 L45 = numpy.matrix([[numpy.sign(P[1, 0]) * numpy.sqrt(3), -numpy.sign(P[0, 0])]])
Brian Silverman6dd2c532014-03-29 23:34:39 -0700312 if L45[0, 1] == 0:
313 L45[0, 1] = 1
314 if L45[0, 0] == 0:
315 L45[0, 0] = 1
316 w45 = numpy.matrix([[0]])
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700317
Brian Silverman6dd2c532014-03-29 23:34:39 -0700318 if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]):
319 LH = numpy.matrix([[0, 1]])
320 else:
321 LH = numpy.matrix([[1, 0]])
322 wh = LH * P
323 standard = numpy.concatenate((L45, LH))
324 #print "Standard", standard
325 W = numpy.concatenate((w45, wh))
326 #print "W is", W
327 intersection = numpy.linalg.inv(standard) * W
328 print "intersection point %s" % intersection
329 print "Intersection power is ", velocity_K * velocity_error + position_K * intersection
330 adjusted_pos_error_h, is_inside_h = polydrivetrain.DoCoerceGoal(pos_poly,
331 LH, wh, position_error)
332 adjusted_pos_error_45, is_inside_45 = polydrivetrain.DoCoerceGoal(pos_poly,
333 L45, w45, intersection)
334 if pos_poly.IsInside(intersection):
335 adjusted_pos_error = adjusted_pos_error_h
336 print "horizontal"
337 else:
338 if is_inside_h:
339 if numpy.linalg.norm(adjusted_pos_error_h) > numpy.linalg.norm(adjusted_pos_error_45):
340 adjusted_pos_error = adjusted_pos_error_h
341 else:
342 adjusted_pos_error = adjusted_pos_error_45
343 else:
344 adjusted_pos_error = adjusted_pos_error_45
345 print "45"
346 print velocity_K * velocity_error + position_K * adjusted_pos_error
347 print "45"
348 print adjusted_pos_error
349
350 print "Actual power is ", velocity_K * velocity_error + position_K * adjusted_pos_error
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700351 return velocity_K * velocity_error + position_K * adjusted_pos_error
352
353 #U = Kpos * poserror + Kvel * velerror
354
355 #scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u))
356
357 #top_u *= scalar
358 #bottom_u *= scalar
359
360 return numpy.matrix([[bottom_u], [top_u]])
361
Brian Silverman1704f332014-03-26 21:50:09 -0700362def run_test(claw, initial_X, goal, max_separation_error=0.01, show_graph=False, iterations=200):
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700363 """Runs the claw plant on a given claw (claw) with an initial condition (initial_X) and goal (goal).
Austin Schuhc8ca2442013-02-23 12:29:33 -0800364
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700365 The tests themselves are not terribly sophisticated; I just test for
366 whether the goal has been reached and whether the separation goes
Brian Silverman6dd2c532014-03-29 23:34:39 -0700367 outside of the initial and goal values by more than max_separation_error.
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700368 Prints out something for a failure of either condition and returns
369 False if tests fail.
370 Args:
371 claw: claw object to use.
372 initial_X: starting state.
373 goal: goal state.
374 show_graph: Whether or not to display a graph showing the changing
375 states and voltages.
376 iterations: Number of timesteps to run the model for."""
Austin Schuhc8ca2442013-02-23 12:29:33 -0800377
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700378 claw.X = initial_X
379
380 # Various lists for graphing things.
Austin Schuhcda86af2014-02-16 16:16:39 -0800381 t = []
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700382 x_bottom = []
383 x_top = []
384 u_bottom = []
385 u_top = []
386 x_separation = []
Austin Schuhc8ca2442013-02-23 12:29:33 -0800387
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700388 tests_passed = True
389
390 # Bounds which separation should not exceed.
391 lower_bound = (initial_X[1, 0] if initial_X[1, 0] < goal[1, 0]
392 else goal[1, 0]) - max_separation_error
393 upper_bound = (initial_X[1, 0] if initial_X[1, 0] > goal[1, 0]
394 else goal[1, 0]) + max_separation_error
395
396 for i in xrange(iterations):
397 U = claw.K * (goal - claw.X)
398 U = ScaleU(claw, U, claw.K, goal - claw.X)
399 claw.Update(U)
400
401 if claw.X[1, 0] > upper_bound or claw.X[1, 0] < lower_bound:
402 tests_passed = False
403 print "Claw separation was", claw.X[1, 0]
404 print "Should have been between", lower_bound, "and", upper_bound
405
406 t.append(i * claw.dt)
407 x_bottom.append(claw.X[0, 0] * 10.0)
408 x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10.0)
409 u_bottom.append(U[0, 0])
410 u_top.append(U[1, 0])
411 x_separation.append(claw.X[1, 0] * 10.0)
412
413 if show_graph:
414 pylab.plot(t, x_bottom, label='x bottom * 10')
415 pylab.plot(t, x_top, label='x top * 10')
416 pylab.plot(t, u_bottom, label='u bottom')
417 pylab.plot(t, u_top, label='u top')
418 pylab.plot(t, x_separation, label='separation * 10')
419 pylab.legend()
420 pylab.show()
421
422 # Test to make sure that we are near the goal.
423 if numpy.max(abs(claw.X - goal)) > 1e-4:
424 tests_passed = False
425 print "X was", claw.X, "Expected", goal
426
427 return tests_passed
428
429def main(argv):
430 claw = Claw()
431
432 # Test moving the claw with constant separation.
433 initial_X = numpy.matrix([[-1.0], [0.0], [0.0], [0.0]])
434 R = numpy.matrix([[1.0], [0.0], [0.0], [0.0]])
435 run_test(claw, initial_X, R)
436
437 # Test just changing separation.
438 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
439 R = numpy.matrix([[0.0], [1.0], [0.0], [0.0]])
440 run_test(claw, initial_X, R)
441
Brian Silverman6dd2c532014-03-29 23:34:39 -0700442 # Test changing both separation and position at once.
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700443 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
444 R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
445 run_test(claw, initial_X, R)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800446
Brian Silverman6dd2c532014-03-29 23:34:39 -0700447 # Test a small separation error and a large position one.
448 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
449 R = numpy.matrix([[2.0], [0.05], [0.0], [0.0]])
450 run_test(claw, initial_X, R)
451
Brian Silvermanf05948b2014-03-30 00:24:36 -0700452 # Test a small separation error and a large position one.
453 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
454 R = numpy.matrix([[-0.5], [1.0], [0.0], [0.0]])
455 run_test(claw, initial_X, R)
456
457 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
458 R = numpy.matrix([[-0.05], [2.0], [0.0], [0.0]])
459 run_test(claw, initial_X, R, show_graph=True)
460
Austin Schuh3c542312013-02-24 01:53:50 -0800461 # Write the generated constants out to a file.
Austin Schuhcda86af2014-02-16 16:16:39 -0800462 if len(argv) != 3:
463 print "Expected .h file name and .cc file name for the claw."
Austin Schuhc8ca2442013-02-23 12:29:33 -0800464 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800465 claw = Claw("Claw")
466 loop_writer = control_loop.ControlLoopWriter("Claw", [claw])
Austin Schuh683a0d02013-03-02 01:51:31 -0800467 if argv[1][-3:] == '.cc':
Austin Schuhcda86af2014-02-16 16:16:39 -0800468 loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800469 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800470 loop_writer.Write(argv[1], argv[2])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800471
472if __name__ == '__main__':
473 sys.exit(main(sys.argv))