blob: ab7b163017ac6d36d4fa4c8830922839db815b42 [file] [log] [blame]
James Kuszmaulcdd033e2013-03-02 15:10:43 -08001#!/usr/bin/python
2
3import numpy
4import sys
5from matplotlib import pylab
6import control_loop
7
8class Shooter(control_loop.ControlLoop):
9 def __init__(self):
10 super(Shooter, self).__init__("Shooter")
11 # Stall Torque in N m
12 self.stall_torque = 0.49819248
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
18 self.free_current = 1.4
19 # Moment of inertia of the shooter wheel in kg m^2
20 self.J = 0.00161906
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
24 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
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
34 self.A_continuous = numpy.matrix(
35 [[0, 1],
36 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
37 self.B_continuous = numpy.matrix(
38 [[0],
39 [self.Kt / (self.J * self.G * self.R)]])
40 self.C = numpy.matrix([[1, 0]])
41 self.D = numpy.matrix([[0]])
42
43 self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
44 self.dt, self.C)
45
46 self.PlaceControllerPoles([.2, .15])
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
57def main(argv):
58 # Simulate the response of the system to a step input.
59 shooter = Shooter()
60 simulated_x = []
61 for _ in xrange(500):
62 shooter.Update(numpy.matrix([[12.0]]))
63 simulated_x.append(shooter.X[0, 0])
64
65# pylab.plot(range(500), simulated_x)
66# pylab.show()
67
68 # Simulate the closed loop response of the system to a step input.
69 shooter = Shooter()
70 close_loop_x = []
71 close_loop_U = []
72 velocity_goal = 1050.0
73 R = numpy.matrix([[0.0], [velocity_goal]])
74 for _ in pylab.linspace(0,1.99,200):
75 # Iterate the position up.
76 R = numpy.matrix([[R[0, 0] + 10.5], [velocity_goal]])
77 # Prevents the position goal from going beyond what is necessary.
78 velocity_weight_scalar = 0.35
79 max_reference = ((shooter.U_max[0, 0] - velocity_weight_scalar *
80 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) / shooter.K[0, 0]
81 + shooter.X_hat[0, 0])
82 min_reference = ((shooter.U_min[0, 0] - velocity_weight_scalar *
83 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) / shooter.K[0, 0]
84 + shooter.X_hat[0, 0])
85 R[0, 0] = max(min(R[0, 0], max_reference), min_reference)
86 U = numpy.clip(shooter.K * (R - shooter.X_hat), shooter.U_min, shooter.U_max)
87 shooter.UpdateObserver(U)
88 shooter.Update(U)
89 close_loop_x.append(shooter.X[1, 0])
90 close_loop_U.append(U[0, 0])
91
92# pylab.plotfile("shooter.csv", (0,1))
93# pylab.plot(pylab.linspace(0,1.99,200), close_loop_U, 'ro')
94# pylab.plotfile("shooter.csv", (0,2))
95 pylab.plot(pylab.linspace(0,1.99,200), close_loop_x, 'ro')
96 pylab.show()
97
98 # Simulate spin down.
99 spin_down_x = [];
100 R = numpy.matrix([[0.0], [0.0]])
101 for _ in xrange(150):
102 U = 0
103 shooter.UpdateObserver(U)
104 shooter.Update(U)
105 spin_down_x.append(shooter.X[1, 0])
106
107# pylab.plot(range(150), spin_down_x)
108# pylab.show()
109
110
111 if len(argv) != 3:
112 print "Expected .cc file name and .h file name"
113 else:
114 shooter.DumpHeaderFile(argv[1])
115 shooter.DumpCppFile(argv[2], argv[1])
116
117
118if __name__ == '__main__':
119 sys.exit(main(sys.argv))