GPS Alt in Lua script

Hello Team,
I’m working on a script where it requires to take GPS Altitude. But I couldn’t access it. Can we add the GPS Alt or by requesting for adding the library in the script?

Should be accessible via gps:location(0)

1 Like

Hi, thank you for the reply. But I’m not getting the data properly as such it is giving data from mean sea level. Is there a way to get CTUN Altitude in Lua? As from MAV Explorer I can get CTUN Alt and Des Alt!

I believe the altitude you are looking for is covered under Location: Lua Scripts — Copter documentation

When you ask for GPS altitude, you are asking for an MSL value. The data is “properly” retrieved. But now that we know what you want…

There may be a more straightforward way to get the altitude value you’re after, but I didn’t find one. Here’s how to use the Location object to get altitude above home:

local ALT_FRAME = {
    ABSOLUTE = 0,
	ABOVE_HOME = 1,
	ABOVE_ORIGIN = 2,
    ABOVE_TERRAIN = 3
}

function update()
    cur_pos = ahrs:get_position()
	if not cur_pos then return update, 200 end
	cur_pos:change_alt_frame(ALT_FRAME.ABOVE_HOME)

	-- shows alt in meters above home in Mission Planner's Quick tab; select "MAV_ALT"
	gcs:send_named_float('ALT', cur_pos:alt() / 100)

	return update, 200
end

return update, 1000
1 Like

We also have ahrs:get_hagl() which may be a more elegant method to get what you want.

Would this work?

local hagl = terrain:height_above_terrain(true)

I think the terrain bindings are only valid if you are using a terrain model. I think my answers above are what the OP is after. But that’s a lot of “I think…”

That answered my question. I’m just starting to play with lua, so I’m just getting my head around these different bindings.