local RUN_INTERVAL_MS = 25 local mavlink_msgs = require("MAVLink/mavlink_msgs") local COMMAND_ACK_ID = mavlink_msgs.get_msgid("COMMAND_ACK") local COMMAND_LONG_ID = mavlink_msgs.get_msgid("COMMAND_LONG") local msg_map = {} msg_map[COMMAND_ACK_ID] = "COMMAND_ACK" msg_map[COMMAND_LONG_ID] = "COMMAND_LONG" -- initialize MAVLink rx with number of messages, and buffer depth mavlink:init(1, 10) -- register message id to receive mavlink:register_rx_msgid(COMMAND_LONG_ID) local MAV_CMD_DO_SEND_SCRIPT_MESSAGE = 217 local MAV_CMD_DO_SET_MODE = 176 -- local MAV_CMD_WAYPOINT_USER_1 = 31000 -- MavLink forwarding to SoleonAirController MAV_CMD_SO_CONTROL = 31090 --- derzeit MAV_CMD_SO_SYSMODE --> should be renamed ACO_SYS_ID = 1 ACO_COMPONENT_ID = 84 mavlink_buf = {0,0,0,0,0,0} -- Block AP parsing MAV_CMD_DO_SEND_SCRIPT_MESSAGE so we can deal with it in the script -- Prevents "unsupported" ack mavlink:block_command(MAV_CMD_DO_SEND_SCRIPT_MESSAGE) -- send the buffer to thr SoleonAirController function send_buffer_to_soac() local msg = {} msg.command = MAV_CMD_SO_CONTROL msg.target_system = ACO_SYS_ID msg.target_component = ACO_COMPONENT_ID msg.confirmation = 0 msg.param1 = mavlink_buf[1] msg.param2 = mavlink_buf[2] msg.param3 = mavlink_buf[3] msg.param4 = mavlink_buf[4] msg.param5 = mavlink_buf[5] msg.param6 = mavlink_buf[6] msg.param7 = 0 gcs:send_text(MAV_SEVERITY_NOTICE, ("ACoLua: send to SoAc <%s>"):format(mavlink_buf)) -- just for debugging --- mavlink:send_chan(chan, mavlink_msgs.encode("COMMAND_ACK", msg)) --- muass schaugen end function handle_command_long(cmd) if (cmd.command == MAV_CMD_DO_SET_MODE) then gcs:send_text(0, "Got mode change") elseif (cmd.command == MAV_CMD_DO_SEND_SCRIPT_MESSAGE) then if (0 == tonumber(cmd.param1) ) then mavlink_buf[1] = cmd.param2 mavlink_buf[2] = cmd.param3 mavlink_buf[3] = cmd.param4 else mavlink_buf[4] = cmd.param2 mavlink_buf[5] = cmd.param3 mavlink_buf[6] = cmd.param4 end -- return ack from command param value return math.min(math.max(math.floor(cmd.param1), 0), 1) end return nil end -- initialisation --init_params_tables() --reset_axes_done() --get_all_params() --save_gcs_pid_mask() gcs:send_text(MAV_SEVERITY.INFO, "ACoLua: AcoMissionFwd script loaded") --local last_warning = get_time() -- main update function function update() local msg, chan = mavlink:receive_chan() if (msg ~= nil) then local parsed_msg = mavlink_msgs.decode(msg, msg_map) if (parsed_msg ~= nil) then local result if parsed_msg.msgid == COMMAND_LONG_ID then result = handle_command_long(parsed_msg) end if (result ~= nil) then -- Send ack if the command is one were intrested in local ack = {} ack.command = parsed_msg.command ack.result = result ack.progress = 0 ack.result_param2 = 0 ack.target_system = parsed_msg.sysid ack.target_component = parsed_msg.compid mavlink:send_chan(chan, mavlink_msgs.encode("COMMAND_ACK", ack)) send_buffer_to_soac() -- send end end end end -- wrapper around update(). This calls update() every RUN_INTERVAL_MS, -- and if update faults then an error is displayed, but the script is not -- stopped function protected_wrapper() local success, err = pcall(update) if not success then gcs:send_text(MAV_SEVERITY.CRITICAL, "ACoLua: Internal Error: " .. err) -- when we fault we run the update function again after 1s, slowing it -- down a bit so we don't flood the console with errors return protected_wrapper, 1000 --return end return protected_wrapper, RUN_INTERVAL_MS end -- start running update loop return protected_wrapper()
Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.
OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.
name = io.read("*a")
print ("Hello ", name)
Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.
-- global variables
a = 10
-- local variables
local x = 30
Value Type | Description |
---|---|
number | Represents numbers |
string | Represents text |
nil | Differentiates values whether it has data or not |
boolean | Value can be either true or false |
function | Represents a sub-routine |
userdata | Represents arbitary C data |
thread | Represents independent threads of execution. |
table | Can hold any value except nil |
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition)
do
--code
end
Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.
repeat
--code
until( condition )
For loop is used to iterate a set of statements based on a condition.
for init,max/min value, increment
do
--code
end
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.
optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end