joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import control_loop |
| 4 | import numpy |
| 5 | import sys |
| 6 | from matplotlib import pylab |
| 7 | |
| 8 | class Shooter(control_loop.ControlLoop): |
| 9 | def __init__(self, name="RawShooter"): |
| 10 | super(Shooter, self).__init__(name) |
| 11 | # Stall Torque in N m |
| 12 | self.stall_torque = .4862 |
| 13 | # Stall Current in Amps |
| 14 | self.stall_current = 85 |
| 15 | # Free Speed in RPM |
| 16 | self.free_speed = 19300.0 |
| 17 | # Free Current in Amps |
| 18 | self.free_current = 1.4 |
| 19 | # Moment of inertia of the shooter in kg m^2 |
| 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. |
| 22 | self.J = 2.0 |
| 23 | # Resistance of the motor |
| 24 | self.R = 12.0 / self.stall_current + 0.024 + .003 #TODO comment on these constants |
| 25 | # Motor velocity constant |
| 26 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 27 | (13.5 - self.R * self.free_current)) |
| 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 | |
| 45 | self.A, self.B = self.ContinuousToDiscrete( |
| 46 | self.A_continuous, self.B_continuous, self.dt) |
| 47 | |
| 48 | self.PlaceControllerPoles([0.85, 0.45]) |
| 49 | |
| 50 | self.rpl = .05 |
| 51 | self.ipl = 0.008 |
| 52 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 53 | self.rpl - 1j * self.ipl]) |
| 54 | |
| 55 | self.U_max = numpy.matrix([[12.0]]) |
| 56 | self.U_min = numpy.matrix([[-12.0]]) |
| 57 | |
| 58 | self.InitializeState() |
| 59 | |
| 60 | |
| 61 | class ShooterDeltaU(Shooter): |
| 62 | def __init__(self, name="Shooter"): |
| 63 | super(ShooterDeltaU, self).__init__(name) |
| 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 | |
| 80 | self.PlaceControllerPoles([0.55, 0.35, 0.80]) |
| 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, |
| 90 | self.rpl - 1j * self.ipl, 0.90]) |
| 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 | |
| 100 | def ClipDeltaU(shooter, delta_u): |
| 101 | old_u = numpy.matrix([[shooter.X[2, 0]]]) |
| 102 | new_u = numpy.clip(old_u + delta_u, shooter.U_min, shooter.U_max) |
| 103 | return new_u - old_u |
| 104 | |
| 105 | def main(argv): |
| 106 | # Simulate the response of the system to a step input. |
| 107 | shooter = ShooterDeltaU() |
| 108 | simulated_x = [] |
| 109 | for _ in xrange(100): |
| 110 | shooter.Update(numpy.matrix([[12.0]])) |
| 111 | simulated_x.append(shooter.X[0, 0]) |
| 112 | |
| 113 | pylab.plot(range(100), simulated_x) |
| 114 | pylab.show() |
| 115 | |
| 116 | # Simulate the closed loop response of the system to a step input. |
| 117 | shooter = ShooterDeltaU() |
| 118 | close_loop_x = [] |
| 119 | close_loop_u = [] |
| 120 | R = numpy.matrix([[1.0], [0.0], [0.0]]) |
| 121 | shooter.X[2, 0] = -5 |
| 122 | for _ in xrange(100): |
| 123 | U = numpy.clip(shooter.K * (R - shooter.X_hat), shooter.U_min, shooter.U_max) |
| 124 | U = ClipDeltaU(shooter, U) |
| 125 | shooter.UpdateObserver(U) |
| 126 | shooter.Update(U) |
| 127 | close_loop_x.append(shooter.X[0, 0] * 10) |
| 128 | close_loop_u.append(shooter.X[2, 0]) |
| 129 | |
| 130 | pylab.plot(range(100), close_loop_x) |
| 131 | pylab.plot(range(100), close_loop_u) |
| 132 | pylab.show() |
| 133 | |
| 134 | # Write the generated constants out to a file. |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame^] | 135 | if len(argv) != 5: |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 136 | print "Expected .h file name and .cc file name for" |
| 137 | print "both the plant and unaugmented plant" |
| 138 | else: |
| 139 | unaug_shooter = Shooter("RawShooter") |
| 140 | unaug_loop_writer = control_loop.ControlLoopWriter("RawShooter", |
| 141 | [unaug_shooter]) |
joe | 93778a6 | 2014-02-15 13:22:14 -0800 | [diff] [blame^] | 142 | if argv[3][-3:] == '.cc': |
| 143 | unaug_loop_writer.Write(argv[4], argv[3]) |
| 144 | else: |
| 145 | unaug_loop_writer.Write(argv[3], argv[4]) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 146 | |
| 147 | loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter]) |
| 148 | if argv[1][-3:] == '.cc': |
| 149 | loop_writer.Write(argv[2], argv[1]) |
| 150 | else: |
| 151 | loop_writer.Write(argv[1], argv[2]) |
| 152 | |
| 153 | if __name__ == '__main__': |
| 154 | sys.exit(main(sys.argv)) |