Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 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( |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 139 | lstsq_A, |
| 140 | numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]), |
| 141 | rcond=None)[0] |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 142 | self.K[1, 2] = -lstsq_A[0, 0] * ( |
| 143 | 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] | 144 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 145 | glog.debug('K unaugmented') |
| 146 | glog.debug(str(self.K)) |
| 147 | glog.debug('B * K unaugmented') |
| 148 | glog.debug(str(self.B * self.K)) |
| 149 | F = self.A - self.B * self.K |
| 150 | glog.debug('A - B * K unaugmented') |
| 151 | glog.debug(str(F)) |
| 152 | glog.debug('eigenvalues') |
| 153 | glog.debug(str(numpy.linalg.eig(F)[0])) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 154 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 155 | self.rpl = .09 |
| 156 | self.ipl = 0.030 |
| 157 | self.PlaceObserverPoles([ |
| 158 | self.rpl + 1j * self.ipl, self.rpl + 1j * self.ipl, |
| 159 | self.rpl - 1j * self.ipl, self.rpl - 1j * self.ipl |
| 160 | ]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 161 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 162 | # The box formed by U_min and U_max must encompass all possible values, |
| 163 | # or else Austin's code gets angry. |
| 164 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 165 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 166 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 167 | # For the tests that check the limits, these are (upper, lower) for both |
| 168 | # claws. |
| 169 | self.hard_pos_limits = None |
| 170 | self.pos_limits = None |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 171 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 172 | # Compute the steady state velocities for a given applied voltage. |
| 173 | # The top and bottom of the claw should spin at the same rate if the |
| 174 | # physics is right. |
| 175 | X_ss = numpy.matrix([[0], [0], [0.0], [0]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 176 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 177 | U = numpy.matrix([[1.0], [1.0]]) |
| 178 | A = self.A |
| 179 | B = self.B |
| 180 | #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0] |
| 181 | X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0] |
| 182 | #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] |
| 183 | #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] |
| 184 | X_ss[3, 0] = 1 / (1 - A[3, 3]) * ( |
| 185 | X_ss[2, 0] * A[3, 2] + B[3, 1] * U[1, 0] + B[3, 0] * U[0, 0]) |
| 186 | #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] |
| 187 | X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0] |
| 188 | X_ss[1, 0] = (A[1, 2] * X_ss[2, 0]) + (A[1, 3] * X_ss[3, 0]) + ( |
| 189 | B[1, 0] * U[0, 0]) + (B[1, 1] * U[1, 0]) |
| 190 | |
| 191 | glog.debug('X_ss %s', str(X_ss)) |
| 192 | |
| 193 | self.InitializeState() |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 194 | |
| 195 | |
| 196 | class ClawDeltaU(Claw): |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 197 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 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 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 203 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 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] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 211 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 212 | self.B = numpy.matrix([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], |
| 213 | [1.0, 0.0]]) |
| 214 | self.B[0:4, 1] = B_unaugmented[0:4, 1] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 215 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 216 | self.C = numpy.concatenate( |
| 217 | (C_unaugmented, numpy.matrix([[0.0], [0.0]])), axis=1) |
| 218 | self.D = numpy.matrix([[0.0, 0.0], [0.0, 0.0]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 219 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 220 | #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80]) |
| 221 | self.Q = numpy.matrix([[(1.0 / (0.04**2.0)), 0.0, 0.0, 0.0, 0.0], |
| 222 | [0.0, (1.0 / (0.01**2)), 0.0, 0.0, 0.0], |
| 223 | [0.0, 0.0, 0.01, 0.0, 0.0], |
| 224 | [0.0, 0.0, 0.0, 0.08, 0.0], |
| 225 | [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] | 226 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 227 | self.R = numpy.matrix([[0.000001, 0.0], [0.0, 1.0 / (10.0**2.0)]]) |
| 228 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 229 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 230 | self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0], |
| 231 | [50.0, 0.0, 10.0, 0.0, 1.0]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 232 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 233 | controllability = controls.ctrb(self.A, self.B) |
| 234 | glog.debug('Rank of augmented controllability matrix: %d', |
| 235 | numpy.linalg.matrix_rank(controllability)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 236 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 237 | glog.debug('K') |
| 238 | glog.debug(str(self.K)) |
| 239 | glog.debug('Placed controller poles are') |
| 240 | glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
| 241 | glog.debug( |
| 242 | str([ |
| 243 | numpy.abs(x) |
| 244 | for x in numpy.linalg.eig(self.A - self.B * self.K)[0] |
| 245 | ])) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 246 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 247 | self.rpl = .05 |
| 248 | self.ipl = 0.008 |
| 249 | self.PlaceObserverPoles([ |
| 250 | self.rpl + 1j * self.ipl, 0.10, 0.09, self.rpl - 1j * self.ipl, 0.90 |
| 251 | ]) |
| 252 | #print "A is" |
| 253 | #print self.A |
| 254 | #print "L is" |
| 255 | #print self.L |
| 256 | #print "C is" |
| 257 | #print self.C |
| 258 | #print "A - LC is" |
| 259 | #print self.A - self.L * self.C |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 260 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 261 | #print "Placed observer poles are" |
| 262 | #print numpy.linalg.eig(self.A - self.L * self.C)[0] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 263 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 264 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 265 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
| 266 | |
| 267 | self.InitializeState() |
| 268 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 269 | |
| 270 | def ScaleU(claw, U, K, error): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 271 | """Clips U as necessary. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 272 | |
| 273 | Args: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 274 | claw: claw object containing moments of inertia and U limits. |
| 275 | U: Input matrix to clip as necessary. |
| 276 | """ |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 277 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 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] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 282 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 283 | U_poly = polytope.HPolytope( |
| 284 | numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]), |
| 285 | numpy.matrix([[12], [12], [12], [12]])) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 286 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 287 | if (bottom_u > claw.U_max[0, 0] or bottom_u < claw.U_min[0, 0] or |
| 288 | 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] | 289 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 290 | position_K = K[:, 0:2] |
| 291 | velocity_K = K[:, 2:] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 292 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 293 | # H * U <= k |
| 294 | # U = UPos + UVel |
| 295 | # H * (UPos + UVel) <= k |
| 296 | # H * UPos <= k - H * UVel |
| 297 | # |
| 298 | # Now, we can do a coordinate transformation and say the following. |
| 299 | # |
| 300 | # UPos = position_K * position_error |
| 301 | # (H * position_K) * position_error <= k - H * UVel |
| 302 | # |
| 303 | # Add in the constraint that 0 <= t <= 1 |
| 304 | # Now, there are 2 ways this can go. Either we have a region, or we don't |
| 305 | # have a region. If we have a region, then pick the largest t and go for it. |
| 306 | # 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] | 307 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 308 | pos_poly = polytope.HPolytope( |
| 309 | U_poly.H * position_K, |
| 310 | U_poly.k - U_poly.H * velocity_K * velocity_error) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 311 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 312 | # The actual angle for the line we call 45. |
| 313 | angle_45 = numpy.matrix([[numpy.sqrt(3), 1]]) |
| 314 | if claw.pos_limits and claw.hard_pos_limits and ( |
| 315 | claw.X[0, 0] + claw.X[1, 0]) > claw.pos_limits[1]: |
| 316 | angle_45 = numpy.matrix([[1, 1]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 317 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 318 | P = position_error |
| 319 | L45 = numpy.multiply( |
| 320 | numpy.matrix([[numpy.sign(P[1, 0]), -numpy.sign(P[0, 0])]]), |
| 321 | angle_45) |
| 322 | if L45[0, 1] == 0: |
| 323 | L45[0, 1] = 1 |
| 324 | if L45[0, 0] == 0: |
| 325 | L45[0, 0] = 1 |
| 326 | w45 = numpy.matrix([[0]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 327 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 328 | if numpy.abs(P[0, 0]) > numpy.abs(P[1, 0]): |
| 329 | LH = numpy.matrix([[0, 1]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 330 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 331 | LH = numpy.matrix([[1, 0]]) |
| 332 | wh = LH * P |
| 333 | standard = numpy.concatenate((L45, LH)) |
| 334 | W = numpy.concatenate((w45, wh)) |
| 335 | intersection = numpy.linalg.inv(standard) * W |
| 336 | adjusted_pos_error_h, is_inside_h = polydrivetrain.DoCoerceGoal( |
| 337 | pos_poly, LH, wh, position_error) |
| 338 | adjusted_pos_error_45, is_inside_45 = polydrivetrain.DoCoerceGoal( |
| 339 | pos_poly, L45, w45, intersection) |
| 340 | if pos_poly.IsInside(intersection): |
| 341 | adjusted_pos_error = adjusted_pos_error_h |
| 342 | else: |
| 343 | if is_inside_h: |
| 344 | if numpy.linalg.norm(adjusted_pos_error_h) > numpy.linalg.norm( |
| 345 | adjusted_pos_error_45): |
| 346 | adjusted_pos_error = adjusted_pos_error_h |
| 347 | else: |
| 348 | adjusted_pos_error = adjusted_pos_error_45 |
| 349 | else: |
| 350 | adjusted_pos_error = adjusted_pos_error_45 |
| 351 | #print adjusted_pos_error |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 352 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 353 | #print "Actual power is ", velocity_K * velocity_error + position_K * adjusted_pos_error |
| 354 | return velocity_K * velocity_error + position_K * adjusted_pos_error |
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 | #U = Kpos * poserror + Kvel * velerror |
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 | #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] | 359 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 360 | #top_u *= scalar |
| 361 | #bottom_u *= scalar |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 362 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 363 | return numpy.matrix([[bottom_u], [top_u]]) |
| 364 | |
| 365 | |
| 366 | def run_test(claw, |
| 367 | initial_X, |
| 368 | goal, |
| 369 | max_separation_error=0.01, |
| 370 | show_graph=True, |
| 371 | iterations=200): |
| 372 | """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] | 373 | |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 374 | The tests themselves are not terribly sophisticated; I just test for |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 375 | whether the goal has been reached and whether the separation goes |
| 376 | outside of the initial and goal values by more than max_separation_error. |
| 377 | Prints out something for a failure of either condition and returns |
| 378 | False if tests fail. |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 379 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 380 | Args: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 381 | claw: claw object to use. |
| 382 | initial_X: starting state. |
| 383 | goal: goal state. |
| 384 | show_graph: Whether or not to display a graph showing the changing |
| 385 | states and voltages. |
| 386 | iterations: Number of timesteps to run the model for.""" |
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 | claw.X = initial_X |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 389 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 390 | # Various lists for graphing things. |
| 391 | t = [] |
| 392 | x_bottom = [] |
| 393 | x_top = [] |
| 394 | u_bottom = [] |
| 395 | u_top = [] |
| 396 | x_separation = [] |
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 | tests_passed = True |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 399 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 400 | # Bounds which separation should not exceed. |
| 401 | lower_bound = (initial_X[1, 0] if initial_X[1, 0] < goal[1, 0] else |
| 402 | goal[1, 0]) - max_separation_error |
| 403 | upper_bound = (initial_X[1, 0] if initial_X[1, 0] > goal[1, 0] else |
| 404 | goal[1, 0]) + max_separation_error |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 405 | |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 406 | for i in range(iterations): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 407 | U = claw.K * (goal - claw.X) |
| 408 | U = ScaleU(claw, U, claw.K, goal - claw.X) |
| 409 | claw.Update(U) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 410 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 411 | if claw.X[1, 0] > upper_bound or claw.X[1, 0] < lower_bound: |
| 412 | tests_passed = False |
| 413 | glog.info('Claw separation was %f', claw.X[1, 0]) |
| 414 | glog.info("Should have been between", lower_bound, "and", |
| 415 | upper_bound) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 416 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 417 | if claw.hard_pos_limits and \ |
| 418 | (claw.X[0, 0] > claw.hard_pos_limits[1] or |
| 419 | claw.X[0, 0] < claw.hard_pos_limits[0] or |
| 420 | claw.X[0, 0] + claw.X[1, 0] > claw.hard_pos_limits[1] or |
| 421 | claw.X[0, 0] + claw.X[1, 0] < claw.hard_pos_limits[0]): |
| 422 | tests_passed = False |
| 423 | glog.info('Claws at %f and %f', claw.X[0, 0], |
| 424 | claw.X[0, 0] + claw.X[1, 0]) |
| 425 | glog.info("Both should be in %s, definitely %s", claw.pos_limits, |
| 426 | claw.hard_pos_limits) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 427 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 428 | t.append(i * claw.dt) |
| 429 | x_bottom.append(claw.X[0, 0] * 10.0) |
| 430 | x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10.0) |
| 431 | u_bottom.append(U[0, 0]) |
| 432 | u_top.append(U[1, 0]) |
| 433 | x_separation.append(claw.X[1, 0] * 10.0) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 434 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 435 | if show_graph: |
| 436 | pylab.plot(t, x_bottom, label='x bottom * 10') |
| 437 | pylab.plot(t, x_top, label='x top * 10') |
| 438 | pylab.plot(t, u_bottom, label='u bottom') |
| 439 | pylab.plot(t, u_top, label='u top') |
| 440 | pylab.plot(t, x_separation, label='separation * 10') |
| 441 | pylab.legend() |
| 442 | pylab.show() |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 443 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 444 | # Test to make sure that we are near the goal. |
| 445 | if numpy.max(abs(claw.X - goal)) > 1e-4: |
| 446 | tests_passed = False |
| 447 | glog.error('X was %s Expected %s', str(claw.X), str(goal)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 448 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 449 | return tests_passed |
| 450 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 451 | |
| 452 | def main(argv): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 453 | argv = FLAGS(argv) |
Austin Schuh | a3b4255 | 2015-11-27 16:30:12 -0800 | [diff] [blame] | 454 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 455 | claw = Claw() |
| 456 | if FLAGS.plot: |
| 457 | # Test moving the claw with constant separation. |
| 458 | initial_X = numpy.matrix([[-1.0], [0.0], [0.0], [0.0]]) |
| 459 | R = numpy.matrix([[1.0], [0.0], [0.0], [0.0]]) |
| 460 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 461 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 462 | # Test just changing separation. |
| 463 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 464 | R = numpy.matrix([[0.0], [1.0], [0.0], [0.0]]) |
| 465 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 466 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 467 | # Test changing both separation and position at once. |
| 468 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 469 | R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]]) |
| 470 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 471 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 472 | # Test a small separation error and a large position one. |
| 473 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 474 | R = numpy.matrix([[2.0], [0.05], [0.0], [0.0]]) |
| 475 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 476 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 477 | # Test a small separation error and a large position one. |
| 478 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 479 | R = numpy.matrix([[-0.5], [1.0], [0.0], [0.0]]) |
| 480 | run_test(claw, initial_X, R) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 481 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 482 | # Test opening with the top claw at the limit. |
| 483 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 484 | R = numpy.matrix([[-1.5], [1.5], [0.0], [0.0]]) |
| 485 | claw.hard_pos_limits = (-1.6, 0.1) |
| 486 | claw.pos_limits = (-1.5, 0.0) |
| 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 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 490 | # Test opening with the bottom claw at the limit. |
| 491 | initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]]) |
| 492 | R = numpy.matrix([[0], [1.5], [0.0], [0.0]]) |
| 493 | claw.hard_pos_limits = (-0.1, 1.6) |
| 494 | claw.pos_limits = (0.0, 1.6) |
| 495 | run_test(claw, initial_X, R) |
| 496 | claw.pos_limits = None |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 497 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 498 | # Write the generated constants out to a file. |
| 499 | if len(argv) != 3: |
| 500 | glog.fatal('Expected .h file name and .cc file name for the claw.') |
| 501 | else: |
| 502 | namespaces = ['y2014', 'control_loops', 'claw'] |
| 503 | claw = Claw('Claw') |
| 504 | loop_writer = control_loop.ControlLoopWriter( |
| 505 | 'Claw', [claw], namespaces=namespaces) |
| 506 | loop_writer.AddConstant( |
| 507 | control_loop.Constant('kClawMomentOfInertiaRatio', '%f', |
| 508 | claw.J_top / claw.J_bottom)) |
| 509 | loop_writer.AddConstant(control_loop.Constant('kDt', '%f', claw.dt)) |
| 510 | loop_writer.Write(argv[1], argv[2]) |
| 511 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 512 | |
| 513 | if __name__ == '__main__': |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 514 | sys.exit(main(sys.argv)) |