Skip to content

Commit c64007f

Browse files
committed
AP_Scripting: added qnh_alt example
useful for BVLOS ops
1 parent 3b96788 commit c64007f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--[[
2+
example that sends QNH altitude to GCS as a NAMED_VALUE_FLOAT QNG_ALT_FT in feet
3+
4+
Note: operator must be QNH_PRESSURE in hPa before each flight!
5+
--]]
6+
7+
local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7}
8+
9+
local PARAM_TABLE_KEY = 93
10+
local PARAM_TABLE_PREFIX = "QNH_"
11+
12+
-- add a parameter and bind it to a variable
13+
function bind_add_param(name, idx, default_value)
14+
assert(param:add_param(PARAM_TABLE_KEY, idx, name, default_value), string.format('could not add param %s', name))
15+
return Parameter(PARAM_TABLE_PREFIX .. name)
16+
end
17+
18+
-- setup script specific parameters
19+
assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 8), 'could not add param table')
20+
21+
--[[
22+
// @Param: QNH_PRESSURE
23+
// @DisplayName: QNH pressure in hPa
24+
// @Description: QNH pressure in hPa
25+
// @Range: 900 1200
26+
// @Units: hPa
27+
// @User: Standard
28+
--]]
29+
local QNH_PRESSURE = bind_add_param('PRESSURE', 1, 0)
30+
31+
--[[
32+
send QNH based alt to GCS
33+
--]]
34+
local function send_qnh_alt()
35+
local pressure_Pa = baro:get_pressure()
36+
local qnh_Pa = QNH_PRESSURE:get()*100
37+
local alt_m = baro:get_altitude_difference(qnh_Pa, pressure_Pa)
38+
local alt_feet = alt_m * 3.280839895013123
39+
gcs:send_named_float("QNH_ALT_FT", alt_feet)
40+
end
41+
42+
function update()
43+
if QNH_PRESSURE:get() > 0 then
44+
send_qnh_alt()
45+
end
46+
return update,1000
47+
end
48+
49+
gcs:send_text(MAV_SEVERITY.INFO, "Loaded qnh_alt")
50+
51+
return update,1000

0 commit comments

Comments
 (0)