The "gyro" array and accelwithg array are both the returned data from the hardware, respectevely for accelerometer and gyrometer.
My thought process was as follows:
- Calculate time difference between each frame
- add up all the angles
- Rotation matrix for xyz rotation
- Multiply the rotation matrix to the gravity array (0,0,9.8) to get an acceleration without gravity
However, I've noticed this method doesn't consistently work, as in the data varies a lot and the gravity doesn't get filtered out properly. Is there a better method to go on about this?
# gyro-meter calculations
dt = (ts - last_ts_gyro) / 1000
last_ts_gyro = ts
gyro_angle_x = gyro[0] * dt
gyro_angle_y = gyro[1] * dt
gyro_angle_z = gyro[2] * dt
if firstGyro:
total_x = gyro_angle_x
total_y = gyro_angle_y
total_z = gyro_angle_z
firstGyro = False
# totals
total_x += gyro_angle_x
total_y += gyro_angle_y
total_z += gyro_angle_z
# rad = > degree
dtotal_x = np.rad2deg(total_x) % 360
dtotal_y = np.rad2deg(total_y) % 360
dtotal_z = np.rad2deg(total_z) % 360
# rotation matrix
Qx = np.array(
[[1, 0, 0], [0, np.cos(dtotal_x[0]), -np.sin(dtotal_x[0])], [0, np.sin(dtotal_x[0]), np.cos(dtotal_x[0])]])
Qy = np.array(
[[np.cos(dtotal_y[0]), 0, np.sin(dtotal_y[0])], [0, 1, 0], [-np.sin(dtotal_y[0]), 0, np.cos(dtotal_y[0])]])
Qz = np.array(
[[np.cos(dtotal_z[0]), -np.sin(dtotal_z[0]), 0], [np.sin(dtotal_z[0]), np.cos(dtotal_z[0]), 0], [0, 0, 1]])
Qxyz = Qx@Qy@Qz
# a -Qxyz*g to filter out gravity
g = np.array([[0], [0], [gravity_norm]])
rotated_g = Qxyz @ g
accelwithoutg = np.subtract(accelwithg, rotated_g)