blob: d31fc72c04f041efeddc67e2eb83855d12b8112a [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -07002
3from __future__ import print_function
Austin Schuh075a5072017-10-21 18:05:25 -07004
5import numpy
6from matplotlib import pylab
7import scipy.integrate
8from frc971.control_loops.python import controls
9import time
10import operator
11
12K1 = 1.81e04
13K2 = -2.65e03
14
15# Make the amplitude of the fundamental 1 for ease of playing with.
16K2 /= K1
17K1 = 1
18
James Kuszmaul521eb652018-10-17 19:09:33 -070019vcc = 31.5 # volts
Brian Silverman37a95d62018-11-09 16:08:32 -080020R = 0.0079 # ohms for system
Austin Schuh075a5072017-10-21 18:05:25 -070021
Brian Silverman37a95d62018-11-09 16:08:32 -080022L = 5.0 * 1e-6 # Henries
James Kuszmaul521eb652018-10-17 19:09:33 -070023M = 0.0
Austin Schuh075a5072017-10-21 18:05:25 -070024
James Kuszmaul521eb652018-10-17 19:09:33 -070025Kv_vcc = 30.0
26Kv = 22000.0 * 2.0 * numpy.pi / 60.0 / Kv_vcc * 2.0
27Kv = 1.0 / 0.00315
Austin Schuh075a5072017-10-21 18:05:25 -070028J = 0.0000007
29
30R_shunt = 0.0003
31
32# RC circuit for current sense filtering.
33R_sense1 = 768.0
34R_sense2 = 1470.0
35C_sense = 10.0 * 1e-9
36
37# So, we measured the inductance by switching between ~5 and ~20 amps through
38# the motor.
39# We then looked at the change in voltage that should give us (assuming duty
40# cycle * vin), and divided it by the corresponding change in current.
41
42# We then looked at the amount of time it took to decay the current to 1/e
43# That gave us the inductance.
44
45# Overrides for experiments
46J = J * 10.0
47
48# Firing phase A -> 0.0
49# Firing phase B -> - numpy.pi * 2.0 / 3.0
50# Firing phase C -> + numpy.pi * 2.0 / 3.0
51
52hz = 20000.0
53
54#switching_pattern = 'front'
55switching_pattern = 'centered'
56#switching_pattern = 'rear'
57#switching_pattern = 'centered front shifted'
58#switching_pattern = 'anticentered'
59
Ravago Jones5127ccc2022-07-31 16:32:45 -070060Vconv = numpy.matrix([[2.0, -1.0, -1.0], [-1.0, 2.0, -1.0], [-1.0, -1.0, 2.0]
61 ]) / 3.0
62
Austin Schuh075a5072017-10-21 18:05:25 -070063
64def f_single(theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -070065 return K1 * numpy.sin(theta) + K2 * numpy.sin(theta * 5)
66
Austin Schuh075a5072017-10-21 18:05:25 -070067
68def g_single(theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -070069 return K1 * numpy.sin(theta) - K2 * numpy.sin(theta * 5)
70
Austin Schuh075a5072017-10-21 18:05:25 -070071
72def gdot_single(theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -070073 """Derivitive of the current.
Austin Schuh075a5072017-10-21 18:05:25 -070074
75 Must be multiplied by omega externally.
76 """
Ravago Jones5127ccc2022-07-31 16:32:45 -070077 return K1 * numpy.cos(theta) - 5.0 * K2 * numpy.cos(theta * 5.0)
Austin Schuh075a5072017-10-21 18:05:25 -070078
Ravago Jones5127ccc2022-07-31 16:32:45 -070079
80f = numpy.vectorize(f_single, otypes=(numpy.float, ))
81g = numpy.vectorize(g_single, otypes=(numpy.float, ))
82gdot = numpy.vectorize(gdot_single, otypes=(numpy.float, ))
83
Austin Schuh075a5072017-10-21 18:05:25 -070084
85def torque(theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -070086 return f(theta) * g(theta)
87
Austin Schuh075a5072017-10-21 18:05:25 -070088
89def phase_a(function, theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -070090 return function(theta)
91
Austin Schuh075a5072017-10-21 18:05:25 -070092
93def phase_b(function, theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -070094 return function(theta + 2 * numpy.pi / 3)
95
Austin Schuh075a5072017-10-21 18:05:25 -070096
97def phase_c(function, theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -070098 return function(theta + 4 * numpy.pi / 3)
99
Austin Schuh075a5072017-10-21 18:05:25 -0700100
101def phases(function, theta):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700102 return numpy.matrix([[phase_a(function,
103 theta)], [phase_b(function, theta)],
104 [phase_c(function, theta)]])
105
Austin Schuh075a5072017-10-21 18:05:25 -0700106
107def all_phases(function, theta_range):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700108 return (phase_a(function, theta_range) + phase_b(function, theta_range) +
109 phase_c(function, theta_range))
110
Austin Schuh075a5072017-10-21 18:05:25 -0700111
112theta_range = numpy.linspace(start=0, stop=4 * numpy.pi, num=10000)
Ravago Jones5127ccc2022-07-31 16:32:45 -0700113one_amp_driving_voltage = R * g(theta_range) + (
114 L * gdot(theta_range) + M * gdot(theta_range + 2.0 / 3.0 * numpy.pi) +
115 M * gdot(theta_range - 2.0 / 3.0 * numpy.pi)) * Kv * vcc / 2.0
Austin Schuh075a5072017-10-21 18:05:25 -0700116
117max_one_amp_driving_voltage = max(one_amp_driving_voltage)
118
119# The number to divide the product of the unit BEMF and the per phase current
120# by to get motor current.
121one_amp_scalar = (phases(f_single, 0.0).T * phases(g_single, 0.0))[0, 0]
122
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -0700123print('Max BEMF', max(f(theta_range)))
124print('Max current', max(g(theta_range)))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700125print('Max drive voltage (one_amp_driving_voltage)',
126 max(one_amp_driving_voltage))
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -0700127print('one_amp_scalar', one_amp_scalar)
Austin Schuh075a5072017-10-21 18:05:25 -0700128
129pylab.figure()
130pylab.subplot(1, 1, 1)
131pylab.plot(theta_range, f(theta_range), label='bemf')
132pylab.plot(theta_range, g(theta_range), label='phase_current')
133pylab.plot(theta_range, torque(theta_range), label='phase_torque')
Ravago Jones5127ccc2022-07-31 16:32:45 -0700134pylab.plot(theta_range,
135 all_phases(torque, theta_range),
136 label='sum_torque/current')
Austin Schuh075a5072017-10-21 18:05:25 -0700137pylab.legend()
138
139
140def full_sample_times(Ton, Toff, dt, n, start_time):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700141 """Returns n + 4 samples for the provided switching times.
Austin Schuh075a5072017-10-21 18:05:25 -0700142
143 We need the timesteps and Us to integrate.
144
Brian Silvermane044a512018-01-05 12:55:00 -0800145 Args:
146 Ton: On times for each phase.
147 Toff: Off times for each phase.
148 dt: The cycle time.
149 n: Number of intermediate points to include in the result.
150 start_time: Starting value for the t values in the result.
151
Austin Schuh075a5072017-10-21 18:05:25 -0700152 Returns:
153 array of [t, U matrix]
154 """
155
Ravago Jones5127ccc2022-07-31 16:32:45 -0700156 assert ((Toff <= 1.0).all())
157 assert ((Ton <= 1.0).all())
158 assert ((Toff >= 0.0).all())
159 assert ((Ton >= 0.0).all())
Austin Schuh075a5072017-10-21 18:05:25 -0700160
Ravago Jones5127ccc2022-07-31 16:32:45 -0700161 if (Ton <= Toff).all():
162 on_before_off = True
Austin Schuh075a5072017-10-21 18:05:25 -0700163 else:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700164 # Verify that they are all ordered correctly.
165 assert (not (Ton <= Toff).any())
166 on_before_off = False
Austin Schuh075a5072017-10-21 18:05:25 -0700167
Ravago Jones5127ccc2022-07-31 16:32:45 -0700168 Toff = Toff.copy() * dt
169 Toff[Toff < 100e-9] = -1.0
170 Toff[Toff > dt] = dt
171
172 Ton = Ton.copy() * dt
173 Ton[Ton < 100e-9] = -1.0
174 Ton[Ton > dt - 100e-9] = dt + 1.0
175
176 result = []
177 t = 0
178
179 result_times = numpy.concatenate(
180 (numpy.linspace(0, dt, num=n),
181 numpy.reshape(
182 numpy.asarray(Ton[numpy.logical_and(Ton < dt, Ton > 0.0)]),
183 (-1, )),
184 numpy.reshape(
185 numpy.asarray(Toff[numpy.logical_and(Toff < dt, Toff > 0.0)]),
186 (-1, ))))
187 result_times.sort()
188 assert ((result_times >= 0).all())
189 assert ((result_times <= dt).all())
190
191 for t in result_times:
192 if on_before_off:
193 U = numpy.matrix([[vcc], [vcc], [vcc]])
194 U[t <= Ton] = 0.0
195 U[Toff < t] = 0.0
196 else:
197 U = numpy.matrix([[0.0], [0.0], [0.0]])
198 U[t > Ton] = vcc
199 U[t <= Toff] = vcc
200 result.append((float(t + start_time), U.copy()))
201
202 return result
203
Austin Schuh075a5072017-10-21 18:05:25 -0700204
205def sample_times(T, dt, n, start_time):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700206 if switching_pattern == 'rear':
207 T = 1.0 - T
208 ans = full_sample_times(T,
209 numpy.matrix(numpy.ones((3, 1))) * 1.0, dt, n,
210 start_time)
211 elif switching_pattern == 'centered front shifted':
212 # Centered, but shifted to the beginning of the cycle.
213 Ton = 0.5 - T / 2.0
214 Toff = 0.5 + T / 2.0
Austin Schuh075a5072017-10-21 18:05:25 -0700215
Ravago Jones5127ccc2022-07-31 16:32:45 -0700216 tn = min(Ton)[0, 0]
217 Ton -= tn
218 Toff -= tn
Austin Schuh075a5072017-10-21 18:05:25 -0700219
Ravago Jones5127ccc2022-07-31 16:32:45 -0700220 ans = full_sample_times(Ton, Toff, dt, n, start_time)
221 elif switching_pattern == 'centered':
222 # Centered, looks waaay better.
223 Ton = 0.5 - T / 2.0
224 Toff = 0.5 + T / 2.0
Austin Schuh075a5072017-10-21 18:05:25 -0700225
Ravago Jones5127ccc2022-07-31 16:32:45 -0700226 ans = full_sample_times(Ton, Toff, dt, n, start_time)
227 elif switching_pattern == 'anticentered':
228 # Centered, looks waaay better.
229 Toff = T / 2.0
230 Ton = 1.0 - T / 2.0
Austin Schuh075a5072017-10-21 18:05:25 -0700231
Ravago Jones5127ccc2022-07-31 16:32:45 -0700232 ans = full_sample_times(Ton, Toff, dt, n, start_time)
233 elif switching_pattern == 'front':
234 ans = full_sample_times(numpy.matrix(numpy.zeros((3, 1))), T, dt, n,
235 start_time)
236 else:
237 assert (False)
Austin Schuh075a5072017-10-21 18:05:25 -0700238
Ravago Jones5127ccc2022-07-31 16:32:45 -0700239 return ans
240
Austin Schuh075a5072017-10-21 18:05:25 -0700241
242class DataLogger(object):
Austin Schuh075a5072017-10-21 18:05:25 -0700243
Ravago Jones5127ccc2022-07-31 16:32:45 -0700244 def __init__(self, title=None):
245 self.title = title
246 self.ia = []
247 self.ib = []
248 self.ic = []
249 self.ia_goal = []
250 self.ib_goal = []
251 self.ic_goal = []
252 self.ia_controls = []
253 self.ib_controls = []
254 self.ic_controls = []
255 self.isensea = []
256 self.isenseb = []
257 self.isensec = []
Austin Schuh075a5072017-10-21 18:05:25 -0700258
Ravago Jones5127ccc2022-07-31 16:32:45 -0700259 self.va = []
260 self.vb = []
261 self.vc = []
262 self.van = []
263 self.vbn = []
264 self.vcn = []
Austin Schuh075a5072017-10-21 18:05:25 -0700265
Ravago Jones5127ccc2022-07-31 16:32:45 -0700266 self.ea = []
267 self.eb = []
268 self.ec = []
Austin Schuh075a5072017-10-21 18:05:25 -0700269
Ravago Jones5127ccc2022-07-31 16:32:45 -0700270 self.theta = []
271 self.omega = []
Austin Schuh075a5072017-10-21 18:05:25 -0700272
Ravago Jones5127ccc2022-07-31 16:32:45 -0700273 self.i_goal = []
Austin Schuh075a5072017-10-21 18:05:25 -0700274
Ravago Jones5127ccc2022-07-31 16:32:45 -0700275 self.time = []
276 self.controls_time = []
277 self.predicted_time = []
Austin Schuh075a5072017-10-21 18:05:25 -0700278
Ravago Jones5127ccc2022-07-31 16:32:45 -0700279 self.ia_pred = []
280 self.ib_pred = []
281 self.ic_pred = []
Austin Schuh075a5072017-10-21 18:05:25 -0700282
Ravago Jones5127ccc2022-07-31 16:32:45 -0700283 self.voltage_time = []
284 self.estimated_velocity = []
285 self.U_last = numpy.matrix(numpy.zeros((3, 1)))
Austin Schuh075a5072017-10-21 18:05:25 -0700286
Ravago Jones5127ccc2022-07-31 16:32:45 -0700287 def log_predicted(self, current_time, p):
288 self.predicted_time.append(current_time)
289 self.ia_pred.append(p[0, 0])
290 self.ib_pred.append(p[1, 0])
291 self.ic_pred.append(p[2, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700292
Ravago Jones5127ccc2022-07-31 16:32:45 -0700293 def log_controls(self, current_time, measured_current, In, E,
294 estimated_velocity):
295 self.controls_time.append(current_time)
296 self.ia_controls.append(measured_current[0, 0])
297 self.ib_controls.append(measured_current[1, 0])
298 self.ic_controls.append(measured_current[2, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700299
Ravago Jones5127ccc2022-07-31 16:32:45 -0700300 self.ea.append(E[0, 0])
301 self.eb.append(E[1, 0])
302 self.ec.append(E[2, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700303
Ravago Jones5127ccc2022-07-31 16:32:45 -0700304 self.ia_goal.append(In[0, 0])
305 self.ib_goal.append(In[1, 0])
306 self.ic_goal.append(In[2, 0])
307 self.estimated_velocity.append(estimated_velocity)
Austin Schuh075a5072017-10-21 18:05:25 -0700308
Ravago Jones5127ccc2022-07-31 16:32:45 -0700309 def log_data(self, X, U, current_time, Vn, i_goal):
310 self.ia.append(X[0, 0])
311 self.ib.append(X[1, 0])
312 self.ic.append(X[2, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700313
Ravago Jones5127ccc2022-07-31 16:32:45 -0700314 self.i_goal.append(i_goal)
Austin Schuh075a5072017-10-21 18:05:25 -0700315
Ravago Jones5127ccc2022-07-31 16:32:45 -0700316 self.isensea.append(X[5, 0])
317 self.isenseb.append(X[6, 0])
318 self.isensec.append(X[7, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700319
Ravago Jones5127ccc2022-07-31 16:32:45 -0700320 self.theta.append(X[3, 0])
321 self.omega.append(X[4, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700322
Ravago Jones5127ccc2022-07-31 16:32:45 -0700323 self.time.append(current_time)
Austin Schuh075a5072017-10-21 18:05:25 -0700324
Ravago Jones5127ccc2022-07-31 16:32:45 -0700325 self.van.append(Vn[0, 0])
326 self.vbn.append(Vn[1, 0])
327 self.vcn.append(Vn[2, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700328
Ravago Jones5127ccc2022-07-31 16:32:45 -0700329 if (self.U_last != U).any():
330 self.va.append(self.U_last[0, 0])
331 self.vb.append(self.U_last[1, 0])
332 self.vc.append(self.U_last[2, 0])
333 self.voltage_time.append(current_time)
Austin Schuh075a5072017-10-21 18:05:25 -0700334
Ravago Jones5127ccc2022-07-31 16:32:45 -0700335 self.va.append(U[0, 0])
336 self.vb.append(U[1, 0])
337 self.vc.append(U[2, 0])
338 self.voltage_time.append(current_time)
339 self.U_last = U.copy()
Austin Schuh075a5072017-10-21 18:05:25 -0700340
Ravago Jones5127ccc2022-07-31 16:32:45 -0700341 def plot(self):
342 fig = pylab.figure()
343 pylab.subplot(3, 1, 1)
344 pylab.plot(self.controls_time,
345 self.ia_controls,
346 'ro',
347 label='ia_controls')
348 pylab.plot(self.controls_time,
349 self.ib_controls,
350 'go',
351 label='ib_controls')
352 pylab.plot(self.controls_time,
353 self.ic_controls,
354 'bo',
355 label='ic_controls')
356 pylab.plot(self.controls_time, self.ia_goal, 'r--', label='ia_goal')
357 pylab.plot(self.controls_time, self.ib_goal, 'g--', label='ib_goal')
358 pylab.plot(self.controls_time, self.ic_goal, 'b--', label='ic_goal')
Austin Schuh075a5072017-10-21 18:05:25 -0700359
Ravago Jones5127ccc2022-07-31 16:32:45 -0700360 #pylab.plot(self.controls_time, self.ia_pred, 'r*', label='ia_pred')
361 #pylab.plot(self.controls_time, self.ib_pred, 'g*', label='ib_pred')
362 #pylab.plot(self.controls_time, self.ic_pred, 'b*', label='ic_pred')
363 pylab.plot(self.time, self.isensea, 'r:', label='ia_sense')
364 pylab.plot(self.time, self.isenseb, 'g:', label='ib_sense')
365 pylab.plot(self.time, self.isensec, 'b:', label='ic_sense')
366 pylab.plot(self.time, self.ia, 'r', label='ia')
367 pylab.plot(self.time, self.ib, 'g', label='ib')
368 pylab.plot(self.time, self.ic, 'b', label='ic')
369 pylab.plot(self.time, self.i_goal, label='i_goal')
370 if self.title is not None:
371 fig.canvas.set_window_title(self.title)
372 pylab.legend()
Austin Schuh075a5072017-10-21 18:05:25 -0700373
Ravago Jones5127ccc2022-07-31 16:32:45 -0700374 pylab.subplot(3, 1, 2)
375 pylab.plot(self.voltage_time, self.va, label='va')
376 pylab.plot(self.voltage_time, self.vb, label='vb')
377 pylab.plot(self.voltage_time, self.vc, label='vc')
378 pylab.plot(self.time, self.van, label='van')
379 pylab.plot(self.time, self.vbn, label='vbn')
380 pylab.plot(self.time, self.vcn, label='vcn')
381 pylab.plot(self.controls_time, self.ea, label='ea')
382 pylab.plot(self.controls_time, self.eb, label='eb')
383 pylab.plot(self.controls_time, self.ec, label='ec')
384 pylab.legend()
Austin Schuh075a5072017-10-21 18:05:25 -0700385
Ravago Jones5127ccc2022-07-31 16:32:45 -0700386 pylab.subplot(3, 1, 3)
387 pylab.plot(self.time, self.theta, label='theta')
388 pylab.plot(self.time, self.omega, label='omega')
389 pylab.plot(self.controls_time,
390 self.estimated_velocity,
391 label='estimated omega')
Austin Schuh075a5072017-10-21 18:05:25 -0700392
Ravago Jones5127ccc2022-07-31 16:32:45 -0700393 pylab.legend()
394
395 fig = pylab.figure()
396 pylab.plot(self.controls_time,
397 map(operator.sub, self.ia_goal, self.ia_controls),
398 'r',
399 label='ia_error')
400 pylab.plot(self.controls_time,
401 map(operator.sub, self.ib_goal, self.ib_controls),
402 'g',
403 label='ib_error')
404 pylab.plot(self.controls_time,
405 map(operator.sub, self.ic_goal, self.ic_controls),
406 'b',
407 label='ic_error')
408 if self.title is not None:
409 fig.canvas.set_window_title(self.title)
410 pylab.legend()
411 pylab.show()
Austin Schuh075a5072017-10-21 18:05:25 -0700412
413
414# So, from running a bunch of math, we know the following:
415# Van + Vbn + Vcn = 0
416# ia + ib + ic = 0
417# ea + eb + ec = 0
418# d ia/dt + d ib/dt + d ic/dt = 0
419#
420# We also have:
421# [ Van ] [ 2/3 -1/3 -1/3] [Va]
422# [ Vbn ] = [ -1/3 2/3 -1/3] [Vb]
423# [ Vcn ] [ -1/3 -1/3 2/3] [Vc]
424#
425# or,
426#
427# Vabcn = Vconv * V
428#
429# The base equation is:
430#
431# [ Van ] [ R 0 0 ] [ ia ] [ L M M ] [ dia/dt ] [ ea ]
432# [ Vbn ] = [ 0 R 0 ] [ ib ] + [ M L M ] [ dib/dt ] + [ eb ]
433# [ Vbn ] [ 0 0 R ] [ ic ] [ M M L ] [ dic/dt ] [ ec ]
434#
435# or
436#
437# Vabcn = R_matrix * I + L_matrix * I_dot + E
438#
439# We can re-arrange this as:
440#
441# inv(L_matrix) * (Vconv * V - E - R_matrix * I) = I_dot
442# B * V - inv(L_matrix) * E - A * I = I_dot
443class Simulation(object):
Austin Schuh075a5072017-10-21 18:05:25 -0700444
Ravago Jones5127ccc2022-07-31 16:32:45 -0700445 def __init__(self):
446 self.R_matrix = numpy.matrix(numpy.eye(3)) * R
447 self.L_matrix = numpy.matrix([[L, M, M], [M, L, M], [M, M, L]])
448 self.L_matrix_inv = numpy.linalg.inv(self.L_matrix)
449 self.A = self.L_matrix_inv * self.R_matrix
450 self.B = self.L_matrix_inv * Vconv
451 self.A_discrete, self.B_discrete = controls.c2d(
452 -self.A, self.B, 1.0 / hz)
453 self.B_discrete_inverse = numpy.matrix(
454 numpy.eye(3)) / (self.B_discrete[0, 0] - self.B_discrete[1, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700455
Ravago Jones5127ccc2022-07-31 16:32:45 -0700456 self.R_model = R * 1.0
457 self.L_model = L * 1.0
458 self.M_model = M * 1.0
459 self.R_matrix_model = numpy.matrix(numpy.eye(3)) * self.R_model
460 self.L_matrix_model = numpy.matrix(
461 [[self.L_model, self.M_model, self.M_model],
462 [self.M_model, self.L_model, self.M_model],
463 [self.M_model, self.M_model, self.L_model]])
464 self.L_matrix_inv_model = numpy.linalg.inv(self.L_matrix_model)
465 self.A_model = self.L_matrix_inv_model * self.R_matrix_model
466 self.B_model = self.L_matrix_inv_model * Vconv
467 self.A_discrete_model, self.B_discrete_model = \
468 controls.c2d(-self.A_model, self.B_model, 1.0 / hz)
469 self.B_discrete_inverse_model = numpy.matrix(numpy.eye(3)) / (
470 self.B_discrete_model[0, 0] - self.B_discrete_model[1, 0])
Austin Schuh075a5072017-10-21 18:05:25 -0700471
Ravago Jones5127ccc2022-07-31 16:32:45 -0700472 print('constexpr double kL = %g;' % self.L_model)
473 print('constexpr double kM = %g;' % self.M_model)
474 print('constexpr double kR = %g;' % self.R_model)
475 print('constexpr float kAdiscrete_diagonal = %gf;' %
476 self.A_discrete_model[0, 0])
477 print('constexpr float kAdiscrete_offdiagonal = %gf;' %
478 self.A_discrete_model[1, 0])
479 print('constexpr float kBdiscrete_inv_diagonal = %gf;' %
480 self.B_discrete_inverse_model[0, 0])
481 print('constexpr float kBdiscrete_inv_offdiagonal = %gf;' %
482 self.B_discrete_inverse_model[1, 0])
483 print('constexpr double kOneAmpScalar = %g;' % one_amp_scalar)
484 print('constexpr double kMaxOneAmpDrivingVoltage = %g;' %
485 max_one_amp_driving_voltage)
486 print('A_discrete', self.A_discrete)
487 print('B_discrete', self.B_discrete)
488 print('B_discrete_sub', numpy.linalg.inv(self.B_discrete[0:2, 0:2]))
489 print('B_discrete_inv', self.B_discrete_inverse)
Austin Schuh075a5072017-10-21 18:05:25 -0700490
Ravago Jones5127ccc2022-07-31 16:32:45 -0700491 # Xdot[5:, :] = (R_sense2 + R_sense1) / R_sense2 * (
492 # (1.0 / (R_sense1 * C_sense)) * (-Isense * R_sense2 / (R_sense1 + R_sense2) * (R_sense1 / R_sense2 + 1.0) + I))
493 self.mk1 = (R_sense2 + R_sense1) / R_sense2 * (1.0 /
494 (R_sense1 * C_sense))
495 self.mk2 = -self.mk1 * R_sense2 / (R_sense1 + R_sense2) * (
496 R_sense1 / R_sense2 + 1.0)
Austin Schuh075a5072017-10-21 18:05:25 -0700497
Ravago Jones5127ccc2022-07-31 16:32:45 -0700498 # ia, ib, ic, theta, omega, isensea, isenseb, isensec
499 self.X = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0],
500 [0.0]])
Austin Schuh075a5072017-10-21 18:05:25 -0700501
Ravago Jones5127ccc2022-07-31 16:32:45 -0700502 self.K = 0.05 * Vconv
503 print('A %s' % repr(self.A))
504 print('B %s' % repr(self.B))
505 print('K %s' % repr(self.K))
Austin Schuh075a5072017-10-21 18:05:25 -0700506
Ravago Jones5127ccc2022-07-31 16:32:45 -0700507 print('System poles are %s' % repr(numpy.linalg.eig(self.A)[0]))
508 print('Poles are %s' %
509 repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
Austin Schuh075a5072017-10-21 18:05:25 -0700510
Ravago Jones5127ccc2022-07-31 16:32:45 -0700511 controllability = controls.ctrb(self.A, self.B)
512 print('Rank of augmented controlability matrix. %d' %
513 numpy.linalg.matrix_rank(controllability))
Austin Schuh075a5072017-10-21 18:05:25 -0700514
Ravago Jones5127ccc2022-07-31 16:32:45 -0700515 self.data_logger = DataLogger(switching_pattern)
516 self.current_time = 0.0
Austin Schuh075a5072017-10-21 18:05:25 -0700517
Ravago Jones5127ccc2022-07-31 16:32:45 -0700518 self.estimated_velocity = self.X[4, 0]
Austin Schuh075a5072017-10-21 18:05:25 -0700519
Ravago Jones5127ccc2022-07-31 16:32:45 -0700520 def motor_diffeq(self, x, t, U):
521 I = numpy.matrix(x[0:3]).T
522 theta = x[3]
523 omega = x[4]
524 Isense = numpy.matrix(x[5:]).T
Austin Schuh075a5072017-10-21 18:05:25 -0700525
Ravago Jones5127ccc2022-07-31 16:32:45 -0700526 dflux = phases(f_single, theta) / Kv
Austin Schuh075a5072017-10-21 18:05:25 -0700527
Ravago Jones5127ccc2022-07-31 16:32:45 -0700528 Xdot = numpy.matrix(numpy.zeros((8, 1)))
529 di_dt = -self.A_model * I + self.B_model * U - self.L_matrix_inv_model * dflux * omega
530 torque = I.T * dflux
531 Xdot[0:3, :] = di_dt
532 Xdot[3, :] = omega
533 Xdot[4, :] = torque / J
Austin Schuh075a5072017-10-21 18:05:25 -0700534
Ravago Jones5127ccc2022-07-31 16:32:45 -0700535 Xdot[5:, :] = self.mk1 * I + self.mk2 * Isense
536 return numpy.squeeze(numpy.asarray(Xdot))
Austin Schuh075a5072017-10-21 18:05:25 -0700537
Ravago Jones5127ccc2022-07-31 16:32:45 -0700538 def DoControls(self, goal_current):
539 theta = self.X[3, 0]
540 # Use the actual angular velocity.
541 omega = self.X[4, 0]
Austin Schuh075a5072017-10-21 18:05:25 -0700542
Ravago Jones5127ccc2022-07-31 16:32:45 -0700543 measured_current = self.X[5:, :].copy()
544
545 # Ok, lets now fake it.
546 E_imag1 = numpy.exp(1j * theta) * K1 * numpy.matrix(
547 [[-1j], [-1j * numpy.exp(1j * numpy.pi * 2.0 / 3.0)],
Austin Schuh075a5072017-10-21 18:05:25 -0700548 [-1j * numpy.exp(-1j * numpy.pi * 2.0 / 3.0)]])
Ravago Jones5127ccc2022-07-31 16:32:45 -0700549 E_imag2 = numpy.exp(1j * 5.0 * theta) * K2 * numpy.matrix(
550 [[-1j], [-1j * numpy.exp(-1j * numpy.pi * 2.0 / 3.0)],
Austin Schuh075a5072017-10-21 18:05:25 -0700551 [-1j * numpy.exp(1j * numpy.pi * 2.0 / 3.0)]])
552
Ravago Jones5127ccc2022-07-31 16:32:45 -0700553 overall_measured_current = ((E_imag1 + E_imag2).real.T *
554 measured_current / one_amp_scalar)[0, 0]
Austin Schuh075a5072017-10-21 18:05:25 -0700555
Ravago Jones5127ccc2022-07-31 16:32:45 -0700556 current_error = goal_current - overall_measured_current
557 #print(current_error)
558 self.estimated_velocity += current_error * 1.0
559 omega = self.estimated_velocity
Austin Schuh075a5072017-10-21 18:05:25 -0700560
Ravago Jones5127ccc2022-07-31 16:32:45 -0700561 # Now, apply the transfer function of the inductor.
562 # Use that to difference the current across the cycle.
563 Icurrent = self.Ilast
564 # No history:
565 #Icurrent = phases(g_single, theta) * goal_current
566 Inext = phases(g_single, theta + omega * 1.0 / hz) * goal_current
Austin Schuh075a5072017-10-21 18:05:25 -0700567
Ravago Jones5127ccc2022-07-31 16:32:45 -0700568 deltaI = Inext - Icurrent
Austin Schuh075a5072017-10-21 18:05:25 -0700569
Ravago Jones5127ccc2022-07-31 16:32:45 -0700570 H1 = -numpy.linalg.inv(1j * omega * self.L_matrix +
571 self.R_matrix) * omega / Kv
572 H2 = -numpy.linalg.inv(1j * omega * 5.0 * self.L_matrix +
573 self.R_matrix) * omega / Kv
574 p_imag = H1 * E_imag1 + H2 * E_imag2
575 p_next_imag = numpy.exp(1j * omega * 1.0 / hz) * H1 * E_imag1 + \
576 numpy.exp(1j * omega * 5.0 * 1.0 / hz) * H2 * E_imag2
577 p = p_imag.real
Austin Schuh075a5072017-10-21 18:05:25 -0700578
Ravago Jones5127ccc2022-07-31 16:32:45 -0700579 # So, we now know how much the change in current is due to changes in BEMF.
580 # Subtract that, and then run the stock statespace equation.
581 Vn_ff = self.B_discrete_inverse * (Inext - self.A_discrete *
582 (Icurrent - p) - p_next_imag.real)
583 print('Vn_ff', Vn_ff)
584 print('Inext', Inext)
585 Vn = Vn_ff + self.K * (Icurrent - measured_current)
Austin Schuh075a5072017-10-21 18:05:25 -0700586
Ravago Jones5127ccc2022-07-31 16:32:45 -0700587 E = phases(f_single, self.X[3, 0]) / Kv * self.X[4, 0]
588 self.data_logger.log_controls(self.current_time, measured_current,
589 Icurrent, E, self.estimated_velocity)
Austin Schuh075a5072017-10-21 18:05:25 -0700590
Ravago Jones5127ccc2022-07-31 16:32:45 -0700591 self.Ilast = Inext
Austin Schuh075a5072017-10-21 18:05:25 -0700592
Ravago Jones5127ccc2022-07-31 16:32:45 -0700593 return Vn
Austin Schuh075a5072017-10-21 18:05:25 -0700594
Ravago Jones5127ccc2022-07-31 16:32:45 -0700595 def Simulate(self):
596 start_wall_time = time.time()
597 self.Ilast = numpy.matrix(numpy.zeros((3, 1)))
598 for n in range(200):
599 goal_current = 10.0
600 max_current = (
601 vcc - (self.X[4, 0] / Kv * 2.0)) / max_one_amp_driving_voltage
602 min_current = (
603 -vcc - (self.X[4, 0] / Kv * 2.0)) / max_one_amp_driving_voltage
604 goal_current = max(min_current, min(max_current, goal_current))
Austin Schuh075a5072017-10-21 18:05:25 -0700605
Ravago Jones5127ccc2022-07-31 16:32:45 -0700606 Vn = self.DoControls(goal_current)
Austin Schuh075a5072017-10-21 18:05:25 -0700607
Ravago Jones5127ccc2022-07-31 16:32:45 -0700608 #Vn = numpy.matrix([[0.20], [0.0], [0.0]])
609 #Vn = numpy.matrix([[0.00], [0.20], [0.0]])
610 #Vn = numpy.matrix([[0.00], [0.0], [0.20]])
Austin Schuh075a5072017-10-21 18:05:25 -0700611
Ravago Jones5127ccc2022-07-31 16:32:45 -0700612 # T is the fractional rate.
613 T = Vn / vcc
614 tn = -numpy.min(T)
615 T += tn
616 if (T > 1.0).any():
617 T = T / numpy.max(T)
Austin Schuh075a5072017-10-21 18:05:25 -0700618
Ravago Jones5127ccc2022-07-31 16:32:45 -0700619 for t, U in sample_times(T=T,
620 dt=1.0 / hz,
621 n=10,
622 start_time=self.current_time):
623 # Analog amplifier mode!
624 #U = Vn
Austin Schuh075a5072017-10-21 18:05:25 -0700625
Ravago Jones5127ccc2022-07-31 16:32:45 -0700626 self.data_logger.log_data(self.X, (U - min(U)),
627 self.current_time, Vn, goal_current)
628 t_array = numpy.array([self.current_time, t])
629 self.X = numpy.matrix(
630 scipy.integrate.odeint(self.motor_diffeq,
631 numpy.squeeze(numpy.asarray(
632 self.X)),
633 t_array,
634 args=(U, )))[1, :].T
Austin Schuh075a5072017-10-21 18:05:25 -0700635
Ravago Jones5127ccc2022-07-31 16:32:45 -0700636 self.current_time = t
Austin Schuh075a5072017-10-21 18:05:25 -0700637
Ravago Jones5127ccc2022-07-31 16:32:45 -0700638 print('Took %f to simulate' % (time.time() - start_wall_time))
Austin Schuh075a5072017-10-21 18:05:25 -0700639
Ravago Jones5127ccc2022-07-31 16:32:45 -0700640 self.data_logger.plot()
641
Austin Schuh075a5072017-10-21 18:05:25 -0700642
643simulation = Simulation()
644simulation.Simulate()