Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/SCRIPTS/RF2/background.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local initTask = nil
local adjTellerTask = nil
local customTelemetryTask = nil
local statsTask = nil
local isInitialized = false
local modelIsConnected = false
local lastTimeRssi = nil
Expand Down Expand Up @@ -37,6 +38,7 @@ local function run()
end
adjTellerTask = nil
customTelemetryTask = nil
statsTask = nil
modelIsConnected = false
isInitialized = false
collectgarbage()
Expand All @@ -46,6 +48,7 @@ local function run()
if not isInitialized then
adjTellerTask = nil
customTelemetryTask = nil
statsTask = nil
collectgarbage()
initTask = initTask or rf2.executeScript("background_init")
local initTaskResult = initTask.run(modelIsConnected)
Expand Down Expand Up @@ -76,6 +79,14 @@ local function run()
if customTelemetryTask then
customTelemetryTask.run()
end
if rf2.apiVersion >= 12.09 then
if not statsTask and customTelemetryTask and hasSensor("ARM") then
statsTask = rf2.executeScript("background_stats")
end
if statsTask then
statsTask.readStats()
end
end
end

local function runProtected()
Expand Down
48 changes: 48 additions & 0 deletions src/SCRIPTS/RF2/background_stats.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local mspFlightStats = rf2.useApi("mspFlightStats")
local flighStats = mspFlightStats.getDefaults()

local doReadStats = true
local lastArmedState = false
local lastTelemetryUpdate = 0
local lastReadAttempt = 0

local function checkArmedChanged()
local arm = getValue("ARM")
local currentArmedState = (arm == 1 or arm == 3)
if lastArmedState == true and currentArmedState == false then
doReadStats = true
end
lastArmedState = currentArmedState
end

local function readStats()
checkArmedChanged()

local now = rf2.clock()

if (doReadStats or flighStats.stats_total_flights.value == nil) and (now - lastReadAttempt > 2) then
if rf2.mspQueue:isProcessed() then
mspFlightStats.read(nil, nil, flighStats)
lastReadAttempt = now
doReadStats = false
end
end

if not rf2.mspQueue:isProcessed() then
rf2.mspQueue:processQueue()
end

if flighStats.statsEnabled.value and flighStats.statsEnabled.value == 1 then
if now - lastTelemetryUpdate >= 1 then
lastTelemetryUpdate = now
if flighStats.stats_total_flights.value then
setTelemetryValue(0x2001, 0, 0, flighStats.stats_total_flights.value, UNIT_RAW, 0, "FlyC")
setTelemetryValue(0x2002, 0, 0, flighStats.stats_total_time_s.value, UNIT_RAW, 0, "FlyT")
setTelemetryValue(0x2003, 0, 0, flighStats.stats_total_dist_m.value, UNIT_RAW, 0, "FlyD")
end
end
end

end

return { readStats = readStats }