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