Olaphsen

Untitled

Apr 11th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.62 KB | Source Code | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.spatial.transform import Rotation
  4.  
  5. gyroMeasError = np.radians(3 / 3600)
  6. beta = 0 * np.sqrt(3. / 4.) * gyroMeasError
  7.  
  8. nu_1 = np.radians(0 * 1 / 3600)
  9. nu_2 = np.radians(0 * 1 / 3600)
  10. nu_3 = np.radians(0 * 1 / 3600)
  11. df1 = 0 * 1e-2
  12. df2 = 0 * 1e-2
  13. df3 = 0 * 1e-2
  14.  
  15. phi = np.radians(55.8)
  16. u_0 = 7.292115e-5
  17. uE, uN, uU = 0, np.cos(phi) * u_0, np.sin(phi) * u_0
  18.  
  19. deltat = 0.004
  20.  
  21. directory = '/home/andrei/CLionProjects/imitators/cmake-build-release/tt3axis.imu'
  22.  
  23.  
  24. def quat_mult(a, b):
  25.     q0 = a[0]*b[0] - a[1]*b[1] - a[2]*b[2] - a[3]*b[3]
  26.     q1 = a[0]*b[1] + a[1]*b[0] + a[2]*b[3] - a[3]*b[2]
  27.     q2 = a[0]*b[2] + a[2]*b[0] + a[3]*b[1] - a[1]*b[3]
  28.     q3 = a[0]*b[3] + a[3]*b[0] + a[1]*b[2] - a[2]*b[1]
  29.     return [q0, q1, q2, q3]
  30.  
  31.  
  32. def inverse_quat(q):
  33.     return [q[0], *[-i for i in q[1:]]]
  34.  
  35.  
  36. def deltaq(q1, q0):
  37.     q0_inv = inverse_quat(q0)
  38.     return quat_mult(q1, q0_inv)
  39.  
  40.  
  41. def get_all_data(path):
  42.     data_1 = np.loadtxt(path, skiprows=1)
  43.     w_1, w_2, w_3 = data_1[:, 0], data_1[:, 1], data_1[:, 2]
  44.     f_1, f_2, f_3 = data_1[:, 3], data_1[:, 4], data_1[:, 5]
  45.     q0, q1, q2, q3 = data_1[:, 6], data_1[:, 7], data_1[:, 8], data_1[:, 9]
  46.     return w_1, w_2, w_3, f_1, f_2, f_3, q0, q1, q2, q3
  47.  
  48.  
  49. data_file = get_all_data(directory)
  50. nu = np.array([nu_1, nu_2, nu_3])
  51. df = np.array([df1, df2, df3])
  52. w_init = np.radians(data_file[0:3]).T.tolist()
  53. f_init = (data_file[3:6] + df[:, np.newaxis]).T.tolist()
  54. w = np.radians(data_file[:3]) + nu[:, np.newaxis]
  55. f = data_file[3:6] + df[:, np.newaxis]
  56. q_data = data_file[6:]
  57. w1, w2, w3 = w[0], w[1], w[2]
  58. f1, f2, f3 = f[0], f[1], f[2]
  59. q0_data, q1_data, q2_data, q3_data = q_data[0], q_data[1], q_data[2], q_data[3]
  60.  
  61.  
  62. def initial_alignment(f_init, w_init):
  63.     f_z = np.mean(f_init[:30*250], axis=0)
  64.     w_z = np.mean(w_init[:30*250], axis=0)
  65.     L_xz = np.array([np.cross(w_z, f_z) / np.linalg.norm(np.cross(w_z, f_z)),
  66.                      np.cross(f_z, np.cross(w_z, f_z)) / np.linalg.norm(np.cross(f_z, np.cross(w_z, f_z))),
  67.                      f_z / np.linalg.norm(f_z)])
  68.     r = Rotation.from_matrix(L_xz)
  69.     r_ = r.as_quat()[[3, 0, 1, 2]]
  70.     return L_xz, r_
  71.  
  72. L_xz, r_ = initial_alignment(f_init, w_init)
  73. qmy_0, qmy_1, qmy_2, qmy_3 = r_
  74.  
  75.  
  76. def f_data_initial():
  77.     g0_ = np.sqrt(np.mean(f_init[:30*250], axis=0)[0] ** 2 +
  78.                   np.mean(f_init[:30*250], axis=0)[1] ** 2 +
  79.                   np.mean(f_init[:30*250], axis=0)[2] ** 2)
  80.     return g0_
  81.  
  82.  
  83. g0 = f_data_initial()
  84.  
  85.  
  86. def rotation_imu():
  87.     f = []
  88.     q0, q1, q2, q3 = [], [], [], []
  89.     for m in range(len(data_file[0])):
  90.         f.append(FilterUpdate_My(w1[m], w2[m], w3[m], f1[m], f2[m], f3[m]))
  91.         q0.append(float(f[m][0]))
  92.         q1.append(float(f[m][1]))
  93.         q2.append(float(f[m][2]))
  94.         q3.append(float(f[m][3]))
  95.     return q0, q1, q2, q3
  96.  
  97.  
  98. def FilterUpdate_My(w_x, w_y, w_z, a_x, a_y, a_z):
  99.     global qmy_0, qmy_1, qmy_2, qmy_3
  100.  
  101.     norm = np.sqrt(a_x ** 2 + a_y ** 2 + a_z ** 2)
  102.     a_x /= norm
  103.     a_y /= norm
  104.     a_z /= norm
  105.  
  106.     dp0, dp1, dp2, dp3 = quat_mult([qmy_0 / 2, qmy_1 / 2, qmy_2 / 2, qmy_3 / 2], [0, w_x, w_y, w_z])
  107.     p = qmy_0 + dp0 * deltat / 2, qmy_1 + dp1 * deltat / 2, qmy_2 + dp2 * deltat / 2, qmy_3 + dp3 * deltat / 2,
  108.  
  109.  
  110.     u = np.cross([a_x, a_y, a_z], quat_mult(quat_mult(inverse_quat(p), [0, 0, 0, g0]), p)[1:])
  111.     norm_u = np.linalg.norm(u)
  112.     if norm_u > 0:
  113.         u_1, u_2, u_3 = (-2 * beta * u[:3]) / norm_u
  114.     else:
  115.         u_0, u_1, u_2, u_3 = 0, 0, 0, 0
  116.     qq_omega_0, qq_omega_1, qq_omega_2, qq_omega_3 = quat_mult([0.5 * qmy_0, 0.5 * qmy_1, 0.5 * qmy_2, 0.5 * qmy_3],
  117.                                                                [0, w_x - u_1, w_y - u_2, w_z - u_3])
  118.  
  119.     qmy_0 += qq_omega_0 * deltat
  120.     qmy_1 += qq_omega_1 * deltat
  121.     qmy_2 += qq_omega_2 * deltat
  122.     qmy_3 += qq_omega_3 * deltat
  123.  
  124.     norm = np.linalg.norm([qmy_0, qmy_1, qmy_2, qmy_3])
  125.     qmy_0 /= norm
  126.     qmy_1 /= norm
  127.     qmy_2 /= norm
  128.     qmy_3 /= norm
  129.     return [qmy_0, qmy_1, qmy_2, qmy_3]
  130.  
  131.  
  132. if __name__ == '__main__':
  133.     dq = []
  134.     A = [0, 0, 0]
  135.     nu_x = []
  136.     g_new = []
  137.     E_aa_g = []
  138.     qq = []
  139.     up = []
  140.     norm_ = []
  141.     om_u = []
  142.     q0_my, q1_my, q2_my, q3_my = rotation_imu()
  143.     T = np.arange(0, len(q0_data[:210 * 250]), 1) * deltat
  144.  
  145.     for i in range(len(T)):
  146.         dq.append(deltaq([q0_my[i], q1_my[i], q2_my[i], q3_my[i]],
  147.                          [q0_data[i], q1_data[i], q2_data[i], q3_data[i]]))
  148.  
  149.     a_E, a_N, a_U = np.empty(250 * 210), np.empty(250 * 210), np.empty(250 * 210)
  150.  
  151.     for i in range(len(T)):
  152.         if dq[i][0] < 0:
  153.             dq[i][1] = -dq[i][1]
  154.             dq[i][2] = -dq[i][2]
  155.             dq[i][3] = -dq[i][3]
  156.  
  157.     a_E[0], a_N[0], a_U[0] = dq[0][1] * 2, dq[0][2] * 2, dq[0][3] * 2
  158.  
  159.     for i in range(len(T) - 1):
  160.         E_aa_g.append([0,
  161.                        A[0] - np.cross(A, [a_E[i], a_N[i], a_U[i]])[0] + g0 * a_N[i],
  162.                        A[1] - np.cross(A, [a_E[i], a_N[i], a_U[i]])[1] - g0 * a_E[i],
  163.                        A[2] - np.cross(A, [a_E[i], a_N[i], a_U[i]])[2] + g0])
  164.         qq.append(quat_mult(quat_mult(inverse_quat([q0_my[i], q1_my[i], q2_my[i], q3_my[i]]), E_aa_g[i]),
  165.                             [q0_my[i], q1_my[i], q2_my[i], q3_my[i]])[1:] + df)
  166.         g_new.append(quat_mult(quat_mult(inverse_quat([q0_my[i], q1_my[i], q2_my[i], q3_my[i]]), [0, 0, 0, g0]),
  167.                                [q0_my[i], q1_my[i], q2_my[i], q3_my[i]])[1:])
  168.         up.append(np.cross(qq[i], g_new[i]))
  169.         norm_.append(np.linalg.norm(up[i]))
  170.         om_u.append(-2 * beta * up[i] / norm_[i])
  171.  
  172.         nu_x.append(quat_mult(quat_mult([q0_my[i], q1_my[i], q2_my[i], q3_my[i]],
  173.                                         [0, nu_1 - om_u[i][0], nu_2 - om_u[i][1], nu_3 - om_u[i][2]]),
  174.                               inverse_quat([q0_my[i], q1_my[i], q2_my[i], q3_my[i]])))
  175.  
  176.         a_E[i + 1] = a_E[i] + (uE + nu_x[:][i][1]) * deltat
  177.         a_N[i + 1] = a_N[i] + (uN + nu_x[:][i][2]) * deltat
  178.         a_U[i + 1] = a_U[i] + (uU + nu_x[:][i][3]) * deltat
  179.  
  180.  
  181. # if __name__ == '__main__':
  182. #     q_my = rotation_imu()
  183. #     T = np.arange(0, len(q0_data), 1) * deltat
  184. #     A = np.array([np.zeros(len(T)), np.zeros(len(T)), np.zeros(len(T))])
  185. #
  186. #     a_E, a_N, a_U = np.empty(len(T)), np.empty(len(T)), np.empty(len(T))
  187. #     dq = np.array(deltaq(q_my, [q0_data, q1_data, q2_data, q3_data]))
  188. #     dq[:, dq[0] < 0] *= -1
  189. #     a_E[0], a_N[0], a_U[0] = dq[1][0] * 2, dq[2][0] * 2, dq[3][0] * 2
  190. #
  191. #     g_new = quat_mult(quat_mult(inverse_quat(np.array(q_my)),
  192. #                                 [np.zeros(len(T)), np.zeros(len(T)), np.zeros(len(T)), np.full(len(T), g0)]), q_my)[1:]  # ?
  193. #     g_a = np.array([g0 * a_N, -g0 * a_E, np.full(len(T), g0)])  # ?
  194. #
  195. #     Aa = np.cross(A.T, np.array([a_E, a_N, a_U]).T).T  # ?
  196. #     E_aa_g = np.insert((A + g_a - Aa), 0, np.zeros(len(T)), axis=0)  # ?
  197. #     qq = np.array(quat_mult(quat_mult(inverse_quat(np.array(q_my)), E_aa_g), q_my))[1:] + np.array([np.full(len(T), df1), np.full(len(T), df2), np.full(len(T), df3)])  # ?
  198. #     up = np.cross(qq.T, np.array(g_new).T).T  # ?
  199. #     norm_ = np.linalg.norm(up)  # ?
  200. #     om_u = -2 * beta * up / norm_  # ?
  201. #     nu_omu = np.array([np.full(len(T), nu_1), np.full(len(T), nu_2), np.full(len(T), nu_3)]) - np.array([om_u[0], om_u[1], om_u[2]])  # ?
  202. #     nu_omu_0 = np.insert(nu_omu, 0, np.zeros(len(T)), axis=0)
  203. #
  204. #     for i in range(len(T)-1):
  205. #         a_E[i + 1] = a_E[i] + (uE + nu_omu_0[1][i]) * deltat
  206. #         a_N[i + 1] = a_N[i] + (uN + nu_omu_0[2][i]) * deltat
  207. #         a_U[i + 1] = a_U[i] + (uU + nu_omu_0[3][i]) * deltat
Advertisement
Add Comment
Please, Sign In to add comment