'Align IMU orientation to an 3D object

I am using a IMU to control 3D humanoid, while the wearing orientation of the IMU may affect the result of humanoid control. I want to have a function that do the calibration but there is some problem in the maths.

The IMU uses to control a bone, let's say the hip. The user need to wear the IMU in the hip to control the virtual hip. The wearing orientation supposed to be fixed. However, since there may be error due to slippery shirt or anything, the angles of x and y axis may not be as expected. We may suppose that the z axis may always be facing the oppsite to the user (out of the hip). the angles of x and y axis may change in every wearing but will not change after the user wore it. so calibration process may occur once the user wore the IMU.

enter image description here

The picture shown that in (a), IMU may not in the expected orientation. A calibration process is needed and produce the result in (b)

Since the orientation in (a) is not a regular orientation, I tried so many different ways to change the orientation system but not success. I first change the IMU orientation to the world orientation by hardcoding a sensordiff to world: QUATimu_world = QUATsensorDiff * QUATimu_realtime and then change the QUATimu_world to the hip orientation system with the same way. I guess there are problems in my maths. Please advice. Thank you so much.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source