Ability to read Altitude value using Lua?

I have been working on a Lua script to send data over a serial port, and one of the pieces of data I want to send is the altitude. After looking at the bindings.desc file, I figured alt, which is a field of Location, is what I am looking for. I tried printing the output to the messages tab first to make sure that Location(alt) is in fact the parameter I am looking for, but couldn’t get anything to display. I figured I would check to make sure I was calling the parameter correctly by checking the type of Location(alt) and saw that it is userdata.

I am still very new to Lua, but after doing a fair amount of research, I couldn’t figure out a way to get an altitude value due to the type being userdata. Is there a way I can get altitude data from this, or is there another parameter I should be looking at altogether?

location is a variable type, like a float or bool.

To get altitude you can do this:
local dist = ahrs:get_relative_position_NED_home()
local altitude = -1*dist:z()

The -1 converts from the north east down reference frame used in the ahrs to the ‘up’ reference frame your probably looking for. That just gets altitude above home, for a absolute altitude you could use:

local position = ahrs:get_position() – this returns a location variable type
local altitude = position:alt()

Thank you for the clarification.

I am still getting an error when I try the solutions you posed, however. ahrs:get_relative_position_NED_home() and ahrs:get_position() both seem to return nil values, so I get the error that I am attempting to index a nil value when I try to get z() or alt().

You do have GPS lock? and have set home?

That was it, had to add a check to make sure a home was set before I could read the altitude. Thanks!


here i did what u say mr but no thing show to me

Check for ahrs:healthy() before attempting to use the get_position() method.

thank u for reply
i still beginer can u clear for me more or write the code
thank u bro

if ahrs:healthy() then
    -- do your calls using the ahrs: object
end

i wrote the code this but no response

function update ()
if ahrs:healthy() then
local position = ahrs:get_position()
local altitude = position:alt()
gcs:send_text(0,altitude)
end
return update()
end
return update()

function update ()
    if ahrs:healthy() then
        local position = ahrs:get_position()
        local altitude = position:alt()
        gcs:send_text(5, altitude)
    end
    return update, 100 -- schedule this function again in 100 milliseconds
end
return update()

Watch this video. Read the documentation. Reference the examples.

1 Like

thank u bro
but no thing change so no result

and very thank u for ur youtube channel i shared with u and i learnt firs script from ur video

I just tested that exact script in SITL. It works. You need a fully initialized AHRS with good position reporting before it will do anything.

You can try this instead, if you want to see something on the messages screen before that:

local FREQUENCY = 1000 -- (ms)

function update ()

    if not ahrs:healthy() then
        gcs:send_text(5, 'Script awaiting AHRS initialization...')
        return update, FREQUENCY
    end

    local position = ahrs:get_position()
    local altitude = position:alt()
    gcs:send_text(5, altitude)

    return update, FREQUENCY
end

return update()

2 Likes

thank u so much mister now it work
can i take ur email please

Just ask for help here on the forum.

ok mr and thank you
can u help me please how connect i2c with ardupilot by script

You can’t directly do that. You’ll need to use a supported I2C device, and if it’s supported, there may not be a need for scripting at all.

On 4.2.dev scripting has direct access to i2c, UART and CAN. Totally possible to write drivers for custom sensors.

See this i2c example:

That’s very cool. Thank you for the example!