How to calculate initial Rotation Matrix/ Euler Angles/ Quaternion for IMU data (gyroscope and accelerometer)?

I am trying to develop a code to track vehicle using GNSS/GPS data (NMEA) and IMU data (Gyroscope x,y,z; accelerometer x,y,z) i.e. dead reckoning using kalman filter.

I know there's enough literature on Dead Reckoning but nowhere has anyone explained how to derive this initial rotation matrix.

It is quite easy to calculate Euler angle from Gyroscope data if initial matrix is given. But how do I calculate this initial rotation matrix? It is assumed a given everywhere.

• Your gyroscope can only measure changes in your attitude, so you need to know what your starting attitude is - hence it always being assumed as given. You would need a star tracker (or maybe a combination of GNSS position and a magnetometer or sun sensors) to determine what your starting attitude is. Commented Dec 9, 2022 at 7:26
• why not just start with the identity matrix to start? Commented Dec 9, 2022 at 15:20
• If your vehicle starts on the ground, in a state of rest, you can use the gravity vector from the accelerometer to fix one of the axes of your initial frame. Another axis from a magnetometer, and a third axis by cross product. Look up the triad.
– AJN
Commented Dec 10, 2022 at 1:53
• Not a complete answer, but this may get you started: There is an excellent book on this subject by J.B. Kuipers (2002), Quaternions and Rotations Sequences, that also includes translations to/from Euler angles. I have used this book many times in my work in missile flight simulation work. I also used it to create my own quaternion transformation libraries in MATLAB and Python (until recently even the SciPy library was a bit incomplete). It is easily the most comprehensive modern text and is written Commented Dec 10, 2022 at 15:15
• Kalman filters require initial guesses. So guess. It should converge within a few cycles if there is not a lot of noise. As someone said, make it all zero except the diagonal, which you make all 1's. That is the identity and it means it does nothing to start with. Commented Dec 15, 2022 at 5:30

I'm interpreting this question as- how to set an initial attitude (t=0), changes in which can then be tracked when the sensor readings start pouring in.

1. As Tristan's comment mentions, you could just assume an initial attitude and track, as it changes over time.

2. If you are trying to find the attitude of 'your system' in some external reference frame, (for which you are asking how to build a rotation matrix), then you need at-least 2 vectors, which are 'known' in the frames you are trying to relate.

As you already have an accelerometer, you could use the gravitational acceleration vector, and the 2nd one could be the magnetic north, the sun, and so on. Once you have these, you can build a triad, for the conversion. This video by Ross Dynamics Lab shows how to do it, and also has a MATLAB tutorial.

If the application is not super serious, then another relatively stupid way of doing it could be:

Physically place the system at some well-known angles with respect to the external frame, say 45 deg about all 3 axes, and then build a direction cosine matrix with 45 deg as the angles. And then track as it changes.

Hope this helps!