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()
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().
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()
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()