I wrote some code that modifies the baro altitude whenever sonar is in range, but only tested it with HC-SR04 series of sonar. I ordered and received a MaxBotix 1240 sensor, but have not played with the MaxBotix since I received it.
When flying altitude hold within sonar range, it follows terrain so going up a hill it will climb and going down a hill it descends to maintain the same height above the ground. There is a lag in response that you must tune out (altitude PID) or flying moderately fast up hill it will hit the ground before responding. Likewise flying moderately fast down hill it will get so much distance from the ground that it looses sonar signal and switches to baro (and thus simply maintains absolute height above sea level).
It smoothly manages transitions from baro to sonar and back. If you are flying altitude hold within sonar range of the ground and suddenly fly off the top edge of a cliff, it switches from sonar to baro and the altitude remains the same. Likewise if you are flying altitude hold outside of sonar range (on baro) and suddenly you are in sonar range (you came back after flying off the cliff), it starts to track the sonar range and follow ground terrain.
If you know git and build your own code, the old code is available in git at: theothercliff/LP-225_sonar_support
Don't bother trying it with HC-SR04, you will just get frustrated...
password:oops