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