blob: 1d68f51f28726f582688d216798fab0b7088f219 [file] [log] [blame]
James Kuszmaulf7f5ec12013-11-01 17:58:58 -07001#!/usr/bin/python
2
3import numpy
4import sys
5from matplotlib import pylab
6import control_loop
James Kuszmaulc4405f92013-11-05 12:44:50 -08007import slycot
James Kuszmaulf7f5ec12013-11-01 17:58:58 -07008
9class 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 Kuszmaulc4405f92013-11-05 12:44:50 -080036 [[-self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070037 self.B_continuous = numpy.matrix(
James Kuszmaulc4405f92013-11-05 12:44:50 -080038 [[self.Kt / (self.J * self.G * self.R)]])
39 self.C = numpy.matrix([[1]])
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070040 self.D = numpy.matrix([[0]])
41
James Kuszmaule78add72013-11-01 19:37:49 -070042 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
43 self.dt)
44
45 self.InitializeState()
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070046
James Kuszmaulc4405f92013-11-05 12:44:50 -080047 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 Kuszmaulf7f5ec12013-11-01 17:58:58 -070054
James Kuszmaulc4405f92013-11-05 12:44:50 -080055
56 self.PlaceObserverPoles([0.45])
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070057
58 self.U_max = numpy.matrix([[12.0]])
59 self.U_min = numpy.matrix([[-12.0]])
60
61
62def 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 Kuszmaulc4405f92013-11-05 12:44:50 -080080# 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 Kuszmaule78add72013-11-01 19:37:49 -070085# pylab.show()
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070086
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 Kuszmaulc4405f92013-11-05 12:44:50 -080091 velocity_goal = 400
92 R = numpy.matrix([[velocity_goal]])
93 goal = False
94 for i in pylab.linspace(0,1.99,200):
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070095 # Iterate the position up.
James Kuszmaulc4405f92013-11-05 12:44:50 -080096 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 Kuszmaulf7f5ec12013-11-01 17:58:58 -070099 shooter.U_min, shooter.U_max)
100 shooter.UpdateObserver(U)
101 shooter.Update(U)
James Kuszmaulc4405f92013-11-05 12:44:50 -0800102 close_loop_x.append(shooter.X[0, 0])
James Kuszmaulf7f5ec12013-11-01 17:58:58 -0700103 close_loop_U.append(U[0, 0])
James Kuszmaulc4405f92013-11-05 12:44:50 -0800104 if (abs(R[0, 0] - shooter.X[0, 0]) < R[0, 0]* 0.01 and (not goal)):
105 goal = True
106 print i
James Kuszmaulf7f5ec12013-11-01 17:58:58 -0700107
108 #pylab.plotfile("shooter.csv", (0,1))
James Kuszmaulc4405f92013-11-05 12:44:50 -0800109 pylab.plot(pylab.linspace(0,1.99,200), close_loop_U)
James Kuszmaulf7f5ec12013-11-01 17:58:58 -0700110 #pylab.plotfile("shooter.csv", (0,2))
James Kuszmaulc4405f92013-11-05 12:44:50 -0800111 pylab.plot(pylab.linspace(0,1.99,200), close_loop_x)
112 pylab.show()
James Kuszmaulf7f5ec12013-11-01 17:58:58 -0700113
114 # Simulate spin down.
115 spin_down_x = [];
James Kuszmaulf7f5ec12013-11-01 17:58:58 -0700116 for _ in xrange(150):
117 U = 0
118 shooter.UpdateObserver(U)
119 shooter.Update(U)
James Kuszmaulc4405f92013-11-05 12:44:50 -0800120 spin_down_x.append(shooter.X[0, 0])
James Kuszmaulf7f5ec12013-11-01 17:58:58 -0700121
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
135if __name__ == '__main__':
136 sys.exit(main(sys.argv))