blob: 663a6b2151dde6953442978878200ff7b94a6b12 [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
Austin Schuh0e38a5d2013-03-03 03:53:35 -080012 self.stall_torque = 0.49819248
James Kuszmaulcdd033e2013-03-02 15:10:43 -080013 # Stall Current in Amps
Austin Schuh0e38a5d2013-03-03 03:53:35 -080014 self.stall_current = 85
James Kuszmaulcdd033e2013-03-02 15:10:43 -080015 # Free Speed in RPM
Austin Schuh0e38a5d2013-03-03 03:53:35 -080016 self.free_speed = 19300.0
James Kuszmaulcdd033e2013-03-02 15:10:43 -080017 # 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
Austin Schuh0e38a5d2013-03-03 03:53:35 -080024 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
James Kuszmaulcdd033e2013-03-02 15:10:43 -080025 (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
Austin Schuh0e38a5d2013-03-03 03:53:35 -080034 self.A_continuous = numpy.matrix(
James Kuszmaulcdd033e2013-03-02 15:10:43 -080035 [[0, 1],
36 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
Austin Schuh0e38a5d2013-03-03 03:53:35 -080037 self.B_continuous = numpy.matrix(
James Kuszmaulcdd033e2013-03-02 15:10:43 -080038 [[0],
39 [self.Kt / (self.J * self.G * self.R)]])
Austin Schuh0e38a5d2013-03-03 03:53:35 -080040 self.C = numpy.matrix([[1, 0]])
41 self.D = numpy.matrix([[0]])
James Kuszmaulcdd033e2013-03-02 15:10:43 -080042
Austin Schuh0e38a5d2013-03-03 03:53:35 -080043 self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
James Kuszmaulcdd033e2013-03-02 15:10:43 -080044 self.dt, self.C)
45
Austin Schuh0e38a5d2013-03-03 03:53:35 -080046 self.PlaceControllerPoles([.6, .981])
James Kuszmaulcdd033e2013-03-02 15:10:43 -080047
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 = []
Austin Schuh0e38a5d2013-03-03 03:53:35 -080072 velocity_goal = 300
James Kuszmaulcdd033e2013-03-02 15:10:43 -080073 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
Austin Schuh0e38a5d2013-03-03 03:53:35 -080079 max_reference = (
80 (shooter.U_max[0, 0] - velocity_weight_scalar *
81 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) /
82 shooter.K[0, 0] +
83 shooter.X_hat[0, 0])
84 min_reference = (
85 (shooter.U_min[0, 0] - velocity_weight_scalar *
86 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) /
87 shooter.K[0, 0] +
88 shooter.X_hat[0, 0])
89 R[0, 0] = numpy.clip(R[0, 0], min_reference, max_reference)
90 U = numpy.clip(shooter.K * (R - shooter.X_hat),
91 shooter.U_min, shooter.U_max)
James Kuszmaulcdd033e2013-03-02 15:10:43 -080092 shooter.UpdateObserver(U)
93 shooter.Update(U)
94 close_loop_x.append(shooter.X[1, 0])
95 close_loop_U.append(U[0, 0])
96
Austin Schuh0e38a5d2013-03-03 03:53:35 -080097 #pylab.plotfile("shooter.csv", (0,1))
98 #pylab.plot(pylab.linspace(0,1.99,200), close_loop_U, 'ro')
99 #pylab.plotfile("shooter.csv", (0,2))
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800100 pylab.plot(pylab.linspace(0,1.99,200), close_loop_x, 'ro')
101 pylab.show()
102
103 # Simulate spin down.
104 spin_down_x = [];
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800105 R = numpy.matrix([[50.0], [0.0]])
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800106 for _ in xrange(150):
107 U = 0
108 shooter.UpdateObserver(U)
109 shooter.Update(U)
110 spin_down_x.append(shooter.X[1, 0])
111
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800112 #pylab.plot(range(150), spin_down_x)
113 #pylab.show()
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800114
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800115 if len(argv) != 3:
116 print "Expected .h file name and .cc file name"
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800117 else:
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800118 if argv[1][-3:] == '.cc':
119 print '.cc file is second'
120 else:
121 shooter.DumpHeaderFile(argv[1])
122 shooter.DumpCppFile(argv[2], argv[1])
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800123
124
125if __name__ == '__main__':
126 sys.exit(main(sys.argv))