blob: ed041984dbddb9ba2be9fe14394e8a4d3db37f15 [file] [log] [blame]
James Kuszmaul4a4622b2013-03-02 16:28:29 -08001#!/usr/bin/python
2
3import control_loop
4import numpy
5import sys
6from matplotlib import pylab
7
8class AngleAdjust(control_loop.ControlLoop):
9 def __init__(self):
10 super(AngleAdjust, self).__init__("AngleAdjust")
11 # Stall Torque in N m
12 self.stall_torque = .428
13 # Stall Current in Amps
14 self.stall_current = 63.8
15 # Free Speed in RPM
16 self.free_speed = 16000.0
17 # Free Current in Amps
18 self.free_current = 1.2
19 # Moment of inertia of the angle adjust about the shooter's pivot in kg m^2
20 self.J = 0.41085133
21 # Resistance of the motor
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 of the gearbox multiplied by the ratio of the radii of
29 # the output and the angle adjust curve, which is essentially another gear.
30 self.G = (1.0 / 50.0) * (0.01905 / 0.41964)
31 # Control loop time step
32 self.dt = 0.01
33
34 # State feedback matrices
35 self.A_continuous = numpy.matrix(
36 [[0, 1],
37 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
38 self.B_continuous = numpy.matrix(
39 [[0],
40 [self.Kt / (self.J * self.G * self.R)]])
41 self.C = numpy.matrix([[1, 0]])
42 self.D = numpy.matrix([[0]])
43
44 self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
45 self.dt, self.C)
46
47 self.PlaceControllerPoles([.89, .85])
48
49 self.rpl = .05
50 self.ipl = 0.008
51 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
52 self.rpl - 1j * self.ipl])
53
54 self.U_max = numpy.matrix([[12.0]])
55 self.U_min = numpy.matrix([[-12.0]])
56
57def main(argv):
58 # Simulate the response of the system to a step input.
59 angle_adjust = AngleAdjust()
60 simulated_x = []
61 for _ in xrange(100):
62 angle_adjust.Update(numpy.matrix([[12.0]]))
63 simulated_x.append(angle_adjust.X[0, 0])
64
65 pylab.plot(range(100), simulated_x)
66 pylab.show()
67
68 # Simulate the closed loop response of the system to a step input.
69 angle_adjust = AngleAdjust()
70 close_loop_x = []
71 R = numpy.matrix([[1.0], [0.0]])
72 for _ in xrange(100):
73 U = numpy.clip(angle_adjust.K * (R - angle_adjust.X_hat), angle_adjust.U_min, angle_adjust.U_max)
74 angle_adjust.UpdateObserver(U)
75 angle_adjust.Update(U)
76 close_loop_x.append(angle_adjust.X[0, 0])
77
78 pylab.plot(range(100), close_loop_x)
79 pylab.show()
80
81 # Write the generated constants out to a file.
82 if len(argv) != 3:
83 print "Expected .cc file name and .h file name"
84 else:
85 angle_adjust.DumpHeaderFile(argv[1])
86 angle_adjust.DumpCppFile(argv[2], argv[1])
87
88if __name__ == '__main__':
89 sys.exit(main(sys.argv))