blob: 7cbe617fdf31bcd26b34bebd15e7721dac75dfab [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
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 = 2.42211227883219
13 # Stall Current in Amps
14 self.stall_current = 133
15 # Free Speed in RPM
16 self.free_speed = 4650.0
17 # Free Current in Amps
18 self.free_current = 2.7
19 # Moment of inertia of the shooter wheel in kg m^2
20 self.J = 0.0032
21 # Resistance of the motor, divided by 2 to account for the 2 motors
22 self.R = 12.0 / self.stall_current
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 = 40.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([.6, .981])
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_data = numpy.genfromtxt('shooter/shooter_data.csv', delimiter=',')
60 shooter = Shooter()
61 simulated_x = []
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]]]))
68 simulated_x.append(shooter.X[0, 0])
69 x_offset = shooter_data[i, 2] - initial_x
70 real_x.append(x_offset)
71 x_vel.append((shooter_data[i, 2] - last_x) * 100.0)
72 last_x = shooter_data[i, 2]
73
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()
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 = []
86 velocity_goal = 300
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
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)
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
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))
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 = [];
119 R = numpy.matrix([[50.0], [0.0]])
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
126 #pylab.plot(range(150), spin_down_x)
127 #pylab.show()
128
129 if len(argv) != 3:
130 print "Expected .h file name and .cc file name"
131 else:
132 loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter])
133 if argv[1][-3:] == '.cc':
134 loop_writer.Write(argv[2], argv[1])
135 else:
136 loop_writer.Write(argv[1], argv[2])
137
138
139if __name__ == '__main__':
140 sys.exit(main(sys.argv))