def tilt_correction(): global initQ global acc resultQ = np.array([1.0, 0.0, 0.0, 0.0]) g = np.array([0.0, 0.0, -1.0]) #* normalized gravity vector norm_a = norm(acc) if norm_a == 0: resultQ = np.array([1.0, 0.0, 0.0, 0.0]) return resultQ #* quaternion-ized vector, 4x1 accQ = np.append(0, acc / norm_a) #* transfrom accQ to global frame dummy = Quat.multiplication(initQ, accQ) G_accQ = Quat.multiplication(dummy, Quat.inverse(initQ)) G_acc = G_accQ[1:] #* convert back to 3x1 vector G_acc = G_acc / norm(G_acc) print("norm of G_acc is ... ", norm(G_acc)) #* finding out angle & axis regarding tilt correction n = np.cross(G_acc, g) phi = math.acos(-G_acc[2]) #print("phi is ... ", phi) #* alpha: complementary filter parameter alpha = 0.97 phi = (1.0 - alpha) * phi aa = np.append(phi, n) resultQ = Quat.rodriguesToQuat(aa) #print("resultQ is ... ", resultQ) return resultQ
def rotate_by_quaternion(quaternion, vector): """ Ref. Paper [LUDW2011]_ eq. [9] rotate vector by quaternion """ q_vector = vector_to_quat(vector) q_rotated_vector = quaternion * q_vector * quaternion.inverse() return quat_to_vector(q_rotated_vector)
def quaterion_angle(loca_quater, overall_quaterion, id): return_array = [] for j, i in enumerate(overall_quaterion): diff = mult(loca_quater, inverse(i)) if j == id: w = 1 else: w = diff[0] theta = 2 * math.acos(w) return_array.append(theta) return np.degrees(return_array)
def test_indiv_methods(q): """ Prints the output for various methods in the Quaternion class for an input quaternion array Parameters ----------- q: Input quaternion array * A quaternion array of size (5 x n) Returns -------- None """ print 'in human readable form\n', quat.display(q), '\n' # quat.display(q) q0 = quat.getq0(q) q1 = quat.getq1(q) q2 = quat.getq2(q) q3 = quat.getq3(q) print 'q0 component/s', q0, '\n' print 'q1 component/s', q1, '\n' print 'q2 component/s', q2, '\n' print 'q3 component/s', q3, '\n' print 'type ', quat.get_type(q), '\n' print 'size is ', quat.get_size(q), '\n' print 'antipodal ', quat.antipodal(q), '\n' print 'inverse', quat.inverse(q), '\n' st1 = quat.quat2mat(q) print 'quat2mat ', st1, '\n' print 'mat2quat, using matrices generated in test above', quat.mat2quat( st1), '\n' return
def test_indiv_methods(q): """ Prints the output for various methods in the Quaternion class for an input quaternion array Parameters ----------- q: Input quaternion array * A quaternion array of size (5 x n) Returns -------- None """ print 'in human readable form\n', quat.display(q), '\n' # quat.display(q) q0 = quat.getq0(q) q1 = quat.getq1(q) q2 = quat.getq2(q) q3 = quat.getq3(q) print 'q0 component/s', q0, '\n' print 'q1 component/s', q1, '\n' print 'q2 component/s', q2, '\n' print 'q3 component/s', q3, '\n' print 'type ', quat.get_type(q), '\n' print 'size is ', quat.get_size(q), '\n' print 'antipodal ', quat.antipodal(q), '\n' print 'inverse', quat.inverse(q), '\n' st1 = quat.quat2mat(q) print 'quat2mat ', st1, '\n' print 'mat2quat, using matrices generated in test above', quat.mat2quat(st1), '\n' return
def misorient_fz(misquats, cryst_ptgrp, tol=1e-12): """ The function takes as input the misorientations and the corresponding crystallographic point group. It converts them using symmetry operations and returns the disorientations Parameters ---------- misquats: Quaternion class Quaternion misorientations cryst_ptgrp: string Crystallogrphic point group in Schoenflies notation tol: float Tolerance for the disorientation to belong in the fundamental zone Returns ------- disquats: quaternion class Disorientations for the given misorientations """ file_dir = os.path.dirname(os.path.realpath(__file__)) fil2 = file_dir+'/pkl_files/symm_quats_'+cryst_ptgrp+'.pkl' pkl_fil2 = open(fil2, 'rb') symm_quat = pickle.load(pkl_fil2) if misquats.ndim == 1: misquats = np.reshape(misquats, (5, 1)) disquats = quat.Quaternion(np.zeros(np.shape(misquats))) disquats[:] = np.NaN msz = quat.get_size(misquats) symm_sz = quat.get_size(symm_quat) for ct1 in range(msz): tq1 = misquats[:, ct1] tcheck = 0 for j in range(symm_sz): tsymm_q1 = quat.mtimes(tq1, symm_quat[:, j]) for k in range(symm_sz): tsymm_q2 = quat.mtimes(symm_quat[:, k], tsymm_q1) tqn1 = quat.Quaternion(np.copy(tsymm_q2)) tqn2 = quat.antipodal(tsymm_q2) tqn3 = quat.inverse(tsymm_q2) tqn4 = quat.inverse(quat.antipodal(tsymm_q2)) if check_cond(tqn1, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn1 break elif check_cond(tqn2, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn2 break elif check_cond(tqn3, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn3 break elif check_cond(tqn4, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn4 break if tcheck == 1: break if tcheck == 0: raise Exception('FZ Quaternion not found') return disquats
def misorient_fz(misquats, cryst_ptgrp, tol=1e-12): """ The function takes as input the misorientations and the corresponding crystallographic point group. It converts them using symmetry operations and returns the disorientations Parameters ---------- misquats: Quaternion class Quaternion misorientations cryst_ptgrp: string Crystallogrphic point group in Schoenflies notation tol: float Tolerance for the disorientation to belong in the fundamental zone Returns ------- disquats: quaternion class Disorientations for the given misorientations """ file_dir = os.path.dirname(os.path.realpath(__file__)) fil2 = file_dir + '/pkl_files/symm_quats_' + cryst_ptgrp + '.pkl' pkl_fil2 = open(fil2, 'rb') symm_quat = pickle.load(pkl_fil2) if misquats.ndim == 1: misquats = np.reshape(misquats, (5, 1)) disquats = quat.Quaternion(np.zeros(np.shape(misquats))) disquats[:] = np.NaN msz = quat.get_size(misquats) symm_sz = quat.get_size(symm_quat) for ct1 in range(msz): tq1 = misquats[:, ct1] tcheck = 0 for j in range(symm_sz): tsymm_q1 = quat.mtimes(tq1, symm_quat[:, j]) for k in range(symm_sz): tsymm_q2 = quat.mtimes(symm_quat[:, k], tsymm_q1) tqn1 = quat.Quaternion(np.copy(tsymm_q2)) tqn2 = quat.antipodal(tsymm_q2) tqn3 = quat.inverse(tsymm_q2) tqn4 = quat.inverse(quat.antipodal(tsymm_q2)) if check_cond(tqn1, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn1 break elif check_cond(tqn2, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn2 break elif check_cond(tqn3, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn3 break elif check_cond(tqn4, cryst_ptgrp, tol): tcheck = 1 disquats[:, ct1] = tqn4 break if tcheck == 1: break if tcheck == 0: raise Exception('FZ Quaternion not found') return disquats