blob: 346a188ec5dff6439009e887eaeebd885a39a48a [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#!/usr/bin/python
2
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(
139 lstsq_A, numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]))[0]
140 self.K[1, 2] = -lstsq_A[0, 0] * (
141 self.K[0, 2] - out_x[0]) / lstsq_A[0, 1] + out_x[1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700142
Tyler Chatow6738c362019-02-16 14:12:30 -0800143 glog.debug('K unaugmented')
144 glog.debug(str(self.K))
145 glog.debug('B * K unaugmented')
146 glog.debug(str(self.B * self.K))
147 F = self.A - self.B * self.K
148 glog.debug('A - B * K unaugmented')
149 glog.debug(str(F))
150 glog.debug('eigenvalues')
151 glog.debug(str(numpy.linalg.eig(F)[0]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700152
Tyler Chatow6738c362019-02-16 14:12:30 -0800153 self.rpl = .09
154 self.ipl = 0.030
155 self.PlaceObserverPoles([
156 self.rpl + 1j * self.ipl, self.rpl + 1j * self.ipl,
157 self.rpl - 1j * self.ipl, self.rpl - 1j * self.ipl
158 ])
Brian Silverman17f503e2015-08-02 18:17:18 -0700159
Tyler Chatow6738c362019-02-16 14:12:30 -0800160 # The box formed by U_min and U_max must encompass all possible values,
161 # or else Austin's code gets angry.
162 self.U_max = numpy.matrix([[12.0], [12.0]])
163 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700164
Tyler Chatow6738c362019-02-16 14:12:30 -0800165 # For the tests that check the limits, these are (upper, lower) for both
166 # claws.
167 self.hard_pos_limits = None
168 self.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700169
Tyler Chatow6738c362019-02-16 14:12:30 -0800170 # Compute the steady state velocities for a given applied voltage.
171 # The top and bottom of the claw should spin at the same rate if the
172 # physics is right.
173 X_ss = numpy.matrix([[0], [0], [0.0], [0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700174
Tyler Chatow6738c362019-02-16 14:12:30 -0800175 U = numpy.matrix([[1.0], [1.0]])
176 A = self.A
177 B = self.B
178 #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0]
179 X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0]
180 #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]
181 #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]
182 X_ss[3, 0] = 1 / (1 - A[3, 3]) * (
183 X_ss[2, 0] * A[3, 2] + B[3, 1] * U[1, 0] + B[3, 0] * U[0, 0])
184 #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]
185 X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0]
186 X_ss[1, 0] = (A[1, 2] * X_ss[2, 0]) + (A[1, 3] * X_ss[3, 0]) + (
187 B[1, 0] * U[0, 0]) + (B[1, 1] * U[1, 0])
188
189 glog.debug('X_ss %s', str(X_ss))
190
191 self.InitializeState()
Brian Silverman17f503e2015-08-02 18:17:18 -0700192
193
194class ClawDeltaU(Claw):
Brian Silverman17f503e2015-08-02 18:17:18 -0700195
Tyler Chatow6738c362019-02-16 14:12:30 -0800196 def __init__(self, name="Claw"):
197 super(ClawDeltaU, self).__init__(name)
198 A_unaugmented = self.A
199 B_unaugmented = self.B
200 C_unaugmented = self.C
Brian Silverman17f503e2015-08-02 18:17:18 -0700201
Tyler Chatow6738c362019-02-16 14:12:30 -0800202 self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0],
203 [0.0, 0.0, 0.0, 0.0, 0.0],
204 [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, 1.0]])
207 self.A[0:4, 0:4] = A_unaugmented
208 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700209
Tyler Chatow6738c362019-02-16 14:12:30 -0800210 self.B = numpy.matrix([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0],
211 [1.0, 0.0]])
212 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700213
Tyler Chatow6738c362019-02-16 14:12:30 -0800214 self.C = numpy.concatenate(
215 (C_unaugmented, numpy.matrix([[0.0], [0.0]])), axis=1)
216 self.D = numpy.matrix([[0.0, 0.0], [0.0, 0.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700217
Tyler Chatow6738c362019-02-16 14:12:30 -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))]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700224
Tyler Chatow6738c362019-02-16 14:12:30 -0800225 self.R = numpy.matrix([[0.000001, 0.0], [0.0, 1.0 / (10.0**2.0)]])
226 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700227
Tyler Chatow6738c362019-02-16 14:12:30 -0800228 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
229 [50.0, 0.0, 10.0, 0.0, 1.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700230
Tyler Chatow6738c362019-02-16 14:12:30 -0800231 controllability = controls.ctrb(self.A, self.B)
232 glog.debug('Rank of augmented controllability matrix: %d',
233 numpy.linalg.matrix_rank(controllability))
Brian Silverman17f503e2015-08-02 18:17:18 -0700234
Tyler Chatow6738c362019-02-16 14:12:30 -0800235 glog.debug('K')
236 glog.debug(str(self.K))
237 glog.debug('Placed controller poles are')
238 glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
239 glog.debug(
240 str([
241 numpy.abs(x)
242 for x in numpy.linalg.eig(self.A - self.B * self.K)[0]
243 ]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700244
Tyler Chatow6738c362019-02-16 14:12:30 -0800245 self.rpl = .05
246 self.ipl = 0.008
247 self.PlaceObserverPoles([
248 self.rpl + 1j * self.ipl, 0.10, 0.09, self.rpl - 1j * self.ipl, 0.90
249 ])
250 #print "A is"
251 #print self.A
252 #print "L is"
253 #print self.L
254 #print "C is"
255 #print self.C
256 #print "A - LC is"
257 #print self.A - self.L * self.C
Brian Silverman17f503e2015-08-02 18:17:18 -0700258
Tyler Chatow6738c362019-02-16 14:12:30 -0800259 #print "Placed observer poles are"
260 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700261
Tyler Chatow6738c362019-02-16 14:12:30 -0800262 self.U_max = numpy.matrix([[12.0], [12.0]])
263 self.U_min = numpy.matrix([[-12.0], [-12.0]])
264
265 self.InitializeState()
266
Brian Silverman17f503e2015-08-02 18:17:18 -0700267
268def ScaleU(claw, U, K, error):
Tyler Chatow6738c362019-02-16 14:12:30 -0800269 """Clips U as necessary.
Brian Silverman17f503e2015-08-02 18:17:18 -0700270
271 Args:
Tyler Chatow6738c362019-02-16 14:12:30 -0800272 claw: claw object containing moments of inertia and U limits.
273 U: Input matrix to clip as necessary.
274 """
Brian Silverman17f503e2015-08-02 18:17:18 -0700275
Tyler Chatow6738c362019-02-16 14:12:30 -0800276 bottom_u = U[0, 0]
277 top_u = U[1, 0]
278 position_error = error[0:2, 0]
279 velocity_error = error[2:, 0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700280
Tyler Chatow6738c362019-02-16 14:12:30 -0800281 U_poly = polytope.HPolytope(
282 numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]),
283 numpy.matrix([[12], [12], [12], [12]]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700284
Tyler Chatow6738c362019-02-16 14:12:30 -0800285 if (bottom_u > claw.U_max[0, 0] or bottom_u < claw.U_min[0, 0] or
286 top_u > claw.U_max[0, 0] or top_u < claw.U_min[0, 0]):
Brian Silverman17f503e2015-08-02 18:17:18 -0700287
Tyler Chatow6738c362019-02-16 14:12:30 -0800288 position_K = K[:, 0:2]
289 velocity_K = K[:, 2:]
Brian Silverman17f503e2015-08-02 18:17:18 -0700290
Tyler Chatow6738c362019-02-16 14:12:30 -0800291 # H * U <= k
292 # U = UPos + UVel
293 # H * (UPos + UVel) <= k
294 # H * UPos <= k - H * UVel
295 #
296 # Now, we can do a coordinate transformation and say the following.
297 #
298 # UPos = position_K * position_error
299 # (H * position_K) * position_error <= k - H * UVel
300 #
301 # Add in the constraint that 0 <= t <= 1
302 # Now, there are 2 ways this can go. Either we have a region, or we don't
303 # have a region. If we have a region, then pick the largest t and go for it.
304 # If we don't have a region, we need to pick a good comprimise.
Brian Silverman17f503e2015-08-02 18:17:18 -0700305
Tyler Chatow6738c362019-02-16 14:12:30 -0800306 pos_poly = polytope.HPolytope(
307 U_poly.H * position_K,
308 U_poly.k - U_poly.H * velocity_K * velocity_error)
Brian Silverman17f503e2015-08-02 18:17:18 -0700309
Tyler Chatow6738c362019-02-16 14:12:30 -0800310 # The actual angle for the line we call 45.
311 angle_45 = numpy.matrix([[numpy.sqrt(3), 1]])
312 if claw.pos_limits and claw.hard_pos_limits and (
313 claw.X[0, 0] + claw.X[1, 0]) > claw.pos_limits[1]:
314 angle_45 = numpy.matrix([[1, 1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700315
Tyler Chatow6738c362019-02-16 14:12:30 -0800316 P = position_error
317 L45 = numpy.multiply(
318 numpy.matrix([[numpy.sign(P[1, 0]), -numpy.sign(P[0, 0])]]),
319 angle_45)
320 if L45[0, 1] == 0:
321 L45[0, 1] = 1
322 if L45[0, 0] == 0:
323 L45[0, 0] = 1
324 w45 = numpy.matrix([[0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700325
Tyler Chatow6738c362019-02-16 14:12:30 -0800326 if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]):
327 LH = numpy.matrix([[0, 1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700328 else:
Tyler Chatow6738c362019-02-16 14:12:30 -0800329 LH = numpy.matrix([[1, 0]])
330 wh = LH * P
331 standard = numpy.concatenate((L45, LH))
332 W = numpy.concatenate((w45, wh))
333 intersection = numpy.linalg.inv(standard) * W
334 adjusted_pos_error_h, is_inside_h = polydrivetrain.DoCoerceGoal(
335 pos_poly, LH, wh, position_error)
336 adjusted_pos_error_45, is_inside_45 = polydrivetrain.DoCoerceGoal(
337 pos_poly, L45, w45, intersection)
338 if pos_poly.IsInside(intersection):
339 adjusted_pos_error = adjusted_pos_error_h
340 else:
341 if is_inside_h:
342 if numpy.linalg.norm(adjusted_pos_error_h) > numpy.linalg.norm(
343 adjusted_pos_error_45):
344 adjusted_pos_error = adjusted_pos_error_h
345 else:
346 adjusted_pos_error = adjusted_pos_error_45
347 else:
348 adjusted_pos_error = adjusted_pos_error_45
349 #print adjusted_pos_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700350
Tyler Chatow6738c362019-02-16 14:12:30 -0800351 #print "Actual power is ", velocity_K * velocity_error + position_K * adjusted_pos_error
352 return velocity_K * velocity_error + position_K * adjusted_pos_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700353
Tyler Chatow6738c362019-02-16 14:12:30 -0800354 #U = Kpos * poserror + Kvel * velerror
Brian Silverman17f503e2015-08-02 18:17:18 -0700355
Tyler Chatow6738c362019-02-16 14:12:30 -0800356 #scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u))
Brian Silverman17f503e2015-08-02 18:17:18 -0700357
Tyler Chatow6738c362019-02-16 14:12:30 -0800358 #top_u *= scalar
359 #bottom_u *= scalar
Brian Silverman17f503e2015-08-02 18:17:18 -0700360
Tyler Chatow6738c362019-02-16 14:12:30 -0800361 return numpy.matrix([[bottom_u], [top_u]])
362
363
364def run_test(claw,
365 initial_X,
366 goal,
367 max_separation_error=0.01,
368 show_graph=True,
369 iterations=200):
370 """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 -0700371
372 The tests themselves are not terribly sophisticated; I just test for
373 whether the goal has been reached and whether the separation goes
374 outside of the initial and goal values by more than max_separation_error.
375 Prints out something for a failure of either condition and returns
376 False if tests fail.
Tyler Chatow6738c362019-02-16 14:12:30 -0800377
Brian Silverman17f503e2015-08-02 18:17:18 -0700378 Args:
Tyler Chatow6738c362019-02-16 14:12:30 -0800379 claw: claw object to use.
380 initial_X: starting state.
381 goal: goal state.
382 show_graph: Whether or not to display a graph showing the changing
383 states and voltages.
384 iterations: Number of timesteps to run the model for."""
Brian Silverman17f503e2015-08-02 18:17:18 -0700385
Tyler Chatow6738c362019-02-16 14:12:30 -0800386 claw.X = initial_X
Brian Silverman17f503e2015-08-02 18:17:18 -0700387
Tyler Chatow6738c362019-02-16 14:12:30 -0800388 # Various lists for graphing things.
389 t = []
390 x_bottom = []
391 x_top = []
392 u_bottom = []
393 u_top = []
394 x_separation = []
Brian Silverman17f503e2015-08-02 18:17:18 -0700395
Tyler Chatow6738c362019-02-16 14:12:30 -0800396 tests_passed = True
Brian Silverman17f503e2015-08-02 18:17:18 -0700397
Tyler Chatow6738c362019-02-16 14:12:30 -0800398 # Bounds which separation should not exceed.
399 lower_bound = (initial_X[1, 0] if initial_X[1, 0] < goal[1, 0] else
400 goal[1, 0]) - max_separation_error
401 upper_bound = (initial_X[1, 0] if initial_X[1, 0] > goal[1, 0] else
402 goal[1, 0]) + max_separation_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700403
Austin Schuh5ea48472021-02-02 20:46:41 -0800404 for i in range(iterations):
Tyler Chatow6738c362019-02-16 14:12:30 -0800405 U = claw.K * (goal - claw.X)
406 U = ScaleU(claw, U, claw.K, goal - claw.X)
407 claw.Update(U)
Brian Silverman17f503e2015-08-02 18:17:18 -0700408
Tyler Chatow6738c362019-02-16 14:12:30 -0800409 if claw.X[1, 0] > upper_bound or claw.X[1, 0] < lower_bound:
410 tests_passed = False
411 glog.info('Claw separation was %f', claw.X[1, 0])
412 glog.info("Should have been between", lower_bound, "and",
413 upper_bound)
Brian Silverman17f503e2015-08-02 18:17:18 -0700414
Tyler Chatow6738c362019-02-16 14:12:30 -0800415 if claw.hard_pos_limits and \
416 (claw.X[0, 0] > claw.hard_pos_limits[1] or
417 claw.X[0, 0] < claw.hard_pos_limits[0] or
418 claw.X[0, 0] + claw.X[1, 0] > claw.hard_pos_limits[1] or
419 claw.X[0, 0] + claw.X[1, 0] < claw.hard_pos_limits[0]):
420 tests_passed = False
421 glog.info('Claws at %f and %f', claw.X[0, 0],
422 claw.X[0, 0] + claw.X[1, 0])
423 glog.info("Both should be in %s, definitely %s", claw.pos_limits,
424 claw.hard_pos_limits)
Brian Silverman17f503e2015-08-02 18:17:18 -0700425
Tyler Chatow6738c362019-02-16 14:12:30 -0800426 t.append(i * claw.dt)
427 x_bottom.append(claw.X[0, 0] * 10.0)
428 x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10.0)
429 u_bottom.append(U[0, 0])
430 u_top.append(U[1, 0])
431 x_separation.append(claw.X[1, 0] * 10.0)
Brian Silverman17f503e2015-08-02 18:17:18 -0700432
Tyler Chatow6738c362019-02-16 14:12:30 -0800433 if show_graph:
434 pylab.plot(t, x_bottom, label='x bottom * 10')
435 pylab.plot(t, x_top, label='x top * 10')
436 pylab.plot(t, u_bottom, label='u bottom')
437 pylab.plot(t, u_top, label='u top')
438 pylab.plot(t, x_separation, label='separation * 10')
439 pylab.legend()
440 pylab.show()
Brian Silverman17f503e2015-08-02 18:17:18 -0700441
Tyler Chatow6738c362019-02-16 14:12:30 -0800442 # Test to make sure that we are near the goal.
443 if numpy.max(abs(claw.X - goal)) > 1e-4:
444 tests_passed = False
445 glog.error('X was %s Expected %s', str(claw.X), str(goal))
Brian Silverman17f503e2015-08-02 18:17:18 -0700446
Tyler Chatow6738c362019-02-16 14:12:30 -0800447 return tests_passed
448
Brian Silverman17f503e2015-08-02 18:17:18 -0700449
450def main(argv):
Tyler Chatow6738c362019-02-16 14:12:30 -0800451 argv = FLAGS(argv)
Austin Schuha3b42552015-11-27 16:30:12 -0800452
Tyler Chatow6738c362019-02-16 14:12:30 -0800453 claw = Claw()
454 if FLAGS.plot:
455 # Test moving the claw with constant separation.
456 initial_X = numpy.matrix([[-1.0], [0.0], [0.0], [0.0]])
457 R = numpy.matrix([[1.0], [0.0], [0.0], [0.0]])
458 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700459
Tyler Chatow6738c362019-02-16 14:12:30 -0800460 # Test just changing separation.
461 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
462 R = numpy.matrix([[0.0], [1.0], [0.0], [0.0]])
463 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700464
Tyler Chatow6738c362019-02-16 14:12:30 -0800465 # Test changing both separation and position at once.
466 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
467 R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
468 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700469
Tyler Chatow6738c362019-02-16 14:12:30 -0800470 # Test a small separation error and a large position one.
471 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
472 R = numpy.matrix([[2.0], [0.05], [0.0], [0.0]])
473 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700474
Tyler Chatow6738c362019-02-16 14:12:30 -0800475 # Test a small separation error and a large position one.
476 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
477 R = numpy.matrix([[-0.5], [1.0], [0.0], [0.0]])
478 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700479
Tyler Chatow6738c362019-02-16 14:12:30 -0800480 # Test opening with the top claw at the limit.
481 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
482 R = numpy.matrix([[-1.5], [1.5], [0.0], [0.0]])
483 claw.hard_pos_limits = (-1.6, 0.1)
484 claw.pos_limits = (-1.5, 0.0)
485 run_test(claw, initial_X, R)
486 claw.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700487
Tyler Chatow6738c362019-02-16 14:12:30 -0800488 # Test opening with the bottom claw at the limit.
489 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
490 R = numpy.matrix([[0], [1.5], [0.0], [0.0]])
491 claw.hard_pos_limits = (-0.1, 1.6)
492 claw.pos_limits = (0.0, 1.6)
493 run_test(claw, initial_X, R)
494 claw.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700495
Tyler Chatow6738c362019-02-16 14:12:30 -0800496 # Write the generated constants out to a file.
497 if len(argv) != 3:
498 glog.fatal('Expected .h file name and .cc file name for the claw.')
499 else:
500 namespaces = ['y2014', 'control_loops', 'claw']
501 claw = Claw('Claw')
502 loop_writer = control_loop.ControlLoopWriter(
503 'Claw', [claw], namespaces=namespaces)
504 loop_writer.AddConstant(
505 control_loop.Constant('kClawMomentOfInertiaRatio', '%f',
506 claw.J_top / claw.J_bottom))
507 loop_writer.AddConstant(control_loop.Constant('kDt', '%f', claw.dt))
508 loop_writer.Write(argv[1], argv[2])
509
Brian Silverman17f503e2015-08-02 18:17:18 -0700510
511if __name__ == '__main__':
Tyler Chatow6738c362019-02-16 14:12:30 -0800512 sys.exit(main(sys.argv))