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 | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 9 | def __init__(self): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 10 | super(Wrist, self).__init__("Wrist") |
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. |
| 22 | self.J = 1.51 |
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 | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 45 | self.ContinuousToDiscrete(self.A_continuous, self.B_continuous, |
| 46 | self.dt, self.C) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 47 | |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame^] | 48 | self.PlaceControllerPoles([.84, .84]) |
| 49 | |
| 50 | print self.K |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 51 | |
| 52 | self.rpl = .05 |
| 53 | self.ipl = 0.008 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 54 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 55 | self.rpl - 1j * self.ipl]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 56 | |
| 57 | self.U_max = numpy.matrix([[12.0]]) |
| 58 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 59 | |
| 60 | def main(argv): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 61 | # Simulate the response of the system to a step input. |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 62 | wrist = Wrist() |
| 63 | simulated_x = [] |
| 64 | for _ in xrange(100): |
| 65 | wrist.Update(numpy.matrix([[12.0]])) |
| 66 | simulated_x.append(wrist.X[0, 0]) |
| 67 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 68 | pylab.plot(range(100), simulated_x) |
| 69 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 70 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 71 | # Simulate the closed loop response of the system to a step input. |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 72 | wrist = Wrist() |
| 73 | close_loop_x = [] |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 74 | R = numpy.matrix([[1.0], [0.0]]) |
| 75 | for _ in xrange(100): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 76 | U = numpy.clip(wrist.K * (R - wrist.X_hat), wrist.U_min, wrist.U_max) |
| 77 | wrist.UpdateObserver(U) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 78 | wrist.Update(U) |
| 79 | close_loop_x.append(wrist.X[0, 0]) |
| 80 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 81 | pylab.plot(range(100), close_loop_x) |
| 82 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 83 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 84 | # Write the generated constants out to a file. |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 85 | if len(argv) != 3: |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame^] | 86 | print "Expected .h file name and .cc file name" |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 87 | else: |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame^] | 88 | if argv[1][-3:] == '.cc': |
| 89 | print '.cc file is second' |
| 90 | else: |
| 91 | wrist.DumpHeaderFile(argv[1]) |
| 92 | wrist.DumpCppFile(argv[2], argv[1]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 93 | |
| 94 | if __name__ == '__main__': |
| 95 | sys.exit(main(sys.argv)) |