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