What I don't get is, will AHRS algorithm work properly even if I'm driving and accelerating in a car? How can those algorithm estimate where e.g. gravity is applying, whilst being accelerated in multiple directions?
The answer is "Yes but no". As you already mentioned, there is no way for an IMU sensor to differentiate the constant gravity from a constant lateral acceleration. You can make a lot of assumptions. For example in a car, it is really unlikely that the gravity force will have a high component in the lateral direction because cars usually can't drive up walls. However, for example a plane or a drone has a much more diverse movement pattern. This can become difficult.
The general way is to assume, that accelerations will be of short duration. Most AHRS algos use the following assumptions and ideas:
- Rotation around an axis can be calculated using a gyroscope and integrating its angular velocity over time. Due to offset errors this value will run away over time.
- Accelation can be measured using the acceleromater (Well, that's obvious, right?). Therefore, it can measure the gravity vector which is constant over time. However short accelerations of your object will affect the measurement (Assuming there are no longer acceleration periods, due to physical limits).
The general pattern is:
The accelarometer is used for the long time observation of the gravity vector. Because the gyroscope's integrated angular velocity is not good for determining rotation over a longer period. Howerver, you don't want the higher frequencies of acceletation in your data because linear acceleration would affect your AHRS estimation. Therefore you use a highly averaged accelerometer data and combine it with a short term angular estimation calculated by integration of angular velocity. This way you basically cancel out the negative effect of both measurements and are left with a long term stable grivity vector that also responds quickly on rotations due to the angular velocity measurement.
common Filter techniques to combine the data (buzzword: sensor fusion) are:
- A complementary filter. Not very good. But easy to understand and implement
- a load of proprietary algorithms that people sell for a lot of money.
- My favourite: The Madgwick filter: https://x-io.co.uk/open-source-imu-and-ahrs-algorithms/ There is open source code available and a paper explaining the algorithm.
Also you have to note, that true AHRS is only possible for roll and pith using an IMU. You need an magnetic field sensor or some other sort of fixed reference to determine the yaw heading. IMU algorithms won't work there because the gyroscope drift will change the heading and there is no component in the gravitation field measured by the accelaration sensor that can cancel this out.
Madgwicks algorithm also includes the possiblility to use an magnetic field sensor.
I would recommend playing with a custom written complementary filter for getting a feeling of the matter because it's very easy to implement. However, for soemthing reliable, more complicated algorithms are advantageous.