blob: 718d4d972065c806d11eb564882fcd4ce9ec2592 [file] [log] [blame]
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -07001#!/usr/bin/python
2
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
19vcc = 30.0 # volts
20R_motor = 0.0055 # ohms for the motor
21R = 0.008 # ohms for system
22
23L = 10.0 * 1e-6 # Henries
24M = L / 10.0
25
26Kv = 22000.0 * 2.0 * numpy.pi / 60.0 / vcc * 2.0
27J = 0.0000007
28
29R_shunt = 0.0003
30
31# RC circuit for current sense filtering.
32R_sense1 = 768.0
33R_sense2 = 1470.0
34C_sense = 10.0 * 1e-9
35
36# So, we measured the inductance by switching between ~5 and ~20 amps through
37# the motor.
38# We then looked at the change in voltage that should give us (assuming duty
39# cycle * vin), and divided it by the corresponding change in current.
40
41# We then looked at the amount of time it took to decay the current to 1/e
42# That gave us the inductance.
43
44# Overrides for experiments
45J = J * 10.0
46
47# Firing phase A -> 0.0
48# Firing phase B -> - numpy.pi * 2.0 / 3.0
49# Firing phase C -> + numpy.pi * 2.0 / 3.0
50
51hz = 20000.0
52
53#switching_pattern = 'front'
54switching_pattern = 'centered'
55#switching_pattern = 'rear'
56#switching_pattern = 'centered front shifted'
57#switching_pattern = 'anticentered'
58
59Vconv = numpy.matrix([[2.0, -1.0, -1.0],
60 [-1.0, 2.0, -1.0],
61 [-1.0, -1.0, 2.0]]) / 3.0
62
63def f_single(theta):
64 return K1 * numpy.sin(theta) + K2 * numpy.sin(theta * 5)
65
66def g_single(theta):
67 return K1 * numpy.sin(theta) - K2 * numpy.sin(theta * 5)
68
69def gdot_single(theta):
70 """Derivitive of the current.
71
72 Must be multiplied by omega externally.
73 """
74 return K1 * numpy.cos(theta) - 5.0 * K2 * numpy.cos(theta * 5.0)
75
76f = numpy.vectorize(f_single, otypes=(numpy.float,))
77g = numpy.vectorize(g_single, otypes=(numpy.float,))
78gdot = numpy.vectorize(gdot_single, otypes=(numpy.float,))
79
80def torque(theta):
81 return f(theta) * g(theta)
82
83def phase_a(function, theta):
84 return function(theta)
85
86def phase_b(function, theta):
87 return function(theta + 2 * numpy.pi / 3)
88
89def phase_c(function, theta):
90 return function(theta + 4 * numpy.pi / 3)
91
92def phases(function, theta):
93 return numpy.matrix([[phase_a(function, theta)],
94 [phase_b(function, theta)],
95 [phase_c(function, theta)]])
96
97def all_phases(function, theta_range):
98 return (phase_a(function, theta_range) +
99 phase_b(function, theta_range) +
100 phase_c(function, theta_range))
101
102theta_range = numpy.linspace(start=0, stop=4 * numpy.pi, num=10000)
103one_amp_driving_voltage = R * g(theta_range) + (L * gdot(theta_range) + M * gdot(theta_range + 2.0 / 3.0 * numpy.pi) + M * gdot(theta_range - 2.0 / 3.0 * numpy.pi)) * Kv * vcc / 2.0
104
105max_one_amp_driving_voltage = max(one_amp_driving_voltage)
106
107# The number to divide the product of the unit BEMF and the per phase current
108# by to get motor current.
109one_amp_scalar = (phases(f_single, 0.0).T * phases(g_single, 0.0))[0, 0]
110
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -0700111print('Max BEMF', max(f(theta_range)))
112print('Max current', max(g(theta_range)))
113print('Max drive voltage (one_amp_driving_voltage)', max(one_amp_driving_voltage))
114print('one_amp_scalar', one_amp_scalar)
Austin Schuh075a5072017-10-21 18:05:25 -0700115
116pylab.figure()
117pylab.subplot(1, 1, 1)
118pylab.plot(theta_range, f(theta_range), label='bemf')
119pylab.plot(theta_range, g(theta_range), label='phase_current')
120pylab.plot(theta_range, torque(theta_range), label='phase_torque')
121pylab.plot(theta_range, all_phases(torque, theta_range), label='sum_torque/current')
122pylab.legend()
123
124
125def full_sample_times(Ton, Toff, dt, n, start_time):
126 """Returns n + 4 samples for the provided switching times.
127
128 We need the timesteps and Us to integrate.
129
Brian Silvermane044a512018-01-05 12:55:00 -0800130 Args:
131 Ton: On times for each phase.
132 Toff: Off times for each phase.
133 dt: The cycle time.
134 n: Number of intermediate points to include in the result.
135 start_time: Starting value for the t values in the result.
136
Austin Schuh075a5072017-10-21 18:05:25 -0700137 Returns:
138 array of [t, U matrix]
139 """
140
141 assert((Toff <= 1.0).all())
Brian Silvermane044a512018-01-05 12:55:00 -0800142 assert((Ton <= 1.0).all())
143 assert((Toff >= 0.0).all())
144 assert((Ton >= 0.0).all())
Austin Schuh075a5072017-10-21 18:05:25 -0700145
146 if (Ton <= Toff).all():
Austin Schuh075a5072017-10-21 18:05:25 -0700147 on_before_off = True
148 else:
Brian Silvermane044a512018-01-05 12:55:00 -0800149 # Verify that they are all ordered correctly.
150 assert(not (Ton <= Toff).any())
Austin Schuh075a5072017-10-21 18:05:25 -0700151 on_before_off = False
152
153 Toff = Toff.copy() * dt
154 Toff[Toff < 100e-9] = -1.0
155 Toff[Toff > dt] = dt
156
157 Ton = Ton.copy() * dt
158 Ton[Ton < 100e-9] = -1.0
159 Ton[Ton > dt - 100e-9] = dt + 1.0
160
161 result = []
162 t = 0
163
164 result_times = numpy.concatenate(
165 (numpy.linspace(0, dt, num=n),
166 numpy.reshape(numpy.asarray(Ton[numpy.logical_and(Ton < dt, Ton > 0.0)]), (-1,)),
167 numpy.reshape(numpy.asarray(Toff[numpy.logical_and(Toff < dt, Toff > 0.0)]), (-1,))
168 ))
169 result_times.sort()
Brian Silvermane044a512018-01-05 12:55:00 -0800170 assert((result_times >= 0).all())
171 assert((result_times <= dt).all())
Austin Schuh075a5072017-10-21 18:05:25 -0700172
Brian Silvermane044a512018-01-05 12:55:00 -0800173 for t in result_times:
Austin Schuh075a5072017-10-21 18:05:25 -0700174 if on_before_off:
175 U = numpy.matrix([[vcc], [vcc], [vcc]])
176 U[t <= Ton] = 0.0
177 U[Toff < t] = 0.0
178 else:
179 U = numpy.matrix([[0.0], [0.0], [0.0]])
180 U[t > Ton] = vcc
181 U[t <= Toff] = vcc
182 result.append((float(t + start_time), U.copy()))
183
184 return result
185
186def sample_times(T, dt, n, start_time):
187 if switching_pattern == 'rear':
188 T = 1.0 - T
189 ans = full_sample_times(T, numpy.matrix(numpy.ones((3, 1))) * 1.0, dt, n, start_time)
190 elif switching_pattern == 'centered front shifted':
191 # Centered, but shifted to the beginning of the cycle.
192 Ton = 0.5 - T / 2.0
193 Toff = 0.5 + T / 2.0
194
195 tn = min(Ton)[0, 0]
196 Ton -= tn
197 Toff -= tn
198
199 ans = full_sample_times(Ton, Toff, dt, n, start_time)
200 elif switching_pattern == 'centered':
201 # Centered, looks waaay better.
202 Ton = 0.5 - T / 2.0
203 Toff = 0.5 + T / 2.0
204
205 ans = full_sample_times(Ton, Toff, dt, n, start_time)
206 elif switching_pattern == 'anticentered':
207 # Centered, looks waaay better.
208 Toff = T / 2.0
209 Ton = 1.0 - T / 2.0
210
211 ans = full_sample_times(Ton, Toff, dt, n, start_time)
212 elif switching_pattern == 'front':
213 ans = full_sample_times(numpy.matrix(numpy.zeros((3, 1))), T, dt, n, start_time)
214 else:
215 assert(False)
216
217 return ans
218
219class DataLogger(object):
220 def __init__(self, title=None):
221 self.title = title
222 self.ia = []
223 self.ib = []
224 self.ic = []
225 self.ia_goal = []
226 self.ib_goal = []
227 self.ic_goal = []
228 self.ia_controls = []
229 self.ib_controls = []
230 self.ic_controls = []
231 self.isensea = []
232 self.isenseb = []
233 self.isensec = []
234
235 self.va = []
236 self.vb = []
237 self.vc = []
238 self.van = []
239 self.vbn = []
240 self.vcn = []
241
242 self.ea = []
243 self.eb = []
244 self.ec = []
245
246 self.theta = []
247 self.omega = []
248
249 self.i_goal = []
250
251 self.time = []
252 self.controls_time = []
253 self.predicted_time = []
254
255 self.ia_pred = []
256 self.ib_pred = []
257 self.ic_pred = []
258
259 self.voltage_time = []
260 self.estimated_velocity = []
261 self.U_last = numpy.matrix(numpy.zeros((3, 1)))
262
263 def log_predicted(self, current_time, p):
264 self.predicted_time.append(current_time)
265 self.ia_pred.append(p[0, 0])
266 self.ib_pred.append(p[1, 0])
267 self.ic_pred.append(p[2, 0])
268
269 def log_controls(self, current_time, measured_current, In, E, estimated_velocity):
270 self.controls_time.append(current_time)
271 self.ia_controls.append(measured_current[0, 0])
272 self.ib_controls.append(measured_current[1, 0])
273 self.ic_controls.append(measured_current[2, 0])
274
275 self.ea.append(E[0, 0])
276 self.eb.append(E[1, 0])
277 self.ec.append(E[2, 0])
278
279 self.ia_goal.append(In[0, 0])
280 self.ib_goal.append(In[1, 0])
281 self.ic_goal.append(In[2, 0])
282 self.estimated_velocity.append(estimated_velocity)
283
284 def log_data(self, X, U, current_time, Vn, i_goal):
285 self.ia.append(X[0, 0])
286 self.ib.append(X[1, 0])
287 self.ic.append(X[2, 0])
288
289 self.i_goal.append(i_goal)
290
291 self.isensea.append(X[5, 0])
292 self.isenseb.append(X[6, 0])
293 self.isensec.append(X[7, 0])
294
295 self.theta.append(X[3, 0])
296 self.omega.append(X[4, 0])
297
298 self.time.append(current_time)
299
300 self.van.append(Vn[0, 0])
301 self.vbn.append(Vn[1, 0])
302 self.vcn.append(Vn[2, 0])
303
304 if (self.U_last != U).any():
305 self.va.append(self.U_last[0, 0])
306 self.vb.append(self.U_last[1, 0])
307 self.vc.append(self.U_last[2, 0])
308 self.voltage_time.append(current_time)
309
310 self.va.append(U[0, 0])
311 self.vb.append(U[1, 0])
312 self.vc.append(U[2, 0])
313 self.voltage_time.append(current_time)
314 self.U_last = U.copy()
315
316 def plot(self):
317 fig = pylab.figure()
318 pylab.subplot(3, 1, 1)
319 pylab.plot(self.controls_time, self.ia_controls, 'ro', label='ia_controls')
320 pylab.plot(self.controls_time, self.ib_controls, 'go', label='ib_controls')
321 pylab.plot(self.controls_time, self.ic_controls, 'bo', label='ic_controls')
322 pylab.plot(self.controls_time, self.ia_goal, 'r--', label='ia_goal')
323 pylab.plot(self.controls_time, self.ib_goal, 'g--', label='ib_goal')
324 pylab.plot(self.controls_time, self.ic_goal, 'b--', label='ic_goal')
325
326 #pylab.plot(self.controls_time, self.ia_pred, 'r*', label='ia_pred')
327 #pylab.plot(self.controls_time, self.ib_pred, 'g*', label='ib_pred')
328 #pylab.plot(self.controls_time, self.ic_pred, 'b*', label='ic_pred')
329 pylab.plot(self.time, self.isensea, 'r:', label='ia_sense')
330 pylab.plot(self.time, self.isenseb, 'g:', label='ib_sense')
331 pylab.plot(self.time, self.isensec, 'b:', label='ic_sense')
332 pylab.plot(self.time, self.ia, 'r', label='ia')
333 pylab.plot(self.time, self.ib, 'g', label='ib')
334 pylab.plot(self.time, self.ic, 'b', label='ic')
335 pylab.plot(self.time, self.i_goal, label='i_goal')
336 if self.title is not None:
337 fig.canvas.set_window_title(self.title)
338 pylab.legend()
339
340 pylab.subplot(3, 1, 2)
341 pylab.plot(self.voltage_time, self.va, label='va')
342 pylab.plot(self.voltage_time, self.vb, label='vb')
343 pylab.plot(self.voltage_time, self.vc, label='vc')
344 pylab.plot(self.time, self.van, label='van')
345 pylab.plot(self.time, self.vbn, label='vbn')
346 pylab.plot(self.time, self.vcn, label='vcn')
347 pylab.plot(self.controls_time, self.ea, label='ea')
348 pylab.plot(self.controls_time, self.eb, label='eb')
349 pylab.plot(self.controls_time, self.ec, label='ec')
350 pylab.legend()
351
352 pylab.subplot(3, 1, 3)
353 pylab.plot(self.time, self.theta, label='theta')
354 pylab.plot(self.time, self.omega, label='omega')
355 pylab.plot(self.controls_time, self.estimated_velocity, label='estimated omega')
356
357 pylab.legend()
358
359 fig = pylab.figure()
360 pylab.plot(self.controls_time,
361 map(operator.sub, self.ia_goal, self.ia_controls), 'r', label='ia_error')
362 pylab.plot(self.controls_time,
363 map(operator.sub, self.ib_goal, self.ib_controls), 'g', label='ib_error')
364 pylab.plot(self.controls_time,
365 map(operator.sub, self.ic_goal, self.ic_controls), 'b', label='ic_error')
366 if self.title is not None:
367 fig.canvas.set_window_title(self.title)
368 pylab.legend()
369 pylab.show()
370
371
372# So, from running a bunch of math, we know the following:
373# Van + Vbn + Vcn = 0
374# ia + ib + ic = 0
375# ea + eb + ec = 0
376# d ia/dt + d ib/dt + d ic/dt = 0
377#
378# We also have:
379# [ Van ] [ 2/3 -1/3 -1/3] [Va]
380# [ Vbn ] = [ -1/3 2/3 -1/3] [Vb]
381# [ Vcn ] [ -1/3 -1/3 2/3] [Vc]
382#
383# or,
384#
385# Vabcn = Vconv * V
386#
387# The base equation is:
388#
389# [ Van ] [ R 0 0 ] [ ia ] [ L M M ] [ dia/dt ] [ ea ]
390# [ Vbn ] = [ 0 R 0 ] [ ib ] + [ M L M ] [ dib/dt ] + [ eb ]
391# [ Vbn ] [ 0 0 R ] [ ic ] [ M M L ] [ dic/dt ] [ ec ]
392#
393# or
394#
395# Vabcn = R_matrix * I + L_matrix * I_dot + E
396#
397# We can re-arrange this as:
398#
399# inv(L_matrix) * (Vconv * V - E - R_matrix * I) = I_dot
400# B * V - inv(L_matrix) * E - A * I = I_dot
401class Simulation(object):
402 def __init__(self):
403 self.R_matrix = numpy.matrix(numpy.eye(3)) * R
404 self.L_matrix = numpy.matrix([[L, M, M], [M, L, M], [M, M, L]])
405 self.L_matrix_inv = numpy.linalg.inv(self.L_matrix)
406 self.A = self.L_matrix_inv * self.R_matrix
407 self.B = self.L_matrix_inv * Vconv
408 self.A_discrete, self.B_discrete = controls.c2d(-self.A, self.B, 1.0 / hz)
409 self.B_discrete_inverse = numpy.matrix(numpy.eye(3)) / (self.B_discrete[0, 0] - self.B_discrete[1, 0])
410
411 self.R_model = R * 1.0
412 self.L_model = L * 1.0
413 self.M_model = M * 1.0
414 self.R_matrix_model = numpy.matrix(numpy.eye(3)) * self.R_model
415 self.L_matrix_model = numpy.matrix([[self.L_model, self.M_model, self.M_model],
416 [self.M_model, self.L_model, self.M_model],
417 [self.M_model, self.M_model, self.L_model]])
418 self.L_matrix_inv_model = numpy.linalg.inv(self.L_matrix_model)
419 self.A_model = self.L_matrix_inv_model * self.R_matrix_model
420 self.B_model = self.L_matrix_inv_model * Vconv
421 self.A_discrete_model, self.B_discrete_model = \
422 controls.c2d(-self.A_model, self.B_model, 1.0 / hz)
423 self.B_discrete_inverse_model = numpy.matrix(numpy.eye(3)) / (self.B_discrete_model[0, 0] - self.B_discrete_model[1, 0])
424
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -0700425 print('constexpr double kL = %g;' % self.L_model)
426 print('constexpr double kM = %g;' % self.M_model)
427 print('constexpr double kR = %g;' % self.R_model)
428 print('constexpr float kAdiscrete_diagonal = %gf;' % self.A_discrete_model[0, 0])
429 print('constexpr float kAdiscrete_offdiagonal = %gf;' % self.A_discrete_model[1, 0])
430 print('constexpr float kBdiscrete_inv_diagonal = %gf;' % self.B_discrete_inverse_model[0, 0])
431 print('constexpr float kBdiscrete_inv_offdiagonal = %gf;' % self.B_discrete_inverse_model[1, 0])
432 print('constexpr double kOneAmpScalar = %g;' % one_amp_scalar)
433 print('constexpr double kMaxOneAmpDrivingVoltage = %g;' % max_one_amp_driving_voltage)
Austin Schuh075a5072017-10-21 18:05:25 -0700434 print('A_discrete', self.A_discrete)
435 print('B_discrete', self.B_discrete)
436 print('B_discrete_sub', numpy.linalg.inv(self.B_discrete[0:2, 0:2]))
437 print('B_discrete_inv', self.B_discrete_inverse)
438
439 # Xdot[5:, :] = (R_sense2 + R_sense1) / R_sense2 * (
440 # (1.0 / (R_sense1 * C_sense)) * (-Isense * R_sense2 / (R_sense1 + R_sense2) * (R_sense1 / R_sense2 + 1.0) + I))
441 self.mk1 = (R_sense2 + R_sense1) / R_sense2 * (1.0 / (R_sense1 * C_sense))
442 self.mk2 = -self.mk1 * R_sense2 / (R_sense1 + R_sense2) * (R_sense1 / R_sense2 + 1.0)
443
444 # ia, ib, ic, theta, omega, isensea, isenseb, isensec
445 self.X = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]])
446
447 self.K = 0.05 * Vconv
448 print('A %s' % repr(self.A))
449 print('B %s' % repr(self.B))
450 print('K %s' % repr(self.K))
451
452 print('System poles are %s' % repr(numpy.linalg.eig(self.A)[0]))
453 print('Poles are %s' % repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
454
455 controllability = controls.ctrb(self.A, self.B)
456 print('Rank of augmented controlability matrix. %d' % numpy.linalg.matrix_rank(
457 controllability))
458
459 self.data_logger = DataLogger(switching_pattern)
460 self.current_time = 0.0
461
462 self.estimated_velocity = self.X[4, 0]
463
464 def motor_diffeq(self, x, t, U):
465 I = numpy.matrix(x[0:3]).T
466 theta = x[3]
467 omega = x[4]
468 Isense = numpy.matrix(x[5:]).T
469
470 dflux = phases(f_single, theta) / Kv
471
472 Xdot = numpy.matrix(numpy.zeros((8, 1)))
473 di_dt = -self.A_model * I + self.B_model * U - self.L_matrix_inv_model * dflux * omega
474 torque = I.T * dflux
475 Xdot[0:3, :] = di_dt
476 Xdot[3, :] = omega
477 Xdot[4, :] = torque / J
478
479 Xdot[5:, :] = self.mk1 * I + self.mk2 * Isense
480 return numpy.squeeze(numpy.asarray(Xdot))
481
482 def DoControls(self, goal_current):
483 theta = self.X[3, 0]
484 # Use the actual angular velocity.
485 omega = self.X[4, 0]
486
487 measured_current = self.X[5:, :].copy()
488
489 # Ok, lets now fake it.
490 E_imag1 = numpy.exp(1j * theta) * K1 * numpy.matrix(
491 [[-1j],
492 [-1j * numpy.exp(1j * numpy.pi * 2.0 / 3.0)],
493 [-1j * numpy.exp(-1j * numpy.pi * 2.0 / 3.0)]])
494 E_imag2 = numpy.exp(1j * 5.0 * theta) * K2 * numpy.matrix(
495 [[-1j],
496 [-1j * numpy.exp(-1j * numpy.pi * 2.0 / 3.0)],
497 [-1j * numpy.exp(1j * numpy.pi * 2.0 / 3.0)]])
498
499 overall_measured_current = ((E_imag1 + E_imag2).real.T * measured_current / one_amp_scalar)[0, 0]
500
501 current_error = goal_current - overall_measured_current
502 #print(current_error)
503 self.estimated_velocity += current_error * 1.0
504 omega = self.estimated_velocity
505
506 # Now, apply the transfer function of the inductor.
507 # Use that to difference the current across the cycle.
508 Icurrent = self.Ilast
509 # No history:
510 #Icurrent = phases(g_single, theta) * goal_current
511 Inext = phases(g_single, theta + omega * 1.0 / hz) * goal_current
512
513 deltaI = Inext - Icurrent
514
515 H1 = -numpy.linalg.inv(1j * omega * self.L_matrix + self.R_matrix) * omega / Kv
516 H2 = -numpy.linalg.inv(1j * omega * 5.0 * self.L_matrix + self.R_matrix) * omega / Kv
517 p_imag = H1 * E_imag1 + H2 * E_imag2
518 p_next_imag = numpy.exp(1j * omega * 1.0 / hz) * H1 * E_imag1 + \
519 numpy.exp(1j * omega * 5.0 * 1.0 / hz) * H2 * E_imag2
520 p = p_imag.real
521
522 # So, we now know how much the change in current is due to changes in BEMF.
523 # Subtract that, and then run the stock statespace equation.
524 Vn_ff = self.B_discrete_inverse * (Inext - self.A_discrete * (Icurrent - p) - p_next_imag.real)
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -0700525 print('Vn_ff', Vn_ff)
526 print('Inext', Inext)
Austin Schuh075a5072017-10-21 18:05:25 -0700527 Vn = Vn_ff + self.K * (Icurrent - measured_current)
528
529 E = phases(f_single, self.X[3, 0]) / Kv * self.X[4, 0]
530 self.data_logger.log_controls(self.current_time, measured_current, Icurrent, E, self.estimated_velocity)
531
532 self.Ilast = Inext
533
534 return Vn
535
536 def Simulate(self):
537 start_wall_time = time.time()
538 self.Ilast = numpy.matrix(numpy.zeros((3, 1)))
539 for n in range(200):
540 goal_current = 10.0
541 max_current = (vcc - (self.X[4, 0] / Kv * 2.0)) / max_one_amp_driving_voltage
542 min_current = (-vcc - (self.X[4, 0] / Kv * 2.0)) / max_one_amp_driving_voltage
543 goal_current = max(min_current, min(max_current, goal_current))
544
545 Vn = self.DoControls(goal_current)
546
547 #Vn = numpy.matrix([[0.20], [0.0], [0.0]])
548 #Vn = numpy.matrix([[0.00], [0.20], [0.0]])
549 #Vn = numpy.matrix([[0.00], [0.0], [0.20]])
550
551 # T is the fractional rate.
552 T = Vn / vcc
553 tn = -numpy.min(T)
554 T += tn
555 if (T > 1.0).any():
556 T = T / numpy.max(T)
557
558 for t, U in sample_times(T = T,
559 dt = 1.0 / hz, n = 10,
560 start_time = self.current_time):
561 # Analog amplifier mode!
562 #U = Vn
563
564 self.data_logger.log_data(self.X, (U - min(U)), self.current_time, Vn, goal_current)
565 t_array = numpy.array([self.current_time, t])
566 self.X = numpy.matrix(scipy.integrate.odeint(
567 self.motor_diffeq,
568 numpy.squeeze(numpy.asarray(self.X)),
569 t_array, args=(U,)))[1, :].T
570
571 self.current_time = t
572
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -0700573 print('Took %f to simulate' % (time.time() - start_wall_time))
Austin Schuh075a5072017-10-21 18:05:25 -0700574
575 self.data_logger.plot()
576
577simulation = Simulation()
578simulation.Simulate()