blob: c910f4c237c96ca3d91191fb3e9c0d4604a4f8a2 [file] [log] [blame]
Austin Schuh1a387962015-01-31 16:36:20 -08001#!/usr/bin/python
2
3import control_loop
4import controls
5import polytope
6import polydrivetrain
7import numpy
8import sys
9import matplotlib
10from matplotlib import pylab
11
12class Arm(control_loop.ControlLoop):
13 def __init__(self, name="Arm", mass=None):
14 super(Arm, self).__init__(name)
15 # Stall Torque in N m
16 self.stall_torque = 0.476
17 # Stall Current in Amps
18 self.stall_current = 80.730
19 # Free Speed in RPM
20 self.free_speed = 13906.0
21 # Free Current in Amps
22 self.free_current = 5.820
23 # Mass of the arm
24 if mass is None:
25 self.mass = 13.0
26 else:
27 self.mass = mass
28
29 # Resistance of the motor
30 self.R = 12.0 / self.stall_current
31 # Motor velocity constant
32 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
33 (12.0 - self.R * self.free_current))
34 # Torque constant
35 self.Kt = self.stall_torque / self.stall_current
36 # Gear ratio
37 self.G = (44.0 / 12.0) * (54.0 / 14.0) * (54.0 / 14.0) * (44.0 / 20.0) * (72.0 / 16.0)
38 # Fridge arm length
39 self.r = 32 * 0.0254
40 # Control loop time step
41 self.dt = 0.005
42
43 # Arm moment of inertia
44 self.J = self.r * self.mass
45
46 # Arm left/right spring constant (N*m / radian)
47 self.spring = 3000.0
48
49 # State is [average position, average velocity,
50 # position difference/2, velocity difference/2]
51 # Position difference is 1 - 2
52 # Input is [Voltage 1, Voltage 2]
53
54 C1 = self.spring / (self.J * 0.5)
55 C2 = self.Kt * self.G / (self.J * 0.5 * self.R)
56 C3 = self.G * self.G * self.Kt / (self.R * self.J * 0.5 * self.Kv)
57
58 self.A_continuous = numpy.matrix(
59 [[0, 1, 0, 0],
60 [0, -C3, 0, 0],
61 [0, 0, 0, 1],
62 [0, 0, -C1 * 2.0, -C3]])
63
64 print 'Full speed is', C2 / C3 * 12.0
65
66 # Start with the unmodified input
67 self.B_continuous = numpy.matrix(
68 [[0, 0],
69 [C2 / 2.0, C2 / 2.0],
70 [0, 0],
71 [C2 / 2.0, -C2 / 2.0]])
72
73 self.C = numpy.matrix([[1, 0, 1, 0],
74 [1, 0, -1, 0]])
75 self.D = numpy.matrix([[0, 0],
76 [0, 0]])
77
78 self.A, self.B = self.ContinuousToDiscrete(
79 self.A_continuous, self.B_continuous, self.dt)
80
81 controlability = controls.ctrb(self.A, self.B);
82 print 'Rank of augmented controlability matrix.', numpy.linalg.matrix_rank(
83 controlability)
84
85 q_pos = 0.02
86 q_vel = 0.300
87 q_pos_diff = 0.01
88 q_vel_diff = 0.15
89 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0],
90 [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0],
91 [0.0, 0.0, (1.0 / (q_pos_diff ** 2.0)), 0.0],
92 [0.0, 0.0, 0.0, (1.0 / (q_vel_diff ** 2.0))]])
93
94 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
95 [0.0, 1.0 / (12.0 ** 2.0)]])
96 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
97 print 'Controller'
98 print self.K
99
100 print 'Controller Poles'
101 print numpy.linalg.eig(self.A - self.B * self.K)[0]
102
103 self.rpl = 0.20
104 self.ipl = 0.05
105 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
106 self.rpl + 1j * self.ipl,
107 self.rpl - 1j * self.ipl,
108 self.rpl - 1j * self.ipl])
109
110 # The box formed by U_min and U_max must encompass all possible values,
111 # or else Austin's code gets angry.
112 self.U_max = numpy.matrix([[12.0], [12.0]])
113 self.U_min = numpy.matrix([[-12.0], [-12.0]])
114
115 self.InitializeState()
116
117
118def CapU(U):
119 if U[0, 0] - U[1, 0] > 24:
120 return numpy.matrix([[12], [-12]])
121 elif U[0, 0] - U[1, 0] < -24:
122 return numpy.matrix([[-12], [12]])
123 else:
124 max_u = max(U[0, 0], U[1, 0])
125 min_u = min(U[0, 0], U[1, 0])
126 if max_u > 12:
127 return U - (max_u - 12)
128 if min_u < -12:
129 return U - (min_u + 12)
130 return U
131
132
133def run_test(arm, initial_X, goal, max_separation_error=0.01,
134 show_graph=True, iterations=200, controller_arm=None,
135 observer_arm=None):
136 """Runs the arm plant with an initial condition and goal.
137
138 The tests themselves are not terribly sophisticated; I just test for
139 whether the goal has been reached and whether the separation goes
140 outside of the initial and goal values by more than max_separation_error.
141 Prints out something for a failure of either condition and returns
142 False if tests fail.
143 Args:
144 arm: arm object to use.
145 initial_X: starting state.
146 goal: goal state.
147 show_graph: Whether or not to display a graph showing the changing
148 states and voltages.
149 iterations: Number of timesteps to run the model for.
150 controller_arm: arm object to get K from, or None if we should
151 use arm.
152 observer_arm: arm object to use for the observer, or None if we should
153 use the actual state.
154 """
155
156 arm.X = initial_X
157
158 if controller_arm is None:
159 controller_arm = arm
160
161 if observer_arm is not None:
162 observer_arm.X_hat = initial_X + 0.01
163 observer_arm.X_hat = initial_X
164
165 # Various lists for graphing things.
166 t = []
167 x_avg = []
168 x_sep = []
169 x_hat_avg = []
170 x_hat_sep = []
171 v_avg = []
172 v_sep = []
173 u_left = []
174 u_right = []
175
176 sep_plot_gain = 100.0
177
178 for i in xrange(iterations):
179 X_hat = arm.X
180 if observer_arm is not None:
181 X_hat = observer_arm.X_hat
182 x_hat_avg.append(observer_arm.X_hat[0, 0])
183 x_hat_sep.append(observer_arm.X_hat[2, 0] * sep_plot_gain)
184 U = controller_arm.K * (goal - X_hat)
185 U = CapU(U)
186 x_avg.append(arm.X[0, 0])
187 v_avg.append(arm.X[1, 0])
188 x_sep.append(arm.X[2, 0] * sep_plot_gain)
189 v_sep.append(arm.X[3, 0])
190 if observer_arm is not None:
191 observer_arm.PredictObserver(U)
192 arm.Update(U)
193 if observer_arm is not None:
194 observer_arm.Y = arm.Y
195 observer_arm.CorrectObserver(U)
196
197 t.append(i * arm.dt)
198 u_left.append(U[0, 0])
199 u_right.append(U[1, 0])
200
201 print numpy.linalg.inv(arm.A)
202 print "delta time is ", arm.dt
203 print "Velocity at t=0 is ", x_avg[0], v_avg[0], x_sep[0], v_sep[0]
204 print "Velocity at t=1+dt is ", x_avg[1], v_avg[1], x_sep[1], v_sep[1]
205
206 if show_graph:
207 pylab.subplot(2, 1, 1)
208 pylab.plot(t, x_avg, label='x avg')
209 pylab.plot(t, x_sep, label='x sep')
210 if observer_arm is not None:
211 pylab.plot(t, x_hat_avg, label='x_hat avg')
212 pylab.plot(t, x_hat_sep, label='x_hat sep')
213 pylab.legend()
214
215 pylab.subplot(2, 1, 2)
216 pylab.plot(t, u_left, label='u left')
217 pylab.plot(t, u_right, label='u right')
218 pylab.legend()
219 pylab.show()
220
221
222def main(argv):
223 loaded_mass = 25
224 #loaded_mass = 0
225 arm = Arm(mass=13 + loaded_mass)
226 arm_controller = Arm(mass=13 + 15)
227 observer_arm = Arm(mass=13 + 15)
228 #observer_arm = None
229
230 # Test moving the arm with constant separation.
231 initial_X = numpy.matrix([[0.0], [0.0], [0.01], [0.0]])
232 #initial_X = numpy.matrix([[0.0], [0.0], [0.00], [0.0]])
233 R = numpy.matrix([[1.0], [0.0], [0.0], [0.0]])
234 run_test(arm, initial_X, R, controller_arm=arm_controller,
235 observer_arm=observer_arm)
236
237 # Write the generated constants out to a file.
238 if len(argv) != 3:
239 print "Expected .h file name and .cc file name for the arm."
240 else:
241 arm = Arm("Arm")
242 loop_writer = control_loop.ControlLoopWriter("Arm", [arm])
243 if argv[1][-3:] == '.cc':
244 loop_writer.Write(argv[2], argv[1])
245 else:
246 loop_writer.Write(argv[1], argv[2])
247
248if __name__ == '__main__':
249 sys.exit(main(sys.argv))