--[[
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
MAVLinkAttitude
A MAVlink message receiver for ATTITUDE messages specifically
--]]
local MAVLinkAttitude = {}
MAVLinkAttitude.SCRIPT_VERSION = "4.7.0-009"
MAVLinkAttitude.SCRIPT_NAME = "MAVLink Attitude"
MAVLinkAttitude.SCRIPT_NAME_SHORT = "MAVATT"
ATTITUDE_MESSAGE = "ATTITUDE"
MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7}
--[[
a lua implementation of the jitter correction algorithm from libraries/AP_RTC
note that the use of a 32 bit float lua number for a uint32_t
milliseconds means we lose accuracy over time. At 9 hours we have
an accuracy of about 1 millisecond
--]]
function MAVLinkAttitude.JitterCorrection(_max_lag_ms, _convergence_loops)
local self = {}
local max_lag_ms = _max_lag_ms
local convergence_loops = _convergence_loops
local link_offset_ms = 0
local min_sample_ms = 0
local initialised = false
local min_sample_counter = 0
function self.correct_offboard_timestamp_msec(offboard_ms, local_ms)
local diff_ms = local_ms - offboard_ms
if not initialised or diff_ms < link_offset_ms then
--[[
this message arrived from the remote system with a
timestamp that would imply the message was from the
future. We know that isn't possible, so we adjust down the
correction value
--]]
link_offset_ms = diff_ms
initialised = true
end
local estimate_ms = offboard_ms + link_offset_ms
if estimate_ms + max_lag_ms < local_ms then
--[[
this implies the message came from too far in the past. clamp the lag estimate
to assume the message had maximum lag
--]]
estimate_ms = local_ms - max_lag_ms
link_offset_ms = estimate_ms - offboard_ms
end
if min_sample_counter == 0 then
min_sample_ms = diff_ms
end
min_sample_counter = (min_sample_counter+1)
if diff_ms < min_sample_ms then
min_sample_ms = diff_ms
end
if min_sample_counter == convergence_loops then
--[[
we have the requested number of samples of the transport
lag for convergence. To account for long term clock drift
we set the diff we will use in future to this value
--]]
link_offset_ms = min_sample_ms
min_sample_counter = 0
end
return estimate_ms
end
return self
end
function MAVLinkAttitude.mavlink_attitude_receiver()
local self = {}
local ATTITUDE_map = {}
local last_timestamp = millis():tofloat()
ATTITUDE_map.id = 30
ATTITUDE_map.fields = {
{ "time_boot_ms", "