ardupilot/libraries/AP_Scripting/tests/mavlink_test.lua

39 lines
1.3 KiB
Lua
Raw Normal View History

local mavlink_msgs = require("mavlink/mavlink_msgs")
2023-04-05 04:09:10 -03:00
local msg_map = {}
2023-04-05 04:09:10 -03:00
local heartbeat_msgid = mavlink_msgs.get_msgid("HEARTBEAT")
2023-04-05 04:09:10 -03:00
msg_map[heartbeat_msgid] = "HEARTBEAT"
-- initialise mavlink rx with number of messages, and buffer depth
mavlink.init(1, 10)
-- register message id to receive
mavlink.register_rx_msgid(heartbeat_msgid)
local test_named_value = 0.0
2023-04-05 04:09:10 -03:00
function str_to_bytes(str)
local str_len = string.len(str)
local bytes = {}
2023-04-05 04:09:10 -03:00
for i = 1, str_len do
bytes[i] = string.byte(str, i)
end
return bytes
end
function update()
local msg,chan,timestamp_ms = mavlink.receive_chan()
2023-04-05 04:09:10 -03:00
if msg then
gcs:send_text(6, string.format("Received message on channel %d at %s", chan, tostring(timestamp_ms)))
local parsed_msg = mavlink_msgs.decode(msg, msg_map)
2023-04-05 04:09:10 -03:00
if parsed_msg.msgid == heartbeat_msgid then
gcs:send_text(6, string.format("Received heartbeat from %d", parsed_msg.sysid))
end
else
gcs:send_text(6, "No heartbeat received")
end
test_named_value = test_named_value + 1.0
-- send named value float
mavlink.send_chan(0, mavlink_msgs.encode("NAMED_VALUE_FLOAT", {time_boot_ms = millis():toint(), name = str_to_bytes("test"), value = test_named_value}))
2023-04-05 04:09:10 -03:00
return update, 1000
end
return update()