blob: f165afeeec31adcd2ec752e718345a26359c211b [file] [log] [blame]
Austin Schuhd78ab542013-03-01 22:22:19 -08001#!/usr/bin/python
2
3import control_loop
4import numpy
5import sys
6from matplotlib import pylab
7
8class Index(control_loop.ControlLoop):
Austin Schuhe3490622013-03-13 01:24:30 -07009 def __init__(self, J=0.00013, name="Index"):
10 super(Index, self).__init__(name)
Austin Schuhd78ab542013-03-01 22:22:19 -080011 # Stall Torque in N m
12 self.stall_torque = 0.4862
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.5
19 # Moment of inertia of the index in kg m^2
Austin Schuhe3490622013-03-13 01:24:30 -070020 self.J = J
Austin Schuhd78ab542013-03-01 22:22:19 -080021 # Resistance of the motor
22 self.R = 12.0 / self.stall_current + 0.024 + .003
23 # Motor velocity constant
24 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
25 (13.5 - self.R * self.free_current))
26 # Torque constant
27 self.Kt = self.stall_torque / self.stall_current
28 # Gear ratio
29 self.G = 1.0 / ((40.0 / 11.0) * (34.0 / 30.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
Austin Schuhe3490622013-03-13 01:24:30 -070046 self.PlaceControllerPoles([.40, .63])
Austin Schuhd78ab542013-03-01 22:22:19 -080047
48 self.rpl = .05
49 self.ipl = 0.008
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 index = Index()
60 simulated_x = []
61 simulated_v = []
62 for _ in xrange(100):
63 index.Update(numpy.matrix([[12.0]]))
64 simulated_x.append(index.X[0, 0])
65 simulated_v.append(index.X[1, 0])
66
67 pylab.plot(range(100), simulated_v)
68 pylab.show()
69
70 # Simulate the closed loop response of the system to a step input.
71 index = Index()
72 close_loop_x = []
73 R = numpy.matrix([[1.0], [0.0]])
74 for _ in xrange(100):
75 U = numpy.clip(index.K * (R - index.X_hat), index.U_min, index.U_max)
76 index.UpdateObserver(U)
77 index.Update(U)
78 close_loop_x.append(index.X[0, 0])
79
80 pylab.plot(range(100), close_loop_x)
81 pylab.show()
82
Austin Schuhe3490622013-03-13 01:24:30 -070083 # Set the constants for the number of discs that we expect to see.
84 # The c++ code expects that the index in the array will be the number of
85 # discs.
86 index0 = Index(0.00010, "Index0Disc")
87 index1 = Index(0.00013, "Index1Disc")
88 index2 = Index(0.00013, "Index2Disc")
89 index3 = Index(0.00018, "Index3Disc")
90 index4 = Index(0.00025, "Index4Disc")
91
Austin Schuhd78ab542013-03-01 22:22:19 -080092 # Write the generated constants out to a file.
93 if len(argv) != 3:
Austin Schuhf8c52252013-03-03 02:25:49 -080094 print "Expected .h file name and .c file name"
Austin Schuhd78ab542013-03-01 22:22:19 -080095 else:
Austin Schuhe3490622013-03-13 01:24:30 -070096 loop_writer = control_loop.ControlLoopWriter(
97 "Index", [index0, index1, index2, index3, index4])
Austin Schuhf8c52252013-03-03 02:25:49 -080098 if argv[1][-3:] == '.cc':
Austin Schuhe3490622013-03-13 01:24:30 -070099 loop_writer.Write(argv[2], argv[1])
Austin Schuhf8c52252013-03-03 02:25:49 -0800100 else:
Austin Schuhe3490622013-03-13 01:24:30 -0700101 loop_writer.Write(argv[1], argv[2])
Austin Schuhd78ab542013-03-01 22:22:19 -0800102
103if __name__ == '__main__':
104 sys.exit(main(sys.argv))