Sonar as primary source of height (at low altitudes)


I’m trying to use my sonar as a primary source of height at low altitudes. Basically, in some of my latest flights, I’ve seen the barometer going down up to -8 m while landing (Alt value in Mission Planner). At that moment, my sonar (Maxbotix MB1240) was taking more accurate measurements (around 1 m). How can I make sure that at altitudes below say 2.5 m the EKF is using my sonar instead of the barometer?

What also surprises me is that in the flight logs I have no indication of this negative altitude I do see in Mission Planner while flying. In the CTUN values I have Alt (which I assume is the one the EKF uses), SAlt (which I assume is my sonar alt) and BAlt (which I assume is the raw barometer altitude). Is all of this correct? If so, what altitude does Mission Planner sees while flying, and why don’t I see the negative altitudes in my flight logs?

I tried setting the parameter EK2_RNG_USE_HGT to 50%, while my RNGFND_MAX_CMD is 5 m (by default, it should be 7 m, but I know I have a lot of noise around that altitude, so I lowered it a bit). I understand that this should make the sonar the primary height source at 2.5 m. However, I didn’t see any difference and I can’t see it in the flight log either. I am attaching a flight log which shows two AUTO flights without the parameters set, and then two AUTO flights with the parameter set. Could you help me spot any difference (specially regarding the CTUN values)?


Bin file


The way the rangefinder reading is used is not really displayed in an intuitive way. Even when the flight controller is using the sonar, CTUN.SAlt won’t necessarily equal the EKF altitude or the displayed altitude. This is because the EKF altitude isn’t necessarily 0 when you’re on the ground.

Take a look at CTUN.DSAlt. This is desired sonar altitude. In the second half of the log during your Alt Hold and Loiter flight, you can see that the copter is maintaining its altitude by matching SAlt and DSAlt. This should mean that you have the parameters configured correctly for sonar use. Note that while it is not the same value as EKF alt, the offset between the two stays pretty constant during a flight.

DSAlt stays at 0 during the Auto flights though, and I’m actually not sure whether that is the correct behavior. Possibly the parameter WPNAV_RFND_USE will enable it during Auto/Guided, or maybe it’s being used anyways.

I dug around in the Mission Planner code to see what its Altitude reading really is, and it is a calculation that is equivalent to the log field POS.RelHomeAlt, which is absolute altitude (Pos.Alt) minus the absolute home offset (second value of ORGN.Alt), and tends to diverge from CTUN.Alt for reasons I’m not certain of.