Bug using EK2_RNG_USE_HGT on rc3

Hi everyone,

I’ve been working to try to get an analog sonar work in AUTO mode but without great succes so far.
I thought that setting EK2_RNG_USE_HGT to 50% of the sonar maximum height would allow the drone to listen to the sonar data close to the ground, but maybe I’m wrong.

Anyway, I haven’t been able to try this because if I set EK2_RNG_USE_HGT = 50 (or any other value) and connect the sonar, I get Bad AHRS, and after a few seconds everything gets crazy. When I look at the screen in mission planner Flight Data it looks like the IMU sees the drone turning upside down even when it’s actually level on my table.
Mission planner says “Error pos horiz variance”.

I’ve no idea where this comes from, but when I put EK2_RNG_USE_HGT back to -1, everything works fine. The sonar works perfectly in Loiter mode but doesn’t seem to be used in AUTO mode.

Any clue to where this might come from ?

Thanks for your help,

Thomasfull_parameter_list.pdf (82.7 KB)

According to the image you have no GPS fix, so it’s not surprising that it has a horizontal position variance error. Do you want to share a log? We need it to dig deeper.

Thanks OXINARF for your answer.
Indeed I haven’t got GPS which would explain de horizontal variance, but the other probem remains with a good GPS lock.

I’ve tested a little further though and made some progress: apparently the problem doesn’t occur when I switch on the pixhawk at more than 20 cm above ground.
20 cm is my RNG_MIN_CM, so it looks like if the copter is switched on whith “unreliable” sonar information it causes problems (i’ve still EK2_RNG_USE_HGT = 50).
I’ve even tried to switch it on at 30 cm and then put the drone back below. Like this, the problem doesn’t seem to occur anymore.

I’m going to try this outside see if I manage to arm the drone and i’ll keep you up to date.

Other unrelated question: how can I have logs if i’m unable to arm the drone ? I always thought that the logs only got memorized once the drone is armed.

Thanks again for your help.


There were a sonar sanity check. I guess it still lives in APM.
You should raise the copter above the sonar minimum height in order APM to “know” that sonar is healthy.

Maybe that’s what is happening to you

Hi wjaxxx,

Thanks for your help.
I don’t know if it’s exactly what you’re describing but it seems to work this way.
For now I’ll try to continue my work with this method and do some more tests.

@wjaxxx is correct that there is a pre-arm check for the rangefinder. I was under the impression that it only run when optical flow was being used as well, but I might be wrong. You should see a pre-arm message if that’s the problem.

The check passes if:

  • max distance sensed is at least 50cm > min distance sensed
  • max distance < 200cm
  • min distance sensed is within 10cm of ground clearance or sensor’s minimum distance

Regarding the logs: that is the default behavior, but you can enable it to log while disarmed. Previous versions had a bit in the LOG_BITMASK parameter, but that has now been changed to a separate parameter, LOG_DISARMED.

I would also recommend that you update to rc4.

On RC2 I’ve tried to play with ek2_rng_use_hgt, but my altitude controller went crazy. I’ve find out that the sonar was being reflected by the landing skid that I changed a few weeks ago. The problem was there but it was hidden until rc2. I have repositioned it and now everything is fine. I’m almost sure that I read here or in diydrones some dev saying that it should not be necessary to change ek2_rng_use_hgt. It is being used following ek2 logic even without this parameter.

Thomasss, are you getting reliable readings with your sonar? That could explain the problems. What is the minimum reading that the sensor reads when putting something very close to it? mine reads 22cm, however it is 16cm above floor when on the ground. So I’ve setted rng_min_cm to 22 and rngfnd_gndclear to 22cm as well. Everything is working as expected.

PS. I’m running rc4 now

I’ve documented Terrain Following here: http://ardupilot.org/copter/docs/terrain-following.html

…but in short, there’s no need to set the EK2_RNG_USE_HGT parameter. Actually I’m wondering what is leading people to do this. Is it written some where on the wiki or are people just finding this parameter directly? Sorry, I’ve just seen a few people try this and I’d like to put some advice for them in an appropriate place so they aren’t led astray.

Re the range finder pre-arm check, as @OXINARF says, it should only be required when using optical flow.

I see we actually have two pages related to terrain following so I’ll remove the “common” one in favour of the copter specific one.

Randy: is my connection or 2m terrain following with Lidar video example at the end of the page dissapears? http://ardupilot.org/copter/docs/terrain-following.html

Hi Cala,
You’re right. We’re moving some servers around and there’s a problem with the rendering of youtube videos on the wiki. I noticed about 2 days ago and Jani’s working on it. I’m sure he’ll have it fixed soon.

1 Like

Terrain following and EK2_RNG_USE_HGT are two different things. EK2_RNG_USE_HGT actually only uses the rangefinder when you are moving slowly and the terrain isn’t changing too much. This is because it is used as a substitute for the altitude provided by the baro or GPS. Before, when not using optical flow, you couldn’t use the rangefinder for altitude source because it is relative to the ground and not an absolute altitude, like baro or GPS is.

Hi rmackay9,

Thanks a lot for your help.
The link you send is exactly what I was looking for, i’m going to go through it step by step and see if it works. I wonder why I haven’t found it earlier because i’ve searched quite a lot (i’ve stepped unpon the “common” one) … so thank you.

Originaly I wasn’t planning on using the EK2_RNG_USE_HGT, but after several tries I only managed to get the sonar working in Loiter mode and not in AUTO mode. So I went through the Complete Parameters List on the wiki and the description of this parameter seemed rather interesting. That’s why I tried.

I’ll see if I manage to make it work with the help of the wiki page. If the problem persists i’ll upload the logs.

Hi again,

So I tried to set up terrain following with all your comments and it worked !
Thanks a lot !

But now I have a quick other question: is it possible to do terrain following only on specific waypoints ?
For example, I would like a mission with a first waypoint at 20 m (which is way above the maximum sonar range) in relative, and then start a descent until 2 m above ground (the 2 m being measured by the sonar).

Is something like this possible ? I tried it this morning launching the mission at 20 m above ground (so no sonar data) but the copter went in failsafe mode because of “no terrain data”.

As you, I thought that if i fly over rangefinder max altitude, the copter continue flying with baro but I don’t try yet, thank’s for comment, Isn’t possible to disable that failsafe?

1 Like

The bad AHRS caused at startup with a combination of invalid (less than expected on ground value) range-finder data and a positive value for EK2_RNG_HGT_USE may have been fixed by this PR: https://github.com/ArduPilot/ardupilot/pull/4809

1 Like

Sorry for the slow reply.
I’ve been updating a lot of wiki pages over the past week. I moved around the copter specific terrain following wiki page so I think it’s old location was harder to find.
The MP doesn’t support sending terrain following altitudes for only some waypoints. It’s a limitation of the ground station but not the underlying Copter flight code. So you could try asking MichaelO to change the behaviour if you’d like by adding an issue here: https://github.com/ArduPilot/MissionPlanner/issues

So, I think that if you fly over the range finder’s max altitude it will simply trigger the terrain failsafe which will lead to a return-to-home. Tridge and I discussed maybe changing the behaviour so that if the terrain-altitude of the command is over the range finder’s max altitude that it uses the baro+google-earth data for terrain following.

Thank you rmackay for your reply.
I’ll ask MichaelO if it’s a feature he can add, it would be helpfull.
About Cala2’s question, I also thought that going out of the sonar range would make the drone continue in baro+google-earth data instead of triggering the failsafe. In mw opinion it seems like an interesting option to add.

Thanks anyway for all the great new features.

Perhaps a choose param indicating what to do when is out of lidar range? 1. continue flying with baro 2. RTL failsafe 3.G Earth terrain following. etc. I figure a flight that first stage you fly low following terrain but them you want to fly higher over trees, or other obstacles underside the copter but don´t want to “jump” everything so you can set the max altitude that you like to fly with lidar or change to baro; another possibility ; to have a switch function to change altimeters during fly (if it it reliable), only an idea.