blob: 09718e5c562dc5fa31a1fb0fbb6de85d31bf1c1b [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 Schuh0d9467a2014-02-15 22:36:45 -08004import controls
Austin Schuhc8ca2442013-02-23 12:29:33 -08005import numpy
Austin Schuhc8ca2442013-02-23 12:29:33 -08006import sys
Austin Schuhc8ca2442013-02-23 12:29:33 -08007from matplotlib import pylab
Austin Schuhc8ca2442013-02-23 12:29:33 -08008
Austin Schuh3bb9a442014-02-02 16:01:45 -08009class Claw(control_loop.ControlLoop):
10 def __init__(self, name="RawClaw"):
11 super(Claw, self).__init__(name)
Austin Schuhc8ca2442013-02-23 12:29:33 -080012 # Stall Torque in N m
James Kuszmaule1755b32014-02-13 06:27:48 -080013 self.stall_torque = 2.42
Austin Schuhc8ca2442013-02-23 12:29:33 -080014 # Stall Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080015 self.stall_current = 133
16 # Free Speed in RPM, pulled from drivetrain
17 self.free_speed = 4650.0
Austin Schuh3c542312013-02-24 01:53:50 -080018 # Free Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080019 self.free_current = 2.7
Austin Schuh3bb9a442014-02-02 16:01:45 -080020 # Moment of inertia of the claw in kg m^2
James Kuszmaule1755b32014-02-13 06:27:48 -080021 # approzimately 0.76 (without ball) in CAD
22 self.J = 0.76
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
James Kuszmaule1755b32014-02-13 06:27:48 -080031 self.G = 14.0 / 48.0 * 18.0 / 32.0 * 18.0 / 66.0 * 12.0 / 60.0
Austin Schuhc8ca2442013-02-23 12:29:33 -080032 # Control loop time step
33 self.dt = 0.01
34
Austin Schuh0d9467a2014-02-15 22:36:45 -080035 # State is [bottom position, top - bottom position,
36 # bottom velocity, top - bottom velocity]
37 # Input is [bottom power, top power]
38 # Motor time constant.
39 self.motor_timeconstant = self.Kt / self.Kv / (self.J * self.G * self.G * self.R)
Austin Schuhc8ca2442013-02-23 12:29:33 -080040 # State feedback matrices
41 self.A_continuous = numpy.matrix(
Austin Schuh0d9467a2014-02-15 22:36:45 -080042 [[0, 0, 1, 0],
43 [0, 0, 0, 1],
44 [0, 0, -self.motor_timeconstant, 0],
45 [0, 0, 0, -self.motor_timeconstant]])
46
47 self.motor_feedforward = self.Kt / (self.J * self.G * self.R)
48
Austin Schuhc8ca2442013-02-23 12:29:33 -080049 self.B_continuous = numpy.matrix(
Austin Schuh0d9467a2014-02-15 22:36:45 -080050 [[0, 0],
51 [0, 0],
52 [self.motor_feedforward, 0],
Austin Schuhcda86af2014-02-16 16:16:39 -080053 [0.0, self.motor_feedforward]])
Austin Schuh0d9467a2014-02-15 22:36:45 -080054 self.C = numpy.matrix([[1, 0, 0, 0],
55 [1, 1, 0, 0]])
56 self.D = numpy.matrix([[0, 0],
57 [0, 0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080058
Austin Schuhc1f68892013-03-16 17:06:27 -070059 self.A, self.B = self.ContinuousToDiscrete(
60 self.A_continuous, self.B_continuous, self.dt)
Austin Schuhc8ca2442013-02-23 12:29:33 -080061
Austin Schuh0d9467a2014-02-15 22:36:45 -080062 #controlability = controls.ctrb(self.A, self.B);
63 #print "Rank of controlability matrix.", numpy.linalg.matrix_rank(controlability)
64
Austin Schuhcda86af2014-02-16 16:16:39 -080065 self.Q = numpy.matrix([[(1.0 / (0.10 ** 2.0)), 0.0, 0.0, 0.0],
66 [0.0, (1.0 / (0.03 ** 2.0)), 0.0, 0.0],
67 [0.0, 0.0, 0.2, 0.0],
68 [0.0, 0.0, 0.0, 2.00]])
69
70 self.R = numpy.matrix([[(1.0 / (20.0 ** 2.0)), 0.0],
71 [0.0, (1.0 / (20.0 ** 2.0))]])
72 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
73
74 print "K unaugmented"
75 print self.K
Austin Schuhc8ca2442013-02-23 12:29:33 -080076
77 self.rpl = .05
78 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080079 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
Austin Schuh0d9467a2014-02-15 22:36:45 -080080 self.rpl - 1j * self.ipl,
81 self.rpl + 1j * self.ipl,
Austin Schuh3c542312013-02-24 01:53:50 -080082 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080083
Austin Schuhcda86af2014-02-16 16:16:39 -080084 self.U_max = numpy.matrix([[12.0], [24.0]])
85 self.U_min = numpy.matrix([[-12.0], [-24.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080086
Austin Schuhc1f68892013-03-16 17:06:27 -070087 self.InitializeState()
88
89
Austin Schuh3bb9a442014-02-02 16:01:45 -080090class ClawDeltaU(Claw):
91 def __init__(self, name="Claw"):
92 super(ClawDeltaU, self).__init__(name)
Austin Schuhc1f68892013-03-16 17:06:27 -070093 A_unaugmented = self.A
94 B_unaugmented = self.B
Austin Schuh0d9467a2014-02-15 22:36:45 -080095 C_unaugmented = self.C
Austin Schuhc1f68892013-03-16 17:06:27 -070096
Austin Schuh0d9467a2014-02-15 22:36:45 -080097 self.A = numpy.matrix([[0.0, 0.0, 0.0, 0.0, 0.0],
98 [0.0, 0.0, 0.0, 0.0, 0.0],
99 [0.0, 0.0, 0.0, 0.0, 0.0],
100 [0.0, 0.0, 0.0, 0.0, 0.0],
101 [0.0, 0.0, 0.0, 0.0, 1.0]])
102 self.A[0:4, 0:4] = A_unaugmented
103 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Austin Schuhc1f68892013-03-16 17:06:27 -0700104
Austin Schuh0d9467a2014-02-15 22:36:45 -0800105 self.B = numpy.matrix([[0.0, 0.0],
106 [0.0, 0.0],
107 [0.0, 0.0],
108 [0.0, 0.0],
109 [1.0, 0.0]])
110 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Austin Schuhc1f68892013-03-16 17:06:27 -0700111
Austin Schuh0d9467a2014-02-15 22:36:45 -0800112 self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])),
113 axis=1)
114 self.D = numpy.matrix([[0.0, 0.0],
115 [0.0, 0.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700116
Austin Schuh0d9467a2014-02-15 22:36:45 -0800117 #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80])
118 self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0],
119 [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0],
120 [0.0, 0.0, 0.01, 0.0, 0.0],
121 [0.0, 0.0, 0.0, 0.08, 0.0],
122 [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]])
123
124 self.R = numpy.matrix([[0.000001, 0.0],
125 [0.0, 1.0 / (10.0 ** 2.0)]])
126 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
127
128 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
129 [50.0, 0.0, 10.0, 0.0, 1.0]])
130 #self.K = numpy.matrix([[50.0, -100.0, 0, -10, 0],
131 # [50.0, 100.0, 0, 10, 0]])
132
133 controlability = controls.ctrb(self.A, self.B);
134 print "Rank of augmented controlability matrix.", numpy.linalg.matrix_rank(controlability)
Austin Schuhc1f68892013-03-16 17:06:27 -0700135
136 print "K"
137 print self.K
138 print "Placed controller poles are"
139 print numpy.linalg.eig(self.A - self.B * self.K)[0]
Austin Schuh0d9467a2014-02-15 22:36:45 -0800140 print [numpy.abs(x) for x in numpy.linalg.eig(self.A - self.B * self.K)[0]]
Austin Schuhc1f68892013-03-16 17:06:27 -0700141
142 self.rpl = .05
143 self.ipl = 0.008
Austin Schuh0d9467a2014-02-15 22:36:45 -0800144 self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09,
Brian Silverman23a67ca2013-03-16 23:48:50 -0700145 self.rpl - 1j * self.ipl, 0.90])
Austin Schuh0d9467a2014-02-15 22:36:45 -0800146 #print "A is"
147 #print self.A
148 #print "L is"
149 #print self.L
150 #print "C is"
151 #print self.C
152 #print "A - LC is"
153 #print self.A - self.L * self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700154
Austin Schuh0d9467a2014-02-15 22:36:45 -0800155 #print "Placed observer poles are"
156 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
157
158 self.U_max = numpy.matrix([[12.0], [12.0]])
159 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700160
161 self.InitializeState()
162
163
Austin Schuhcda86af2014-02-16 16:16:39 -0800164def FullSeparationPriority(claw, U):
165 bottom_u = U[0, 0]
166 top_u = U[1, 0] + bottom_u
167
168 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
169 if bottom_u > claw.U_max[0, 0]:
170 #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0]
171 top_u -= bottom_u - claw.U_max[0, 0]
172 if top_u < claw.U_min[1, 0]:
173 top_u = claw.U_min[1, 0]
174
175 bottom_u = claw.U_max[0, 0]
176 if top_u > claw.U_max[1, 0]:
177 bottom_u -= top_u - claw.U_max[1, 0]
178 if bottom_u < claw.U_min[0, 0]:
179 bottom_u = claw.U_min[0, 0]
180
181 top_u = claw.U_max[1, 0]
182 if top_u < claw.U_min[1, 0]:
183 bottom_u -= top_u - claw.U_min[1, 0]
184 if bottom_u > claw.U_max[0, 0]:
185 bottom_u = claw.U_max[0, 0]
186
187 top_u = claw.U_min[1, 0]
188 if bottom_u < claw.U_min[0, 0]:
189 top_u -= bottom_u - claw.U_min[0, 0]
190 if top_u > claw.U_max[1, 0]:
191 top_u = claw.U_max[1, 0]
192
193 bottom_u = claw.U_min[0, 0]
194
195 return numpy.matrix([[bottom_u], [top_u - bottom_u]])
196
197def AverageUFix(claw, U):
198 bottom_u = U[0, 0]
199 top_u = U[1, 0] + bottom_u
200
201 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
202 if (bottom_u > claw.U_max[0, 0] or top_u > claw.U_max[1, 0] or
203 top_u < claw.U_min[1, 0] or bottom_u < claw.U_min[0, 0]):
204 scalar = 12.0 / max(numpy.abs(top_u), numpy.abs(bottom_u))
205 top_u *= scalar
206 bottom_u *= scalar
207
208 return numpy.matrix([[bottom_u], [top_u - bottom_u]])
209
Austin Schuh0d9467a2014-02-15 22:36:45 -0800210def ClipDeltaU(claw, U):
211 delta_u = U[0, 0]
212 top_u = U[1, 0]
213 old_bottom_u = claw.X[4, 0]
214
215 # TODO(austin): Preserve the difference between the top and bottom power.
216 new_unclipped_bottom_u = old_bottom_u + delta_u
217
218 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
219 if new_unclipped_bottom_u > claw.U_max[0, 0]:
220 #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0]
221 top_u -= new_unclipped_bottom_u - claw.U_max[0, 0]
222 new_unclipped_bottom_u = claw.U_max[0, 0]
223 if top_u > claw.U_max[1, 0]:
224 new_unclipped_bottom_u -= top_u - claw.U_max[1, 0]
225 top_u = claw.U_max[1, 0]
226 if top_u < claw.U_min[1, 0]:
227 new_unclipped_bottom_u -= top_u - claw.U_min[1, 0]
228 top_u = claw.U_min[1, 0]
229 if new_unclipped_bottom_u < claw.U_min[0, 0]:
230 top_u -= new_unclipped_bottom_u - claw.U_min[0, 0]
231 new_unclipped_bottom_u = claw.U_min[0, 0]
232
233 new_bottom_u = numpy.clip(new_unclipped_bottom_u, claw.U_min[0, 0], claw.U_max[0, 0])
234 new_top_u = numpy.clip(top_u, claw.U_min[1, 0], claw.U_max[1, 0])
235
236 return numpy.matrix([[new_bottom_u - old_bottom_u], [new_top_u]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700237
Austin Schuhc8ca2442013-02-23 12:29:33 -0800238def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -0800239 # Simulate the response of the system to a step input.
Austin Schuh0d9467a2014-02-15 22:36:45 -0800240 #claw = ClawDeltaU()
241 #simulated_x = []
242 #for _ in xrange(100):
243 # claw.Update(numpy.matrix([[12.0]]))
244 # simulated_x.append(claw.X[0, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800245
Austin Schuh0d9467a2014-02-15 22:36:45 -0800246 #pylab.plot(range(100), simulated_x)
247 #pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800248
Austin Schuh3c542312013-02-24 01:53:50 -0800249 # Simulate the closed loop response of the system to a step input.
Austin Schuhcda86af2014-02-16 16:16:39 -0800250 claw = Claw("TopClaw")
251 t = []
Austin Schuh0d9467a2014-02-15 22:36:45 -0800252 close_loop_x_bottom = []
253 close_loop_x_sep = []
254 close_loop_u_bottom = []
255 close_loop_u_top = []
Austin Schuhcda86af2014-02-16 16:16:39 -0800256 R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
257 claw.X[0, 0] = 0
258 for i in xrange(100):
259 #print "Error is", (R - claw.X_hat)
260 U = claw.K * (R - claw.X_hat)
261 #U = numpy.clip(claw.K * (R - claw.X_hat), claw.U_min, claw.U_max)
262 #U = FullSeparationPriority(claw, U)
263 U = AverageUFix(claw, U)
264 #U = claw.K * (R - claw.X_hat)
265 #U = ClipDeltaU(claw, U)
266 claw.UpdateObserver(U)
267 claw.Update(U)
268 close_loop_x_bottom.append(claw.X[0, 0] * 10)
269 close_loop_u_bottom.append(U[0, 0])
270 close_loop_x_sep.append(claw.X[1, 0] * 10)
271 close_loop_u_top.append(U[1, 0] + U[0, 0])
272 t.append(0.01 * i)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800273
Austin Schuhcda86af2014-02-16 16:16:39 -0800274 pylab.plot(t, close_loop_x_bottom, label='x bottom')
Brian Silverman7c021c42014-02-17 15:15:56 -0800275 pylab.plot(t, close_loop_x_sep, label='separation')
Austin Schuhcda86af2014-02-16 16:16:39 -0800276 pylab.plot(t, close_loop_u_bottom, label='u bottom')
277 pylab.plot(t, close_loop_u_top, label='u top')
Austin Schuh0d9467a2014-02-15 22:36:45 -0800278 pylab.legend()
Austin Schuhfa033692013-02-24 01:00:55 -0800279 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800280
Austin Schuh3c542312013-02-24 01:53:50 -0800281 # Write the generated constants out to a file.
Austin Schuhcda86af2014-02-16 16:16:39 -0800282 if len(argv) != 3:
283 print "Expected .h file name and .cc file name for the claw."
Austin Schuhc8ca2442013-02-23 12:29:33 -0800284 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800285 claw = Claw("Claw")
286 loop_writer = control_loop.ControlLoopWriter("Claw", [claw])
Austin Schuh683a0d02013-03-02 01:51:31 -0800287 if argv[1][-3:] == '.cc':
Austin Schuhcda86af2014-02-16 16:16:39 -0800288 loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800289 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800290 loop_writer.Write(argv[1], argv[2])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800291
292if __name__ == '__main__':
293 sys.exit(main(sys.argv))