Servers by jDrones

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!

Servers by jDrones