Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import control_loop |
| 4 | import numpy |
| 5 | import sys |
| 6 | from matplotlib import pylab |
| 7 | |
| 8 | class Index(control_loop.ControlLoop): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 9 | def __init__(self, J=0.00013, name="Index"): |
| 10 | super(Index, self).__init__(name) |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 11 | # 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 Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 20 | self.J = J |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 21 | # 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 Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 46 | self.PlaceControllerPoles([.40, .63]) |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 47 | |
| 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 | |
| 57 | def 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 Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 83 | # 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 Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 92 | # Write the generated constants out to a file. |
| 93 | if len(argv) != 3: |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 94 | print "Expected .h file name and .c file name" |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 95 | else: |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 96 | loop_writer = control_loop.ControlLoopWriter( |
| 97 | "Index", [index0, index1, index2, index3, index4]) |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 98 | if argv[1][-3:] == '.cc': |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 99 | loop_writer.Write(argv[2], argv[1]) |
Austin Schuh | f8c5225 | 2013-03-03 02:25:49 -0800 | [diff] [blame] | 100 | else: |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 101 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame] | 102 | |
| 103 | if __name__ == '__main__': |
| 104 | sys.exit(main(sys.argv)) |