Happy new year!!!
I am calculate quaternion rate with Python.But there are problems and I dont know why.I think there must be something wrong with IMU update.
My calculated and measured values just like this picture.The calculated value is a solid line and the measured value is a dashed line.
My IMU update code just like this. Before the calculation, I used a low-pass filter with Acc and Gyro.
def update(self, gyro, acc):
gyro = np.matrix(gyro)
acc = np.matrix(acc)
q_matrix = np.dot(self.q.T,self.q)
acc = acc / np.sqrt(np.sum(np.multiply(acc,acc)))
vx = 2*(q_matrix[1,3] - q_matrix[0,2])
vy = 2*(q_matrix[0,1] + q_matrix[2,3])
vz = q_matrix[0,0] - q_matrix[1,1] - q_matrix[2,2] + q_matrix[3,3]
ax = acc[0,0]
ay = acc[0,1]
az = acc[0,2]
ex = ay * vz - az * vy
ey = az * vx - ax * vz
ez = ax * vy - ay * vx
self.exInt += ex * self.Ki
self.eyInt += ey * self.Ki
self.ezInt += ez * self.Ki
gx = gyro[0,0]
gy = gyro[0,1]
gz = gyro[0,2]
gx += self.Kp * ex + self.exInt
gy += self.Kp * ey + self.eyInt
gz += self.Kp * ez + self.ezInt
q0t = self.q[0,0]
q1t = self.q[0,1]
q2t = self.q[0,2]
q3t = self.q[0,3]
self.q[0,0] = q0t + (-q1t*gx - q2t*gy -q3t*gz) * self.halfT
self.q[0,1] = q1t + (q0t*gx + q2t*gz -q3t*gy) * self.halfT
self.q[0,2] = q2t + (q0t*gy - q1t*gz +q3t*gx) * self.halfT
self.q[0,3] = q3t + (q0t*gz + q1t*gy -q2t*gx) * self.halfT
self.q = self.q / np.sqrt(np.sum(np.multiply(self.q, self.q)))
q0 = self.q[0,0]
q1 = self.q[0,1]
q2 = self.q[0,2]
q3 = self.q[0,3]
roll = math.atan2(2*(q0*q1 + q2*q3), 1 - 2*(q1*q1 + q2*q2))
pitch = math.asin(2*(q0*q2 - q3*q1))
yaw = math.atan2(2*(q0*q3 + q1*q2), 1 - 2*(q2*q2 + q3*q3))