AHRS.Source lua script errors

I’m trying to run the AHRS Source GPS Optflow.lua and I get the following error:

ahrs-source-gps-optiflow.lua:124: attempt to call a nil value (method 'distance_orient')

I’ve also tried ardupilot/libraries/AP_Scripting/applets/ahrs-source-extnav-optflow.lua at master · ArduPilot/ardupilot · GitHub and same error in what appears to be the same code.

(Yes, I did have the wind calibration script running, but I’ve removed that in case somehow that was causing the issue. It wasn’t)

The only edit I made to either script was to change from switch 1 (300) to switch 2 (301).

-- get rangefinder distance
  local rngfnd_distance_m = 0
  if rangefinder:has_data_orient(rangefinder_rotation) then
    rngfnd_distance_m = rangefinder:distance_orient(rangefinder_rotation)  -- this is line 124
  end
  local rngfnd_over_threshold = (rngfnd_distance_m == 0) or (rngfnd_distance_m > rangefinder_thresh_dist)

Matek H743-Bdshot AC4.5.3 Micoair MTF-01 Optical flow (all calibrated and working)

Any ideas?

Edit: Just add the ranger finder was working and I was getting accurate data from it.

Just been playing with this some more and have more info, but still no solution:

When I power the drone with USB the script works as advertised, however there is also an error that there’s no range finder data.

Once I plug in battery power the script returns to the error I mentioned above. It doesn’t seem to matter what order I apply power it’s the same. Script works with USB power (but no range finder data). Script doesn’t work on Battery (range finder working).

I’m open to suggestions.

I’ve tried a couple other range finder related scripts from the examples and none seem to work. But the range finder is working otherwise.

Does it matter that the micoair MTF-01P uses Mavlink rather than a specific driver?

I’ve just tried running the script rangefinder_test.lua. I get the GCS message that I have one rangefinder. But then I get the same Nil value errors when it tries to return any data about the range finder. (line 22…)

I’ve also tried a boot delay on the board of 3 seconds. No joy.

Anybody?