blob: 77ccf14180d7bf9a34d394d72dd0f02e3c39a151 [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Brian Silverman17f503e2015-08-02 18:17:18 -07002
Austin Schuhedc317c2015-11-08 14:07:42 -08003from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
5from frc971.control_loops.python import polytope
6from y2014.control_loops.python import polydrivetrain
Brian Silverman17f503e2015-08-02 18:17:18 -07007import numpy
8import sys
9from matplotlib import pylab
Austin Schuha3b42552015-11-27 16:30:12 -080010import gflags
11import glog
12
13FLAGS = gflags.FLAGS
14
15try:
Tyler Chatow6738c362019-02-16 14:12:30 -080016 gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
Austin Schuha3b42552015-11-27 16:30:12 -080017except gflags.DuplicateFlagError:
Tyler Chatow6738c362019-02-16 14:12:30 -080018 pass
19
Brian Silverman17f503e2015-08-02 18:17:18 -070020
21class Claw(control_loop.ControlLoop):
Brian Silverman17f503e2015-08-02 18:17:18 -070022
Tyler Chatow6738c362019-02-16 14:12:30 -080023 def __init__(self, name="RawClaw"):
24 super(Claw, self).__init__(name)
25 # Stall Torque in N m
26 self.stall_torque = 2.42
27 # Stall Current in Amps
28 self.stall_current = 133
29 # Free Speed in RPM
30 self.free_speed = 5500.0
31 # Free Current in Amps
32 self.free_current = 2.7
33 # Moment of inertia of the claw in kg m^2
34 self.J_top = 2.8
35 self.J_bottom = 3.0
Brian Silverman17f503e2015-08-02 18:17:18 -070036
Tyler Chatow6738c362019-02-16 14:12:30 -080037 # Resistance of the motor
38 self.R = 12.0 / self.stall_current
39 # Motor velocity constant
40 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
41 (13.5 - self.R * self.free_current))
42 # Torque constant
43 self.Kt = self.stall_torque / self.stall_current
44 # Gear ratio
45 self.G = 14.0 / 48.0 * 18.0 / 32.0 * 18.0 / 66.0 * 12.0 / 60.0
46 # Control loop time step
47 self.dt = 0.005
Brian Silverman17f503e2015-08-02 18:17:18 -070048
Tyler Chatow6738c362019-02-16 14:12:30 -080049 # State is [bottom position, bottom velocity, top - bottom position,
50 # top - bottom velocity]
51 # Input is [bottom power, top power - bottom power * J_top / J_bottom]
52 # Motor time constants. difference_bottom refers to the constant for how the
53 # bottom velocity affects the difference of the top and bottom velocities.
54 self.common_motor_constant = -self.Kt / self.Kv / (
55 self.G * self.G * self.R)
56 self.bottom_bottom = self.common_motor_constant / self.J_bottom
57 self.difference_bottom = -self.common_motor_constant * (
58 1 / self.J_bottom - 1 / self.J_top)
59 self.difference_difference = self.common_motor_constant / self.J_top
60 # State feedback matrices
Brian Silverman17f503e2015-08-02 18:17:18 -070061
Tyler Chatow6738c362019-02-16 14:12:30 -080062 self.A_continuous = numpy.matrix(
63 [[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, self.bottom_bottom, 0],
64 [0, 0, self.difference_bottom, self.difference_difference]])
Brian Silverman17f503e2015-08-02 18:17:18 -070065
Tyler Chatow6738c362019-02-16 14:12:30 -080066 self.A_bottom_cont = numpy.matrix([[0, 1], [0, self.bottom_bottom]])
Brian Silverman17f503e2015-08-02 18:17:18 -070067
Tyler Chatow6738c362019-02-16 14:12:30 -080068 self.A_diff_cont = numpy.matrix([[0, 1],
69 [0, self.difference_difference]])
Brian Silverman17f503e2015-08-02 18:17:18 -070070
Tyler Chatow6738c362019-02-16 14:12:30 -080071 self.motor_feedforward = self.Kt / (self.G * self.R)
72 self.motor_feedforward_bottom = self.motor_feedforward / self.J_bottom
73 self.motor_feedforward_difference = self.motor_feedforward / self.J_top
74 self.motor_feedforward_difference_bottom = (
75 self.motor_feedforward * (1 / self.J_bottom - 1 / self.J_top))
76 self.B_continuous = numpy.matrix([[0, 0], [0, 0],
77 [self.motor_feedforward_bottom, 0],
78 [
79 -self.motor_feedforward_bottom,
80 self.motor_feedforward_difference
81 ]])
Brian Silverman17f503e2015-08-02 18:17:18 -070082
Tyler Chatow6738c362019-02-16 14:12:30 -080083 glog.debug('Cont X_ss %f',
84 self.motor_feedforward / -self.common_motor_constant)
Brian Silverman17f503e2015-08-02 18:17:18 -070085
Tyler Chatow6738c362019-02-16 14:12:30 -080086 self.B_bottom_cont = numpy.matrix([[0],
87 [self.motor_feedforward_bottom]])
Brian Silverman17f503e2015-08-02 18:17:18 -070088
Tyler Chatow6738c362019-02-16 14:12:30 -080089 self.B_diff_cont = numpy.matrix([[0],
90 [self.motor_feedforward_difference]])
Brian Silverman17f503e2015-08-02 18:17:18 -070091
Tyler Chatow6738c362019-02-16 14:12:30 -080092 self.C = numpy.matrix([[1, 0, 0, 0], [1, 1, 0, 0]])
93 self.D = numpy.matrix([[0, 0], [0, 0]])
Brian Silverman17f503e2015-08-02 18:17:18 -070094
Tyler Chatow6738c362019-02-16 14:12:30 -080095 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
96 self.B_continuous, self.dt)
Brian Silverman17f503e2015-08-02 18:17:18 -070097
Tyler Chatow6738c362019-02-16 14:12:30 -080098 self.A_bottom, self.B_bottom = controls.c2d(self.A_bottom_cont,
99 self.B_bottom_cont, self.dt)
100 self.A_diff, self.B_diff = controls.c2d(self.A_diff_cont,
101 self.B_diff_cont, self.dt)
Brian Silverman17f503e2015-08-02 18:17:18 -0700102
Tyler Chatow6738c362019-02-16 14:12:30 -0800103 self.K_bottom = controls.dplace(self.A_bottom, self.B_bottom,
104 [0.87 + 0.05j, 0.87 - 0.05j])
105 self.K_diff = controls.dplace(self.A_diff, self.B_diff,
106 [0.85 + 0.05j, 0.85 - 0.05j])
Brian Silverman17f503e2015-08-02 18:17:18 -0700107
Tyler Chatow6738c362019-02-16 14:12:30 -0800108 glog.debug('K_diff %s', str(self.K_diff))
109 glog.debug('K_bottom %s', str(self.K_bottom))
Brian Silverman17f503e2015-08-02 18:17:18 -0700110
Tyler Chatow6738c362019-02-16 14:12:30 -0800111 glog.debug('A')
112 glog.debug(self.A)
113 glog.debug('B')
114 glog.debug(self.B)
Brian Silverman17f503e2015-08-02 18:17:18 -0700115
Tyler Chatow6738c362019-02-16 14:12:30 -0800116 self.Q = numpy.matrix([[(1.0 / (0.10**2.0)), 0.0, 0.0, 0.0],
117 [0.0, (1.0 / (0.06**2.0)), 0.0, 0.0],
118 [0.0, 0.0, 0.10, 0.0], [0.0, 0.0, 0.0, 0.1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700119
Tyler Chatow6738c362019-02-16 14:12:30 -0800120 self.R = numpy.matrix([[(1.0 / (40.0**2.0)), 0.0],
121 [0.0, (1.0 / (5.0**2.0))]])
122 #self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700123
Tyler Chatow6738c362019-02-16 14:12:30 -0800124 self.K = numpy.matrix(
125 [[self.K_bottom[0, 0], 0.0, self.K_bottom[0, 1], 0.0],
126 [0.0, self.K_diff[0, 0], 0.0, self.K_diff[0, 1]]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700127
Tyler Chatow6738c362019-02-16 14:12:30 -0800128 # Compute the feed forwards aceleration term.
129 self.K[1, 0] = -self.B[1, 0] * self.K[0, 0] / self.B[1, 1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700130
Tyler Chatow6738c362019-02-16 14:12:30 -0800131 lstsq_A = numpy.identity(2)
132 lstsq_A[0, :] = self.B[1, :]
133 lstsq_A[1, :] = self.B[3, :]
134 glog.debug('System of Equations coefficients:')
135 glog.debug(str(lstsq_A))
136 glog.debug('det %s', str(numpy.linalg.det(lstsq_A)))
Brian Silverman17f503e2015-08-02 18:17:18 -0700137
Tyler Chatow6738c362019-02-16 14:12:30 -0800138 out_x = numpy.linalg.lstsq(
Austin Schuh085eab92020-11-26 13:54:51 -0800139 lstsq_A,
140 numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]),
141 rcond=None)[0]
Tyler Chatow6738c362019-02-16 14:12:30 -0800142 self.K[1, 2] = -lstsq_A[0, 0] * (
143 self.K[0, 2] - out_x[0]) / lstsq_A[0, 1] + out_x[1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700144
Tyler Chatow6738c362019-02-16 14:12:30 -0800145 glog.debug('K unaugmented')
146 glog.debug(str(self.K))
147 glog.debug('B * K unaugmented')
148 glog.debug(str(self.B * self.K))
149 F = self.A - self.B * self.K
150 glog.debug('A - B * K unaugmented')
151 glog.debug(str(F))
152 glog.debug('eigenvalues')
153 glog.debug(str(numpy.linalg.eig(F)[0]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700154
Tyler Chatow6738c362019-02-16 14:12:30 -0800155 self.rpl = .09
156 self.ipl = 0.030
157 self.PlaceObserverPoles([
158 self.rpl + 1j * self.ipl, self.rpl + 1j * self.ipl,
159 self.rpl - 1j * self.ipl, self.rpl - 1j * self.ipl
160 ])
Brian Silverman17f503e2015-08-02 18:17:18 -0700161
Tyler Chatow6738c362019-02-16 14:12:30 -0800162 # The box formed by U_min and U_max must encompass all possible values,
163 # or else Austin's code gets angry.
164 self.U_max = numpy.matrix([[12.0], [12.0]])
165 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700166
Tyler Chatow6738c362019-02-16 14:12:30 -0800167 # For the tests that check the limits, these are (upper, lower) for both
168 # claws.
169 self.hard_pos_limits = None
170 self.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700171
Tyler Chatow6738c362019-02-16 14:12:30 -0800172 # Compute the steady state velocities for a given applied voltage.
173 # The top and bottom of the claw should spin at the same rate if the
174 # physics is right.
175 X_ss = numpy.matrix([[0], [0], [0.0], [0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700176
Tyler Chatow6738c362019-02-16 14:12:30 -0800177 U = numpy.matrix([[1.0], [1.0]])
178 A = self.A
179 B = self.B
180 #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0]
181 X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0]
182 #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]
183 #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]
184 X_ss[3, 0] = 1 / (1 - A[3, 3]) * (
185 X_ss[2, 0] * A[3, 2] + B[3, 1] * U[1, 0] + B[3, 0] * U[0, 0])
186 #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]
187 X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0]
188 X_ss[1, 0] = (A[1, 2] * X_ss[2, 0]) + (A[1, 3] * X_ss[3, 0]) + (
189 B[1, 0] * U[0, 0]) + (B[1, 1] * U[1, 0])
190
191 glog.debug('X_ss %s', str(X_ss))
192
193 self.InitializeState()
Brian Silverman17f503e2015-08-02 18:17:18 -0700194
195
196class ClawDeltaU(Claw):
Brian Silverman17f503e2015-08-02 18:17:18 -0700197
Tyler Chatow6738c362019-02-16 14:12:30 -0800198 def __init__(self, name="Claw"):
199 super(ClawDeltaU, self).__init__(name)
200 A_unaugmented = self.A
201 B_unaugmented = self.B
202 C_unaugmented = self.C
Brian Silverman17f503e2015-08-02 18:17:18 -0700203
Tyler Chatow6738c362019-02-16 14:12:30 -0800204 self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0],
205 [0.0, 0.0, 0.0, 0.0, 0.0],
206 [0.0, 0.0, 0.0, 0.0, 0.0],
207 [0.0, 0.0, 0.0, 0.0, 0.0],
208 [0.0, 0.0, 0.0, 0.0, 1.0]])
209 self.A[0:4, 0:4] = A_unaugmented
210 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700211
Tyler Chatow6738c362019-02-16 14:12:30 -0800212 self.B = numpy.matrix([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0],
213 [1.0, 0.0]])
214 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700215
Tyler Chatow6738c362019-02-16 14:12:30 -0800216 self.C = numpy.concatenate(
217 (C_unaugmented, numpy.matrix([[0.0], [0.0]])), axis=1)
218 self.D = numpy.matrix([[0.0, 0.0], [0.0, 0.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700219
Tyler Chatow6738c362019-02-16 14:12:30 -0800220 #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80])
221 self.Q = numpy.matrix([[(1.0 / (0.04**2.0)), 0.0, 0.0, 0.0, 0.0],
222 [0.0, (1.0 / (0.01**2)), 0.0, 0.0, 0.0],
223 [0.0, 0.0, 0.01, 0.0, 0.0],
224 [0.0, 0.0, 0.0, 0.08, 0.0],
225 [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0**2))]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700226
Tyler Chatow6738c362019-02-16 14:12:30 -0800227 self.R = numpy.matrix([[0.000001, 0.0], [0.0, 1.0 / (10.0**2.0)]])
228 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700229
Tyler Chatow6738c362019-02-16 14:12:30 -0800230 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
231 [50.0, 0.0, 10.0, 0.0, 1.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700232
Tyler Chatow6738c362019-02-16 14:12:30 -0800233 controllability = controls.ctrb(self.A, self.B)
234 glog.debug('Rank of augmented controllability matrix: %d',
235 numpy.linalg.matrix_rank(controllability))
Brian Silverman17f503e2015-08-02 18:17:18 -0700236
Tyler Chatow6738c362019-02-16 14:12:30 -0800237 glog.debug('K')
238 glog.debug(str(self.K))
239 glog.debug('Placed controller poles are')
240 glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
241 glog.debug(
242 str([
243 numpy.abs(x)
244 for x in numpy.linalg.eig(self.A - self.B * self.K)[0]
245 ]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700246
Tyler Chatow6738c362019-02-16 14:12:30 -0800247 self.rpl = .05
248 self.ipl = 0.008
249 self.PlaceObserverPoles([
250 self.rpl + 1j * self.ipl, 0.10, 0.09, self.rpl - 1j * self.ipl, 0.90
251 ])
252 #print "A is"
253 #print self.A
254 #print "L is"
255 #print self.L
256 #print "C is"
257 #print self.C
258 #print "A - LC is"
259 #print self.A - self.L * self.C
Brian Silverman17f503e2015-08-02 18:17:18 -0700260
Tyler Chatow6738c362019-02-16 14:12:30 -0800261 #print "Placed observer poles are"
262 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700263
Tyler Chatow6738c362019-02-16 14:12:30 -0800264 self.U_max = numpy.matrix([[12.0], [12.0]])
265 self.U_min = numpy.matrix([[-12.0], [-12.0]])
266
267 self.InitializeState()
268
Brian Silverman17f503e2015-08-02 18:17:18 -0700269
270def ScaleU(claw, U, K, error):
Tyler Chatow6738c362019-02-16 14:12:30 -0800271 """Clips U as necessary.
Brian Silverman17f503e2015-08-02 18:17:18 -0700272
273 Args:
Tyler Chatow6738c362019-02-16 14:12:30 -0800274 claw: claw object containing moments of inertia and U limits.
275 U: Input matrix to clip as necessary.
276 """
Brian Silverman17f503e2015-08-02 18:17:18 -0700277
Tyler Chatow6738c362019-02-16 14:12:30 -0800278 bottom_u = U[0, 0]
279 top_u = U[1, 0]
280 position_error = error[0:2, 0]
281 velocity_error = error[2:, 0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700282
Tyler Chatow6738c362019-02-16 14:12:30 -0800283 U_poly = polytope.HPolytope(
284 numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]),
285 numpy.matrix([[12], [12], [12], [12]]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700286
Tyler Chatow6738c362019-02-16 14:12:30 -0800287 if (bottom_u > claw.U_max[0, 0] or bottom_u < claw.U_min[0, 0] or
288 top_u > claw.U_max[0, 0] or top_u < claw.U_min[0, 0]):
Brian Silverman17f503e2015-08-02 18:17:18 -0700289
Tyler Chatow6738c362019-02-16 14:12:30 -0800290 position_K = K[:, 0:2]
291 velocity_K = K[:, 2:]
Brian Silverman17f503e2015-08-02 18:17:18 -0700292
Tyler Chatow6738c362019-02-16 14:12:30 -0800293 # H * U <= k
294 # U = UPos + UVel
295 # H * (UPos + UVel) <= k
296 # H * UPos <= k - H * UVel
297 #
298 # Now, we can do a coordinate transformation and say the following.
299 #
300 # UPos = position_K * position_error
301 # (H * position_K) * position_error <= k - H * UVel
302 #
303 # Add in the constraint that 0 <= t <= 1
304 # Now, there are 2 ways this can go. Either we have a region, or we don't
305 # have a region. If we have a region, then pick the largest t and go for it.
306 # If we don't have a region, we need to pick a good comprimise.
Brian Silverman17f503e2015-08-02 18:17:18 -0700307
Tyler Chatow6738c362019-02-16 14:12:30 -0800308 pos_poly = polytope.HPolytope(
309 U_poly.H * position_K,
310 U_poly.k - U_poly.H * velocity_K * velocity_error)
Brian Silverman17f503e2015-08-02 18:17:18 -0700311
Tyler Chatow6738c362019-02-16 14:12:30 -0800312 # The actual angle for the line we call 45.
313 angle_45 = numpy.matrix([[numpy.sqrt(3), 1]])
314 if claw.pos_limits and claw.hard_pos_limits and (
315 claw.X[0, 0] + claw.X[1, 0]) > claw.pos_limits[1]:
316 angle_45 = numpy.matrix([[1, 1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700317
Tyler Chatow6738c362019-02-16 14:12:30 -0800318 P = position_error
319 L45 = numpy.multiply(
320 numpy.matrix([[numpy.sign(P[1, 0]), -numpy.sign(P[0, 0])]]),
321 angle_45)
322 if L45[0, 1] == 0:
323 L45[0, 1] = 1
324 if L45[0, 0] == 0:
325 L45[0, 0] = 1
326 w45 = numpy.matrix([[0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700327
Tyler Chatow6738c362019-02-16 14:12:30 -0800328 if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]):
329 LH = numpy.matrix([[0, 1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700330 else:
Tyler Chatow6738c362019-02-16 14:12:30 -0800331 LH = numpy.matrix([[1, 0]])
332 wh = LH * P
333 standard = numpy.concatenate((L45, LH))
334 W = numpy.concatenate((w45, wh))
335 intersection = numpy.linalg.inv(standard) * W
336 adjusted_pos_error_h, is_inside_h = polydrivetrain.DoCoerceGoal(
337 pos_poly, LH, wh, position_error)
338 adjusted_pos_error_45, is_inside_45 = polydrivetrain.DoCoerceGoal(
339 pos_poly, L45, w45, intersection)
340 if pos_poly.IsInside(intersection):
341 adjusted_pos_error = adjusted_pos_error_h
342 else:
343 if is_inside_h:
344 if numpy.linalg.norm(adjusted_pos_error_h) > numpy.linalg.norm(
345 adjusted_pos_error_45):
346 adjusted_pos_error = adjusted_pos_error_h
347 else:
348 adjusted_pos_error = adjusted_pos_error_45
349 else:
350 adjusted_pos_error = adjusted_pos_error_45
351 #print adjusted_pos_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700352
Tyler Chatow6738c362019-02-16 14:12:30 -0800353 #print "Actual power is ", velocity_K * velocity_error + position_K * adjusted_pos_error
354 return velocity_K * velocity_error + position_K * adjusted_pos_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700355
Tyler Chatow6738c362019-02-16 14:12:30 -0800356 #U = Kpos * poserror + Kvel * velerror
Brian Silverman17f503e2015-08-02 18:17:18 -0700357
Tyler Chatow6738c362019-02-16 14:12:30 -0800358 #scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u))
Brian Silverman17f503e2015-08-02 18:17:18 -0700359
Tyler Chatow6738c362019-02-16 14:12:30 -0800360 #top_u *= scalar
361 #bottom_u *= scalar
Brian Silverman17f503e2015-08-02 18:17:18 -0700362
Tyler Chatow6738c362019-02-16 14:12:30 -0800363 return numpy.matrix([[bottom_u], [top_u]])
364
365
366def run_test(claw,
367 initial_X,
368 goal,
369 max_separation_error=0.01,
370 show_graph=True,
371 iterations=200):
372 """Runs the claw plant on a given claw (claw) with an initial condition (initial_X) and goal (goal).
Brian Silverman17f503e2015-08-02 18:17:18 -0700373
Austin Schuh085eab92020-11-26 13:54:51 -0800374 The tests themselves are not terribly sophisticated; I just test for
Brian Silverman17f503e2015-08-02 18:17:18 -0700375 whether the goal has been reached and whether the separation goes
376 outside of the initial and goal values by more than max_separation_error.
377 Prints out something for a failure of either condition and returns
378 False if tests fail.
Tyler Chatow6738c362019-02-16 14:12:30 -0800379
Brian Silverman17f503e2015-08-02 18:17:18 -0700380 Args:
Tyler Chatow6738c362019-02-16 14:12:30 -0800381 claw: claw object to use.
382 initial_X: starting state.
383 goal: goal state.
384 show_graph: Whether or not to display a graph showing the changing
385 states and voltages.
386 iterations: Number of timesteps to run the model for."""
Brian Silverman17f503e2015-08-02 18:17:18 -0700387
Tyler Chatow6738c362019-02-16 14:12:30 -0800388 claw.X = initial_X
Brian Silverman17f503e2015-08-02 18:17:18 -0700389
Tyler Chatow6738c362019-02-16 14:12:30 -0800390 # Various lists for graphing things.
391 t = []
392 x_bottom = []
393 x_top = []
394 u_bottom = []
395 u_top = []
396 x_separation = []
Brian Silverman17f503e2015-08-02 18:17:18 -0700397
Tyler Chatow6738c362019-02-16 14:12:30 -0800398 tests_passed = True
Brian Silverman17f503e2015-08-02 18:17:18 -0700399
Tyler Chatow6738c362019-02-16 14:12:30 -0800400 # Bounds which separation should not exceed.
401 lower_bound = (initial_X[1, 0] if initial_X[1, 0] < goal[1, 0] else
402 goal[1, 0]) - max_separation_error
403 upper_bound = (initial_X[1, 0] if initial_X[1, 0] > goal[1, 0] else
404 goal[1, 0]) + max_separation_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700405
Austin Schuh5ea48472021-02-02 20:46:41 -0800406 for i in range(iterations):
Tyler Chatow6738c362019-02-16 14:12:30 -0800407 U = claw.K * (goal - claw.X)
408 U = ScaleU(claw, U, claw.K, goal - claw.X)
409 claw.Update(U)
Brian Silverman17f503e2015-08-02 18:17:18 -0700410
Tyler Chatow6738c362019-02-16 14:12:30 -0800411 if claw.X[1, 0] > upper_bound or claw.X[1, 0] < lower_bound:
412 tests_passed = False
413 glog.info('Claw separation was %f', claw.X[1, 0])
414 glog.info("Should have been between", lower_bound, "and",
415 upper_bound)
Brian Silverman17f503e2015-08-02 18:17:18 -0700416
Tyler Chatow6738c362019-02-16 14:12:30 -0800417 if claw.hard_pos_limits and \
418 (claw.X[0, 0] > claw.hard_pos_limits[1] or
419 claw.X[0, 0] < claw.hard_pos_limits[0] or
420 claw.X[0, 0] + claw.X[1, 0] > claw.hard_pos_limits[1] or
421 claw.X[0, 0] + claw.X[1, 0] < claw.hard_pos_limits[0]):
422 tests_passed = False
423 glog.info('Claws at %f and %f', claw.X[0, 0],
424 claw.X[0, 0] + claw.X[1, 0])
425 glog.info("Both should be in %s, definitely %s", claw.pos_limits,
426 claw.hard_pos_limits)
Brian Silverman17f503e2015-08-02 18:17:18 -0700427
Tyler Chatow6738c362019-02-16 14:12:30 -0800428 t.append(i * claw.dt)
429 x_bottom.append(claw.X[0, 0] * 10.0)
430 x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10.0)
431 u_bottom.append(U[0, 0])
432 u_top.append(U[1, 0])
433 x_separation.append(claw.X[1, 0] * 10.0)
Brian Silverman17f503e2015-08-02 18:17:18 -0700434
Tyler Chatow6738c362019-02-16 14:12:30 -0800435 if show_graph:
436 pylab.plot(t, x_bottom, label='x bottom * 10')
437 pylab.plot(t, x_top, label='x top * 10')
438 pylab.plot(t, u_bottom, label='u bottom')
439 pylab.plot(t, u_top, label='u top')
440 pylab.plot(t, x_separation, label='separation * 10')
441 pylab.legend()
442 pylab.show()
Brian Silverman17f503e2015-08-02 18:17:18 -0700443
Tyler Chatow6738c362019-02-16 14:12:30 -0800444 # Test to make sure that we are near the goal.
445 if numpy.max(abs(claw.X - goal)) > 1e-4:
446 tests_passed = False
447 glog.error('X was %s Expected %s', str(claw.X), str(goal))
Brian Silverman17f503e2015-08-02 18:17:18 -0700448
Tyler Chatow6738c362019-02-16 14:12:30 -0800449 return tests_passed
450
Brian Silverman17f503e2015-08-02 18:17:18 -0700451
452def main(argv):
Tyler Chatow6738c362019-02-16 14:12:30 -0800453 argv = FLAGS(argv)
Austin Schuha3b42552015-11-27 16:30:12 -0800454
Tyler Chatow6738c362019-02-16 14:12:30 -0800455 claw = Claw()
456 if FLAGS.plot:
457 # Test moving the claw with constant separation.
458 initial_X = numpy.matrix([[-1.0], [0.0], [0.0], [0.0]])
459 R = numpy.matrix([[1.0], [0.0], [0.0], [0.0]])
460 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700461
Tyler Chatow6738c362019-02-16 14:12:30 -0800462 # Test just changing separation.
463 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
464 R = numpy.matrix([[0.0], [1.0], [0.0], [0.0]])
465 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700466
Tyler Chatow6738c362019-02-16 14:12:30 -0800467 # Test changing both separation and position at once.
468 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
469 R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
470 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700471
Tyler Chatow6738c362019-02-16 14:12:30 -0800472 # Test a small separation error and a large position one.
473 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
474 R = numpy.matrix([[2.0], [0.05], [0.0], [0.0]])
475 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700476
Tyler Chatow6738c362019-02-16 14:12:30 -0800477 # Test a small separation error and a large position one.
478 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
479 R = numpy.matrix([[-0.5], [1.0], [0.0], [0.0]])
480 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700481
Tyler Chatow6738c362019-02-16 14:12:30 -0800482 # Test opening with the top claw at the limit.
483 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
484 R = numpy.matrix([[-1.5], [1.5], [0.0], [0.0]])
485 claw.hard_pos_limits = (-1.6, 0.1)
486 claw.pos_limits = (-1.5, 0.0)
487 run_test(claw, initial_X, R)
488 claw.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700489
Tyler Chatow6738c362019-02-16 14:12:30 -0800490 # Test opening with the bottom claw at the limit.
491 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
492 R = numpy.matrix([[0], [1.5], [0.0], [0.0]])
493 claw.hard_pos_limits = (-0.1, 1.6)
494 claw.pos_limits = (0.0, 1.6)
495 run_test(claw, initial_X, R)
496 claw.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700497
Tyler Chatow6738c362019-02-16 14:12:30 -0800498 # Write the generated constants out to a file.
499 if len(argv) != 3:
500 glog.fatal('Expected .h file name and .cc file name for the claw.')
501 else:
502 namespaces = ['y2014', 'control_loops', 'claw']
503 claw = Claw('Claw')
504 loop_writer = control_loop.ControlLoopWriter(
505 'Claw', [claw], namespaces=namespaces)
506 loop_writer.AddConstant(
507 control_loop.Constant('kClawMomentOfInertiaRatio', '%f',
508 claw.J_top / claw.J_bottom))
509 loop_writer.AddConstant(control_loop.Constant('kDt', '%f', claw.dt))
510 loop_writer.Write(argv[1], argv[2])
511
Brian Silverman17f503e2015-08-02 18:17:18 -0700512
513if __name__ == '__main__':
Tyler Chatow6738c362019-02-16 14:12:30 -0800514 sys.exit(main(sys.argv))