AP_Scripting:Add mission to Script_Controller and improve

This commit is contained in:
Henry Wurzburg authored and Andrew Tridgell committed 2023-01-17 10:24:08 +11:00
1 parent 8806088404
commit c8617ccf01
2 files changed
+74 -12

No files matched your search

@@ -4,8 +4,13 @@
--]]
local THIS_SCRIPT = "Script_Controller.lua"
local SEL_CH = 302
local sel_ch = Parameter("SCR_USER6")
SEL_CH = sel_ch:get()
if SEL_CH == 0 then
SEL_CH = 302
end
MISSION_FILENAME = "mission.txt"
--[[
check that directory exists
--]]
@@ -157,6 +162,55 @@ function copy_scripts(subdir)
return ret
end
--[[
load a mission from a MISSION_FILENAME file in subdirectory n
--]]
function mission_load(n)
file_name = get_scripts_dir() .. "/" .. n .."/" .. MISSION_FILENAME
-- Open file
file = io.open(file_name)
if not file then
return
end
-- check header
assert(string.find(file:read('l'),'QGC WPL 110') == 1, file_name .. ': incorrect format')
-- clear any existing mission
assert(mission:clear(), 'Could not clear current mission')
-- read each line and write to mission
local item = mavlink_mission_item_int_t()
local index = 0
while true do
local data = {}
for i = 1, 12 do
data[i] = file:read('n')
if data[i] == nil then
if i == 1 then
gcs:send_text(6, 'loaded mission: ' .. file_name)
return -- got to the end of the file
else
mission:clear() -- clear part loaded mission
error('failed to read file')
end
end
end
item:seq(data[1])
item:frame(data[3])
item:command(data[4])
item:param1(data[5])
item:param2(data[6])
item:param3(data[7])
item:param4(data[8])
item:x(data[9]*10^7)
item:y(data[10]*10^7)
item:z(data[11])
if not mission:set_item(index,item) then
mission:clear() -- clear part loaded mission
error(string.format('failed to set mission item %i',index))
end
index = index + 1
end
end
--[[
activate a scripting subdirectory
--]]
@@ -171,31 +225,33 @@ end
local sw_last = -1
function update()
local sw_current = rc:get_aux_cached(SEL_CH)
if sw_current == sw_last then
if sw_current == sw_last or sw_current == nil then
return update, 500
end
if sw_current == 0 then
subdir = 1
if sw_current == 1 then
subdir = 2
elseif sw_current == 2 then
subdir = 3
else
subdir = 2
subdir = 1 --default if RC not established yet
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
changes_made = activate_subdir(subdir)
if changes_made then
scripting:restart_all()
else
gcs:send_text(0, string.format("Script subbdirectory %s active", subdir))
mission_load(subdir)
gcs:send_text(0, string.format("Scripts subbdirectory %s active", subdir))
end
return update, 500
end
gcs:send_text(5,"Loaded Script_Controller.lua")
gcs:send_text(6,"Loaded Script_Controller.lua")
return update, 500