blob: cc7930ff31ef33dbddfb29f7abb6706c36643138 [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
James Kuszmaule78add72013-11-01 19:37:49 -070043 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
44 self.dt)
45
46 self.InitializeState()
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070047
48 self.PlaceControllerPoles([.6, .981])
49
50 self.rpl = .45
51 self.ipl = 0.07
52 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
53 self.rpl - 1j * self.ipl])
54
55 self.U_max = numpy.matrix([[12.0]])
56 self.U_min = numpy.matrix([[-12.0]])
57
58
59def main(argv):
60 # Simulate the response of the system to a step input.
61 shooter_data = numpy.genfromtxt('shooter/shooter_data.csv', delimiter=',')
62 shooter = Shooter()
63 simulated_x = []
64 real_x = []
65 x_vel = []
66 initial_x = shooter_data[0, 2]
67 last_x = initial_x
68 for i in xrange(shooter_data.shape[0]):
69 shooter.Update(numpy.matrix([[shooter_data[i, 1]]]))
70 simulated_x.append(shooter.X[0, 0])
71 x_offset = shooter_data[i, 2] - initial_x
72 real_x.append(x_offset)
73 x_vel.append((shooter_data[i, 2] - last_x) * 100.0)
74 last_x = shooter_data[i, 2]
75
76 sim_delay = 1
77 pylab.plot(range(sim_delay, shooter_data.shape[0] + sim_delay),
78 simulated_x, label='Simulation')
79 pylab.plot(range(shooter_data.shape[0]), real_x, label='Reality')
80 pylab.plot(range(shooter_data.shape[0]), x_vel, label='Velocity')
81 pylab.legend()
James Kuszmaule78add72013-11-01 19:37:49 -070082# pylab.show()
James Kuszmaulf7f5ec12013-11-01 17:58:58 -070083
84 # Simulate the closed loop response of the system to a step input.
85 shooter = Shooter()
86 close_loop_x = []
87 close_loop_U = []
88 velocity_goal = 300
89 R = numpy.matrix([[0.0], [velocity_goal]])
90 for _ in pylab.linspace(0,1.99,200):
91 # Iterate the position up.
92 R = numpy.matrix([[R[0, 0] + 10.5], [velocity_goal]])
93 # Prevents the position goal from going beyond what is necessary.
94 velocity_weight_scalar = 0.35
95 max_reference = (
96 (shooter.U_max[0, 0] - velocity_weight_scalar *
97 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) /
98 shooter.K[0, 0] +
99 shooter.X_hat[0, 0])
100 min_reference = (
101 (shooter.U_min[0, 0] - velocity_weight_scalar *
102 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) /
103 shooter.K[0, 0] +
104 shooter.X_hat[0, 0])
105 R[0, 0] = numpy.clip(R[0, 0], min_reference, max_reference)
106 U = numpy.clip(shooter.K * (R - shooter.X_hat),
107 shooter.U_min, shooter.U_max)
108 shooter.UpdateObserver(U)
109 shooter.Update(U)
110 close_loop_x.append(shooter.X[1, 0])
111 close_loop_U.append(U[0, 0])
112
113 #pylab.plotfile("shooter.csv", (0,1))
114 #pylab.plot(pylab.linspace(0,1.99,200), close_loop_U, 'ro')
115 #pylab.plotfile("shooter.csv", (0,2))
116 pylab.plot(pylab.linspace(0,1.99,200), close_loop_x, 'ro')
James Kuszmaule78add72013-11-01 19:37:49 -0700117# pylab.show()
James Kuszmaulf7f5ec12013-11-01 17:58:58 -0700118
119 # Simulate spin down.
120 spin_down_x = [];
121 R = numpy.matrix([[50.0], [0.0]])
122 for _ in xrange(150):
123 U = 0
124 shooter.UpdateObserver(U)
125 shooter.Update(U)
126 spin_down_x.append(shooter.X[1, 0])
127
128 #pylab.plot(range(150), spin_down_x)
129 #pylab.show()
130
131 if len(argv) != 3:
132 print "Expected .h file name and .cc file name"
133 else:
134 loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter])
135 if argv[1][-3:] == '.cc':
136 loop_writer.Write(argv[2], argv[1])
137 else:
138 loop_writer.Write(argv[1], argv[2])
139
140
141if __name__ == '__main__':
142 sys.exit(main(sys.argv))