AP_Scripting:Add mission to Script_Controller and improve

This commit is contained in:
Henry Wurzburg 2023-01-05 07:20:56 -06:00 committed by Andrew Tridgell
parent 8806088404
commit c8617ccf01
2 changed files with 74 additions and 12 deletions

View File

@ -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

View File

@ -1,16 +1,22 @@
# Script Control LUA Script
This script allows the user to have groups of scripts in three subdirectories of the main scripting directory and copy them into the main directory, removing any files in that directory with the .lua extension (except itself) and issue a scripting restart based on the position of a switch with the auxiliary function of "302". This provides means to have differing scripting depending on the mission or conditions, and use whichever set is desired without having them consume memory all the time and without requiring a GCS to manage the content of the scripts directory.
This script allows the user to have groups of scripts in three subdirectories of the main scripting directory and copy them into the main scripts directory, removing any files in that directory with the .lua extension (except itself) and issue a scripting restart based on the position of a switch with a programmable auxiliary function option (default is "302", but can be changed by user during setup) or using Mission Planner's AUX Function tab. This provides means to have differing scripts depending on the mission or conditions, and use whichever set is desired without having them consume memory all the time, and without requiring a GCS to manage the content of the scripts directory. It also allows a new mission to be loaded associated with those new scripts, if desired.
If the designated RC switch auxiliary option is not assigned to a RC channel via an RCx_OPTION, or the selected subdirectory does not exist, no action is taken, so the script can reside in the scripts directory even if not being used.
# Setup and Operation
The user must setup an RCx_OPTION to function 302 to determine which of the three scripts subdirectories will be used: LOW:scripts/1, MIDDLE:scripts/2, or HIGH:scripts/3. If no RC has been established during ground start, it will behave as if subdirectory 1 is selected. Changing this switch position either prior to ground start or after, will remove ALL the *.lua files in the scripts directory (except itself), and copy any *.lua files (only) from the desginated subdirectory, and then issue a scripting restart.
The auxiliary function option (300 - 307) that can be assigned to an RC channel and/or used by Mission Planner is set by the scripting parameter SCR_USER6. If not set (this param's default is "0") then function "302" is assigned automatically. Setting an RC channel's option to this value allows RC control of the scripting subdirectory that will be used for scripts (and optionally to load a mission file from within that subdirectory). Mission Planner can control switching also, or in place of the RC channel, using its AUX Function tab.
The auxiliary function (default "302") is used to determine which of the three scripts subdirectories will be used: LOW:scripts/1, MIDDLE:scripts/2, or HIGH:scripts/3. If an RC channel option has been set to this,but no RC has been established during ground start, it will behave as if subdirectory 1 is selected. Changing this switch position either prior to ground start or after (or using Mission Planner), if the selected subdirectory exists, will remove ALL the *.lua files in the scripts directory (except itself), and copy any *.lua files (only) from the desginated subdirectory, load a new mission if a mission file named "mission.txt" exists in the subdirectory, and then issue a scripting restart. If the subdirectory does not exist, then no action is taken and a GCS message issued.
# CAUTIONS
If parameters are being created and used be sure that different scripts do not use the same PARAMETER KEY to create their param tables, unless you desire to use the same parameter set in two different scripts. Otherwise, param values will be corrupt when you change scripts.
Be sure to have unique parameter names for different parameter sets. Names are only visible and used by Ground Control Stations. While indentically named params in different scripts will operate fine, you will not be able to distinguish them in the GCS displays!
If parameters are being created by scripts be sure that different scripts do not use the same PARAMETER KEY to create their param tables, unless you desire to use the same parameter set in two different scripts. Otherwise, param values will be corrupt when you change scripts.
Be sure to have unique parameter names for different parameter sets. This script does not create any parameters for potential conflict.
Any .lua script in the scripts directory will be ERASED! when this script is run. In order to keep another .lua script running when using this script, place it in every subdirectory so that it will be copied over when changing directories.
Parameter names are only visible and used by Ground Control Stations. While indentically named params in different scripts will operate fine, you will not be able to distinguish them in the GCS displays!
Any .lua script in the scripts directory will be ERASED! when this script is run. In order to keep another .lua script running when using this script, place it in every subdirectory so that it will be copied into the main scripts directory whenever changing directories.