blob: b5ea6a121e0ceb18159452b7a2c1892d85bd7c05 [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
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700159 # For the tests that check the limits, these are (upper, lower) for both
160 # claws.
161 self.hard_pos_limits = None
162 self.pos_limits = None
163
Austin Schuh2758ade2014-02-22 16:58:21 -0800164 # Compute the steady state velocities for a given applied voltage.
165 # The top and bottom of the claw should spin at the same rate if the
166 # physics is right.
167 X_ss = numpy.matrix([[0], [0], [0.0], [0]])
168
169 U = numpy.matrix([[1.0],[1.0]])
170 A = self.A
171 B = self.B
172 #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0]
173 X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0]
174 #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]
175 #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]
176 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])
177 #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]
178 X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0]
179 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]
180
181 print "X_ss", X_ss
182
Austin Schuhc1f68892013-03-16 17:06:27 -0700183 self.InitializeState()
184
185
Austin Schuh3bb9a442014-02-02 16:01:45 -0800186class ClawDeltaU(Claw):
187 def __init__(self, name="Claw"):
188 super(ClawDeltaU, self).__init__(name)
Austin Schuhc1f68892013-03-16 17:06:27 -0700189 A_unaugmented = self.A
190 B_unaugmented = self.B
Austin Schuh0d9467a2014-02-15 22:36:45 -0800191 C_unaugmented = self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700192
Austin Schuh0d9467a2014-02-15 22:36:45 -0800193 self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0],
194 [0.0, 0.0, 0.0, 0.0, 0.0],
195 [0.0, 0.0, 0.0, 0.0, 0.0],
196 [0.0, 0.0, 0.0, 0.0, 0.0],
197 [0.0, 0.0, 0.0, 0.0, 1.0]])
198 self.A[0:4, 0:4] = A_unaugmented
199 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Austin Schuhc1f68892013-03-16 17:06:27 -0700200
Austin Schuh0d9467a2014-02-15 22:36:45 -0800201 self.B = numpy.matrix([[0.0, 0.0],
202 [0.0, 0.0],
203 [0.0, 0.0],
204 [0.0, 0.0],
205 [1.0, 0.0]])
206 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Austin Schuhc1f68892013-03-16 17:06:27 -0700207
Austin Schuh0d9467a2014-02-15 22:36:45 -0800208 self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])),
209 axis=1)
210 self.D = numpy.matrix([[0.0, 0.0],
211 [0.0, 0.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700212
Austin Schuh0d9467a2014-02-15 22:36:45 -0800213 #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80])
214 self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0],
215 [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0],
216 [0.0, 0.0, 0.01, 0.0, 0.0],
217 [0.0, 0.0, 0.0, 0.08, 0.0],
218 [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]])
219
220 self.R = numpy.matrix([[0.000001, 0.0],
221 [0.0, 1.0 / (10.0 ** 2.0)]])
222 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
223
224 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
225 [50.0, 0.0, 10.0, 0.0, 1.0]])
226 #self.K = numpy.matrix([[50.0, -100.0, 0, -10, 0],
227 # [50.0, 100.0, 0, 10, 0]])
228
229 controlability = controls.ctrb(self.A, self.B);
230 print "Rank of augmented controlability matrix.", numpy.linalg.matrix_rank(controlability)
Austin Schuhc1f68892013-03-16 17:06:27 -0700231
232 print "K"
233 print self.K
234 print "Placed controller poles are"
235 print numpy.linalg.eig(self.A - self.B * self.K)[0]
Austin Schuh0d9467a2014-02-15 22:36:45 -0800236 print [numpy.abs(x) for x in numpy.linalg.eig(self.A - self.B * self.K)[0]]
Austin Schuhc1f68892013-03-16 17:06:27 -0700237
238 self.rpl = .05
239 self.ipl = 0.008
Austin Schuh0d9467a2014-02-15 22:36:45 -0800240 self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09,
Brian Silverman23a67ca2013-03-16 23:48:50 -0700241 self.rpl - 1j * self.ipl, 0.90])
Austin Schuh0d9467a2014-02-15 22:36:45 -0800242 #print "A is"
243 #print self.A
244 #print "L is"
245 #print self.L
246 #print "C is"
247 #print self.C
248 #print "A - LC is"
249 #print self.A - self.L * self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700250
Austin Schuh0d9467a2014-02-15 22:36:45 -0800251 #print "Placed observer poles are"
252 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
253
254 self.U_max = numpy.matrix([[12.0], [12.0]])
255 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700256
257 self.InitializeState()
258
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700259def ScaleU(claw, U, K, error):
260 """Clips U as necessary.
261
262 Args:
263 claw: claw object containing moments of inertia and U limits.
264 U: Input matrix to clip as necessary.
265 """
Brian Silverman6dd2c532014-03-29 23:34:39 -0700266
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700267 bottom_u = U[0, 0]
268 top_u = U[1, 0]
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700269 position_error = error[0:2, 0]
270 velocity_error = error[2:, 0]
271
272 U_poly = polytope.HPolytope(
273 numpy.matrix([[1, 0],
274 [-1, 0],
275 [0, 1],
276 [0, -1]]),
277 numpy.matrix([[12],
278 [12],
279 [12],
280 [12]]))
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700281
282 if (bottom_u > claw.U_max[0, 0] or bottom_u < claw.U_min[0, 0] or
283 top_u > claw.U_max[0, 0] or top_u < claw.U_min[0, 0]):
284
285 position_K = K[:, 0:2]
286 velocity_K = K[:, 2:]
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700287
288 # H * U <= k
289 # U = UPos + UVel
290 # H * (UPos + UVel) <= k
291 # H * UPos <= k - H * UVel
292 #
293 # Now, we can do a coordinate transformation and say the following.
294 #
295 # UPos = position_K * position_error
296 # (H * position_K) * position_error <= k - H * UVel
297 #
298 # Add in the constraint that 0 <= t <= 1
299 # Now, there are 2 ways this can go. Either we have a region, or we don't
300 # have a region. If we have a region, then pick the largest t and go for it.
301 # If we don't have a region, we need to pick a good comprimise.
302
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700303 pos_poly = polytope.HPolytope(
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700304 U_poly.H * position_K,
305 U_poly.k - U_poly.H * velocity_K * velocity_error)
306
307 # The actual angle for the line we call 45.
308 angle_45 = numpy.matrix([[numpy.sqrt(3), 1]])
309 if claw.pos_limits and claw.hard_pos_limits and claw.X[0, 0] + claw.X[1, 0] > claw.pos_limits[1]:
310 angle_45 = numpy.matrix([[1, 1]])
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700311
Brian Silverman6dd2c532014-03-29 23:34:39 -0700312 P = position_error
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700313 L45 = numpy.multiply(numpy.matrix([[numpy.sign(P[1, 0]), -numpy.sign(P[0, 0])]]), angle_45)
Brian Silverman6dd2c532014-03-29 23:34:39 -0700314 if L45[0, 1] == 0:
315 L45[0, 1] = 1
316 if L45[0, 0] == 0:
317 L45[0, 0] = 1
318 w45 = numpy.matrix([[0]])
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700319
Brian Silverman6dd2c532014-03-29 23:34:39 -0700320 if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]):
321 LH = numpy.matrix([[0, 1]])
322 else:
323 LH = numpy.matrix([[1, 0]])
324 wh = LH * P
325 standard = numpy.concatenate((L45, LH))
Brian Silverman6dd2c532014-03-29 23:34:39 -0700326 W = numpy.concatenate((w45, wh))
Brian Silverman6dd2c532014-03-29 23:34:39 -0700327 intersection = numpy.linalg.inv(standard) * W
Brian Silverman6dd2c532014-03-29 23:34:39 -0700328 adjusted_pos_error_h, is_inside_h = polydrivetrain.DoCoerceGoal(pos_poly,
329 LH, wh, position_error)
330 adjusted_pos_error_45, is_inside_45 = polydrivetrain.DoCoerceGoal(pos_poly,
331 L45, w45, intersection)
332 if pos_poly.IsInside(intersection):
333 adjusted_pos_error = adjusted_pos_error_h
Brian Silverman6dd2c532014-03-29 23:34:39 -0700334 else:
335 if is_inside_h:
336 if numpy.linalg.norm(adjusted_pos_error_h) > numpy.linalg.norm(adjusted_pos_error_45):
337 adjusted_pos_error = adjusted_pos_error_h
338 else:
339 adjusted_pos_error = adjusted_pos_error_45
340 else:
341 adjusted_pos_error = adjusted_pos_error_45
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700342 #print adjusted_pos_error
Brian Silverman6dd2c532014-03-29 23:34:39 -0700343
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700344 #print "Actual power is ", velocity_K * velocity_error + position_K * adjusted_pos_error
Austin Schuhfe6d37e2014-03-24 09:45:01 -0700345 return velocity_K * velocity_error + position_K * adjusted_pos_error
346
347 #U = Kpos * poserror + Kvel * velerror
348
349 #scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u))
350
351 #top_u *= scalar
352 #bottom_u *= scalar
353
354 return numpy.matrix([[bottom_u], [top_u]])
355
Brian Silverman1704f332014-03-26 21:50:09 -0700356def run_test(claw, initial_X, goal, max_separation_error=0.01, show_graph=False, iterations=200):
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700357 """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 -0800358
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700359 The tests themselves are not terribly sophisticated; I just test for
360 whether the goal has been reached and whether the separation goes
Brian Silverman6dd2c532014-03-29 23:34:39 -0700361 outside of the initial and goal values by more than max_separation_error.
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700362 Prints out something for a failure of either condition and returns
363 False if tests fail.
364 Args:
365 claw: claw object to use.
366 initial_X: starting state.
367 goal: goal state.
368 show_graph: Whether or not to display a graph showing the changing
369 states and voltages.
370 iterations: Number of timesteps to run the model for."""
Austin Schuhc8ca2442013-02-23 12:29:33 -0800371
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700372 claw.X = initial_X
373
374 # Various lists for graphing things.
Austin Schuhcda86af2014-02-16 16:16:39 -0800375 t = []
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700376 x_bottom = []
377 x_top = []
378 u_bottom = []
379 u_top = []
380 x_separation = []
Austin Schuhc8ca2442013-02-23 12:29:33 -0800381
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700382 tests_passed = True
383
384 # Bounds which separation should not exceed.
385 lower_bound = (initial_X[1, 0] if initial_X[1, 0] < goal[1, 0]
386 else goal[1, 0]) - max_separation_error
387 upper_bound = (initial_X[1, 0] if initial_X[1, 0] > goal[1, 0]
388 else goal[1, 0]) + max_separation_error
389
390 for i in xrange(iterations):
391 U = claw.K * (goal - claw.X)
392 U = ScaleU(claw, U, claw.K, goal - claw.X)
393 claw.Update(U)
394
395 if claw.X[1, 0] > upper_bound or claw.X[1, 0] < lower_bound:
396 tests_passed = False
397 print "Claw separation was", claw.X[1, 0]
398 print "Should have been between", lower_bound, "and", upper_bound
399
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700400 if claw.hard_pos_limits and \
401 (claw.X[0, 0] > claw.hard_pos_limits[1] or
402 claw.X[0, 0] < claw.hard_pos_limits[0] or
403 claw.X[0, 0] + claw.X[1, 0] > claw.hard_pos_limits[1] or
404 claw.X[0, 0] + claw.X[1, 0] < claw.hard_pos_limits[0]):
405 tests_passed = False
406 print "Claws at %f and %f" % (claw.X[0, 0], claw.X[0, 0] + claw.X[1, 0])
407 print "Both should be in %s, definitely %s" % \
408 (claw.pos_limits, claw.hard_pos_limits)
409
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700410 t.append(i * claw.dt)
411 x_bottom.append(claw.X[0, 0] * 10.0)
412 x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10.0)
413 u_bottom.append(U[0, 0])
414 u_top.append(U[1, 0])
415 x_separation.append(claw.X[1, 0] * 10.0)
416
417 if show_graph:
418 pylab.plot(t, x_bottom, label='x bottom * 10')
419 pylab.plot(t, x_top, label='x top * 10')
420 pylab.plot(t, u_bottom, label='u bottom')
421 pylab.plot(t, u_top, label='u top')
422 pylab.plot(t, x_separation, label='separation * 10')
423 pylab.legend()
424 pylab.show()
425
426 # Test to make sure that we are near the goal.
427 if numpy.max(abs(claw.X - goal)) > 1e-4:
428 tests_passed = False
429 print "X was", claw.X, "Expected", goal
430
431 return tests_passed
432
433def main(argv):
434 claw = Claw()
435
436 # Test moving the claw with constant separation.
437 initial_X = numpy.matrix([[-1.0], [0.0], [0.0], [0.0]])
438 R = numpy.matrix([[1.0], [0.0], [0.0], [0.0]])
439 run_test(claw, initial_X, R)
440
441 # Test just changing separation.
442 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
443 R = numpy.matrix([[0.0], [1.0], [0.0], [0.0]])
444 run_test(claw, initial_X, R)
445
Brian Silverman6dd2c532014-03-29 23:34:39 -0700446 # Test changing both separation and position at once.
James Kuszmaulf63b0ae2014-03-25 16:52:11 -0700447 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
448 R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
449 run_test(claw, initial_X, R)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800450
Brian Silverman6dd2c532014-03-29 23:34:39 -0700451 # Test a small separation error and a large position one.
452 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
453 R = numpy.matrix([[2.0], [0.05], [0.0], [0.0]])
454 run_test(claw, initial_X, R)
455
Brian Silvermanf05948b2014-03-30 00:24:36 -0700456 # Test a small separation error and a large position one.
457 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
458 R = numpy.matrix([[-0.5], [1.0], [0.0], [0.0]])
459 run_test(claw, initial_X, R)
460
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700461 # Test opening with the top claw at the limit.
Brian Silvermanf05948b2014-03-30 00:24:36 -0700462 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
Brian Silvermanb087c0a2014-03-30 12:59:52 -0700463 R = numpy.matrix([[-1.5], [1.5], [0.0], [0.0]])
464 claw.hard_pos_limits = (-1.6, 0.1)
465 claw.pos_limits = (-1.5, 0.0)
466 run_test(claw, initial_X, R)
467 claw.pos_limits = None
468
469 # Test opening with the bottom claw at the limit.
470 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
471 R = numpy.matrix([[0], [1.5], [0.0], [0.0]])
472 claw.hard_pos_limits = (-0.1, 1.6)
473 claw.pos_limits = (0.0, 1.6)
474 run_test(claw, initial_X, R)
475 claw.pos_limits = None
Brian Silvermanf05948b2014-03-30 00:24:36 -0700476
Austin Schuh3c542312013-02-24 01:53:50 -0800477 # Write the generated constants out to a file.
Austin Schuhcda86af2014-02-16 16:16:39 -0800478 if len(argv) != 3:
479 print "Expected .h file name and .cc file name for the claw."
Austin Schuhc8ca2442013-02-23 12:29:33 -0800480 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800481 claw = Claw("Claw")
482 loop_writer = control_loop.ControlLoopWriter("Claw", [claw])
Brian Silvermanf246dbd2014-03-30 13:54:35 -0700483 loop_writer.AddConstant(control_loop.Constant("kClawMomentOfInertiaRatio",
484 "%f", claw.J_top / claw.J_bottom))
Austin Schuh683a0d02013-03-02 01:51:31 -0800485 if argv[1][-3:] == '.cc':
Austin Schuhcda86af2014-02-16 16:16:39 -0800486 loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800487 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800488 loop_writer.Write(argv[1], argv[2])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800489
490if __name__ == '__main__':
491 sys.exit(main(sys.argv))