u[0, i] = (M22 * (v[0, i] - L2H1) - M12 * (v[1, i] - L2H2)) / detA u[1, i] = (-M21 * (v[0, i] - L2H1) + M11 * (v[1, i] - L2H2)) / detA # inverter model dx_temp = nl(x[0, i], x[1, i], x[2, i], x[3, i], pqf[0, i], pqf[1, i], w, L, C, u[0, i], u[1, i]) dx[0, i] = dx_temp[0] dx[1, i] = dx_temp[1] dx[2, i] = dx_temp[2] dx[3, i] = dx_temp[3] for j in range(4): x[j, i + 1] = x[j, i] + dx_temp[j] * t_delta # measurement y[0, i + 1] = x[0, i + 1] y[1, i + 1] = x[1, i + 1] [iL[0, i + 1], iL[1, i + 1]] = il(x[0, i], x[1, i], x[2, i], x[3, i], pqf[0, i], pqf[1, i], w, C, L) # estimate states dz_temp = ss(z[1, i], z[3, i], v[0, i], v[1, i]) for j in range(4): z[j, i + 1] = z[j, i] + dz_temp[j] * t_delta dz_temp[0] = dz_temp[0] + 0.0559 * (y[0, i + 1] - z[0, i + 1]) dz_temp[1] = dz_temp[1] - 0.5000 * (y[0, i + 1] - z[0, i + 1]) dz_temp[2] = dz_temp[2] + 0.0559 * (y[1, i + 1] - z[2, i + 1]) dz_temp[3] = dz_temp[3] - 0.5000 * (y[1, i + 1] - z[2, i + 1]) for j in range(4): z[j, i + 1] = z[j, i] + dz_temp[j] * t_delta print([ise, iae, itae]) # plot result fig, ax = plt.subplots() t1 = 0 t2 = 200
q[i], w, L, C) M11 = m11(x[0, i], x[1, i], x[2, i], w, L, C) M12 = m12(x[0, i], x[1, i], x[3, i], w, L, C) M21 = m21(x[0, i], x[1, i], x[2, i], w, L, C) M22 = m22(x[0, i], x[1, i], x[3, i], w, L, C) detA = M11 * M22 - M12 * M21 u1[i] = (M22 * (v1[i] - L2H1) - M12 * (v2[i] - L2H2)) / detA u2[i] = (-M21 * (v1[i] - L2H1) + M11 * (v2[i] - L2H2)) / detA # updating state variable dx_temp = nl(x[0, i], x[1, i], x[2, i], x[3, i], p[i], q[i], w, L, C, u1[i], u2[i]) dx[2, i] = dx_temp[2] dx[3, i] = dx_temp[3] for j in range(4): x[j, i + 1] = x[j, i] + dx_temp[j] * t_delta dz_temp = ss(dx[0, i], dx[1, i], v1[i], v2[i]) for j in range(4): z[j, i + 1] = z[j, i] + dz_temp[j] * t_delta dz_temp[0] = dz_temp[0] + 0.0838 * (x[0, i + 1] - z[0, i + 1]) dz_temp[1] = dz_temp[1] - 0.5000 * (x[0, i + 1] - z[0, i + 1]) dz_temp[2] = dz_temp[2] + 0.0838 * (x[1, i + 1] - z[2, i + 1]) dz_temp[3] = dz_temp[3] - 0.5000 * (x[1, i + 1] - z[2, i + 1]) for j in range(4): z[j, i + 1] = z[j, i] + dz_temp[j] * t_delta # updating value x[0, i + 1] = z[0, i + 1] x[1, i + 1] = z[2, i + 1] dx[0, i + 1] = z[1, i + 1] dx[1, i + 1] = z[3, i + 1] iL[0, i + 1] = -C * dx[0, i] + C * w * x[1, i + 1] + x[2, i + 1] iL[1, i + 1] = -C * dx[1, i] - C * w * x[0, i + 1] + x[3, i + 1]
ser = serial.Serial('COM3', baudrate=9600, timeout=1) z = np.zeros((4, len(t))) tmp = [' ', ' ', ' ', ' '] z[0, 1] = 0.247346 z[1, 1] = 48.64925 z[2, 1] = 0 z[3, 1] = 0 for idx in range(len(t) - 1): for i in range(4): tmp[i] = str(round(1000 * z[i, idx], 2)) + "," send = tmp[0] + tmp[2] + tmp[1] + tmp[3] ser.write(send.encode()) time.sleep(1.0) data = ser.readline().decode('ascii') if len(data) > 0: split = 0 for j in range(len(data)): if data[j] == ',': split = j v1 = float(data[0:split]) v2 = float(data[split + 1:len(data) + 1]) dz_temp = ss(z[1, idx], z[3, idx], v1, v2) for k in range(4): z[k, idx + 1] = z[k, idx] + dz_temp[k] * t_delta plt.plot(t, z[0, :]) plt.show()