Lua Interface: Problem getting altitude data

So I have a lua script and all I want to do is get the vehicle altitude. Currently in Arduplane SITL, I call the function:

ahrs:get_hagl()

and it continues to return nil, and I am not quite sure why

From the screenshot above, I have my lua script simply printing out the waypoint and then calling the get_hagl() function. EK2 is enabled and it’s sending altitude data to the GCS so I’m not sure why the Lua call does not work

I think get_hagl might only work if there is terrain data available.

You can use:

local dist = ahrs:get_relative_position_NED_home()
if dist then
altitude = -1*dist:z() – convert from down to up
end

2 Likes

Thanks that worked for me!