Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Brian Silverman | b0ebf1d | 2018-10-17 23:36:40 -0700 | [diff] [blame] | 2 | |
| 3 | from __future__ import print_function |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 4 | |
| 5 | import numpy |
| 6 | from matplotlib import pylab |
| 7 | import scipy.integrate |
| 8 | from frc971.control_loops.python import controls |
| 9 | import time |
| 10 | import operator |
| 11 | |
| 12 | K1 = 1.81e04 |
| 13 | K2 = -2.65e03 |
| 14 | |
| 15 | # Make the amplitude of the fundamental 1 for ease of playing with. |
| 16 | K2 /= K1 |
| 17 | K1 = 1 |
| 18 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 19 | vcc = 31.5 # volts |
Brian Silverman | 37a95d6 | 2018-11-09 16:08:32 -0800 | [diff] [blame] | 20 | R = 0.0079 # ohms for system |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 21 | |
Brian Silverman | 37a95d6 | 2018-11-09 16:08:32 -0800 | [diff] [blame] | 22 | L = 5.0 * 1e-6 # Henries |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 23 | M = 0.0 |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 24 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 25 | Kv_vcc = 30.0 |
| 26 | Kv = 22000.0 * 2.0 * numpy.pi / 60.0 / Kv_vcc * 2.0 |
| 27 | Kv = 1.0 / 0.00315 |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 28 | J = 0.0000007 |
| 29 | |
| 30 | R_shunt = 0.0003 |
| 31 | |
| 32 | # RC circuit for current sense filtering. |
| 33 | R_sense1 = 768.0 |
| 34 | R_sense2 = 1470.0 |
| 35 | C_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 |
| 46 | J = 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 | |
| 52 | hz = 20000.0 |
| 53 | |
| 54 | #switching_pattern = 'front' |
| 55 | switching_pattern = 'centered' |
| 56 | #switching_pattern = 'rear' |
| 57 | #switching_pattern = 'centered front shifted' |
| 58 | #switching_pattern = 'anticentered' |
| 59 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 60 | Vconv = 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 63 | |
| 64 | def f_single(theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 65 | return K1 * numpy.sin(theta) + K2 * numpy.sin(theta * 5) |
| 66 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 67 | |
| 68 | def g_single(theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 69 | return K1 * numpy.sin(theta) - K2 * numpy.sin(theta * 5) |
| 70 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 71 | |
| 72 | def gdot_single(theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 73 | """Derivitive of the current. |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 74 | |
| 75 | Must be multiplied by omega externally. |
| 76 | """ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 77 | return K1 * numpy.cos(theta) - 5.0 * K2 * numpy.cos(theta * 5.0) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 78 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 79 | |
| 80 | f = numpy.vectorize(f_single, otypes=(numpy.float, )) |
| 81 | g = numpy.vectorize(g_single, otypes=(numpy.float, )) |
| 82 | gdot = numpy.vectorize(gdot_single, otypes=(numpy.float, )) |
| 83 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 84 | |
| 85 | def torque(theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 86 | return f(theta) * g(theta) |
| 87 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 88 | |
| 89 | def phase_a(function, theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 90 | return function(theta) |
| 91 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 92 | |
| 93 | def phase_b(function, theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 94 | return function(theta + 2 * numpy.pi / 3) |
| 95 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 96 | |
| 97 | def phase_c(function, theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 98 | return function(theta + 4 * numpy.pi / 3) |
| 99 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 100 | |
| 101 | def phases(function, theta): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 102 | return numpy.matrix([[phase_a(function, |
| 103 | theta)], [phase_b(function, theta)], |
| 104 | [phase_c(function, theta)]]) |
| 105 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 106 | |
| 107 | def all_phases(function, theta_range): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 108 | return (phase_a(function, theta_range) + phase_b(function, theta_range) + |
| 109 | phase_c(function, theta_range)) |
| 110 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 111 | |
| 112 | theta_range = numpy.linspace(start=0, stop=4 * numpy.pi, num=10000) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 113 | one_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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 116 | |
| 117 | max_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. |
| 121 | one_amp_scalar = (phases(f_single, 0.0).T * phases(g_single, 0.0))[0, 0] |
| 122 | |
Brian Silverman | b0ebf1d | 2018-10-17 23:36:40 -0700 | [diff] [blame] | 123 | print('Max BEMF', max(f(theta_range))) |
| 124 | print('Max current', max(g(theta_range))) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 125 | print('Max drive voltage (one_amp_driving_voltage)', |
| 126 | max(one_amp_driving_voltage)) |
Brian Silverman | b0ebf1d | 2018-10-17 23:36:40 -0700 | [diff] [blame] | 127 | print('one_amp_scalar', one_amp_scalar) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 128 | |
| 129 | pylab.figure() |
| 130 | pylab.subplot(1, 1, 1) |
| 131 | pylab.plot(theta_range, f(theta_range), label='bemf') |
| 132 | pylab.plot(theta_range, g(theta_range), label='phase_current') |
| 133 | pylab.plot(theta_range, torque(theta_range), label='phase_torque') |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 134 | pylab.plot(theta_range, |
| 135 | all_phases(torque, theta_range), |
| 136 | label='sum_torque/current') |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 137 | pylab.legend() |
| 138 | |
| 139 | |
| 140 | def full_sample_times(Ton, Toff, dt, n, start_time): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 141 | """Returns n + 4 samples for the provided switching times. |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 142 | |
| 143 | We need the timesteps and Us to integrate. |
| 144 | |
Brian Silverman | e044a51 | 2018-01-05 12:55:00 -0800 | [diff] [blame] | 145 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 152 | Returns: |
| 153 | array of [t, U matrix] |
| 154 | """ |
| 155 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 156 | assert ((Toff <= 1.0).all()) |
| 157 | assert ((Ton <= 1.0).all()) |
| 158 | assert ((Toff >= 0.0).all()) |
| 159 | assert ((Ton >= 0.0).all()) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 160 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 161 | if (Ton <= Toff).all(): |
| 162 | on_before_off = True |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 163 | else: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 164 | # Verify that they are all ordered correctly. |
| 165 | assert (not (Ton <= Toff).any()) |
| 166 | on_before_off = False |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 167 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 168 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 204 | |
| 205 | def sample_times(T, dt, n, start_time): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 206 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 215 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 216 | tn = min(Ton)[0, 0] |
| 217 | Ton -= tn |
| 218 | Toff -= tn |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 219 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 220 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 225 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 226 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 231 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 232 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 238 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 239 | return ans |
| 240 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 241 | |
| 242 | class DataLogger(object): |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 243 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 244 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 258 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 259 | self.va = [] |
| 260 | self.vb = [] |
| 261 | self.vc = [] |
| 262 | self.van = [] |
| 263 | self.vbn = [] |
| 264 | self.vcn = [] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 265 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 266 | self.ea = [] |
| 267 | self.eb = [] |
| 268 | self.ec = [] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 269 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 270 | self.theta = [] |
| 271 | self.omega = [] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 272 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 273 | self.i_goal = [] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 274 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 275 | self.time = [] |
| 276 | self.controls_time = [] |
| 277 | self.predicted_time = [] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 278 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 279 | self.ia_pred = [] |
| 280 | self.ib_pred = [] |
| 281 | self.ic_pred = [] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 282 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 283 | self.voltage_time = [] |
| 284 | self.estimated_velocity = [] |
| 285 | self.U_last = numpy.matrix(numpy.zeros((3, 1))) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 286 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 287 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 292 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 293 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 299 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 300 | self.ea.append(E[0, 0]) |
| 301 | self.eb.append(E[1, 0]) |
| 302 | self.ec.append(E[2, 0]) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 303 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 304 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 308 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 309 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 313 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 314 | self.i_goal.append(i_goal) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 315 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 316 | self.isensea.append(X[5, 0]) |
| 317 | self.isenseb.append(X[6, 0]) |
| 318 | self.isensec.append(X[7, 0]) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 319 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 320 | self.theta.append(X[3, 0]) |
| 321 | self.omega.append(X[4, 0]) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 322 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 323 | self.time.append(current_time) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 324 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 325 | self.van.append(Vn[0, 0]) |
| 326 | self.vbn.append(Vn[1, 0]) |
| 327 | self.vcn.append(Vn[2, 0]) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 328 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 329 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 334 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 335 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 340 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 341 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 359 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 360 | #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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 373 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 374 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 385 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 386 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 392 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 393 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 412 | |
| 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 |
| 443 | class Simulation(object): |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 444 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 445 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 455 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 456 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 471 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 472 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 490 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 491 | # 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 497 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 498 | # 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 501 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 502 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 506 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 507 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 510 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 511 | controllability = controls.ctrb(self.A, self.B) |
| 512 | print('Rank of augmented controlability matrix. %d' % |
| 513 | numpy.linalg.matrix_rank(controllability)) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 514 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 515 | self.data_logger = DataLogger(switching_pattern) |
| 516 | self.current_time = 0.0 |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 517 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 518 | self.estimated_velocity = self.X[4, 0] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 519 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 520 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 525 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 526 | dflux = phases(f_single, theta) / Kv |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 527 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 528 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 534 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 535 | Xdot[5:, :] = self.mk1 * I + self.mk2 * Isense |
| 536 | return numpy.squeeze(numpy.asarray(Xdot)) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 537 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 538 | def DoControls(self, goal_current): |
| 539 | theta = self.X[3, 0] |
| 540 | # Use the actual angular velocity. |
| 541 | omega = self.X[4, 0] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 542 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 543 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 548 | [-1j * numpy.exp(-1j * numpy.pi * 2.0 / 3.0)]]) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 549 | E_imag2 = numpy.exp(1j * 5.0 * theta) * K2 * numpy.matrix( |
| 550 | [[-1j], [-1j * numpy.exp(-1j * numpy.pi * 2.0 / 3.0)], |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 551 | [-1j * numpy.exp(1j * numpy.pi * 2.0 / 3.0)]]) |
| 552 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 553 | overall_measured_current = ((E_imag1 + E_imag2).real.T * |
| 554 | measured_current / one_amp_scalar)[0, 0] |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 555 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 556 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 560 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 561 | # 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 567 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 568 | deltaI = Inext - Icurrent |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 569 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 570 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 578 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 579 | # 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 586 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 587 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 590 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 591 | self.Ilast = Inext |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 592 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 593 | return Vn |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 594 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 595 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 605 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 606 | Vn = self.DoControls(goal_current) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 607 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 608 | #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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 611 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 612 | # 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 618 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 619 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 625 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 626 | 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 Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 635 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 636 | self.current_time = t |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 637 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 638 | print('Took %f to simulate' % (time.time() - start_wall_time)) |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 639 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 640 | self.data_logger.plot() |
| 641 | |
Austin Schuh | 075a507 | 2017-10-21 18:05:25 -0700 | [diff] [blame] | 642 | |
| 643 | simulation = Simulation() |
| 644 | simulation.Simulate() |