Blending magnetometers data?

Hello,

I am making a quadcopter with 3 gps/mag modules onboard connected through UAVCAN. Unfortunately, in PX4, GPS blending is hardcoded to use exactly 2 GPS receivers. And UAVCAN driver can’t work with more than one receiver.
So I’ve implemented GPS blending myself and it works well. Same as in PX4, it calculates weighted average of GPS fixes from all connected receivers, using eph/epv as a weight for position, speed accuracy as a weight for speed estimation etc.

I have an intuition that this blending solution is better than only using one GPS or the hardswitch logic.

The question is - can something similar be done for magnetometers? Instead of assigning priorities and hard-switching on mag with higher priority, can we use data from all magnetometers at the same time to make some predictions about the attitude that would increase accuracy in any way?

2 Likes

Some updates. I’m pretty confident that blending mags can at least increase reliability. So far, px4 uses hard-switch logic: if an “active” mag fails (e.g. timeout), px4 switches to the next one. During that “switch”, ekf2 resets magbias and the UAV starts wobbling around in the air for some time.

Having more than one external magnetometer, can we mitigate this effect?

Here I’ve plotted mag_sensor data received from 2 magnetometers simultaneously (x,y,z; scaling applied). During this “test” I intentionally rotated the UAV around various axes.



You can clarly see that values given by 2 mags are not identical, and it can be for different reasons:

  • sensors may have been set up on the frame at a slightly different angle. It’s impossible to set them perfectly aligned;
  • sensors may be at a different distance to UAV hardware distorting the magnetic field;
  • scale & offset obtained during the calibration procedure isn’t perfect;
  • overall, magnetometers measure magnetic field with certain error, and calibration can’t make it perfect

If we find the dependency between these outputs OR somehow blend them, then we can soft-swtich between mags at any point, as long as all mags won’t fail at the same time. Here’s a general idea of how this may work:
virtual_mag

  1. Magnetometers publish data to internal “virtual mag” node;
  2. Virtual mag calculates the “blended” state and publishes it to ekf2 as it was a real magnetometer
  3. Whenever one mag fails, the virtual mag proceeds to update its internal state based on what the remaining mags output, hence no abrupt jump happens.

I will try this out for my own experiments (already done this for GPS), but that being said, I have absolutely no understanding or intuition regarding how ekf2 will react to that. Averaging magnetometer data or taking the median (out of 3 mags) for each axes doesn’t seem to be a good idea for me. If you have any thoughts or you know anyone who has done similar things, please share.

Thanks.

1 Like

Hi!
Interesting project.

Same as in PX4, it calculates weighted average of GPS fixes from all connected receivers, using eph/epv as a weight for position, speed accuracy as a weight for speed estimation etc.

Since you have extended this logic to three receivers I guess it is already clear, but just to be sure, we don’t just calculate a weighted instantaneous average of GPS position based on the weighted average from the different receivers. That would give a significantly worse estimate than just using one sensor due to the properties of the GPS data.

The question is - can something similar be done for magnetometers? Instead of assigning priorities and hard-switching on mag with higher priority, can we use data from all magnetometers at the same time to make some predictions about the attitude that would increase accuracy in any way?

I think before embarking on such a project you need to have your goal and requirements clearly stated. Do you want more accuracy, reliability or both?

E.g. for reliability, what are the main failure modes that you want to mitigate?

If we find the dependency between these outputs OR somehow blend them, then we can soft-swtich between mags at any point, as long as all mags won’t fail at the same time

The most straight forward way to use multiple magnetometers is to run multiple EKFs in parallel, e.g. one for each external mag on the vehicle (preferably also using different IMUs) and then switch between them based on different metrics.

1 Like

This. I understand that I must figure out WHY these jumps happen in the first place, but as I’m trying to locate this subtle issue, it’s nice to have a mechanism that switches smoothly between mags in case this happens.
Multiple EKFs (one per each IMU) will mitigate this for sure (in case this issue only “locks” one mag at a time). I didn’t know that was coming in 1.12, thanks!

It is not obvious to me why woud an average of 2 independent measurements be worse estimate than a measurement from a single sensor :frowning: Can you please give an explanation or point to some article/video that explains that?

I understand that I must figure out WHY these jumps happen in the first place, but as I’m trying to locate this subtle issue, it’s nice to have a mechanism that switches smoothly between mags in case this happens.

Yeah if you could figure that out it would be great, I have seen them for many years. We solved it by low pass filtering the norm and rejecting the mag measurements that fall outside of a thresholds.

It is not obvious to me why woud an average of 2 independent measurements be worse estimate than a measurement from a single sensor :frowning: Can you please give an explanation or point to some article/video that explains that?

It really depends on what you mean with “worse”. E.g. say you have two mags both giving identical perfect data. Now you take the mean value and push that into the EKF. You gained zero accuracy but double the probability of a sensor failure. But what I was talking about above regarding the GPS is the following:
Say you have two GPS modules currently reporting a position estimate 10 meters apart with the same accuracy. Just taking the mean value would give a blended solution right in the middle. Now you are flying around so the reported accuracy of the sensors start to rapidly change, and the relative weight of the modules jumps back and forth. The blended GPS solution would then move around completely uncorrelated to the actual GPS velocity measurements and deteriorate the quality of the EKF state estimate.

1 Like

In the discussion, @JulianOes said he hasn’t seen timeouts like this ever, and you’re saying you have seen them for many years. Strange :smiley:
Are you sure that’s exactly what you have encountered? Low-passing quick spikes is a good solution, but trying to low-pass stale data that lasts 1-3 seconds…?

Are you sure that’s exactly what you have encountered?

No but it looks exactly the same and with similar duration

Low-passing quick spikes is a good solution, but trying to low-pass stale data that lasts 1-3 seconds…?

No, we low pass filter the norm and reject the data if the new measurements are too far away from the filter output. So the bad data never gets into the low pass filter and the effect is only that no mag data is pushed into the EKF for 1-3 seconds, which is fine

1 Like

I have only used the Here GPS and mag in bench testing, so that’s all the data I have. If these timeouts are indeed a thing it would probably be good to have an issue on GitHub for it.