James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import numpy |
| 4 | import sys |
| 5 | from matplotlib import pylab |
| 6 | import control_loop |
| 7 | |
| 8 | class Shooter(control_loop.ControlLoop): |
| 9 | def __init__(self): |
| 10 | super(Shooter, self).__init__("Shooter") |
| 11 | # Stall Torque in N m |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 12 | self.stall_torque = 0.49819248 |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 13 | # Stall Current in Amps |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 14 | self.stall_current = 85 |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 15 | # Free Speed in RPM |
Austin Schuh | e052d8a | 2013-03-10 18:58:18 -0700 | [diff] [blame] | 16 | self.free_speed = 19300.0 - 1500.0 |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 17 | # Free Current in Amps |
| 18 | self.free_current = 1.4 |
| 19 | # Moment of inertia of the shooter wheel in kg m^2 |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 20 | self.J = 0.0032 |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 21 | # Resistance of the motor, divided by 2 to account for the 2 motors |
| 22 | self.R = 12.0 / self.stall_current / 2 |
| 23 | # Motor velocity constant |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 24 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 25 | (12.0 - self.R * self.free_current)) |
| 26 | # Torque constant |
| 27 | self.Kt = self.stall_torque / self.stall_current |
| 28 | # Gear ratio |
| 29 | self.G = 11.0 / 34.0 |
| 30 | # Control loop time step |
| 31 | self.dt = 0.01 |
| 32 | |
| 33 | # State feedback matrices |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 34 | self.A_continuous = numpy.matrix( |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 35 | [[0, 1], |
| 36 | [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]]) |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 37 | self.B_continuous = numpy.matrix( |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 38 | [[0], |
| 39 | [self.Kt / (self.J * self.G * self.R)]]) |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 40 | self.C = numpy.matrix([[1, 0]]) |
| 41 | self.D = numpy.matrix([[0]]) |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 43 | self.ContinuousToDiscrete(self.A_continuous, self.B_continuous, |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 44 | self.dt, self.C) |
| 45 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 46 | self.PlaceControllerPoles([.6, .981]) |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 47 | |
| 48 | self.rpl = .45 |
| 49 | self.ipl = 0.07 |
| 50 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 51 | self.rpl - 1j * self.ipl]) |
| 52 | |
| 53 | self.U_max = numpy.matrix([[12.0]]) |
| 54 | self.U_min = numpy.matrix([[-12.0]]) |
| 55 | |
| 56 | |
| 57 | def main(argv): |
| 58 | # Simulate the response of the system to a step input. |
Austin Schuh | 383878f | 2013-03-10 01:34:34 -0800 | [diff] [blame] | 59 | shooter_data = numpy.genfromtxt('shooter/shooter_data.csv', delimiter=',') |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 60 | shooter = Shooter() |
| 61 | simulated_x = [] |
Austin Schuh | 383878f | 2013-03-10 01:34:34 -0800 | [diff] [blame] | 62 | real_x = [] |
| 63 | x_vel = [] |
| 64 | initial_x = shooter_data[0, 2] |
| 65 | last_x = initial_x |
| 66 | for i in xrange(shooter_data.shape[0]): |
| 67 | shooter.Update(numpy.matrix([[shooter_data[i, 1]]])) |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 68 | simulated_x.append(shooter.X[0, 0]) |
Austin Schuh | 383878f | 2013-03-10 01:34:34 -0800 | [diff] [blame] | 69 | x_offset = shooter_data[i, 2] - initial_x |
Austin Schuh | e052d8a | 2013-03-10 18:58:18 -0700 | [diff] [blame] | 70 | real_x.append(x_offset) |
| 71 | x_vel.append((shooter_data[i, 2] - last_x) * 100.0) |
Austin Schuh | 383878f | 2013-03-10 01:34:34 -0800 | [diff] [blame] | 72 | last_x = shooter_data[i, 2] |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 73 | |
Austin Schuh | 383878f | 2013-03-10 01:34:34 -0800 | [diff] [blame] | 74 | sim_delay = 1 |
| 75 | pylab.plot(range(sim_delay, shooter_data.shape[0] + sim_delay), |
| 76 | simulated_x, label='Simulation') |
| 77 | pylab.plot(range(shooter_data.shape[0]), real_x, label='Reality') |
| 78 | pylab.plot(range(shooter_data.shape[0]), x_vel, label='Velocity') |
| 79 | pylab.legend() |
| 80 | pylab.show() |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 81 | |
| 82 | # Simulate the closed loop response of the system to a step input. |
| 83 | shooter = Shooter() |
| 84 | close_loop_x = [] |
| 85 | close_loop_U = [] |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 86 | velocity_goal = 300 |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 87 | R = numpy.matrix([[0.0], [velocity_goal]]) |
| 88 | for _ in pylab.linspace(0,1.99,200): |
| 89 | # Iterate the position up. |
| 90 | R = numpy.matrix([[R[0, 0] + 10.5], [velocity_goal]]) |
| 91 | # Prevents the position goal from going beyond what is necessary. |
| 92 | velocity_weight_scalar = 0.35 |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 93 | max_reference = ( |
| 94 | (shooter.U_max[0, 0] - velocity_weight_scalar * |
| 95 | (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) / |
| 96 | shooter.K[0, 0] + |
| 97 | shooter.X_hat[0, 0]) |
| 98 | min_reference = ( |
| 99 | (shooter.U_min[0, 0] - velocity_weight_scalar * |
| 100 | (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) / |
| 101 | shooter.K[0, 0] + |
| 102 | shooter.X_hat[0, 0]) |
| 103 | R[0, 0] = numpy.clip(R[0, 0], min_reference, max_reference) |
| 104 | U = numpy.clip(shooter.K * (R - shooter.X_hat), |
| 105 | shooter.U_min, shooter.U_max) |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 106 | shooter.UpdateObserver(U) |
| 107 | shooter.Update(U) |
| 108 | close_loop_x.append(shooter.X[1, 0]) |
| 109 | close_loop_U.append(U[0, 0]) |
| 110 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 111 | #pylab.plotfile("shooter.csv", (0,1)) |
| 112 | #pylab.plot(pylab.linspace(0,1.99,200), close_loop_U, 'ro') |
| 113 | #pylab.plotfile("shooter.csv", (0,2)) |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 114 | pylab.plot(pylab.linspace(0,1.99,200), close_loop_x, 'ro') |
| 115 | pylab.show() |
| 116 | |
| 117 | # Simulate spin down. |
| 118 | spin_down_x = []; |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 119 | R = numpy.matrix([[50.0], [0.0]]) |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 120 | for _ in xrange(150): |
| 121 | U = 0 |
| 122 | shooter.UpdateObserver(U) |
| 123 | shooter.Update(U) |
| 124 | spin_down_x.append(shooter.X[1, 0]) |
| 125 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 126 | #pylab.plot(range(150), spin_down_x) |
| 127 | #pylab.show() |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 128 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 129 | if len(argv) != 3: |
| 130 | print "Expected .h file name and .cc file name" |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 131 | else: |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 132 | loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter]) |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 133 | if argv[1][-3:] == '.cc': |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 134 | loop_writer.Write(argv[2], argv[1]) |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 135 | else: |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 136 | loop_writer.Write(argv[1], argv[2]) |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 137 | |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | sys.exit(main(sys.argv)) |