blob: 9e2bb74da408bd1f573c00da8b32811dbb59207a [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
13 self.stall_torque = 1.4
14 # Stall Current in Amps
15 self.stall_current = 86
16 # Free Speed in RPM
17 self.free_speed = 6200.0
Austin Schuh3c542312013-02-24 01:53:50 -080018 # Free Current in Amps
19 self.free_current = 1.5
Austin Schuh3bb9a442014-02-02 16:01:45 -080020 # Moment of inertia of the claw in kg m^2
Austin Schuh683a0d02013-03-02 01:51:31 -080021 # TODO(aschuh): Measure this in reality. It doesn't seem high enough.
22 # James measured 0.51, but that can't be right given what I am seeing.
Brian Silverman0f637382013-03-03 17:44:46 -080023 self.J = 2.0
Austin Schuhc8ca2442013-02-23 12:29:33 -080024 # Resistance of the motor
25 self.R = 12.0 / self.stall_current + 0.024 + .003
26 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080027 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
28 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080029 # Torque constant
30 self.Kt = self.stall_torque / self.stall_current
31 # Gear ratio
32 self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0))
33 # Control loop time step
34 self.dt = 0.01
35
Austin Schuh0d9467a2014-02-15 22:36:45 -080036 # State is [bottom position, top - bottom position,
37 # bottom velocity, top - bottom velocity]
38 # Input is [bottom power, top power]
39 # Motor time constant.
40 self.motor_timeconstant = self.Kt / self.Kv / (self.J * self.G * self.G * self.R)
Austin Schuhc8ca2442013-02-23 12:29:33 -080041 # State feedback matrices
42 self.A_continuous = numpy.matrix(
Austin Schuh0d9467a2014-02-15 22:36:45 -080043 [[0, 0, 1, 0],
44 [0, 0, 0, 1],
45 [0, 0, -self.motor_timeconstant, 0],
46 [0, 0, 0, -self.motor_timeconstant]])
47
48 self.motor_feedforward = self.Kt / (self.J * self.G * self.R)
49
Austin Schuhc8ca2442013-02-23 12:29:33 -080050 self.B_continuous = numpy.matrix(
Austin Schuh0d9467a2014-02-15 22:36:45 -080051 [[0, 0],
52 [0, 0],
53 [self.motor_feedforward, 0],
Austin Schuhcda86af2014-02-16 16:16:39 -080054 [0.0, self.motor_feedforward]])
Austin Schuh0d9467a2014-02-15 22:36:45 -080055 self.C = numpy.matrix([[1, 0, 0, 0],
56 [1, 1, 0, 0]])
57 self.D = numpy.matrix([[0, 0],
58 [0, 0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080059
Austin Schuhc1f68892013-03-16 17:06:27 -070060 self.A, self.B = self.ContinuousToDiscrete(
61 self.A_continuous, self.B_continuous, self.dt)
Austin Schuhc8ca2442013-02-23 12:29:33 -080062
Austin Schuh0d9467a2014-02-15 22:36:45 -080063 #controlability = controls.ctrb(self.A, self.B);
64 #print "Rank of controlability matrix.", numpy.linalg.matrix_rank(controlability)
65
Austin Schuhcda86af2014-02-16 16:16:39 -080066 self.Q = numpy.matrix([[(1.0 / (0.10 ** 2.0)), 0.0, 0.0, 0.0],
67 [0.0, (1.0 / (0.03 ** 2.0)), 0.0, 0.0],
68 [0.0, 0.0, 0.2, 0.0],
69 [0.0, 0.0, 0.0, 2.00]])
70
71 self.R = numpy.matrix([[(1.0 / (20.0 ** 2.0)), 0.0],
72 [0.0, (1.0 / (20.0 ** 2.0))]])
73 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
74
75 print "K unaugmented"
76 print self.K
Austin Schuhc8ca2442013-02-23 12:29:33 -080077
78 self.rpl = .05
79 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080080 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
Austin Schuh0d9467a2014-02-15 22:36:45 -080081 self.rpl - 1j * self.ipl,
82 self.rpl + 1j * self.ipl,
Austin Schuh3c542312013-02-24 01:53:50 -080083 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080084
Austin Schuhcda86af2014-02-16 16:16:39 -080085 self.U_max = numpy.matrix([[12.0], [24.0]])
86 self.U_min = numpy.matrix([[-12.0], [-24.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080087
Austin Schuhc1f68892013-03-16 17:06:27 -070088 self.InitializeState()
89
90
Austin Schuh3bb9a442014-02-02 16:01:45 -080091class ClawDeltaU(Claw):
92 def __init__(self, name="Claw"):
93 super(ClawDeltaU, self).__init__(name)
Austin Schuhc1f68892013-03-16 17:06:27 -070094 A_unaugmented = self.A
95 B_unaugmented = self.B
Austin Schuh0d9467a2014-02-15 22:36:45 -080096 C_unaugmented = self.C
Austin Schuhc1f68892013-03-16 17:06:27 -070097
Austin Schuh0d9467a2014-02-15 22:36:45 -080098 self.A = numpy.matrix([[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, 0.0],
102 [0.0, 0.0, 0.0, 0.0, 1.0]])
103 self.A[0:4, 0:4] = A_unaugmented
104 self.A[0:4, 4] = B_unaugmented[0:4, 0]
Austin Schuhc1f68892013-03-16 17:06:27 -0700105
Austin Schuh0d9467a2014-02-15 22:36:45 -0800106 self.B = numpy.matrix([[0.0, 0.0],
107 [0.0, 0.0],
108 [0.0, 0.0],
109 [0.0, 0.0],
110 [1.0, 0.0]])
111 self.B[0:4, 1] = B_unaugmented[0:4, 1]
Austin Schuhc1f68892013-03-16 17:06:27 -0700112
Austin Schuh0d9467a2014-02-15 22:36:45 -0800113 self.C = numpy.concatenate((C_unaugmented, numpy.matrix([[0.0], [0.0]])),
114 axis=1)
115 self.D = numpy.matrix([[0.0, 0.0],
116 [0.0, 0.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700117
Austin Schuh0d9467a2014-02-15 22:36:45 -0800118 #self.PlaceControllerPoles([0.55, 0.35, 0.55, 0.35, 0.80])
119 self.Q = numpy.matrix([[(1.0 / (0.04 ** 2.0)), 0.0, 0.0, 0.0, 0.0],
120 [0.0, (1.0 / (0.01 ** 2)), 0.0, 0.0, 0.0],
121 [0.0, 0.0, 0.01, 0.0, 0.0],
122 [0.0, 0.0, 0.0, 0.08, 0.0],
123 [0.0, 0.0, 0.0, 0.0, (1.0 / (10.0 ** 2))]])
124
125 self.R = numpy.matrix([[0.000001, 0.0],
126 [0.0, 1.0 / (10.0 ** 2.0)]])
127 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
128
129 self.K = numpy.matrix([[50.0, 0.0, 10.0, 0.0, 1.0],
130 [50.0, 0.0, 10.0, 0.0, 1.0]])
131 #self.K = numpy.matrix([[50.0, -100.0, 0, -10, 0],
132 # [50.0, 100.0, 0, 10, 0]])
133
134 controlability = controls.ctrb(self.A, self.B);
135 print "Rank of augmented controlability matrix.", numpy.linalg.matrix_rank(controlability)
Austin Schuhc1f68892013-03-16 17:06:27 -0700136
137 print "K"
138 print self.K
139 print "Placed controller poles are"
140 print numpy.linalg.eig(self.A - self.B * self.K)[0]
Austin Schuh0d9467a2014-02-15 22:36:45 -0800141 print [numpy.abs(x) for x in numpy.linalg.eig(self.A - self.B * self.K)[0]]
Austin Schuhc1f68892013-03-16 17:06:27 -0700142
143 self.rpl = .05
144 self.ipl = 0.008
Austin Schuh0d9467a2014-02-15 22:36:45 -0800145 self.PlaceObserverPoles([self.rpl + 1j * self.ipl, 0.10, 0.09,
Brian Silverman23a67ca2013-03-16 23:48:50 -0700146 self.rpl - 1j * self.ipl, 0.90])
Austin Schuh0d9467a2014-02-15 22:36:45 -0800147 #print "A is"
148 #print self.A
149 #print "L is"
150 #print self.L
151 #print "C is"
152 #print self.C
153 #print "A - LC is"
154 #print self.A - self.L * self.C
Austin Schuhc1f68892013-03-16 17:06:27 -0700155
Austin Schuh0d9467a2014-02-15 22:36:45 -0800156 #print "Placed observer poles are"
157 #print numpy.linalg.eig(self.A - self.L * self.C)[0]
158
159 self.U_max = numpy.matrix([[12.0], [12.0]])
160 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700161
162 self.InitializeState()
163
164
Austin Schuhcda86af2014-02-16 16:16:39 -0800165def FullSeparationPriority(claw, U):
166 bottom_u = U[0, 0]
167 top_u = U[1, 0] + bottom_u
168
169 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
170 if bottom_u > claw.U_max[0, 0]:
171 #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0]
172 top_u -= bottom_u - claw.U_max[0, 0]
173 if top_u < claw.U_min[1, 0]:
174 top_u = claw.U_min[1, 0]
175
176 bottom_u = claw.U_max[0, 0]
177 if top_u > claw.U_max[1, 0]:
178 bottom_u -= top_u - claw.U_max[1, 0]
179 if bottom_u < claw.U_min[0, 0]:
180 bottom_u = claw.U_min[0, 0]
181
182 top_u = claw.U_max[1, 0]
183 if top_u < claw.U_min[1, 0]:
184 bottom_u -= top_u - claw.U_min[1, 0]
185 if bottom_u > claw.U_max[0, 0]:
186 bottom_u = claw.U_max[0, 0]
187
188 top_u = claw.U_min[1, 0]
189 if bottom_u < claw.U_min[0, 0]:
190 top_u -= bottom_u - claw.U_min[0, 0]
191 if top_u > claw.U_max[1, 0]:
192 top_u = claw.U_max[1, 0]
193
194 bottom_u = claw.U_min[0, 0]
195
196 return numpy.matrix([[bottom_u], [top_u - bottom_u]])
197
198def AverageUFix(claw, U):
199 bottom_u = U[0, 0]
200 top_u = U[1, 0] + bottom_u
201
202 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
203 if (bottom_u > claw.U_max[0, 0] or top_u > claw.U_max[1, 0] or
204 top_u < claw.U_min[1, 0] or bottom_u < claw.U_min[0, 0]):
205 scalar = 12.0 / max(numpy.abs(top_u), numpy.abs(bottom_u))
206 top_u *= scalar
207 bottom_u *= scalar
208
209 return numpy.matrix([[bottom_u], [top_u - bottom_u]])
210
Austin Schuh0d9467a2014-02-15 22:36:45 -0800211def ClipDeltaU(claw, U):
212 delta_u = U[0, 0]
213 top_u = U[1, 0]
214 old_bottom_u = claw.X[4, 0]
215
216 # TODO(austin): Preserve the difference between the top and bottom power.
217 new_unclipped_bottom_u = old_bottom_u + delta_u
218
219 #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
220 if new_unclipped_bottom_u > claw.U_max[0, 0]:
221 #print "Bottom is too big. Was", new_unclipped_bottom_u, "changing top by", new_unclipped_bottom_u - claw.U_max[0, 0]
222 top_u -= new_unclipped_bottom_u - claw.U_max[0, 0]
223 new_unclipped_bottom_u = claw.U_max[0, 0]
224 if top_u > claw.U_max[1, 0]:
225 new_unclipped_bottom_u -= top_u - claw.U_max[1, 0]
226 top_u = claw.U_max[1, 0]
227 if top_u < claw.U_min[1, 0]:
228 new_unclipped_bottom_u -= top_u - claw.U_min[1, 0]
229 top_u = claw.U_min[1, 0]
230 if new_unclipped_bottom_u < claw.U_min[0, 0]:
231 top_u -= new_unclipped_bottom_u - claw.U_min[0, 0]
232 new_unclipped_bottom_u = claw.U_min[0, 0]
233
234 new_bottom_u = numpy.clip(new_unclipped_bottom_u, claw.U_min[0, 0], claw.U_max[0, 0])
235 new_top_u = numpy.clip(top_u, claw.U_min[1, 0], claw.U_max[1, 0])
236
237 return numpy.matrix([[new_bottom_u - old_bottom_u], [new_top_u]])
Austin Schuhc1f68892013-03-16 17:06:27 -0700238
Austin Schuhc8ca2442013-02-23 12:29:33 -0800239def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -0800240 # Simulate the response of the system to a step input.
Austin Schuh0d9467a2014-02-15 22:36:45 -0800241 #claw = ClawDeltaU()
242 #simulated_x = []
243 #for _ in xrange(100):
244 # claw.Update(numpy.matrix([[12.0]]))
245 # simulated_x.append(claw.X[0, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800246
Austin Schuh0d9467a2014-02-15 22:36:45 -0800247 #pylab.plot(range(100), simulated_x)
248 #pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800249
Austin Schuh3c542312013-02-24 01:53:50 -0800250 # Simulate the closed loop response of the system to a step input.
Austin Schuhcda86af2014-02-16 16:16:39 -0800251 claw = Claw("TopClaw")
252 t = []
Austin Schuh0d9467a2014-02-15 22:36:45 -0800253 close_loop_x_bottom = []
254 close_loop_x_sep = []
255 close_loop_u_bottom = []
256 close_loop_u_top = []
Austin Schuhcda86af2014-02-16 16:16:39 -0800257 R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
258 claw.X[0, 0] = 0
259 for i in xrange(100):
260 #print "Error is", (R - claw.X_hat)
261 U = claw.K * (R - claw.X_hat)
262 #U = numpy.clip(claw.K * (R - claw.X_hat), claw.U_min, claw.U_max)
263 #U = FullSeparationPriority(claw, U)
264 U = AverageUFix(claw, U)
265 #U = claw.K * (R - claw.X_hat)
266 #U = ClipDeltaU(claw, U)
267 claw.UpdateObserver(U)
268 claw.Update(U)
269 close_loop_x_bottom.append(claw.X[0, 0] * 10)
270 close_loop_u_bottom.append(U[0, 0])
271 close_loop_x_sep.append(claw.X[1, 0] * 10)
272 close_loop_u_top.append(U[1, 0] + U[0, 0])
273 t.append(0.01 * i)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800274
Austin Schuhcda86af2014-02-16 16:16:39 -0800275 pylab.plot(t, close_loop_x_bottom, label='x bottom')
Brian Silverman7c021c42014-02-17 15:15:56 -0800276 pylab.plot(t, close_loop_x_sep, label='separation')
Austin Schuhcda86af2014-02-16 16:16:39 -0800277 pylab.plot(t, close_loop_u_bottom, label='u bottom')
278 pylab.plot(t, close_loop_u_top, label='u top')
Austin Schuh0d9467a2014-02-15 22:36:45 -0800279 pylab.legend()
Austin Schuhfa033692013-02-24 01:00:55 -0800280 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800281
Austin Schuh3c542312013-02-24 01:53:50 -0800282 # Write the generated constants out to a file.
Austin Schuhcda86af2014-02-16 16:16:39 -0800283 if len(argv) != 3:
284 print "Expected .h file name and .cc file name for the claw."
Austin Schuhc8ca2442013-02-23 12:29:33 -0800285 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800286 claw = Claw("Claw")
287 loop_writer = control_loop.ControlLoopWriter("Claw", [claw])
Austin Schuh683a0d02013-03-02 01:51:31 -0800288 if argv[1][-3:] == '.cc':
Austin Schuhcda86af2014-02-16 16:16:39 -0800289 loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800290 else:
Austin Schuhcda86af2014-02-16 16:16:39 -0800291 loop_writer.Write(argv[1], argv[2])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800292
293if __name__ == '__main__':
294 sys.exit(main(sys.argv))