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 |
| 20 | self.J = 0.51 |
| 21 | # Resistance of the motor |
| 22 | self.R = 12.0 / self.stall_current + 0.024 + .003 |
| 23 | # Motor velocity constant |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 24 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 25 | (13.5 - self.R * self.free_current)) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 26 | # Torque constant |
| 27 | self.Kt = self.stall_torque / self.stall_current |
| 28 | # Gear ratio |
| 29 | self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.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 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 43 | self.ContinuousToDiscrete(self.A_continuous, self.B_continuous, |
| 44 | self.dt, self.C) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 45 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 46 | self.PlaceControllerPoles([.89, .85]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 47 | |
| 48 | self.rpl = .05 |
| 49 | self.ipl = 0.008 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 50 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 51 | self.rpl - 1j * self.ipl]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 52 | |
| 53 | self.U_max = numpy.matrix([[12.0]]) |
| 54 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 55 | |
| 56 | def main(argv): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 57 | # Simulate the response of the system to a step input. |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 58 | wrist = Wrist() |
| 59 | simulated_x = [] |
| 60 | for _ in xrange(100): |
| 61 | wrist.Update(numpy.matrix([[12.0]])) |
| 62 | simulated_x.append(wrist.X[0, 0]) |
| 63 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 64 | pylab.plot(range(100), simulated_x) |
| 65 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 66 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 67 | # Simulate the closed loop response of the system to a step input. |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 68 | wrist = Wrist() |
| 69 | close_loop_x = [] |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 70 | R = numpy.matrix([[1.0], [0.0]]) |
| 71 | for _ in xrange(100): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 72 | U = numpy.clip(wrist.K * (R - wrist.X_hat), wrist.U_min, wrist.U_max) |
| 73 | wrist.UpdateObserver(U) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 74 | wrist.Update(U) |
| 75 | close_loop_x.append(wrist.X[0, 0]) |
| 76 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 77 | pylab.plot(range(100), close_loop_x) |
| 78 | pylab.show() |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 79 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 80 | # Write the generated constants out to a file. |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 81 | if len(argv) != 3: |
| 82 | print "Expected .cc file name and .h file name" |
| 83 | else: |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 84 | wrist.DumpHeaderFile(argv[1]) |
| 85 | wrist.DumpCppFile(argv[2], argv[1]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 86 | |
| 87 | if __name__ == '__main__': |
| 88 | sys.exit(main(sys.argv)) |