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 |
| 43 | self.difference_bottom = self.common_motor_constant * (1 / self.J_bottom |
| 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 | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame^] | 49 | [[0, 0, 0, 0], |
| 50 | [0, 0, 0, 0], |
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 | |
| 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 |
| 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 | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 73 | [self.motor_feedforward_bottom, 0], |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame^] | 74 | [0, 0], |
| 75 | [0,#self.motor_feedforward_difference_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 | |
| 78 | self.B_bottom_cont = numpy.matrix( |
| 79 | [[0], |
| 80 | [self.motor_feedforward_bottom]]) |
| 81 | |
| 82 | self.B_diff_cont = numpy.matrix( |
| 83 | [[0], |
| 84 | [self.motor_feedforward_difference]]) |
| 85 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 86 | self.C = numpy.matrix([[1, 0, 0, 0], |
| 87 | [1, 1, 0, 0]]) |
| 88 | self.D = numpy.matrix([[0, 0], |
| 89 | [0, 0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 90 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 91 | self.A, self.B = self.ContinuousToDiscrete( |
| 92 | self.A_continuous, self.B_continuous, self.dt) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 93 | |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame^] | 94 | self.A_bottom, self.B_bottom = controls.c2d( |
| 95 | self.A_bottom_cont, self.B_bottom_cont, self.dt) |
| 96 | self.A_diff, self.B_diff = controls.c2d( |
| 97 | self.A_diff_cont, self.B_diff_cont, self.dt) |
| 98 | |
| 99 | print self.A, self.B |
| 100 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 101 | #controlability = controls.ctrb(self.A, self.B); |
| 102 | #print "Rank of controlability matrix.", numpy.linalg.matrix_rank(controlability) |
| 103 | |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 104 | self.Q = numpy.matrix([[(1.0 / (0.40 ** 2.0)), 0.0, 0.0, 0.0], |
| 105 | [0.0, (1.0 / (0.007 ** 2.0)), 0.0, 0.0], |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 106 | [0.0, 0.0, 0.2, 0.0], |
| 107 | [0.0, 0.0, 0.0, 2.00]]) |
| 108 | |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 109 | self.R = numpy.matrix([[(1.0 / (40.0 ** 2.0)), 0.0], |
| 110 | [0.0, (1.0 / (5.0 ** 2.0))]]) |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame^] | 111 | #self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 112 | |
| 113 | # TODO(james): Fix this for discrete time domain. |
| 114 | self.K = numpy.matrix([[0, 0, 0.0, 0.0], |
| 115 | [0.0, self.A[3, 1] / self.B[3, 1], 0, 0]]) |
| 116 | self.K_bottom = controls.dplace(self.A_bottom, self.B_bottom, [0.5, 0.5]) |
| 117 | self.K_diff = controls.dplace(self.A_diff, self.B_diff, [0.5, 0.5]) |
| 118 | self.K[0, 0:2] = self.K_bottom |
| 119 | self.K[1, 2:4] = self.K_diff |
| 120 | #lstsq_A = numpy.identity(2) |
| 121 | #lstsq_A[0] = self.B[1] |
| 122 | #lstsq_A[1] = self.B[3] |
| 123 | #self.K[0:2, 0] = numpy.linalg.lstsq(lstsq_A, numpy.matrix([[0.0], [0.0]]))[0] |
| 124 | #self.K[0:2, 2] = numpy.linalg.lstsq( |
| 125 | # lstsq_A, |
| 126 | # numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]))[0] |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 127 | |
| 128 | print "K unaugmented" |
| 129 | print self.K |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame^] | 130 | print "B * K unaugmented" |
| 131 | print self.B * self.K |
| 132 | F = self.A - self.B * self.K |
| 133 | F[1, 2] = 0.0 |
| 134 | F[3, 2] = 0.0 |
| 135 | print "A - B * K unaugmented" |
| 136 | print F |
| 137 | print "eigenvalues" |
| 138 | print numpy.linalg.eig(F)[0] |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 139 | |
| 140 | self.rpl = .05 |
| 141 | self.ipl = 0.008 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 142 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 143 | self.rpl + 1j * self.ipl, |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame^] | 144 | self.rpl - 1j * self.ipl, |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 145 | self.rpl - 1j * self.ipl]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 146 | |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 147 | # The box formed by U_min and U_max must encompass all possible values, |
| 148 | # or else Austin's code gets angry. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 149 | self.U_max = numpy.matrix([[12.0], [24.0]]) |
| 150 | self.U_min = numpy.matrix([[-12.0], [-24.0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 151 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 152 | self.InitializeState() |
| 153 | |
| 154 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 155 | class ClawDeltaU(Claw): |
| 156 | def __init__(self, name="Claw"): |
| 157 | super(ClawDeltaU, self).__init__(name) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 158 | A_unaugmented = self.A |
| 159 | B_unaugmented = self.B |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 160 | C_unaugmented = self.C |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 161 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 162 | self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0], |
| 163 | [0.0, 0.0, 0.0, 0.0, 0.0], |
| 164 | [0.0, 0.0, 0.0, 0.0, 0.0], |
| 165 | [0.0, 0.0, 0.0, 0.0, 0.0], |
| 166 | [0.0, 0.0, 0.0, 0.0, 1.0]]) |
| 167 | self.A[0:4, 0:4] = A_unaugmented |
| 168 | self.A[0:4, 4] = B_unaugmented[0:4, 0] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 169 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 170 | self.B = numpy.matrix([[0.0, 0.0], |
| 171 | [0.0, 0.0], |
| 172 | [0.0, 0.0], |
| 173 | [0.0, 0.0], |
| 174 | [1.0, 0.0]]) |
| 175 | self.B[0:4, 1] = B_unaugmented[0:4, 1] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 176 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 177 | self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])), |
| 178 | axis=1) |
| 179 | self.D = numpy.matrix([[0.0, 0.0], |
| 180 | [0.0, 0.0]]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 181 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 182 | #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80]) |
| 183 | self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0], |
| 184 | [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0], |
| 185 | [0.0, 0.0, 0.01, 0.0, 0.0], |
| 186 | [0.0, 0.0, 0.0, 0.08, 0.0], |
| 187 | [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]]) |
| 188 | |
| 189 | self.R = numpy.matrix([[0.000001, 0.0], |
| 190 | [0.0, 1.0 / (10.0 ** 2.0)]]) |
| 191 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 192 | |
| 193 | self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0], |
| 194 | [50.0, 0.0, 10.0, 0.0, 1.0]]) |
| 195 | #self.K = numpy.matrix([[50.0, -100.0, 0, -10, 0], |
| 196 | # [50.0, 100.0, 0, 10, 0]]) |
| 197 | |
| 198 | controlability = controls.ctrb(self.A, self.B); |
| 199 | print "Rank of augmented controlability matrix.", numpy.linalg.matrix_rank(controlability) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 200 | |
| 201 | print "K" |
| 202 | print self.K |
| 203 | print "Placed controller poles are" |
| 204 | print numpy.linalg.eig(self.A - self.B * self.K)[0] |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 205 | 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] | 206 | |
| 207 | self.rpl = .05 |
| 208 | self.ipl = 0.008 |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 209 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09, |
Brian Silverman | 23a67ca | 2013-03-16 23:48:50 -0700 | [diff] [blame] | 210 | self.rpl - 1j * self.ipl, 0.90]) |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 211 | #print "A is" |
| 212 | #print self.A |
| 213 | #print "L is" |
| 214 | #print self.L |
| 215 | #print "C is" |
| 216 | #print self.C |
| 217 | #print "A - LC is" |
| 218 | #print self.A - self.L * self.C |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 219 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 220 | #print "Placed observer poles are" |
| 221 | #print numpy.linalg.eig(self.A - self.L * self.C)[0] |
| 222 | |
| 223 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 224 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 225 | |
| 226 | self.InitializeState() |
| 227 | |
| 228 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 229 | def FullSeparationPriority(claw, U): |
| 230 | bottom_u = U[0, 0] |
| 231 | top_u = U[1, 0] + bottom_u |
| 232 | |
| 233 | #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u |
| 234 | if bottom_u > claw.U_max[0, 0]: |
| 235 | #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0] |
| 236 | top_u -= bottom_u - claw.U_max[0, 0] |
| 237 | if top_u < claw.U_min[1, 0]: |
| 238 | top_u = claw.U_min[1, 0] |
| 239 | |
| 240 | bottom_u = claw.U_max[0, 0] |
| 241 | if top_u > claw.U_max[1, 0]: |
| 242 | bottom_u -= top_u - claw.U_max[1, 0] |
| 243 | if bottom_u < claw.U_min[0, 0]: |
| 244 | bottom_u = claw.U_min[0, 0] |
| 245 | |
| 246 | top_u = claw.U_max[1, 0] |
| 247 | if top_u < claw.U_min[1, 0]: |
| 248 | bottom_u -= top_u - claw.U_min[1, 0] |
| 249 | if bottom_u > claw.U_max[0, 0]: |
| 250 | bottom_u = claw.U_max[0, 0] |
| 251 | |
| 252 | top_u = claw.U_min[1, 0] |
| 253 | if bottom_u < claw.U_min[0, 0]: |
| 254 | top_u -= bottom_u - claw.U_min[0, 0] |
| 255 | if top_u > claw.U_max[1, 0]: |
| 256 | top_u = claw.U_max[1, 0] |
| 257 | |
| 258 | bottom_u = claw.U_min[0, 0] |
| 259 | |
| 260 | return numpy.matrix([[bottom_u], [top_u - bottom_u]]) |
| 261 | |
| 262 | def AverageUFix(claw, U): |
| 263 | bottom_u = U[0, 0] |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 264 | top_u = bottom_u + U[1, 0] |
| 265 | #top_u = claw.J_top * (bottom_u / claw.J_bottom - U[1, 0]) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 266 | |
| 267 | #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 268 | if (bottom_u > claw.U_max[0, 0] or top_u > claw.U_max[0, 0] or |
| 269 | top_u < claw.U_min[0, 0] or bottom_u < claw.U_min[0, 0]): |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 270 | scalar = 12.0 / max(numpy.abs(top_u), numpy.abs(bottom_u)) |
| 271 | top_u *= scalar |
| 272 | bottom_u *= scalar |
| 273 | |
| 274 | return numpy.matrix([[bottom_u], [top_u - bottom_u]]) |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 275 | #return numpy.matrix([[bottom_u], [bottom_u / claw.J_bottom - top_u / claw.J_top]]) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 276 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 277 | def ClipDeltaU(claw, U): |
| 278 | delta_u = U[0, 0] |
| 279 | top_u = U[1, 0] |
| 280 | old_bottom_u = claw.X[4, 0] |
| 281 | |
| 282 | # TODO(austin): Preserve the difference between the top and bottom power. |
| 283 | new_unclipped_bottom_u = old_bottom_u + delta_u |
| 284 | |
| 285 | #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u |
| 286 | if new_unclipped_bottom_u > claw.U_max[0, 0]: |
| 287 | #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0] |
| 288 | top_u -= new_unclipped_bottom_u - claw.U_max[0, 0] |
| 289 | new_unclipped_bottom_u = claw.U_max[0, 0] |
| 290 | if top_u > claw.U_max[1, 0]: |
| 291 | new_unclipped_bottom_u -= top_u - claw.U_max[1, 0] |
| 292 | top_u = claw.U_max[1, 0] |
| 293 | if top_u < claw.U_min[1, 0]: |
| 294 | new_unclipped_bottom_u -= top_u - claw.U_min[1, 0] |
| 295 | top_u = claw.U_min[1, 0] |
| 296 | if new_unclipped_bottom_u < claw.U_min[0, 0]: |
| 297 | top_u -= new_unclipped_bottom_u - claw.U_min[0, 0] |
| 298 | new_unclipped_bottom_u = claw.U_min[0, 0] |
| 299 | |
| 300 | new_bottom_u = numpy.clip(new_unclipped_bottom_u, claw.U_min[0, 0], claw.U_max[0, 0]) |
| 301 | new_top_u = numpy.clip(top_u, claw.U_min[1, 0], claw.U_max[1, 0]) |
| 302 | |
| 303 | return numpy.matrix([[new_bottom_u - old_bottom_u], [new_top_u]]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 304 | |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 305 | def main(argv): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 306 | # Simulate the response of the system to a step input. |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 307 | #claw = ClawDeltaU() |
| 308 | #simulated_x = [] |
| 309 | #for _ in xrange(100): |
| 310 | # claw.Update(numpy.matrix([[12.0]])) |
| 311 | # simulated_x.append(claw.X[0, 0]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 312 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 313 | #pylab.plot(range(100), simulated_x) |
| 314 | #pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 315 | |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 316 | # Simulate the closed loop response of the system. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 317 | claw = Claw("TopClaw") |
| 318 | t = [] |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 319 | close_loop_x_bottom = [] |
| 320 | close_loop_x_sep = [] |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 321 | close_loop_x_top = [] |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 322 | close_loop_u_bottom = [] |
| 323 | close_loop_u_top = [] |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 324 | R = numpy.matrix([[1.1], [0.05], [0.0], [0.0]]) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 325 | claw.X[0, 0] = 0 |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 326 | claw.X[1, 0] = .05 |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 327 | for i in xrange(100): |
| 328 | #print "Error is", (R - claw.X_hat) |
| 329 | U = claw.K * (R - claw.X_hat) |
| 330 | #U = numpy.clip(claw.K * (R - claw.X_hat), claw.U_min, claw.U_max) |
| 331 | #U = FullSeparationPriority(claw, U) |
| 332 | U = AverageUFix(claw, U) |
| 333 | #U = claw.K * (R - claw.X_hat) |
| 334 | #U = ClipDeltaU(claw, U) |
| 335 | claw.UpdateObserver(U) |
| 336 | claw.Update(U) |
| 337 | close_loop_x_bottom.append(claw.X[0, 0] * 10) |
| 338 | close_loop_u_bottom.append(U[0, 0]) |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 339 | close_loop_x_sep.append(claw.X[1, 0] * 100) |
| 340 | close_loop_x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10) |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 341 | close_loop_u_top.append(U[1, 0] + U[0, 0]) |
| 342 | t.append(0.01 * i) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 343 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 344 | pylab.plot(t, close_loop_x_bottom, label='x bottom') |
James Kuszmaul | f2ed6e6 | 2014-02-17 17:52:07 -0800 | [diff] [blame] | 345 | pylab.plot(t, close_loop_x_sep, label='separation') |
| 346 | pylab.plot(t, close_loop_x_top, label='x top') |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 347 | pylab.plot(t, close_loop_u_bottom, label='u bottom') |
| 348 | pylab.plot(t, close_loop_u_top, label='u top') |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 349 | pylab.legend() |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 350 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 351 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 352 | # Write the generated constants out to a file. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 353 | if len(argv) != 3: |
| 354 | print "Expected .h file name and .cc file name for the claw." |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 355 | else: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 356 | claw = Claw("Claw") |
| 357 | loop_writer = control_loop.ControlLoopWriter("Claw", [claw]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 358 | if argv[1][-3:] == '.cc': |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 359 | loop_writer.Write(argv[2], argv[1]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 360 | else: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 361 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 362 | |
| 363 | if __name__ == '__main__': |
| 364 | sys.exit(main(sys.argv)) |