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 | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 4 | import numpy |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 5 | import sys |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 6 | from matplotlib import pylab |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 7 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 8 | class Claw(control_loop.ControlLoop): |
| 9 | def __init__(self, name="RawClaw"): |
| 10 | super(Claw, self).__init__(name) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 11 | # Stall Torque in N m |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame^] | 12 | self.stall_torque = 2.42 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 13 | # Stall Current in Amps |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame^] | 14 | self.stall_current = 133 |
| 15 | # Free Speed in RPM, pulled from drivetrain |
| 16 | self.free_speed = 4650.0 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 17 | # Free Current in Amps |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame^] | 18 | self.free_current = 2.7 |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 19 | # Moment of inertia of the claw in kg m^2 |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame^] | 20 | # approzimately 0.76 (without ball) in CAD |
| 21 | self.J = 0.76 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 22 | # Resistance of the motor |
| 23 | self.R = 12.0 / self.stall_current + 0.024 + .003 |
| 24 | # Motor velocity constant |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 25 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 26 | (13.5 - self.R * self.free_current)) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 27 | # Torque constant |
| 28 | self.Kt = self.stall_torque / self.stall_current |
| 29 | # Gear ratio |
James Kuszmaul | e1755b3 | 2014-02-13 06:27:48 -0800 | [diff] [blame^] | 30 | 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] | 31 | # Control loop time step |
| 32 | self.dt = 0.01 |
| 33 | |
| 34 | # State feedback matrices |
| 35 | self.A_continuous = numpy.matrix( |
| 36 | [[0, 1], |
| 37 | [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]]) |
| 38 | self.B_continuous = numpy.matrix( |
| 39 | [[0], |
| 40 | [self.Kt / (self.J * self.G * self.R)]]) |
| 41 | self.C = numpy.matrix([[1, 0]]) |
| 42 | self.D = numpy.matrix([[0]]) |
| 43 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 44 | self.A, self.B = self.ContinuousToDiscrete( |
| 45 | self.A_continuous, self.B_continuous, self.dt) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 46 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 47 | self.PlaceControllerPoles([0.85, 0.45]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 48 | |
| 49 | self.rpl = .05 |
| 50 | self.ipl = 0.008 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 51 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 52 | self.rpl - 1j * self.ipl]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 53 | |
| 54 | self.U_max = numpy.matrix([[12.0]]) |
| 55 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 56 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 57 | self.InitializeState() |
| 58 | |
| 59 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 60 | class ClawDeltaU(Claw): |
| 61 | def __init__(self, name="Claw"): |
| 62 | super(ClawDeltaU, self).__init__(name) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 63 | A_unaugmented = self.A |
| 64 | B_unaugmented = self.B |
| 65 | |
| 66 | self.A = numpy.matrix([[0.0, 0.0, 0.0], |
| 67 | [0.0, 0.0, 0.0], |
| 68 | [0.0, 0.0, 1.0]]) |
| 69 | self.A[0:2, 0:2] = A_unaugmented |
| 70 | self.A[0:2, 2] = B_unaugmented |
| 71 | |
| 72 | self.B = numpy.matrix([[0.0], |
| 73 | [0.0], |
| 74 | [1.0]]) |
| 75 | |
| 76 | self.C = numpy.matrix([[1.0, 0.0, 0.0]]) |
| 77 | self.D = numpy.matrix([[0.0]]) |
| 78 | |
Brian Silverman | 7fb8e22 | 2013-03-16 20:09:58 -0700 | [diff] [blame] | 79 | self.PlaceControllerPoles([0.55, 0.35, 0.80]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 80 | |
| 81 | print "K" |
| 82 | print self.K |
| 83 | print "Placed controller poles are" |
| 84 | print numpy.linalg.eig(self.A - self.B * self.K)[0] |
| 85 | |
| 86 | self.rpl = .05 |
| 87 | self.ipl = 0.008 |
| 88 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
Brian Silverman | 23a67ca | 2013-03-16 23:48:50 -0700 | [diff] [blame] | 89 | self.rpl - 1j * self.ipl, 0.90]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 90 | print "Placed observer poles are" |
| 91 | print numpy.linalg.eig(self.A - self.L * self.C)[0] |
| 92 | |
| 93 | self.U_max = numpy.matrix([[12.0]]) |
| 94 | self.U_min = numpy.matrix([[-12.0]]) |
| 95 | |
| 96 | self.InitializeState() |
| 97 | |
| 98 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 99 | def ClipDeltaU(claw, delta_u): |
| 100 | old_u = numpy.matrix([[claw.X[2, 0]]]) |
| 101 | new_u = numpy.clip(old_u + delta_u, claw.U_min, claw.U_max) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 102 | return new_u - old_u |
| 103 | |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 104 | def main(argv): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 105 | # Simulate the response of the system to a step input. |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 106 | claw = ClawDeltaU() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 107 | simulated_x = [] |
| 108 | for _ in xrange(100): |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 109 | claw.Update(numpy.matrix([[12.0]])) |
| 110 | simulated_x.append(claw.X[0, 0]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 111 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 112 | pylab.plot(range(100), simulated_x) |
| 113 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 115 | # Simulate the closed loop response of the system to a step input. |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 116 | top_claw = ClawDeltaU("TopClaw") |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 117 | close_loop_x = [] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 118 | close_loop_u = [] |
| 119 | R = numpy.matrix([[1.0], [0.0], [0.0]]) |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 120 | top_claw.X[2, 0] = -5 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 121 | for _ in xrange(100): |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 122 | U = numpy.clip(top_claw.K * (R - top_claw.X_hat), top_claw.U_min, top_claw.U_max) |
| 123 | U = ClipDeltaU(top_claw, U) |
| 124 | top_claw.UpdateObserver(U) |
| 125 | top_claw.Update(U) |
| 126 | close_loop_x.append(top_claw.X[0, 0] * 10) |
| 127 | close_loop_u.append(top_claw.X[2, 0]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 128 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 129 | pylab.plot(range(100), close_loop_x) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 130 | pylab.plot(range(100), close_loop_u) |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 131 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 132 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 133 | # Write the generated constants out to a file. |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 134 | if len(argv) != 9: |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 135 | print "Expected .h file name and .cc file name for" |
| 136 | print "both the plant and unaugmented plant" |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 137 | else: |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 138 | top_unaug_claw = Claw("RawTopClaw") |
| 139 | top_unaug_loop_writer = control_loop.ControlLoopWriter("RawTopClaw", |
| 140 | [top_unaug_claw]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 141 | if argv[1][-3:] == '.cc': |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 142 | top_unaug_loop_writer.Write(argv[2], argv[1]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 143 | else: |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 144 | top_unaug_loop_writer.Write(argv[1], argv[2]) |
| 145 | |
| 146 | top_loop_writer = control_loop.ControlLoopWriter("TopClaw", [top_claw]) |
| 147 | if argv[3][-3:] == '.cc': |
| 148 | top_loop_writer.Write(argv[4], argv[3]) |
| 149 | else: |
| 150 | top_loop_writer.Write(argv[3], argv[4]) |
| 151 | |
| 152 | bottom_claw = ClawDeltaU("BottomClaw") |
| 153 | bottom_unaug_claw = Claw("RawBottomClaw") |
| 154 | bottom_unaug_loop_writer = control_loop.ControlLoopWriter( |
| 155 | "RawBottomClaw", [bottom_unaug_claw]) |
| 156 | if argv[5][-3:] == '.cc': |
| 157 | bottom_unaug_loop_writer.Write(argv[6], argv[5]) |
| 158 | else: |
| 159 | bottom_unaug_loop_writer.Write(argv[5], argv[6]) |
| 160 | |
| 161 | bottom_loop_writer = control_loop.ControlLoopWriter("BottomClaw", |
| 162 | [bottom_claw]) |
| 163 | if argv[7][-3:] == '.cc': |
| 164 | bottom_loop_writer.Write(argv[8], argv[7]) |
| 165 | else: |
| 166 | bottom_loop_writer.Write(argv[7], argv[8]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 167 | |
| 168 | if __name__ == '__main__': |
| 169 | sys.exit(main(sys.argv)) |