blob: 488d79233b71bb29ca5fc792316b235d8334e1cd [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.
Ravago Jones5127ccc2022-07-31 16:32:45 -070054 self.common_motor_constant = -self.Kt / self.Kv / (self.G * self.G *
55 self.R)
Tyler Chatow6738c362019-02-16 14:12:30 -080056 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,
Ravago Jones5127ccc2022-07-31 16:32:45 -070099 self.B_bottom_cont,
100 self.dt)
Tyler Chatow6738c362019-02-16 14:12:30 -0800101 self.A_diff, self.B_diff = controls.c2d(self.A_diff_cont,
102 self.B_diff_cont, self.dt)
Brian Silverman17f503e2015-08-02 18:17:18 -0700103
Tyler Chatow6738c362019-02-16 14:12:30 -0800104 self.K_bottom = controls.dplace(self.A_bottom, self.B_bottom,
105 [0.87 + 0.05j, 0.87 - 0.05j])
106 self.K_diff = controls.dplace(self.A_diff, self.B_diff,
107 [0.85 + 0.05j, 0.85 - 0.05j])
Brian Silverman17f503e2015-08-02 18:17:18 -0700108
Tyler Chatow6738c362019-02-16 14:12:30 -0800109 glog.debug('K_diff %s', str(self.K_diff))
110 glog.debug('K_bottom %s', str(self.K_bottom))
Brian Silverman17f503e2015-08-02 18:17:18 -0700111
Tyler Chatow6738c362019-02-16 14:12:30 -0800112 glog.debug('A')
113 glog.debug(self.A)
114 glog.debug('B')
115 glog.debug(self.B)
Brian Silverman17f503e2015-08-02 18:17:18 -0700116
Tyler Chatow6738c362019-02-16 14:12:30 -0800117 self.Q = numpy.matrix([[(1.0 / (0.10**2.0)), 0.0, 0.0, 0.0],
118 [0.0, (1.0 / (0.06**2.0)), 0.0, 0.0],
119 [0.0, 0.0, 0.10, 0.0], [0.0, 0.0, 0.0, 0.1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700120
Tyler Chatow6738c362019-02-16 14:12:30 -0800121 self.R = numpy.matrix([[(1.0 / (40.0**2.0)), 0.0],
122 [0.0, (1.0 / (5.0**2.0))]])
123 #self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700124
Tyler Chatow6738c362019-02-16 14:12:30 -0800125 self.K = numpy.matrix(
126 [[self.K_bottom[0, 0], 0.0, self.K_bottom[0, 1], 0.0],
127 [0.0, self.K_diff[0, 0], 0.0, self.K_diff[0, 1]]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700128
Tyler Chatow6738c362019-02-16 14:12:30 -0800129 # Compute the feed forwards aceleration term.
130 self.K[1, 0] = -self.B[1, 0] * self.K[0, 0] / self.B[1, 1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700131
Tyler Chatow6738c362019-02-16 14:12:30 -0800132 lstsq_A = numpy.identity(2)
133 lstsq_A[0, :] = self.B[1, :]
134 lstsq_A[1, :] = self.B[3, :]
135 glog.debug('System of Equations coefficients:')
136 glog.debug(str(lstsq_A))
137 glog.debug('det %s', str(numpy.linalg.det(lstsq_A)))
Brian Silverman17f503e2015-08-02 18:17:18 -0700138
Ravago Jones5127ccc2022-07-31 16:32:45 -0700139 out_x = numpy.linalg.lstsq(lstsq_A,
140 numpy.matrix([[self.A[1, 2]],
141 [self.A[3, 2]]]),
142 rcond=None)[0]
143 self.K[1, 2] = -lstsq_A[0, 0] * (self.K[0, 2] -
144 out_x[0]) / lstsq_A[0, 1] + out_x[1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700145
Tyler Chatow6738c362019-02-16 14:12:30 -0800146 glog.debug('K unaugmented')
147 glog.debug(str(self.K))
148 glog.debug('B * K unaugmented')
149 glog.debug(str(self.B * self.K))
150 F = self.A - self.B * self.K
151 glog.debug('A - B * K unaugmented')
152 glog.debug(str(F))
153 glog.debug('eigenvalues')
154 glog.debug(str(numpy.linalg.eig(F)[0]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700155
Tyler Chatow6738c362019-02-16 14:12:30 -0800156 self.rpl = .09
157 self.ipl = 0.030
158 self.PlaceObserverPoles([
159 self.rpl + 1j * self.ipl, self.rpl + 1j * self.ipl,
160 self.rpl - 1j * self.ipl, self.rpl - 1j * self.ipl
161 ])
Brian Silverman17f503e2015-08-02 18:17:18 -0700162
Tyler Chatow6738c362019-02-16 14:12:30 -0800163 # The box formed by U_min and U_max must encompass all possible values,
164 # or else Austin's code gets angry.
165 self.U_max = numpy.matrix([[12.0], [12.0]])
166 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700167
Tyler Chatow6738c362019-02-16 14:12:30 -0800168 # For the tests that check the limits, these are (upper, lower) for both
169 # claws.
170 self.hard_pos_limits = None
171 self.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700172
Tyler Chatow6738c362019-02-16 14:12:30 -0800173 # Compute the steady state velocities for a given applied voltage.
174 # The top and bottom of the claw should spin at the same rate if the
175 # physics is right.
176 X_ss = numpy.matrix([[0], [0], [0.0], [0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700177
Tyler Chatow6738c362019-02-16 14:12:30 -0800178 U = numpy.matrix([[1.0], [1.0]])
179 A = self.A
180 B = self.B
181 #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0]
182 X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0]
183 #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]
184 #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]
Ravago Jones5127ccc2022-07-31 16:32:45 -0700185 X_ss[3,
186 0] = 1 / (1 - A[3, 3]) * (X_ss[2, 0] * A[3, 2] +
187 B[3, 1] * U[1, 0] + B[3, 0] * U[0, 0])
Tyler Chatow6738c362019-02-16 14:12:30 -0800188 #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]
189 X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0]
190 X_ss[1, 0] = (A[1, 2] * X_ss[2, 0]) + (A[1, 3] * X_ss[3, 0]) + (
191 B[1, 0] * U[0, 0]) + (B[1, 1] * U[1, 0])
192
193 glog.debug('X_ss %s', str(X_ss))
194
195 self.InitializeState()
Brian Silverman17f503e2015-08-02 18:17:18 -0700196
197
198class ClawDeltaU(Claw):
Brian Silverman17f503e2015-08-02 18:17:18 -0700199
Tyler Chatow6738c362019-02-16 14:12:30 -0800200 def __init__(self, name="Claw"):
201 super(ClawDeltaU, self).__init__(name)
202 A_unaugmented = self.A
203 B_unaugmented = self.B
204 C_unaugmented = self.C
Brian Silverman17f503e2015-08-02 18:17:18 -0700205
Tyler Chatow6738c362019-02-16 14:12:30 -0800206 self.A = numpy.matrix([[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, 0.0],
209 [0.0, 0.0, 0.0, 0.0, 0.0],
210 [0.0, 0.0, 0.0, 0.0, 1.0]])
211 self.A[0:4, 0:4] = A_unaugmented
212 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700213
Tyler Chatow6738c362019-02-16 14:12:30 -0800214 self.B = numpy.matrix([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0],
215 [1.0, 0.0]])
216 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Brian Silverman17f503e2015-08-02 18:17:18 -0700217
Tyler Chatow6738c362019-02-16 14:12:30 -0800218 self.C = numpy.concatenate(
219 (C_unaugmented, numpy.matrix([[0.0], [0.0]])), axis=1)
220 self.D = numpy.matrix([[0.0, 0.0], [0.0, 0.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700221
Tyler Chatow6738c362019-02-16 14:12:30 -0800222 #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80])
223 self.Q = numpy.matrix([[(1.0 / (0.04**2.0)), 0.0, 0.0, 0.0, 0.0],
224 [0.0, (1.0 / (0.01**2)), 0.0, 0.0, 0.0],
225 [0.0, 0.0, 0.01, 0.0, 0.0],
226 [0.0, 0.0, 0.0, 0.08, 0.0],
227 [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0**2))]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700228
Tyler Chatow6738c362019-02-16 14:12:30 -0800229 self.R = numpy.matrix([[0.000001, 0.0], [0.0, 1.0 / (10.0**2.0)]])
230 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700231
Tyler Chatow6738c362019-02-16 14:12:30 -0800232 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
233 [50.0, 0.0, 10.0, 0.0, 1.0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700234
Tyler Chatow6738c362019-02-16 14:12:30 -0800235 controllability = controls.ctrb(self.A, self.B)
236 glog.debug('Rank of augmented controllability matrix: %d',
237 numpy.linalg.matrix_rank(controllability))
Brian Silverman17f503e2015-08-02 18:17:18 -0700238
Tyler Chatow6738c362019-02-16 14:12:30 -0800239 glog.debug('K')
240 glog.debug(str(self.K))
241 glog.debug('Placed controller poles are')
242 glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
243 glog.debug(
244 str([
245 numpy.abs(x)
246 for x in numpy.linalg.eig(self.A - self.B * self.K)[0]
247 ]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700248
Tyler Chatow6738c362019-02-16 14:12:30 -0800249 self.rpl = .05
250 self.ipl = 0.008
251 self.PlaceObserverPoles([
Ravago Jones5127ccc2022-07-31 16:32:45 -0700252 self.rpl + 1j * self.ipl, 0.10, 0.09, self.rpl - 1j * self.ipl,
253 0.90
Tyler Chatow6738c362019-02-16 14:12:30 -0800254 ])
255 #print "A is"
256 #print self.A
257 #print "L is"
258 #print self.L
259 #print "C is"
260 #print self.C
261 #print "A - LC is"
262 #print self.A - self.L * self.C
Brian Silverman17f503e2015-08-02 18:17:18 -0700263
Tyler Chatow6738c362019-02-16 14:12:30 -0800264 #print "Placed observer poles are"
265 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700266
Tyler Chatow6738c362019-02-16 14:12:30 -0800267 self.U_max = numpy.matrix([[12.0], [12.0]])
268 self.U_min = numpy.matrix([[-12.0], [-12.0]])
269
270 self.InitializeState()
271
Brian Silverman17f503e2015-08-02 18:17:18 -0700272
273def ScaleU(claw, U, K, error):
Tyler Chatow6738c362019-02-16 14:12:30 -0800274 """Clips U as necessary.
Brian Silverman17f503e2015-08-02 18:17:18 -0700275
276 Args:
Tyler Chatow6738c362019-02-16 14:12:30 -0800277 claw: claw object containing moments of inertia and U limits.
278 U: Input matrix to clip as necessary.
279 """
Brian Silverman17f503e2015-08-02 18:17:18 -0700280
Tyler Chatow6738c362019-02-16 14:12:30 -0800281 bottom_u = U[0, 0]
282 top_u = U[1, 0]
283 position_error = error[0:2, 0]
284 velocity_error = error[2:, 0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700285
Tyler Chatow6738c362019-02-16 14:12:30 -0800286 U_poly = polytope.HPolytope(
287 numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]),
288 numpy.matrix([[12], [12], [12], [12]]))
Brian Silverman17f503e2015-08-02 18:17:18 -0700289
Ravago Jones5127ccc2022-07-31 16:32:45 -0700290 if (bottom_u > claw.U_max[0, 0] or bottom_u < claw.U_min[0, 0]
291 or top_u > claw.U_max[0, 0] or top_u < claw.U_min[0, 0]):
Brian Silverman17f503e2015-08-02 18:17:18 -0700292
Tyler Chatow6738c362019-02-16 14:12:30 -0800293 position_K = K[:, 0:2]
294 velocity_K = K[:, 2:]
Brian Silverman17f503e2015-08-02 18:17:18 -0700295
Tyler Chatow6738c362019-02-16 14:12:30 -0800296 # H * U <= k
297 # U = UPos + UVel
298 # H * (UPos + UVel) <= k
299 # H * UPos <= k - H * UVel
300 #
301 # Now, we can do a coordinate transformation and say the following.
302 #
303 # UPos = position_K * position_error
304 # (H * position_K) * position_error <= k - H * UVel
305 #
306 # Add in the constraint that 0 <= t <= 1
307 # Now, there are 2 ways this can go. Either we have a region, or we don't
308 # have a region. If we have a region, then pick the largest t and go for it.
309 # If we don't have a region, we need to pick a good comprimise.
Brian Silverman17f503e2015-08-02 18:17:18 -0700310
Tyler Chatow6738c362019-02-16 14:12:30 -0800311 pos_poly = polytope.HPolytope(
312 U_poly.H * position_K,
313 U_poly.k - U_poly.H * velocity_K * velocity_error)
Brian Silverman17f503e2015-08-02 18:17:18 -0700314
Tyler Chatow6738c362019-02-16 14:12:30 -0800315 # The actual angle for the line we call 45.
316 angle_45 = numpy.matrix([[numpy.sqrt(3), 1]])
317 if claw.pos_limits and claw.hard_pos_limits and (
318 claw.X[0, 0] + claw.X[1, 0]) > claw.pos_limits[1]:
319 angle_45 = numpy.matrix([[1, 1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700320
Tyler Chatow6738c362019-02-16 14:12:30 -0800321 P = position_error
322 L45 = numpy.multiply(
323 numpy.matrix([[numpy.sign(P[1, 0]), -numpy.sign(P[0, 0])]]),
324 angle_45)
325 if L45[0, 1] == 0:
326 L45[0, 1] = 1
327 if L45[0, 0] == 0:
328 L45[0, 0] = 1
329 w45 = numpy.matrix([[0]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700330
Tyler Chatow6738c362019-02-16 14:12:30 -0800331 if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]):
332 LH = numpy.matrix([[0, 1]])
Brian Silverman17f503e2015-08-02 18:17:18 -0700333 else:
Tyler Chatow6738c362019-02-16 14:12:30 -0800334 LH = numpy.matrix([[1, 0]])
335 wh = LH * P
336 standard = numpy.concatenate((L45, LH))
337 W = numpy.concatenate((w45, wh))
338 intersection = numpy.linalg.inv(standard) * W
339 adjusted_pos_error_h, is_inside_h = polydrivetrain.DoCoerceGoal(
340 pos_poly, LH, wh, position_error)
341 adjusted_pos_error_45, is_inside_45 = polydrivetrain.DoCoerceGoal(
342 pos_poly, L45, w45, intersection)
343 if pos_poly.IsInside(intersection):
344 adjusted_pos_error = adjusted_pos_error_h
345 else:
346 if is_inside_h:
347 if numpy.linalg.norm(adjusted_pos_error_h) > numpy.linalg.norm(
348 adjusted_pos_error_45):
349 adjusted_pos_error = adjusted_pos_error_h
350 else:
351 adjusted_pos_error = adjusted_pos_error_45
352 else:
353 adjusted_pos_error = adjusted_pos_error_45
354 #print adjusted_pos_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700355
Tyler Chatow6738c362019-02-16 14:12:30 -0800356 #print "Actual power is ", velocity_K * velocity_error + position_K * adjusted_pos_error
357 return velocity_K * velocity_error + position_K * adjusted_pos_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700358
Tyler Chatow6738c362019-02-16 14:12:30 -0800359 #U = Kpos * poserror + Kvel * velerror
Brian Silverman17f503e2015-08-02 18:17:18 -0700360
Tyler Chatow6738c362019-02-16 14:12:30 -0800361 #scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u))
Brian Silverman17f503e2015-08-02 18:17:18 -0700362
Tyler Chatow6738c362019-02-16 14:12:30 -0800363 #top_u *= scalar
364 #bottom_u *= scalar
Brian Silverman17f503e2015-08-02 18:17:18 -0700365
Tyler Chatow6738c362019-02-16 14:12:30 -0800366 return numpy.matrix([[bottom_u], [top_u]])
367
368
369def run_test(claw,
370 initial_X,
371 goal,
372 max_separation_error=0.01,
373 show_graph=True,
374 iterations=200):
375 """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 -0700376
Austin Schuh085eab92020-11-26 13:54:51 -0800377 The tests themselves are not terribly sophisticated; I just test for
Brian Silverman17f503e2015-08-02 18:17:18 -0700378 whether the goal has been reached and whether the separation goes
379 outside of the initial and goal values by more than max_separation_error.
380 Prints out something for a failure of either condition and returns
381 False if tests fail.
Tyler Chatow6738c362019-02-16 14:12:30 -0800382
Brian Silverman17f503e2015-08-02 18:17:18 -0700383 Args:
Tyler Chatow6738c362019-02-16 14:12:30 -0800384 claw: claw object to use.
385 initial_X: starting state.
386 goal: goal state.
387 show_graph: Whether or not to display a graph showing the changing
388 states and voltages.
389 iterations: Number of timesteps to run the model for."""
Brian Silverman17f503e2015-08-02 18:17:18 -0700390
Tyler Chatow6738c362019-02-16 14:12:30 -0800391 claw.X = initial_X
Brian Silverman17f503e2015-08-02 18:17:18 -0700392
Tyler Chatow6738c362019-02-16 14:12:30 -0800393 # Various lists for graphing things.
394 t = []
395 x_bottom = []
396 x_top = []
397 u_bottom = []
398 u_top = []
399 x_separation = []
Brian Silverman17f503e2015-08-02 18:17:18 -0700400
Tyler Chatow6738c362019-02-16 14:12:30 -0800401 tests_passed = True
Brian Silverman17f503e2015-08-02 18:17:18 -0700402
Tyler Chatow6738c362019-02-16 14:12:30 -0800403 # Bounds which separation should not exceed.
404 lower_bound = (initial_X[1, 0] if initial_X[1, 0] < goal[1, 0] else
405 goal[1, 0]) - max_separation_error
406 upper_bound = (initial_X[1, 0] if initial_X[1, 0] > goal[1, 0] else
407 goal[1, 0]) + max_separation_error
Brian Silverman17f503e2015-08-02 18:17:18 -0700408
Austin Schuh5ea48472021-02-02 20:46:41 -0800409 for i in range(iterations):
Tyler Chatow6738c362019-02-16 14:12:30 -0800410 U = claw.K * (goal - claw.X)
411 U = ScaleU(claw, U, claw.K, goal - claw.X)
412 claw.Update(U)
Brian Silverman17f503e2015-08-02 18:17:18 -0700413
Tyler Chatow6738c362019-02-16 14:12:30 -0800414 if claw.X[1, 0] > upper_bound or claw.X[1, 0] < lower_bound:
415 tests_passed = False
416 glog.info('Claw separation was %f', claw.X[1, 0])
417 glog.info("Should have been between", lower_bound, "and",
418 upper_bound)
Brian Silverman17f503e2015-08-02 18:17:18 -0700419
Tyler Chatow6738c362019-02-16 14:12:30 -0800420 if claw.hard_pos_limits and \
421 (claw.X[0, 0] > claw.hard_pos_limits[1] or
422 claw.X[0, 0] < claw.hard_pos_limits[0] or
423 claw.X[0, 0] + claw.X[1, 0] > claw.hard_pos_limits[1] or
424 claw.X[0, 0] + claw.X[1, 0] < claw.hard_pos_limits[0]):
425 tests_passed = False
426 glog.info('Claws at %f and %f', claw.X[0, 0],
427 claw.X[0, 0] + claw.X[1, 0])
428 glog.info("Both should be in %s, definitely %s", claw.pos_limits,
429 claw.hard_pos_limits)
Brian Silverman17f503e2015-08-02 18:17:18 -0700430
Tyler Chatow6738c362019-02-16 14:12:30 -0800431 t.append(i * claw.dt)
432 x_bottom.append(claw.X[0, 0] * 10.0)
433 x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10.0)
434 u_bottom.append(U[0, 0])
435 u_top.append(U[1, 0])
436 x_separation.append(claw.X[1, 0] * 10.0)
Brian Silverman17f503e2015-08-02 18:17:18 -0700437
Tyler Chatow6738c362019-02-16 14:12:30 -0800438 if show_graph:
439 pylab.plot(t, x_bottom, label='x bottom * 10')
440 pylab.plot(t, x_top, label='x top * 10')
441 pylab.plot(t, u_bottom, label='u bottom')
442 pylab.plot(t, u_top, label='u top')
443 pylab.plot(t, x_separation, label='separation * 10')
444 pylab.legend()
445 pylab.show()
Brian Silverman17f503e2015-08-02 18:17:18 -0700446
Tyler Chatow6738c362019-02-16 14:12:30 -0800447 # Test to make sure that we are near the goal.
448 if numpy.max(abs(claw.X - goal)) > 1e-4:
449 tests_passed = False
450 glog.error('X was %s Expected %s', str(claw.X), str(goal))
Brian Silverman17f503e2015-08-02 18:17:18 -0700451
Tyler Chatow6738c362019-02-16 14:12:30 -0800452 return tests_passed
453
Brian Silverman17f503e2015-08-02 18:17:18 -0700454
455def main(argv):
Tyler Chatow6738c362019-02-16 14:12:30 -0800456 argv = FLAGS(argv)
Austin Schuha3b42552015-11-27 16:30:12 -0800457
Tyler Chatow6738c362019-02-16 14:12:30 -0800458 claw = Claw()
459 if FLAGS.plot:
460 # Test moving the claw with constant separation.
461 initial_X = numpy.matrix([[-1.0], [0.0], [0.0], [0.0]])
462 R = numpy.matrix([[1.0], [0.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 just changing separation.
466 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
467 R = numpy.matrix([[0.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 changing both separation and position at once.
471 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
472 R = numpy.matrix([[1.0], [1.0], [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([[2.0], [0.05], [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 a small separation error and a large position one.
481 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
482 R = numpy.matrix([[-0.5], [1.0], [0.0], [0.0]])
483 run_test(claw, initial_X, R)
Brian Silverman17f503e2015-08-02 18:17:18 -0700484
Tyler Chatow6738c362019-02-16 14:12:30 -0800485 # Test opening with the top claw at the limit.
486 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
487 R = numpy.matrix([[-1.5], [1.5], [0.0], [0.0]])
488 claw.hard_pos_limits = (-1.6, 0.1)
489 claw.pos_limits = (-1.5, 0.0)
490 run_test(claw, initial_X, R)
491 claw.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700492
Tyler Chatow6738c362019-02-16 14:12:30 -0800493 # Test opening with the bottom claw at the limit.
494 initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
495 R = numpy.matrix([[0], [1.5], [0.0], [0.0]])
496 claw.hard_pos_limits = (-0.1, 1.6)
497 claw.pos_limits = (0.0, 1.6)
498 run_test(claw, initial_X, R)
499 claw.pos_limits = None
Brian Silverman17f503e2015-08-02 18:17:18 -0700500
Tyler Chatow6738c362019-02-16 14:12:30 -0800501 # Write the generated constants out to a file.
502 if len(argv) != 3:
503 glog.fatal('Expected .h file name and .cc file name for the claw.')
504 else:
505 namespaces = ['y2014', 'control_loops', 'claw']
506 claw = Claw('Claw')
Ravago Jones5127ccc2022-07-31 16:32:45 -0700507 loop_writer = control_loop.ControlLoopWriter('Claw', [claw],
508 namespaces=namespaces)
Tyler Chatow6738c362019-02-16 14:12:30 -0800509 loop_writer.AddConstant(
510 control_loop.Constant('kClawMomentOfInertiaRatio', '%f',
511 claw.J_top / claw.J_bottom))
512 loop_writer.AddConstant(control_loop.Constant('kDt', '%f', claw.dt))
513 loop_writer.Write(argv[1], argv[2])
514
Brian Silverman17f503e2015-08-02 18:17:18 -0700515
516if __name__ == '__main__':
Tyler Chatow6738c362019-02-16 14:12:30 -0800517 sys.exit(main(sys.argv))