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