James Kuszmaul | adab8f9 | 2013-11-06 08:42:34 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import numpy |
| 4 | import sys |
| 5 | from matplotlib import pylab |
| 6 | import control_loop |
| 7 | import slycot |
| 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( |
| 36 | [[-self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]]) |
| 37 | self.B_continuous = numpy.matrix( |
| 38 | [[self.Kt / (self.J * self.G * self.R)]]) |
| 39 | self.C = numpy.matrix([[1]]) |
| 40 | self.D = numpy.matrix([[0]]) |
| 41 | |
| 42 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, self.B_continuous, |
| 43 | self.dt) |
| 44 | |
| 45 | self.InitializeState() |
| 46 | |
| 47 | self.PlaceControllerPoles([.8]) |
| 48 | # LQR stuff for optimization, if needed. |
| 49 | #print self.K |
| 50 | #self.R_LQR = numpy.matrix([[1.5]]) |
| 51 | #self.P = slycot.sb02od(1, 1, self.A, self.B, self.C * self.C.T, self.R, 'D')[0] |
| 52 | #self.K = (numpy.linalg.inv(self.R_LQR + self.B.T * self.P * self.B) |
| 53 | # * self.B.T * self.P * self.A) |
| 54 | #print numpy.linalg.eig(self.A - self.B * self.K) |
| 55 | |
| 56 | |
| 57 | self.PlaceObserverPoles([0.45]) |
| 58 | |
| 59 | self.U_max = numpy.matrix([[12.0]]) |
| 60 | self.U_min = numpy.matrix([[-12.0]]) |
| 61 | |
| 62 | |
| 63 | def main(argv): |
| 64 | # Simulate the response of the system to a step input. |
| 65 | shooter_data = numpy.genfromtxt('shooter/shooter_data.csv', delimiter=',') |
| 66 | shooter = Shooter() |
| 67 | simulated_x = [] |
| 68 | real_x = [] |
| 69 | x_vel = [] |
| 70 | initial_x = shooter_data[0, 2] |
| 71 | last_x = initial_x |
| 72 | for i in xrange(shooter_data.shape[0]): |
| 73 | shooter.Update(numpy.matrix([[shooter_data[i, 1]]])) |
| 74 | simulated_x.append(shooter.X[0, 0]) |
| 75 | x_offset = shooter_data[i, 2] - initial_x |
| 76 | real_x.append(x_offset) |
| 77 | x_vel.append((shooter_data[i, 2] - last_x) * 100.0) |
| 78 | last_x = shooter_data[i, 2] |
| 79 | |
| 80 | sim_delay = 1 |
| 81 | # pylab.plot(range(sim_delay, shooter_data.shape[0] + sim_delay), |
| 82 | # simulated_x, label='Simulation') |
| 83 | # pylab.plot(range(shooter_data.shape[0]), real_x, label='Reality') |
| 84 | # pylab.plot(range(shooter_data.shape[0]), x_vel, label='Velocity') |
| 85 | # pylab.legend() |
| 86 | # pylab.show() |
| 87 | |
| 88 | # Simulate the closed loop response of the system to a step input. |
| 89 | shooter = Shooter() |
| 90 | close_loop_x = [] |
| 91 | close_loop_U = [] |
| 92 | velocity_goal = 400 |
| 93 | R = numpy.matrix([[velocity_goal]]) |
| 94 | goal = False |
| 95 | for i in pylab.linspace(0,1.99,200): |
| 96 | # Iterate the position up. |
| 97 | R = numpy.matrix([[velocity_goal]]) |
| 98 | U = numpy.clip(shooter.K * (R - shooter.X_hat) + |
| 99 | (numpy.identity(shooter.A.shape[0]) - shooter.A) * R / shooter.B, |
| 100 | shooter.U_min, shooter.U_max) |
| 101 | shooter.UpdateObserver(U) |
| 102 | shooter.Update(U) |
| 103 | close_loop_x.append(shooter.X[0, 0]) |
| 104 | close_loop_U.append(U[0, 0]) |
| 105 | if (abs(R[0, 0] - shooter.X[0, 0]) < R[0, 0]* 0.01 and (not goal)): |
| 106 | goal = True |
| 107 | print i |
| 108 | |
| 109 | #pylab.plotfile("shooter.csv", (0,1)) |
| 110 | pylab.plot(pylab.linspace(0,1.99,200), close_loop_U) |
| 111 | #pylab.plotfile("shooter.csv", (0,2)) |
| 112 | pylab.plot(pylab.linspace(0,1.99,200), close_loop_x) |
| 113 | pylab.show() |
| 114 | |
| 115 | # Simulate spin down. |
| 116 | spin_down_x = []; |
| 117 | for _ in xrange(150): |
| 118 | U = 0 |
| 119 | shooter.UpdateObserver(U) |
| 120 | shooter.Update(U) |
| 121 | spin_down_x.append(shooter.X[0, 0]) |
| 122 | |
| 123 | #pylab.plot(range(150), spin_down_x) |
| 124 | #pylab.show() |
| 125 | |
| 126 | if len(argv) != 3: |
| 127 | print "Expected .h file name and .cc file name" |
| 128 | else: |
| 129 | loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter]) |
| 130 | if argv[1][-3:] == '.cc': |
| 131 | loop_writer.Write(argv[2], argv[1]) |
| 132 | else: |
| 133 | loop_writer.Write(argv[1], argv[2]) |
| 134 | |
| 135 | |
| 136 | if __name__ == '__main__': |
| 137 | sys.exit(main(sys.argv)) |