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: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 16 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame] | 17 | except gflags.DuplicateFlagError: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 18 | pass |
| 19 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 20 | |
| 21 | class Claw(control_loop.ControlLoop): |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 22 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 23 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 36 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 37 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 48 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 49 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 61 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 62 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 65 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 66 | self.A_bottom_cont = numpy.matrix([[0, 1], [0, self.bottom_bottom]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 67 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 68 | self.A_diff_cont = numpy.matrix([[0, 1], |
| 69 | [0, self.difference_difference]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 70 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 71 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 82 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 83 | glog.debug('Cont X_ss %f', |
| 84 | self.motor_feedforward / -self.common_motor_constant) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 85 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 86 | self.B_bottom_cont = numpy.matrix([[0], |
| 87 | [self.motor_feedforward_bottom]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 88 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 89 | self.B_diff_cont = numpy.matrix([[0], |
| 90 | [self.motor_feedforward_difference]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 91 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 92 | self.C = numpy.matrix([[1, 0, 0, 0], [1, 1, 0, 0]]) |
| 93 | self.D = numpy.matrix([[0, 0], [0, 0]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 94 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 95 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 96 | self.B_continuous, self.dt) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 97 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 98 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 102 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 103 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 107 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 108 | glog.debug('K_diff %s', str(self.K_diff)) |
| 109 | glog.debug('K_bottom %s', str(self.K_bottom)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 110 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 111 | glog.debug('A') |
| 112 | glog.debug(self.A) |
| 113 | glog.debug('B') |
| 114 | glog.debug(self.B) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 115 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 116 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 119 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 120 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 123 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 124 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 127 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 128 | # Compute the feed forwards aceleration term. |
| 129 | self.K[1, 0] = -self.B[1, 0] * self.K[0, 0] / self.B[1, 1] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 130 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 131 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 137 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 138 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 142 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 143 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 152 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 153 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 159 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 160 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 164 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 165 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 169 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 170 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 174 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 175 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 192 | |
| 193 | |
| 194 | class ClawDeltaU(Claw): |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 195 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 196 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 201 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 202 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 209 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 210 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 213 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 214 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 217 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 218 | #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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 224 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 225 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 227 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 228 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 230 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 231 | controllability = controls.ctrb(self.A, self.B) |
| 232 | glog.debug('Rank of augmented controllability matrix: %d', |
| 233 | numpy.linalg.matrix_rank(controllability)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 234 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 235 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 244 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 245 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 258 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 259 | #print "Placed observer poles are" |
| 260 | #print numpy.linalg.eig(self.A - self.L * self.C)[0] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 261 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 262 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 267 | |
| 268 | def ScaleU(claw, U, K, error): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 269 | """Clips U as necessary. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 270 | |
| 271 | Args: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 272 | claw: claw object containing moments of inertia and U limits. |
| 273 | U: Input matrix to clip as necessary. |
| 274 | """ |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 275 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 276 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 280 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 281 | U_poly = polytope.HPolytope( |
| 282 | numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]), |
| 283 | numpy.matrix([[12], [12], [12], [12]])) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 284 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 285 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 287 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 288 | position_K = K[:, 0:2] |
| 289 | velocity_K = K[:, 2:] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 290 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 291 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 305 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 306 | pos_poly = polytope.HPolytope( |
| 307 | U_poly.H * position_K, |
| 308 | U_poly.k - U_poly.H * velocity_K * velocity_error) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 309 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 310 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 315 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 316 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 325 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 326 | if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]): |
| 327 | LH = numpy.matrix([[0, 1]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 328 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 329 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 350 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 351 | #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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 353 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 354 | #U = Kpos * poserror + Kvel * velerror |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 355 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 356 | #scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 357 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 358 | #top_u *= scalar |
| 359 | #bottom_u *= scalar |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 360 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 361 | return numpy.matrix([[bottom_u], [top_u]]) |
| 362 | |
| 363 | |
| 364 | def 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 371 | |
| 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 Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 377 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 378 | Args: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 379 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 385 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 386 | claw.X = initial_X |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 387 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 388 | # Various lists for graphing things. |
| 389 | t = [] |
| 390 | x_bottom = [] |
| 391 | x_top = [] |
| 392 | u_bottom = [] |
| 393 | u_top = [] |
| 394 | x_separation = [] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 395 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 396 | tests_passed = True |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 397 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 398 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 403 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 404 | for i in xrange(iterations): |
| 405 | U = claw.K * (goal - claw.X) |
| 406 | U = ScaleU(claw, U, claw.K, goal - claw.X) |
| 407 | claw.Update(U) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 408 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 409 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 414 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 415 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 425 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 426 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 432 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 433 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 441 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 442 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 446 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 447 | return tests_passed |
| 448 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 449 | |
| 450 | def main(argv): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 451 | argv = FLAGS(argv) |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame] | 452 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 453 | 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 459 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 460 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 464 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 465 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 469 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 470 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 474 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 475 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 479 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 480 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 487 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 488 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 495 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 496 | # 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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 510 | |
| 511 | if __name__ == '__main__': |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 512 | sys.exit(main(sys.argv)) |