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 |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 12 | self.stall_torque = .4982 |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 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 |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 18 | self.free_current = 1.2 |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 19 | # Moment of inertia of the shooter in kg m^2 |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 20 | # Calculate Moment of Irtia |
| 21 | self.J = 0.3 |
| 22 | # Resistance of the motor, divided by the number of motors. |
| 23 | self.R = 12.0 / self.stall_current / 2.0 |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 24 | # Motor velocity constant |
| 25 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 26 | (13.5 - self.R * self.free_current)) |
| 27 | # Torque constant |
| 28 | self.Kt = self.stall_torque / self.stall_current |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 29 | # Spring constant for the springs, N/m |
| 30 | self.Ks = 3600.0 |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 31 | # Gear ratio |
| 32 | self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0)) |
| 33 | # Control loop time step |
| 34 | self.dt = 0.01 |
| 35 | |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 36 | |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 37 | # State feedback matrices |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 38 | # TODO(james): Make this work with origins other than at kx = 0. |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 39 | self.A_continuous = numpy.matrix( |
| 40 | [[0, 1], |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 41 | [-self.Ks * 0.01 / self.J, |
| 42 | -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]]) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 43 | self.B_continuous = numpy.matrix( |
| 44 | [[0], |
| 45 | [self.Kt / (self.J * self.G * self.R)]]) |
| 46 | self.C = numpy.matrix([[1, 0]]) |
| 47 | self.D = numpy.matrix([[0]]) |
| 48 | |
| 49 | self.A, self.B = self.ContinuousToDiscrete( |
| 50 | self.A_continuous, self.B_continuous, self.dt) |
| 51 | |
| 52 | self.PlaceControllerPoles([0.85, 0.45]) |
| 53 | |
| 54 | self.rpl = .05 |
| 55 | self.ipl = 0.008 |
| 56 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 57 | self.rpl - 1j * self.ipl]) |
| 58 | |
| 59 | self.U_max = numpy.matrix([[12.0]]) |
| 60 | self.U_min = numpy.matrix([[-12.0]]) |
| 61 | |
| 62 | self.InitializeState() |
| 63 | |
| 64 | |
| 65 | class ShooterDeltaU(Shooter): |
| 66 | def __init__(self, name="Shooter"): |
| 67 | super(ShooterDeltaU, self).__init__(name) |
| 68 | A_unaugmented = self.A |
| 69 | B_unaugmented = self.B |
| 70 | |
| 71 | self.A = numpy.matrix([[0.0, 0.0, 0.0], |
| 72 | [0.0, 0.0, 0.0], |
| 73 | [0.0, 0.0, 1.0]]) |
| 74 | self.A[0:2, 0:2] = A_unaugmented |
| 75 | self.A[0:2, 2] = B_unaugmented |
| 76 | |
| 77 | self.B = numpy.matrix([[0.0], |
| 78 | [0.0], |
| 79 | [1.0]]) |
| 80 | |
| 81 | self.C = numpy.matrix([[1.0, 0.0, 0.0]]) |
| 82 | self.D = numpy.matrix([[0.0]]) |
| 83 | |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 84 | self.PlaceControllerPoles([0.55, 0.45, 0.80]) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 85 | |
| 86 | print "K" |
| 87 | print self.K |
| 88 | print "Placed controller poles are" |
| 89 | print numpy.linalg.eig(self.A - self.B * self.K)[0] |
| 90 | |
| 91 | self.rpl = .05 |
| 92 | self.ipl = 0.008 |
| 93 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 94 | self.rpl - 1j * self.ipl, 0.90]) |
| 95 | print "Placed observer poles are" |
| 96 | print numpy.linalg.eig(self.A - self.L * self.C)[0] |
| 97 | |
| 98 | self.U_max = numpy.matrix([[12.0]]) |
| 99 | self.U_min = numpy.matrix([[-12.0]]) |
| 100 | |
| 101 | self.InitializeState() |
| 102 | |
| 103 | |
| 104 | def ClipDeltaU(shooter, delta_u): |
| 105 | old_u = numpy.matrix([[shooter.X[2, 0]]]) |
| 106 | new_u = numpy.clip(old_u + delta_u, shooter.U_min, shooter.U_max) |
| 107 | return new_u - old_u |
| 108 | |
| 109 | def main(argv): |
| 110 | # Simulate the response of the system to a step input. |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 111 | shooter = Shooter() |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 112 | simulated_x = [] |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 113 | for _ in xrange(1000): |
| 114 | shooter.Update(numpy.matrix([[2.0]])) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 115 | simulated_x.append(shooter.X[0, 0]) |
| 116 | |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 117 | pylab.plot(range(1000), simulated_x) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 118 | pylab.show() |
| 119 | |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 120 | # Simulate the response of the system to a goal. |
| 121 | shooter = Shooter() |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 122 | close_loop_x = [] |
| 123 | close_loop_u = [] |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 124 | R = numpy.matrix([[1.0], [0.0]]) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 125 | for _ in xrange(100): |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 126 | feed_forward = (-numpy.linalg.lstsq(shooter.B_continuous, numpy.identity( |
| 127 | shooter.B_continuous.shape[0]))[0] * |
| 128 | shooter.A_continuous * R) |
| 129 | U = numpy.clip(shooter.K * (R - shooter.X_hat) + feed_forward, |
| 130 | shooter.U_min, shooter.U_max) |
| 131 | #U = ClipDeltaU(shooter, U) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 132 | shooter.UpdateObserver(U) |
| 133 | shooter.Update(U) |
| 134 | close_loop_x.append(shooter.X[0, 0] * 10) |
James Kuszmaul | 49d0e6c | 2014-02-03 19:46:17 -0800 | [diff] [blame^] | 135 | close_loop_u.append(U[0, 0]) |
joe | 2d92e85 | 2014-01-25 14:31:24 -0800 | [diff] [blame] | 136 | |
| 137 | pylab.plot(range(100), close_loop_x) |
| 138 | pylab.plot(range(100), close_loop_u) |
| 139 | pylab.show() |
| 140 | |
| 141 | # Write the generated constants out to a file. |
| 142 | if len(argv) != 3: |
| 143 | print "Expected .h file name and .cc file name for" |
| 144 | print "both the plant and unaugmented plant" |
| 145 | else: |
| 146 | unaug_shooter = Shooter("RawShooter") |
| 147 | unaug_loop_writer = control_loop.ControlLoopWriter("RawShooter", |
| 148 | [unaug_shooter]) |
| 149 | #if argv[3][-3:] == '.cc': |
| 150 | # unaug_loop_writer.Write(argv[4], argv[3]) |
| 151 | #else: |
| 152 | # unaug_loop_writer.Write(argv[3], argv[4]) |
| 153 | |
| 154 | loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter]) |
| 155 | if argv[1][-3:] == '.cc': |
| 156 | loop_writer.Write(argv[2], argv[1]) |
| 157 | else: |
| 158 | loop_writer.Write(argv[1], argv[2]) |
| 159 | |
| 160 | if __name__ == '__main__': |
| 161 | sys.exit(main(sys.argv)) |