Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Austin Schuh | edc317c | 2015-11-08 14:07:42 -0800 | [diff] [blame] | 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
| 5 | from frc971.control_loops.python import polytope |
| 6 | from y2014.control_loops.python import polydrivetrain |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 7 | import numpy |
| 8 | import sys |
| 9 | from matplotlib import pylab |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 10 | import gflags |
| 11 | import glog |
| 12 | |
| 13 | FLAGS = gflags.FLAGS |
| 14 | |
| 15 | try: |
| 16 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
| 17 | except gflags.DuplicateFlagError: |
| 18 | pass |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 19 | |
| 20 | class Claw(control_loop.ControlLoop): |
| 21 | def __init__(self, name="RawClaw"): |
| 22 | super(Claw, self).__init__(name) |
| 23 | # Stall Torque in N m |
| 24 | self.stall_torque = 2.42 |
| 25 | # Stall Current in Amps |
| 26 | self.stall_current = 133 |
| 27 | # Free Speed in RPM |
| 28 | self.free_speed = 5500.0 |
| 29 | # Free Current in Amps |
| 30 | self.free_current = 2.7 |
| 31 | # Moment of inertia of the claw in kg m^2 |
| 32 | self.J_top = 2.8 |
| 33 | self.J_bottom = 3.0 |
| 34 | |
| 35 | # Resistance of the motor |
| 36 | self.R = 12.0 / self.stall_current |
| 37 | # Motor velocity constant |
| 38 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 39 | (13.5 - self.R * self.free_current)) |
| 40 | # Torque constant |
| 41 | self.Kt = self.stall_torque / self.stall_current |
| 42 | # Gear ratio |
| 43 | self.G = 14.0 / 48.0 * 18.0 / 32.0 * 18.0 / 66.0 * 12.0 / 60.0 |
| 44 | # Control loop time step |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 45 | self.dt = 0.005 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 46 | |
| 47 | # State is [bottom position, bottom velocity, top - bottom position, |
| 48 | # top - bottom velocity] |
| 49 | # Input is [bottom power, top power - bottom power * J_top / J_bottom] |
| 50 | # Motor time constants. difference_bottom refers to the constant for how the |
| 51 | # bottom velocity affects the difference of the top and bottom velocities. |
| 52 | self.common_motor_constant = -self.Kt / self.Kv / (self.G * self.G * self.R) |
| 53 | self.bottom_bottom = self.common_motor_constant / self.J_bottom |
| 54 | self.difference_bottom = -self.common_motor_constant * (1 / self.J_bottom |
| 55 | - 1 / self.J_top) |
| 56 | self.difference_difference = self.common_motor_constant / self.J_top |
| 57 | # State feedback matrices |
| 58 | |
| 59 | self.A_continuous = numpy.matrix( |
| 60 | [[0, 0, 1, 0], |
| 61 | [0, 0, 0, 1], |
| 62 | [0, 0, self.bottom_bottom, 0], |
| 63 | [0, 0, self.difference_bottom, self.difference_difference]]) |
| 64 | |
| 65 | self.A_bottom_cont = numpy.matrix( |
| 66 | [[0, 1], |
| 67 | [0, self.bottom_bottom]]) |
| 68 | |
| 69 | self.A_diff_cont = numpy.matrix( |
| 70 | [[0, 1], |
| 71 | [0, self.difference_difference]]) |
| 72 | |
| 73 | self.motor_feedforward = self.Kt / (self.G * self.R) |
| 74 | self.motor_feedforward_bottom = self.motor_feedforward / self.J_bottom |
| 75 | self.motor_feedforward_difference = self.motor_feedforward / self.J_top |
| 76 | self.motor_feedforward_difference_bottom = ( |
| 77 | self.motor_feedforward * (1 / self.J_bottom - 1 / self.J_top)) |
| 78 | self.B_continuous = numpy.matrix( |
| 79 | [[0, 0], |
| 80 | [0, 0], |
| 81 | [self.motor_feedforward_bottom, 0], |
| 82 | [-self.motor_feedforward_bottom, self.motor_feedforward_difference]]) |
| 83 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 84 | glog.debug('Cont X_ss %f', self.motor_feedforward / -self.common_motor_constant) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 85 | |
| 86 | self.B_bottom_cont = numpy.matrix( |
| 87 | [[0], |
| 88 | [self.motor_feedforward_bottom]]) |
| 89 | |
| 90 | self.B_diff_cont = numpy.matrix( |
| 91 | [[0], |
| 92 | [self.motor_feedforward_difference]]) |
| 93 | |
| 94 | self.C = numpy.matrix([[1, 0, 0, 0], |
| 95 | [1, 1, 0, 0]]) |
| 96 | self.D = numpy.matrix([[0, 0], |
| 97 | [0, 0]]) |
| 98 | |
| 99 | self.A, self.B = self.ContinuousToDiscrete( |
| 100 | self.A_continuous, self.B_continuous, self.dt) |
| 101 | |
| 102 | self.A_bottom, self.B_bottom = controls.c2d( |
| 103 | self.A_bottom_cont, self.B_bottom_cont, self.dt) |
| 104 | self.A_diff, self.B_diff = controls.c2d( |
| 105 | self.A_diff_cont, self.B_diff_cont, self.dt) |
| 106 | |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 107 | self.K_bottom = controls.dplace(self.A_bottom, self.B_bottom, |
| 108 | [0.87 + 0.05j, 0.87 - 0.05j]) |
| 109 | self.K_diff = controls.dplace(self.A_diff, self.B_diff, |
| 110 | [0.85 + 0.05j, 0.85 - 0.05j]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 111 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 112 | glog.debug('K_diff %s', str(self.K_diff)) |
| 113 | glog.debug('K_bottom %s', str(self.K_bottom)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 114 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 115 | glog.debug('A') |
| 116 | glog.debug(self.A) |
| 117 | glog.debug('B') |
| 118 | glog.debug(self.B) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | self.Q = numpy.matrix([[(1.0 / (0.10 ** 2.0)), 0.0, 0.0, 0.0], |
| 122 | [0.0, (1.0 / (0.06 ** 2.0)), 0.0, 0.0], |
| 123 | [0.0, 0.0, 0.10, 0.0], |
| 124 | [0.0, 0.0, 0.0, 0.1]]) |
| 125 | |
| 126 | self.R = numpy.matrix([[(1.0 / (40.0 ** 2.0)), 0.0], |
| 127 | [0.0, (1.0 / (5.0 ** 2.0))]]) |
| 128 | #self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 129 | |
| 130 | self.K = numpy.matrix([[self.K_bottom[0, 0], 0.0, self.K_bottom[0, 1], 0.0], |
| 131 | [0.0, self.K_diff[0, 0], 0.0, self.K_diff[0, 1]]]) |
| 132 | |
| 133 | # Compute the feed forwards aceleration term. |
| 134 | self.K[1, 0] = -self.B[1, 0] * self.K[0, 0] / self.B[1, 1] |
| 135 | |
| 136 | lstsq_A = numpy.identity(2) |
| 137 | lstsq_A[0, :] = self.B[1, :] |
| 138 | lstsq_A[1, :] = self.B[3, :] |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 139 | glog.debug('System of Equations coefficients:') |
| 140 | glog.debug(str(lstsq_A)) |
| 141 | glog.debug('det %s', str(numpy.linalg.det(lstsq_A))) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 142 | |
| 143 | out_x = numpy.linalg.lstsq( |
| 144 | lstsq_A, |
| 145 | numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]))[0] |
| 146 | self.K[1, 2] = -lstsq_A[0, 0] * (self.K[0, 2] - out_x[0]) / lstsq_A[0, 1] + out_x[1] |
| 147 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 148 | glog.debug('K unaugmented') |
| 149 | glog.debug(str(self.K)) |
| 150 | glog.debug('B * K unaugmented') |
| 151 | glog.debug(str(self.B * self.K)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 152 | F = self.A - self.B * self.K |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 153 | glog.debug('A - B * K unaugmented') |
| 154 | glog.debug(str(F)) |
| 155 | glog.debug('eigenvalues') |
| 156 | glog.debug(str(numpy.linalg.eig(F)[0])) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 157 | |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 158 | self.rpl = .09 |
| 159 | self.ipl = 0.030 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 160 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 161 | self.rpl + 1j * self.ipl, |
| 162 | self.rpl - 1j * self.ipl, |
| 163 | self.rpl - 1j * self.ipl]) |
| 164 | |
| 165 | # The box formed by U_min and U_max must encompass all possible values, |
| 166 | # or else Austin's code gets angry. |
| 167 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 168 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
| 169 | |
| 170 | # For the tests that check the limits, these are (upper, lower) for both |
| 171 | # claws. |
| 172 | self.hard_pos_limits = None |
| 173 | self.pos_limits = None |
| 174 | |
| 175 | # Compute the steady state velocities for a given applied voltage. |
| 176 | # The top and bottom of the claw should spin at the same rate if the |
| 177 | # physics is right. |
| 178 | X_ss = numpy.matrix([[0], [0], [0.0], [0]]) |
| 179 | |
| 180 | U = numpy.matrix([[1.0],[1.0]]) |
| 181 | A = self.A |
| 182 | B = self.B |
| 183 | #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0] |
| 184 | X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0] |
| 185 | #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] |
| 186 | #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] |
| 187 | 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]) |
| 188 | #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] + B[1, 0] * U[0, 0] + B[1, 1] * U[1, 0] |
| 191 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 192 | glog.debug('X_ss %s', str(X_ss)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 193 | |
| 194 | self.InitializeState() |
| 195 | |
| 196 | |
| 197 | class ClawDeltaU(Claw): |
| 198 | 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 |
| 203 | |
| 204 | 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] |
| 211 | |
| 212 | self.B = numpy.matrix([[0.0, 0.0], |
| 213 | [0.0, 0.0], |
| 214 | [0.0, 0.0], |
| 215 | [0.0, 0.0], |
| 216 | [1.0, 0.0]]) |
| 217 | self.B[0:4, 1] = B_unaugmented[0:4, 1] |
| 218 | |
| 219 | self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])), |
| 220 | axis=1) |
| 221 | self.D = numpy.matrix([[0.0, 0.0], |
| 222 | [0.0, 0.0]]) |
| 223 | |
| 224 | #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80]) |
| 225 | self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0], |
| 226 | [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0], |
| 227 | [0.0, 0.0, 0.01, 0.0, 0.0], |
| 228 | [0.0, 0.0, 0.0, 0.08, 0.0], |
| 229 | [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]]) |
| 230 | |
| 231 | self.R = numpy.matrix([[0.000001, 0.0], |
| 232 | [0.0, 1.0 / (10.0 ** 2.0)]]) |
| 233 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 234 | |
| 235 | self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0], |
| 236 | [50.0, 0.0, 10.0, 0.0, 1.0]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 237 | |
Brian Silverman | 4e55e58 | 2015-11-10 14:16:37 -0500 | [diff] [blame] | 238 | controlability = controls.ctrb(self.A, self.B) |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 239 | glog.debug('Rank of augmented controlability matrix: %d', |
| 240 | numpy.linalg.matrix_rank(controlability)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 241 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 242 | glog.debug('K') |
| 243 | glog.debug(str(self.K)) |
| 244 | glog.debug('Placed controller poles are') |
| 245 | glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
| 246 | glog.debug(str([numpy.abs(x) for x in |
| 247 | numpy.linalg.eig(self.A - self.B * self.K)[0]])) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 248 | |
| 249 | self.rpl = .05 |
| 250 | self.ipl = 0.008 |
| 251 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09, |
| 252 | self.rpl - 1j * self.ipl, 0.90]) |
| 253 | #print "A is" |
| 254 | #print self.A |
| 255 | #print "L is" |
| 256 | #print self.L |
| 257 | #print "C is" |
| 258 | #print self.C |
| 259 | #print "A - LC is" |
| 260 | #print self.A - self.L * self.C |
| 261 | |
| 262 | #print "Placed observer poles are" |
| 263 | #print numpy.linalg.eig(self.A - self.L * self.C)[0] |
| 264 | |
| 265 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 266 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
| 267 | |
| 268 | self.InitializeState() |
| 269 | |
| 270 | def ScaleU(claw, U, K, error): |
| 271 | """Clips U as necessary. |
| 272 | |
| 273 | Args: |
| 274 | claw: claw object containing moments of inertia and U limits. |
| 275 | U: Input matrix to clip as necessary. |
| 276 | """ |
| 277 | |
| 278 | bottom_u = U[0, 0] |
| 279 | top_u = U[1, 0] |
| 280 | position_error = error[0:2, 0] |
| 281 | velocity_error = error[2:, 0] |
| 282 | |
| 283 | U_poly = polytope.HPolytope( |
| 284 | numpy.matrix([[1, 0], |
| 285 | [-1, 0], |
| 286 | [0, 1], |
| 287 | [0, -1]]), |
| 288 | numpy.matrix([[12], |
| 289 | [12], |
| 290 | [12], |
| 291 | [12]])) |
| 292 | |
| 293 | if (bottom_u > claw.U_max[0, 0] or bottom_u < claw.U_min[0, 0] or |
| 294 | top_u > claw.U_max[0, 0] or top_u < claw.U_min[0, 0]): |
| 295 | |
| 296 | position_K = K[:, 0:2] |
| 297 | velocity_K = K[:, 2:] |
| 298 | |
| 299 | # H * U <= k |
| 300 | # U = UPos + UVel |
| 301 | # H * (UPos + UVel) <= k |
| 302 | # H * UPos <= k - H * UVel |
| 303 | # |
| 304 | # Now, we can do a coordinate transformation and say the following. |
| 305 | # |
| 306 | # UPos = position_K * position_error |
| 307 | # (H * position_K) * position_error <= k - H * UVel |
| 308 | # |
| 309 | # Add in the constraint that 0 <= t <= 1 |
| 310 | # Now, there are 2 ways this can go. Either we have a region, or we don't |
| 311 | # have a region. If we have a region, then pick the largest t and go for it. |
| 312 | # If we don't have a region, we need to pick a good comprimise. |
| 313 | |
| 314 | pos_poly = polytope.HPolytope( |
| 315 | U_poly.H * position_K, |
| 316 | U_poly.k - U_poly.H * velocity_K * velocity_error) |
| 317 | |
| 318 | # The actual angle for the line we call 45. |
| 319 | angle_45 = numpy.matrix([[numpy.sqrt(3), 1]]) |
| 320 | if claw.pos_limits and claw.hard_pos_limits and claw.X[0, 0] + claw.X[1, 0] > claw.pos_limits[1]: |
| 321 | angle_45 = numpy.matrix([[1, 1]]) |
| 322 | |
| 323 | P = position_error |
| 324 | L45 = numpy.multiply(numpy.matrix([[numpy.sign(P[1, 0]), -numpy.sign(P[0, 0])]]), 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]]) |
| 330 | |
| 331 | if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]): |
| 332 | LH = numpy.matrix([[0, 1]]) |
| 333 | else: |
| 334 | 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(pos_poly, |
| 340 | LH, wh, position_error) |
| 341 | adjusted_pos_error_45, is_inside_45 = polydrivetrain.DoCoerceGoal(pos_poly, |
| 342 | 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(adjusted_pos_error_45): |
| 348 | adjusted_pos_error = adjusted_pos_error_h |
| 349 | else: |
| 350 | adjusted_pos_error = adjusted_pos_error_45 |
| 351 | else: |
| 352 | adjusted_pos_error = adjusted_pos_error_45 |
| 353 | #print adjusted_pos_error |
| 354 | |
| 355 | #print "Actual power is ", velocity_K * velocity_error + position_K * adjusted_pos_error |
| 356 | return velocity_K * velocity_error + position_K * adjusted_pos_error |
| 357 | |
| 358 | #U = Kpos * poserror + Kvel * velerror |
| 359 | |
| 360 | #scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u)) |
| 361 | |
| 362 | #top_u *= scalar |
| 363 | #bottom_u *= scalar |
| 364 | |
| 365 | return numpy.matrix([[bottom_u], [top_u]]) |
| 366 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 367 | def run_test(claw, initial_X, goal, max_separation_error=0.01, show_graph=True, iterations=200): |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 368 | """Runs the claw plant on a given claw (claw) with an initial condition (initial_X) and goal (goal). |
| 369 | |
| 370 | The tests themselves are not terribly sophisticated; I just test for |
| 371 | whether the goal has been reached and whether the separation goes |
| 372 | outside of the initial and goal values by more than max_separation_error. |
| 373 | Prints out something for a failure of either condition and returns |
| 374 | False if tests fail. |
| 375 | Args: |
| 376 | claw: claw object to use. |
| 377 | initial_X: starting state. |
| 378 | goal: goal state. |
| 379 | show_graph: Whether or not to display a graph showing the changing |
| 380 | states and voltages. |
| 381 | iterations: Number of timesteps to run the model for.""" |
| 382 | |
| 383 | claw.X = initial_X |
| 384 | |
| 385 | # Various lists for graphing things. |
| 386 | t = [] |
| 387 | x_bottom = [] |
| 388 | x_top = [] |
| 389 | u_bottom = [] |
| 390 | u_top = [] |
| 391 | x_separation = [] |
| 392 | |
| 393 | tests_passed = True |
| 394 | |
| 395 | # Bounds which separation should not exceed. |
| 396 | lower_bound = (initial_X[1, 0] if initial_X[1, 0] < goal[1, 0] |
| 397 | else goal[1, 0]) - max_separation_error |
| 398 | upper_bound = (initial_X[1, 0] if initial_X[1, 0] > goal[1, 0] |
| 399 | else goal[1, 0]) + max_separation_error |
| 400 | |
| 401 | for i in xrange(iterations): |
| 402 | U = claw.K * (goal - claw.X) |
| 403 | U = ScaleU(claw, U, claw.K, goal - claw.X) |
| 404 | claw.Update(U) |
| 405 | |
| 406 | if claw.X[1, 0] > upper_bound or claw.X[1, 0] < lower_bound: |
| 407 | tests_passed = False |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 408 | glog.info('Claw separation was %f', claw.X[1, 0]) |
| 409 | glog.info("Should have been between", lower_bound, "and", upper_bound) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 410 | |
| 411 | if claw.hard_pos_limits and \ |
| 412 | (claw.X[0, 0] > claw.hard_pos_limits[1] or |
| 413 | claw.X[0, 0] < claw.hard_pos_limits[0] or |
| 414 | claw.X[0, 0] + claw.X[1, 0] > claw.hard_pos_limits[1] or |
| 415 | claw.X[0, 0] + claw.X[1, 0] < claw.hard_pos_limits[0]): |
| 416 | tests_passed = False |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 417 | glog.info('Claws at %f and %f', claw.X[0, 0], claw.X[0, 0] + claw.X[1, 0]) |
| 418 | glog.info("Both should be in %s, definitely %s", |
| 419 | claw.pos_limits, claw.hard_pos_limits) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 420 | |
| 421 | t.append(i * claw.dt) |
| 422 | x_bottom.append(claw.X[0, 0] * 10.0) |
| 423 | x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10.0) |
| 424 | u_bottom.append(U[0, 0]) |
| 425 | u_top.append(U[1, 0]) |
| 426 | x_separation.append(claw.X[1, 0] * 10.0) |
| 427 | |
| 428 | if show_graph: |
| 429 | pylab.plot(t, x_bottom, label='x bottom * 10') |
| 430 | pylab.plot(t, x_top, label='x top * 10') |
| 431 | pylab.plot(t, u_bottom, label='u bottom') |
| 432 | pylab.plot(t, u_top, label='u top') |
| 433 | pylab.plot(t, x_separation, label='separation * 10') |
| 434 | pylab.legend() |
| 435 | pylab.show() |
| 436 | |
| 437 | # Test to make sure that we are near the goal. |
| 438 | if numpy.max(abs(claw.X - goal)) > 1e-4: |
| 439 | tests_passed = False |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 440 | glog.error('X was %s Expected %s', str(claw.X), str(goal)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 441 | |
| 442 | return tests_passed |
| 443 | |
| 444 | def main(argv): |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 445 | argv = FLAGS(argv) |
| 446 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 447 | claw = Claw() |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 448 | if FLAGS.plot: |
| 449 | # Test moving the claw with constant separation. |
| 450 | initial_X = numpy.matrix([[-1.0], [0.0], [0.0], [0.0]]) |
| 451 | R = numpy.matrix([[1.0], [0.0], [0.0], [0.0]]) |
| 452 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 453 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 454 | # Test just changing separation. |
| 455 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 456 | R = numpy.matrix([[0.0], [1.0], [0.0], [0.0]]) |
| 457 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 458 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 459 | # Test changing both separation and position at once. |
| 460 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 461 | R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]]) |
| 462 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 463 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 464 | # Test a small separation error and a large position one. |
| 465 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 466 | R = numpy.matrix([[2.0], [0.05], [0.0], [0.0]]) |
| 467 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 468 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 469 | # Test a small separation error and a large position one. |
| 470 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 471 | R = numpy.matrix([[-0.5], [1.0], [0.0], [0.0]]) |
| 472 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 473 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 474 | # Test opening with the top claw at the limit. |
| 475 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 476 | R = numpy.matrix([[-1.5], [1.5], [0.0], [0.0]]) |
| 477 | claw.hard_pos_limits = (-1.6, 0.1) |
| 478 | claw.pos_limits = (-1.5, 0.0) |
| 479 | run_test(claw, initial_X, R) |
| 480 | claw.pos_limits = None |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 481 | |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 482 | # Test opening with the bottom claw at the limit. |
| 483 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 484 | R = numpy.matrix([[0], [1.5], [0.0], [0.0]]) |
| 485 | claw.hard_pos_limits = (-0.1, 1.6) |
| 486 | claw.pos_limits = (0.0, 1.6) |
| 487 | run_test(claw, initial_X, R) |
| 488 | claw.pos_limits = None |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 489 | |
| 490 | # Write the generated constants out to a file. |
| 491 | if len(argv) != 3: |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 492 | glog.fatal('Expected .h file name and .cc file name for the claw.') |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 493 | else: |
Austin Schuh | edc317c | 2015-11-08 14:07:42 -0800 | [diff] [blame] | 494 | namespaces = ['y2014', 'control_loops', 'claw'] |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 495 | claw = Claw('Claw') |
| 496 | loop_writer = control_loop.ControlLoopWriter('Claw', [claw], |
Austin Schuh | edc317c | 2015-11-08 14:07:42 -0800 | [diff] [blame] | 497 | namespaces=namespaces) |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 498 | loop_writer.AddConstant(control_loop.Constant('kClawMomentOfInertiaRatio', |
| 499 | '%f', claw.J_top / claw.J_bottom)) |
| 500 | loop_writer.AddConstant(control_loop.Constant('kDt', '%f', |
Austin Schuh | 0e99773 | 2015-11-08 15:14:53 -0800 | [diff] [blame] | 501 | claw.dt)) |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame^] | 502 | loop_writer.Write(argv[1], argv[2]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 503 | |
| 504 | if __name__ == '__main__': |
| 505 | sys.exit(main(sys.argv)) |