Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 3 | import control_loop |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 4 | import controls |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 5 | import numpy |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 6 | import sys |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 7 | from matplotlib import pylab |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 8 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 9 | class Claw(control_loop.ControlLoop): |
| 10 | def __init__(self, name="RawClaw"): |
| 11 | super(Claw, self).__init__(name) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 12 | # Stall Torque in N m |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame] | 13 | self.stall_torque = 2.42 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 14 | # Stall Current in Amps |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame] | 15 | self.stall_current = 133 |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 16 | # Free Speed in RPM |
| 17 | self.free_speed = 5500.0 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 18 | # Free Current in Amps |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame] | 19 | self.free_current = 2.7 |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 20 | # Moment of inertia of the claw in kg m^2 |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 21 | # measured from CAD |
| 22 | self.J_top = 0.3 |
| 23 | self.J_bottom = 0.9 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 24 | # Resistance of the motor |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 25 | self.R = 12.0 / self.stall_current |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 26 | # Motor velocity constant |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 27 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 28 | (13.5 - self.R * self.free_current)) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 29 | # Torque constant |
| 30 | self.Kt = self.stall_torque / self.stall_current |
| 31 | # Gear ratio |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame] | 32 | self.G = 14.0 / 48.0 * 18.0 / 32.0 * 18.0 / 66.0 * 12.0 / 60.0 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 33 | # Control loop time step |
| 34 | self.dt = 0.01 |
| 35 | |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 36 | # State is [bottom position, bottom velocity, top - bottom position, |
| 37 | # top - bottom velocity] |
| 38 | # Input is [bottom power, top power - bottom power * J_top / J_bottom] |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 39 | # Motor time constants. difference_bottom refers to the constant for how the |
| 40 | # bottom velocity affects the difference of the top and bottom velocities. |
| 41 | self.common_motor_constant = -self.Kt / self.Kv / (self.G * self.G * self.R) |
| 42 | self.bottom_bottom = self.common_motor_constant / self.J_bottom |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 43 | self.difference_bottom = -self.common_motor_constant * (1 / self.J_bottom |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 44 | - 1 / self.J_top) |
| 45 | self.difference_difference = self.common_motor_constant / self.J_top |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 46 | # State feedback matrices |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 47 | |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 48 | self.A_continuous = numpy.matrix( |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 49 | [[0, 0, 1, 0], |
| 50 | [0, 0, 0, 1], |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 51 | [0, 0, self.bottom_bottom, 0], |
| 52 | [0, 0, self.difference_bottom, self.difference_difference]]) |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 53 | |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 54 | self.A_bottom_cont = numpy.matrix( |
| 55 | [[0, 1], |
| 56 | [0, self.bottom_bottom]]) |
| 57 | |
| 58 | self.A_diff_cont = numpy.matrix( |
| 59 | [[0, 1], |
| 60 | [0, self.difference_difference]]) |
| 61 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 62 | # self.A_continuous[0:2, 0:2] = self.A_bottom_cont |
| 63 | # self.A_continuous[2:4, 2:4] = self.A_diff_cont |
| 64 | # self.A_continuous[3, 1] = self.difference_bottom |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 65 | |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 66 | self.motor_feedforward = self.Kt / (self.G * self.R) |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 67 | self.motor_feedforward_bottom = self.motor_feedforward / self.J_bottom |
| 68 | self.motor_feedforward_difference = self.motor_feedforward / self.J_top |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 69 | self.motor_feedforward_difference_bottom = ( |
| 70 | self.motor_feedforward * (1 / self.J_bottom - 1 / self.J_top)) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 71 | self.B_continuous = numpy.matrix( |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 72 | [[0, 0], |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 73 | [0, 0], |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 74 | [self.motor_feedforward_bottom, 0], |
| 75 | [-self.motor_feedforward_bottom, |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 76 | self.motor_feedforward_difference]]) |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 77 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 78 | print "Cont X_ss", self.motor_feedforward / -self.common_motor_constant |
| 79 | |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 80 | self.B_bottom_cont = numpy.matrix( |
| 81 | [[0], |
| 82 | [self.motor_feedforward_bottom]]) |
| 83 | |
| 84 | self.B_diff_cont = numpy.matrix( |
| 85 | [[0], |
| 86 | [self.motor_feedforward_difference]]) |
| 87 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 88 | self.C = numpy.matrix([[1, 0, 0, 0], |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 89 | [1, 1, 0, 0]]) |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 90 | self.D = numpy.matrix([[0, 0], |
| 91 | [0, 0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 92 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 93 | self.A, self.B = self.ContinuousToDiscrete( |
| 94 | self.A_continuous, self.B_continuous, self.dt) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 95 | |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 96 | self.A_bottom, self.B_bottom = controls.c2d( |
| 97 | self.A_bottom_cont, self.B_bottom_cont, self.dt) |
| 98 | self.A_diff, self.B_diff = controls.c2d( |
| 99 | self.A_diff_cont, self.B_diff_cont, self.dt) |
| 100 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 101 | print "A" |
| 102 | print self.A |
| 103 | print "B" |
| 104 | print self.B |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 105 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 106 | X_ss = numpy.matrix([[0], [0], [0.0], [0]]) |
| 107 | |
| 108 | U = numpy.matrix([[1.0],[1.0]]) |
| 109 | A = self.A |
| 110 | B = self.B |
| 111 | #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0] |
| 112 | X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0] |
| 113 | #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] |
| 114 | #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] |
| 115 | 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]) |
| 116 | #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] |
| 117 | X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0] |
| 118 | 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] |
| 119 | |
| 120 | print "X_ss", X_ss |
| 121 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 122 | #controlability = controls.ctrb(self.A, self.B); |
| 123 | #print "Rank of controlability matrix.", numpy.linalg.matrix_rank(controlability) |
| 124 | |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 125 | self.Q = numpy.matrix([[(1.0 / (0.40 ** 2.0)), 0.0, 0.0, 0.0], |
| 126 | [0.0, (1.0 / (0.007 ** 2.0)), 0.0, 0.0], |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 127 | [0.0, 0.0, 0.2, 0.0], |
| 128 | [0.0, 0.0, 0.0, 2.00]]) |
| 129 | |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 130 | self.R = numpy.matrix([[(1.0 / (40.0 ** 2.0)), 0.0], |
| 131 | [0.0, (1.0 / (5.0 ** 2.0))]]) |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 132 | #self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 133 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 134 | self.K = numpy.matrix([[50, 0.0, 1, 0.0], |
| 135 | [0.0, 300, 0.0, 1.1]]) |
| 136 | lstsq_A = numpy.identity(2) |
| 137 | lstsq_A[0] = self.B[1] |
| 138 | lstsq_A[1] = self.B[3] |
| 139 | print "System of Equations coefficients:" |
| 140 | print lstsq_A |
| 141 | print "det", numpy.linalg.det(lstsq_A) |
| 142 | self.K[1, 0] = -lstsq_A[0, 0] * self.K[0, 0] / lstsq_A[0, 1] |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 143 | #self.K[0:2, 0] = numpy.linalg.lstsq(lstsq_A, numpy.matrix([[0.0], [0.0]]))[0] |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 144 | out_x = numpy.linalg.lstsq( |
| 145 | lstsq_A, |
| 146 | numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]))[0] |
| 147 | self.K[1, 2] = -lstsq_A[0, 0] * (self.K[0, 2] - out_x[0]) / lstsq_A[0, 1] + out_x[1] |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 148 | |
| 149 | print "K unaugmented" |
| 150 | print self.K |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 151 | print "B * K unaugmented" |
| 152 | print self.B * self.K |
| 153 | F = self.A - self.B * self.K |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 154 | print "A - B * K unaugmented" |
| 155 | print F |
| 156 | print "eigenvalues" |
| 157 | print numpy.linalg.eig(F)[0] |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 158 | |
| 159 | self.rpl = .05 |
| 160 | self.ipl = 0.008 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 161 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 162 | self.rpl + 1j * self.ipl, |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 163 | self.rpl - 1j * self.ipl, |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 164 | self.rpl - 1j * self.ipl]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 165 | |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 166 | # The box formed by U_min and U_max must encompass all possible values, |
| 167 | # or else Austin's code gets angry. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 168 | self.U_max = numpy.matrix([[12.0], [24.0]]) |
| 169 | self.U_min = numpy.matrix([[-12.0], [-24.0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 170 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 171 | self.InitializeState() |
| 172 | |
| 173 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 174 | class ClawDeltaU(Claw): |
| 175 | def __init__(self, name="Claw"): |
| 176 | super(ClawDeltaU, self).__init__(name) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 177 | A_unaugmented = self.A |
| 178 | B_unaugmented = self.B |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 179 | C_unaugmented = self.C |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 180 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 181 | self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0], |
| 182 | [0.0, 0.0, 0.0, 0.0, 0.0], |
| 183 | [0.0, 0.0, 0.0, 0.0, 0.0], |
| 184 | [0.0, 0.0, 0.0, 0.0, 0.0], |
| 185 | [0.0, 0.0, 0.0, 0.0, 1.0]]) |
| 186 | self.A[0:4, 0:4] = A_unaugmented |
| 187 | self.A[0:4, 4] = B_unaugmented[0:4, 0] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 188 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 189 | self.B = numpy.matrix([[0.0, 0.0], |
| 190 | [0.0, 0.0], |
| 191 | [0.0, 0.0], |
| 192 | [0.0, 0.0], |
| 193 | [1.0, 0.0]]) |
| 194 | self.B[0:4, 1] = B_unaugmented[0:4, 1] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 195 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 196 | self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])), |
| 197 | axis=1) |
| 198 | self.D = numpy.matrix([[0.0, 0.0], |
| 199 | [0.0, 0.0]]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 200 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 201 | #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80]) |
| 202 | self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0], |
| 203 | [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0], |
| 204 | [0.0, 0.0, 0.01, 0.0, 0.0], |
| 205 | [0.0, 0.0, 0.0, 0.08, 0.0], |
| 206 | [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]]) |
| 207 | |
| 208 | self.R = numpy.matrix([[0.000001, 0.0], |
| 209 | [0.0, 1.0 / (10.0 ** 2.0)]]) |
| 210 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 211 | |
| 212 | self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0], |
| 213 | [50.0, 0.0, 10.0, 0.0, 1.0]]) |
| 214 | #self.K = numpy.matrix([[50.0, -100.0, 0, -10, 0], |
| 215 | # [50.0, 100.0, 0, 10, 0]]) |
| 216 | |
| 217 | controlability = controls.ctrb(self.A, self.B); |
| 218 | print "Rank of augmented controlability matrix.", numpy.linalg.matrix_rank(controlability) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 219 | |
| 220 | print "K" |
| 221 | print self.K |
| 222 | print "Placed controller poles are" |
| 223 | print numpy.linalg.eig(self.A - self.B * self.K)[0] |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 224 | print [numpy.abs(x) for x in numpy.linalg.eig(self.A - self.B * self.K)[0]] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 225 | |
| 226 | self.rpl = .05 |
| 227 | self.ipl = 0.008 |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 228 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09, |
Brian Silverman | 23a67ca | 2013-03-16 23:48:50 -0700 | [diff] [blame] | 229 | self.rpl - 1j * self.ipl, 0.90]) |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 230 | #print "A is" |
| 231 | #print self.A |
| 232 | #print "L is" |
| 233 | #print self.L |
| 234 | #print "C is" |
| 235 | #print self.C |
| 236 | #print "A - LC is" |
| 237 | #print self.A - self.L * self.C |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 238 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 239 | #print "Placed observer poles are" |
| 240 | #print numpy.linalg.eig(self.A - self.L * self.C)[0] |
| 241 | |
| 242 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 243 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 244 | |
| 245 | self.InitializeState() |
| 246 | |
| 247 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 248 | def FullSeparationPriority(claw, U): |
| 249 | bottom_u = U[0, 0] |
| 250 | top_u = U[1, 0] + bottom_u |
| 251 | |
| 252 | #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u |
| 253 | if bottom_u > claw.U_max[0, 0]: |
| 254 | #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0] |
| 255 | top_u -= bottom_u - claw.U_max[0, 0] |
| 256 | if top_u < claw.U_min[1, 0]: |
| 257 | top_u = claw.U_min[1, 0] |
| 258 | |
| 259 | bottom_u = claw.U_max[0, 0] |
| 260 | if top_u > claw.U_max[1, 0]: |
| 261 | bottom_u -= top_u - claw.U_max[1, 0] |
| 262 | if bottom_u < claw.U_min[0, 0]: |
| 263 | bottom_u = claw.U_min[0, 0] |
| 264 | |
| 265 | top_u = claw.U_max[1, 0] |
| 266 | if top_u < claw.U_min[1, 0]: |
| 267 | bottom_u -= top_u - claw.U_min[1, 0] |
| 268 | if bottom_u > claw.U_max[0, 0]: |
| 269 | bottom_u = claw.U_max[0, 0] |
| 270 | |
| 271 | top_u = claw.U_min[1, 0] |
| 272 | if bottom_u < claw.U_min[0, 0]: |
| 273 | top_u -= bottom_u - claw.U_min[0, 0] |
| 274 | if top_u > claw.U_max[1, 0]: |
| 275 | top_u = claw.U_max[1, 0] |
| 276 | |
| 277 | bottom_u = claw.U_min[0, 0] |
| 278 | |
| 279 | return numpy.matrix([[bottom_u], [top_u - bottom_u]]) |
| 280 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 281 | def AverageUFix(claw, U, preserve_v3=False): |
| 282 | """Clips U as necessary. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 283 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 284 | Args: |
| 285 | claw: claw object containing moments of inertia and U limits. |
| 286 | U: Input matrix to clip as necessary. |
| 287 | preserve_v3: There are two ways to attempt to clip the voltages: |
| 288 | -If you preserve the imaginary v3, then it will attempt to |
| 289 | preserve the effect on the separation of the two claws. |
| 290 | If it is not able to do this, then it calls itself with preserve_v3 |
| 291 | set to False. |
| 292 | -If you preserve the ratio of the voltage of the bottom and the top, |
| 293 | then it will attempt to preserve the ratio of those two. This is |
| 294 | equivalent to preserving the ratio of v3 and the bottom voltage. |
| 295 | """ |
| 296 | bottom_u = U[0, 0] |
| 297 | top_u = U[1, 0] |
| 298 | seperation_u = top_u - bottom_u * claw.J_top / claw.J_bottom |
| 299 | |
| 300 | top_big = top_u > claw.U_max[0, 0] |
| 301 | top_small = top_u < claw.U_min[0, 0] |
| 302 | bot_big = bottom_u > claw.U_max[0, 0] |
| 303 | bot_small = top_u < claw.U_min[0, 0] |
| 304 | bottom_bad = bot_big or bot_small |
| 305 | top_bad = top_big or top_small |
| 306 | scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u)) |
| 307 | if bottom_bad and preserve_v3: |
| 308 | bottom_u *= scalar |
| 309 | top_u = seperation_u + bottom_u * claw.J_top / claw.J_bottom |
| 310 | if abs(top_u) > claw.U_max[0, 0]: |
| 311 | return AverageUFix(claw, U, preserve_v3=False) |
| 312 | elif top_bad and preserve_v3: |
| 313 | top_u *= scalar |
| 314 | bottom_u = (top_u - seperation_u) * claw.J_bottom / claw.J_top |
| 315 | if abs(bottom_u) > claw.U_max[0, 0]: |
| 316 | return AverageUFix(claw, U, preserve_v3=False) |
| 317 | elif (bottom_bad or top_bad) and not preserve_v3: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 318 | top_u *= scalar |
| 319 | bottom_u *= scalar |
| 320 | |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 321 | return numpy.matrix([[bottom_u], [top_u]]) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 322 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 323 | def ClipDeltaU(claw, U): |
| 324 | delta_u = U[0, 0] |
| 325 | top_u = U[1, 0] |
| 326 | old_bottom_u = claw.X[4, 0] |
| 327 | |
| 328 | # TODO(austin): Preserve the difference between the top and bottom power. |
| 329 | new_unclipped_bottom_u = old_bottom_u + delta_u |
| 330 | |
| 331 | #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u |
| 332 | if new_unclipped_bottom_u > claw.U_max[0, 0]: |
| 333 | #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0] |
| 334 | top_u -= new_unclipped_bottom_u - claw.U_max[0, 0] |
| 335 | new_unclipped_bottom_u = claw.U_max[0, 0] |
| 336 | if top_u > claw.U_max[1, 0]: |
| 337 | new_unclipped_bottom_u -= top_u - claw.U_max[1, 0] |
| 338 | top_u = claw.U_max[1, 0] |
| 339 | if top_u < claw.U_min[1, 0]: |
| 340 | new_unclipped_bottom_u -= top_u - claw.U_min[1, 0] |
| 341 | top_u = claw.U_min[1, 0] |
| 342 | if new_unclipped_bottom_u < claw.U_min[0, 0]: |
| 343 | top_u -= new_unclipped_bottom_u - claw.U_min[0, 0] |
| 344 | new_unclipped_bottom_u = claw.U_min[0, 0] |
| 345 | |
| 346 | new_bottom_u = numpy.clip(new_unclipped_bottom_u, claw.U_min[0, 0], claw.U_max[0, 0]) |
| 347 | new_top_u = numpy.clip(top_u, claw.U_min[1, 0], claw.U_max[1, 0]) |
| 348 | |
| 349 | return numpy.matrix([[new_bottom_u - old_bottom_u], [new_top_u]]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 350 | |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 351 | def main(argv): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 352 | # Simulate the response of the system to a step input. |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 353 | #claw = ClawDeltaU() |
| 354 | #simulated_x = [] |
| 355 | #for _ in xrange(100): |
| 356 | # claw.Update(numpy.matrix([[12.0]])) |
| 357 | # simulated_x.append(claw.X[0, 0]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 358 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 359 | #pylab.plot(range(100), simulated_x) |
| 360 | #pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 361 | |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 362 | # Simulate the closed loop response of the system. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 363 | claw = Claw("TopClaw") |
| 364 | t = [] |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 365 | close_loop_x_bottom = [] |
| 366 | close_loop_x_sep = [] |
James Kuszmaul | 4e4ec8e | 2014-02-18 10:46:49 -0800 | [diff] [blame] | 367 | actual_sep = [] |
| 368 | actual_x_bottom = [] |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 369 | close_loop_x_top = [] |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 370 | close_loop_u_bottom = [] |
| 371 | close_loop_u_top = [] |
James Kuszmaul | 4e4ec8e | 2014-02-18 10:46:49 -0800 | [diff] [blame] | 372 | R = numpy.matrix([[0.0], [0.00], [0.0], [0.0]]) |
| 373 | claw.X[0, 0] = 1 |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 374 | claw.X[1, 0] = .0 |
| 375 | claw.X_hat = claw.X |
| 376 | #X_actual = claw.X |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 377 | for i in xrange(100): |
| 378 | #print "Error is", (R - claw.X_hat) |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 379 | U = claw.K * (R - claw.X) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 380 | #U = numpy.clip(claw.K * (R - claw.X_hat), claw.U_min, claw.U_max) |
| 381 | #U = FullSeparationPriority(claw, U) |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 382 | #U = AverageUFix(claw, U, preserve_v3=True) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 383 | #U = claw.K * (R - claw.X_hat) |
| 384 | #U = ClipDeltaU(claw, U) |
| 385 | claw.UpdateObserver(U) |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 386 | claw.Update(U) |
| 387 | #X_actual = claw.A_actual * X_actual + claw.B_actual * U |
| 388 | #claw.Y = claw.C * X_actual |
| 389 | close_loop_x_bottom.append(claw.X[0, 0] * 10) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 390 | close_loop_u_bottom.append(U[0, 0]) |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 391 | #actual_sep.append(X_actual[2, 0] * 100) |
| 392 | #actual_x_bottom.append(X_actual[0, 0] * 10) |
| 393 | close_loop_x_sep.append(claw.X[1, 0] * 100) |
| 394 | close_loop_x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10) |
| 395 | close_loop_u_top.append(U[1, 0]) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 396 | t.append(0.01 * i) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 397 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 398 | pylab.plot(t, close_loop_x_bottom, label='x bottom') |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 399 | pylab.plot(t, close_loop_x_sep, label='separation') |
James Kuszmaul | c02a39a | 2014-02-18 15:45:16 -0800 | [diff] [blame^] | 400 | #pylab.plot(t, actual_x_bottom, label='true x bottom') |
| 401 | #pylab.plot(t, actual_sep, label='true separation') |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 402 | pylab.plot(t, close_loop_x_top, label='x top') |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 403 | pylab.plot(t, close_loop_u_bottom, label='u bottom') |
| 404 | pylab.plot(t, close_loop_u_top, label='u top') |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 405 | pylab.legend() |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 406 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 407 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 408 | # Write the generated constants out to a file. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 409 | if len(argv) != 3: |
| 410 | print "Expected .h file name and .cc file name for the claw." |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 411 | else: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 412 | claw = Claw("Claw") |
| 413 | loop_writer = control_loop.ControlLoopWriter("Claw", [claw]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 414 | if argv[1][-3:] == '.cc': |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 415 | loop_writer.Write(argv[2], argv[1]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 416 | else: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 417 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 418 | |
| 419 | if __name__ == '__main__': |
| 420 | sys.exit(main(sys.argv)) |