blob: f21ed03ed7e78a5c4cb503469c6f07df3e5efca3 [file] [log] [blame]
Austin Schuhc8ca2442013-02-23 12:29:33 -08001#!/usr/bin/python
2
Austin Schuh3c542312013-02-24 01:53:50 -08003import control_loop
Austin Schuhc8ca2442013-02-23 12:29:33 -08004import numpy
Austin Schuhc8ca2442013-02-23 12:29:33 -08005import sys
Austin Schuhc8ca2442013-02-23 12:29:33 -08006from matplotlib import pylab
Austin Schuhc8ca2442013-02-23 12:29:33 -08007
Austin Schuh3c542312013-02-24 01:53:50 -08008class Wrist(control_loop.ControlLoop):
Austin Schuhc1f68892013-03-16 17:06:27 -07009 def __init__(self, name="RawWrist"):
10 super(Wrist, self).__init__(name)
Austin Schuhc8ca2442013-02-23 12:29:33 -080011 # 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 Schuh3c542312013-02-24 01:53:50 -080017 # Free Current in Amps
18 self.free_current = 1.5
Austin Schuhc8ca2442013-02-23 12:29:33 -080019 # Moment of inertia of the wrist in kg m^2
Austin Schuh683a0d02013-03-02 01:51:31 -080020 # 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 Silverman0f637382013-03-03 17:44:46 -080022 self.J = 2.0
Austin Schuhc8ca2442013-02-23 12:29:33 -080023 # Resistance of the motor
24 self.R = 12.0 / self.stall_current + 0.024 + .003
25 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080026 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
27 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080028 # 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 Schuhc1f68892013-03-16 17:06:27 -070045 self.A, self.B = self.ContinuousToDiscrete(
46 self.A_continuous, self.B_continuous, self.dt)
Austin Schuhc8ca2442013-02-23 12:29:33 -080047
Austin Schuhc1f68892013-03-16 17:06:27 -070048 self.PlaceControllerPoles([0.85, 0.45])
Austin Schuhc8ca2442013-02-23 12:29:33 -080049
50 self.rpl = .05
51 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080052 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
53 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080054
55 self.U_max = numpy.matrix([[12.0]])
56 self.U_min = numpy.matrix([[-12.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080057
Austin Schuhc1f68892013-03-16 17:06:27 -070058 self.InitializeState()
59
60
61class 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
100def 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 Schuhc8ca2442013-02-23 12:29:33 -0800105def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -0800106 # Simulate the response of the system to a step input.
Austin Schuhc1f68892013-03-16 17:06:27 -0700107 wrist = WristDeltaU()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800108 simulated_x = []
109 for _ in xrange(100):
110 wrist.Update(numpy.matrix([[12.0]]))
111 simulated_x.append(wrist.X[0, 0])
112
Austin Schuh3c542312013-02-24 01:53:50 -0800113 pylab.plot(range(100), simulated_x)
114 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800115
Austin Schuh3c542312013-02-24 01:53:50 -0800116 # Simulate the closed loop response of the system to a step input.
Austin Schuhc1f68892013-03-16 17:06:27 -0700117 wrist = WristDeltaU()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800118 close_loop_x = []
Austin Schuhc1f68892013-03-16 17:06:27 -0700119 close_loop_u = []
120 R = numpy.matrix([[1.0], [0.0], [0.0]])
121 wrist.X[2, 0] = -5
Austin Schuhc8ca2442013-02-23 12:29:33 -0800122 for _ in xrange(100):
Austin Schuh3c542312013-02-24 01:53:50 -0800123 U = numpy.clip(wrist.K * (R - wrist.X_hat), wrist.U_min, wrist.U_max)
Austin Schuhc1f68892013-03-16 17:06:27 -0700124 U = ClipDeltaU(wrist, U)
Austin Schuh3c542312013-02-24 01:53:50 -0800125 wrist.UpdateObserver(U)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800126 wrist.Update(U)
Austin Schuhc1f68892013-03-16 17:06:27 -0700127 close_loop_x.append(wrist.X[0, 0] * 10)
128 close_loop_u.append(wrist.X[2, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800129
Austin Schuhfa033692013-02-24 01:00:55 -0800130 pylab.plot(range(100), close_loop_x)
Austin Schuhc1f68892013-03-16 17:06:27 -0700131 pylab.plot(range(100), close_loop_u)
Austin Schuhfa033692013-02-24 01:00:55 -0800132 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800133
Austin Schuh3c542312013-02-24 01:53:50 -0800134 # Write the generated constants out to a file.
Austin Schuhc1f68892013-03-16 17:06:27 -0700135 if len(argv) != 5:
136 print "Expected .h file name and .cc file name for"
137 print "both the plant and unaugmented plant"
Austin Schuhc8ca2442013-02-23 12:29:33 -0800138 else:
Austin Schuhc1f68892013-03-16 17:06:27 -0700139 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 Schuhe3490622013-03-13 01:24:30 -0700147 loop_writer = control_loop.ControlLoopWriter("Wrist", [wrist])
Austin Schuh683a0d02013-03-02 01:51:31 -0800148 if argv[1][-3:] == '.cc':
Austin Schuhe3490622013-03-13 01:24:30 -0700149 loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800150 else:
Austin Schuhe3490622013-03-13 01:24:30 -0700151 loop_writer.Write(argv[1], argv[2])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800152
153if __name__ == '__main__':
154 sys.exit(main(sys.argv))