Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 3 | import control_loop |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 4 | import numpy |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 5 | import sys |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 6 | from matplotlib import pylab |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 7 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 8 | class Wrist(control_loop.ControlLoop): |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 9 | def __init__(self, name="RawWrist"): |
| 10 | super(Wrist, self).__init__(name) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 11 | # Stall Torque in N m |
| 12 | self.stall_torque = 1.4 |
| 13 | # Stall Current in Amps |
| 14 | self.stall_current = 86 |
| 15 | # Free Speed in RPM |
| 16 | self.free_speed = 6200.0 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 17 | # Free Current in Amps |
| 18 | self.free_current = 1.5 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 19 | # Moment of inertia of the wrist in kg m^2 |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 20 | # TODO(aschuh): Measure this in reality. It doesn't seem high enough. |
| 21 | # James measured 0.51, but that can't be right given what I am seeing. |
Brian Silverman | 0f63738 | 2013-03-03 17:44:46 -0800 | [diff] [blame] | 22 | self.J = 2.0 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 23 | # Resistance of the motor |
| 24 | self.R = 12.0 / self.stall_current + 0.024 + .003 |
| 25 | # Motor velocity constant |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 26 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 27 | (13.5 - self.R * self.free_current)) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 28 | # Torque constant |
| 29 | self.Kt = self.stall_torque / self.stall_current |
| 30 | # Gear ratio |
| 31 | self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0)) |
| 32 | # Control loop time step |
| 33 | self.dt = 0.01 |
| 34 | |
| 35 | # State feedback matrices |
| 36 | self.A_continuous = numpy.matrix( |
| 37 | [[0, 1], |
| 38 | [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]]) |
| 39 | self.B_continuous = numpy.matrix( |
| 40 | [[0], |
| 41 | [self.Kt / (self.J * self.G * self.R)]]) |
| 42 | self.C = numpy.matrix([[1, 0]]) |
| 43 | self.D = numpy.matrix([[0]]) |
| 44 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 45 | self.A, self.B = self.ContinuousToDiscrete( |
| 46 | self.A_continuous, self.B_continuous, self.dt) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 47 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 48 | self.PlaceControllerPoles([0.85, 0.45]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 49 | |
| 50 | self.rpl = .05 |
| 51 | self.ipl = 0.008 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 52 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 53 | self.rpl - 1j * self.ipl]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 54 | |
| 55 | self.U_max = numpy.matrix([[12.0]]) |
| 56 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 57 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 58 | self.InitializeState() |
| 59 | |
| 60 | |
| 61 | class WristDeltaU(Wrist): |
| 62 | def __init__(self, name="Wrist"): |
| 63 | super(WristDeltaU, self).__init__(name) |
| 64 | A_unaugmented = self.A |
| 65 | B_unaugmented = self.B |
| 66 | |
| 67 | self.A = numpy.matrix([[0.0, 0.0, 0.0], |
| 68 | [0.0, 0.0, 0.0], |
| 69 | [0.0, 0.0, 1.0]]) |
| 70 | self.A[0:2, 0:2] = A_unaugmented |
| 71 | self.A[0:2, 2] = B_unaugmented |
| 72 | |
| 73 | self.B = numpy.matrix([[0.0], |
| 74 | [0.0], |
| 75 | [1.0]]) |
| 76 | |
| 77 | self.C = numpy.matrix([[1.0, 0.0, 0.0]]) |
| 78 | self.D = numpy.matrix([[0.0]]) |
| 79 | |
| 80 | self.PlaceControllerPoles([0.60, 0.37, 0.80]) |
| 81 | |
| 82 | print "K" |
| 83 | print self.K |
| 84 | print "Placed controller poles are" |
| 85 | print numpy.linalg.eig(self.A - self.B * self.K)[0] |
| 86 | |
| 87 | self.rpl = .05 |
| 88 | self.ipl = 0.008 |
| 89 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 90 | self.rpl - 1j * self.ipl, 0.15]) |
| 91 | print "Placed observer poles are" |
| 92 | print numpy.linalg.eig(self.A - self.L * self.C)[0] |
| 93 | |
| 94 | self.U_max = numpy.matrix([[12.0]]) |
| 95 | self.U_min = numpy.matrix([[-12.0]]) |
| 96 | |
| 97 | self.InitializeState() |
| 98 | |
| 99 | |
| 100 | def ClipDeltaU(wrist, delta_u): |
| 101 | old_u = numpy.matrix([[wrist.X[2, 0]]]) |
| 102 | new_u = numpy.clip(old_u + delta_u, wrist.U_min, wrist.U_max) |
| 103 | return new_u - old_u |
| 104 | |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 105 | def main(argv): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 106 | # Simulate the response of the system to a step input. |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 107 | wrist = WristDeltaU() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 108 | simulated_x = [] |
| 109 | for _ in xrange(100): |
| 110 | wrist.Update(numpy.matrix([[12.0]])) |
| 111 | simulated_x.append(wrist.X[0, 0]) |
| 112 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 113 | pylab.plot(range(100), simulated_x) |
| 114 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 115 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 116 | # Simulate the closed loop response of the system to a step input. |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 117 | wrist = WristDeltaU() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 118 | close_loop_x = [] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 119 | close_loop_u = [] |
| 120 | R = numpy.matrix([[1.0], [0.0], [0.0]]) |
| 121 | wrist.X[2, 0] = -5 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 122 | for _ in xrange(100): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 123 | U = numpy.clip(wrist.K * (R - wrist.X_hat), wrist.U_min, wrist.U_max) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 124 | U = ClipDeltaU(wrist, U) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 125 | wrist.UpdateObserver(U) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 126 | wrist.Update(U) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 127 | close_loop_x.append(wrist.X[0, 0] * 10) |
| 128 | close_loop_u.append(wrist.X[2, 0]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 129 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 130 | pylab.plot(range(100), close_loop_x) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 131 | pylab.plot(range(100), close_loop_u) |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 132 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 133 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 134 | # Write the generated constants out to a file. |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 135 | if len(argv) != 5: |
| 136 | print "Expected .h file name and .cc file name for" |
| 137 | print "both the plant and unaugmented plant" |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 138 | else: |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame^] | 139 | unaug_wrist = Wrist("RawWrist") |
| 140 | unaug_loop_writer = control_loop.ControlLoopWriter("RawWrist", |
| 141 | [unaug_wrist]) |
| 142 | if argv[3][-3:] == '.cc': |
| 143 | unaug_loop_writer.Write(argv[4], argv[3]) |
| 144 | else: |
| 145 | unaug_loop_writer.Write(argv[3], argv[4]) |
| 146 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 147 | loop_writer = control_loop.ControlLoopWriter("Wrist", [wrist]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 148 | if argv[1][-3:] == '.cc': |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 149 | loop_writer.Write(argv[2], argv[1]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 150 | else: |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 151 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 152 | |
| 153 | if __name__ == '__main__': |
| 154 | sys.exit(main(sys.argv)) |