It is unfortunately a more frequently occurring problem.
I just moved from a compass based yaw system to a GPS-RTK based system on a petrol powered rover. No matter what I did, there was simply to much random interference from various sources in order to get a good and reliable compass reading.
Possible solution: I don’t use PX4, so can’t help in great detail there. But in general….
- Use shielded wiring for your compass.(GPS) - Not bare as seen in your photo. (you can try to twist wires and use some aluminium foil around it. ….Whilst not perfect, it is much better than nothing at all.
- Keep it away from other cables. Especially motor and battery cables. - You can and should twist those too in order to reduce magnetic interference.
- Ideally any external compass (also GPS) should be at least 30-40cm away from any source of interference. (Cables, motors, RF equipment,…..)
- Quality GPS & compass units come with a GPS mount. –> Use it. This also helps as it lifts the device above all your other electrics and electronics.