Issue with FOLLOW object in Lua script for rover to follow copter

Hello,

I am trying to write a Lua script for my rover to FOLLOW a flying copter. My copter SYSID = 12, and I set rover FOLL_SYSID = 12. Here is a part of my current code:

-- check key parameters
function check_parameters()
    --[[ 
       parameter values which are auto-set on startup
    --]]
    local key_params = {
        FOLL_ENABLE = 1,
        FOLL_OFS_TYPE = 1,
    }

    for p, v in pairs(key_params) do
        local current = param:get(p)
        assert(current, string.format("Parameter %s not found", p))
        if math.abs(v - current) > 0.001 then
            param:set_and_save(p, v)
            gcs:send_text(MAV_SEVERITY.INFO, string.format("Parameter %s set to %.2f was %.2f", p, v, current))
        end
    end
end

-- update target position and velocity
function update_target()
    if not follow:have_target() then
        if have_target then
            gcs:send_text(MAV_SEVERITY.WARNING, "No target")
            arming:set_aux_auth_failed(auth_id, "Follow: no target")
        end
        have_target = false
        return
    end
    if not have_target then
        gcs:send_text(MAV_SEVERITY.INFO, "Have target")
        arming:set_aux_auth_passed(auth_id)
    end
    have_target = true

    target_pos, target_velocity = follow:get_target_location_and_velocity_ofs()
end

However, I am getting the following error:

3/12/2025 5:53:35 PM : Internal Error: ./scripts/servo.lua:134: attempt to index a nil value (global 'follow')

How do I properly initialize the follow object for the rover to follow the flying copter? Or Is there a specific setup or configuration required to enable the follow functionality in ArduPilot Lua scripting?

I would appreciate any advice or suggestions to resolve this issue.
Thank you in advance!

For a reason unknown to me, the devs have chosen to limit the follow binding to Copter or Plane.

@rmackay9 could perhaps elaborate on the rationale.

I don’t think it’d be problematic to simply enable the binding for Rover, but you’d need to set up a build environment and make the change yourself unless Randy or another dev see fit to enable the follow binding for Rover.

See here for the source:

1 Like

@Yuri_Rage Thanks for your response! I’d love to hear from other developers about why the ‘follow’ feature isn’t supported for Rover in Lua scripts, or if there’s a way to modify it to make it work more easily.

1 Like

Thanks @Yuri_Rage for the investigation.

I don’t think that there’s any reason why we can’t enable this for Rover so I think it’s just a small change to add Rover to that list of available vehicles.

I could raise a PR to change that or anyone else could as well I think

P.S. I’ve moved this to the Rover-4.6 category instead of “General”

@PMTAug,

I’ve created a PR that enables the Follow bindings for Rovers and Boats.

If you’d like a firmware to test with please tell me which autopilot you’re using.

BTW, any reason to not simply use Rover’s Follow mode?

Thanks for the testing and feedback!

Hello @rmackay9,

Thank you so much for your work! I’m currently using firmware version 4.5.7.

The reason I didn’t use Rover’s Follow mode is that I want the rover to move to a point calculated by optimizing the positions of several copters. To achieve this, I use a Lua script for control. Following a flying copter is simply a test case to validate the Lua script.

Thanks again for your support!

1 Like

@Yuri_Rage, hello, I was wondering if there’s a way to check how much memory a Lua script is using while it’s running?

Hi @PMTAug,

I suspect that the memory usage is available in the memory.txt file that documented here on the wiki. I’ve never tested this though so if this doesn’t help please ping me.

2 Likes

Didn’t see this. Randy likely has the best answer here.

@PMTAug,

@iampete has some extra info, he sayd, “You can see runtime memory by turning on the logging with `SCR_DEBUG_OPTS”

2 Likes

Thanks, Yuri_Rage and rmackay9 — I followed your suggestions and got what I needed. Really appreciate the support

1 Like

Hi @rmackay9,

Regarding the Rover follow feature using Lua scripting — previously, I only tested whether follow:have_target() would return true, which worked fine.

Now that the official Rover 4.6 release is out, I’ve installed it on my rover to run the full follow behavior. However, I noticed that although follow:have_target() still returns true, other functions like follow:get_target_location_and_velocity_ofs() and follow:get_target_location_and_velocity() almost always return nil.

Occasionally, the target position appears once (as shown in the attached image), but most of the time it’s nil.

If you have time, could you please help take a look? Below is the script I’m using to test the target tracking
target.lua (1.7 KB)

1 Like

Hi @PMTAug,

Thanks for the report. I’ve added this to the 4.6 issues list so we will have a look and report back, thanks!

2 Likes