From ceca374f236469b364c4e9cb3252294301de8a29 Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Sun, 18 Jun 2023 14:00:58 +0100 Subject: [PATCH] AP_Scripting: add MAVLink command long and ack examples --- .../examples/MAVLink_Commands.lua | 65 +++++++++++++ libraries/AP_Scripting/lua_bindings.cpp | 2 +- .../MAVLink/mavlink_msg_COMMAND_ACK.lua | 11 +++ .../MAVLink/mavlink_msg_COMMAND_LONG.lua | 16 ++++ .../modules/MAVLink/mavlink_msgs.lua | 95 +++++++++++++++++++ 5 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 libraries/AP_Scripting/examples/MAVLink_Commands.lua create mode 100644 libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_ACK.lua create mode 100644 libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_LONG.lua create mode 100644 libraries/AP_Scripting/modules/MAVLink/mavlink_msgs.lua diff --git a/libraries/AP_Scripting/examples/MAVLink_Commands.lua b/libraries/AP_Scripting/examples/MAVLink_Commands.lua new file mode 100644 index 0000000000..dab5068fa7 --- /dev/null +++ b/libraries/AP_Scripting/examples/MAVLink_Commands.lua @@ -0,0 +1,65 @@ +-- Example of receiving MAVLink commands + +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_SET_MODE = 176 +local MAV_CMD_WAYPOINT_USER_1 = 31000 + +-- Block AP parsing user1 so we can deal with it in the script +-- Prevents "unsupported" ack +mavlink:block_command(MAV_CMD_WAYPOINT_USER_1) + +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_WAYPOINT_USER_1) then + -- return ack from command param value + return math.min(math.max(math.floor(cmd.param1), 0), 5) + end + return nil +end + +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)) + end + end + end + + return update, 1000 +end + +return update() diff --git a/libraries/AP_Scripting/lua_bindings.cpp b/libraries/AP_Scripting/lua_bindings.cpp index f52b4ab085..c73b0cba3e 100644 --- a/libraries/AP_Scripting/lua_bindings.cpp +++ b/libraries/AP_Scripting/lua_bindings.cpp @@ -183,7 +183,7 @@ int lua_mavlink_send_chan(lua_State *L) { int lua_mavlink_block_command(lua_State *L) { // Allow : and . access - const int arg_offset = (luaL_testudata(L, 1, "mavlnk") != NULL) ? 1 : 0; + const int arg_offset = (luaL_testudata(L, 1, "mavlink") != NULL) ? 1 : 0; binding_argcheck(L, 1+arg_offset); diff --git a/libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_ACK.lua b/libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_ACK.lua new file mode 100644 index 0000000000..0cc6834c96 --- /dev/null +++ b/libraries/AP_Scripting/modules/MAVLink/mavlink_msg_COMMAND_ACK.lua @@ -0,0 +1,11 @@ +local COMMAND_ACK = {} +COMMAND_ACK.id = 77 +COMMAND_ACK.fields = { + { "command", "