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