ardupilot/libraries/AP_Scripting/applets/Param_Controller.lua

95 lines
2.3 KiB
Lua

--[[
a script to select other parameters using an auxillary switch
from subdirectories in scripts directory labeled /1,/2, or /3
--]]
local SEL_CH = 302
local PARAM_FILENAME = "params.param"
--[[
check that directory exists
--]]
function check_subdir_exists(n)
return dirlist(get_scripts_dir() .. "/" .. n )
end
--[[
get the path to the scripts directory. This will be scripts/ on SITL
and APM/scripts on a ChibiOS board
--]]
function get_scripts_dir()
local dlist1 = dirlist("APM/scripts")
if dlist1 and #dlist1 > 0 then
return "APM/scripts"
end
-- otherwise assume scripts/
return "scripts"
end
--[[
load parameters from a file PARAM_FILENAME from directory n
--]]
function param_load(n)
count = 0
failed = false
file_name = get_scripts_dir() .. "/" .. n .."/" .. PARAM_FILENAME
-- Open file
file = io.open(file_name)
if not file then
gcs:send_text(0,string.format("%s not present",file_name))
return
end
while true do
local line = file:read()
if not line then
break
end
-- trim trailing spaces
line = string.gsub(line, '^(.-)%s*$', '%1')
local _, _, parm, value = string.find(line, "^([%w_]+)%s*([%d]*.[%d]*)")
if parm then
if not param:set(parm,value) then
failed = true
else
count = count +1
end
end
end
if not failed then
gcs:send_text(6,string.format("Loaded %u parameters",count))
else
gcs:send_text(6,string.format("Loaded %u parameters but some params did not exist to set",count))
end
end
local sw_last = -1
local load_param = true
function update()
local sw_current = rc:get_aux_cached(SEL_CH)
if (sw_current == sw_last) or (sw_current == nil) then
return update, 500
end
if sw_current == 0 then
subdir = 1
elseif sw_current == 2 then
subdir = 3
else
subdir = 2
end
sw_last = sw_current
if not check_subdir_exists(subdir) then
gcs:send_text(0,string.format("Scripts subdirectory /%s does not exist!",subdir))
return update, 500
end
if load_param then
param_load(subdir)
load_param = false
end
return update, 500
end
gcs:send_text(5,"Loaded Parameter_Controller.lua")
return update, 500