With compass errors arising from magnetic interference one of the major causes of problems, I thought it might be useful to show a controlled demonstration of how that works.
To recap, when the aircraft is powered up the compass (3-axis magnetometer) reading is used to initialized the IMU yaw value, after which the rate gyros are the primary source of rotation data that are used to update the IMU yaw value. The compass data are only used for low-gain (gradual) corrections for drift and bias in the rate gyro data. As a result, it's important that the IMU yaw is correctly initialized for the FC to know the direction that the aircraft is facing. If the yaw value is substantially incorrect then the FC will apply inappropriate corrections to try to hold position.
In this test, shown in the graph below, I used a small, rare-earth magnet to reverse the magnetic field at the
M2P compass (it has only one), so that the compass measured the aircraft heading as north when it was really facing south. The magnet was just a few inches from the aircraft. On power up, the IMU yaw was incorrectly initialized at -46 seconds as approximately north, as expected. At -38 seconds I removed the magnet and, with the spurious magnetic field removed, the compass correctly changed to south. That would normally happen naturally as the aircraft took off and left the (small) area of magnetic interference.
At zero seconds I started the motors to generate a txt log file and establish which IMU was active. The txt OSD_yaw value is identical to the IMU1 yaw value, confirming IMU1 was active.
Note that although the compass and IMU yaw values disagree by around 180°, the IMU yaw value is held steady - that's because the rate gyros haven't detected any rotation. The aircraft reports a compass error but doesn't attempt to resolve the problem. At 33 seconds I rotated the aircraft to face north, as shown correctly by the compass. The rate gyros also detect the 180° rotation and, as a result change the yaw value by that amount to approximately south, even though it's now facing north.
View attachment 73554
As you might expect, this kind of discrepancy would cause serious problems in any of the P modes, such as P-GPS. If, for example, when the aircraft was facing south but the IMU yaw value was north, the aircraft started to drift south due to a north wind, the FC will attempt to bring it back north. But since the IMU thinks the aircraft is facing north, it will do that by applying forward thrust (negative pitch). Since the aircraft is really facing south, negative pitch will actually push it further south. The FC will respond with more negative pitch, leading to uncontrolled flight to the south.
I'm going to repeat this test with an actual takeoff, but that requires a large flat field so that I can switch the aircraft to ATTI mode once it starts to fly off without worrying about obstacles. I'll post the results of that later. In case anyone else is tempted to try this, do not attempt unless you have ATTI mode programmed as one of the mode switch positions for when the flight goes bad.