Flyaway with Rangefinder and 4.0.6

Had a flyaway today. Copter was supposed to stop climbing at 20m but continued until 120m. rangefinder was working perfectly until I updated to 4.0.6 now it does not work at all that I can tell and I had my first flyaway today. Was able to get it into Loiter and come back down. I disabled the rangefinder for now.

Using Terraranger TerraBee One.

Logs here:

The log shows that the range finder is providing 0m readings when the target is out-of-range. That is strange!

@rmackay9 this is the issue we talked on the DevCall.
Maybee a Terraranger device driver bug?
@JimmyU Can you post a log file of a previously working version?

Certainly, attached is a log with version 4.0.5 and the Rangefinder was working correctly. Nothing was changed on the copter except the firmware only.

I think @amilcarlucas is correct that there is a bug in the driver. It looks like @WickedShell has probably fixed the issue with this PR.

I don’t think there is a difference in behaviour between 4.0.5 and 4.0.6 though. I don’t see any code changes in 4.0.6 that could cause the problem. The 4.0.5 log provided the vehicle never climbs above 3.5 meters and it is always in Stabilize or Land which don’t use the rangefinder.

The good news is this fix is included in Copter-4.1.0 which should start beta testing in about a month.

@JimmyU can you please confirm the 4.0.5 also has the bug, and that 4.1.0-dev fixes the bug?

I’ve commented on the fix but although it is an improvement and will avoid flyaways, there is a better solution.