Skip to content

PWM page improvements #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2021
Merged
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
1 change: 1 addition & 0 deletions src/SCRIPTS/BF/BOARD_INFO/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Board info downloaded from the flight controller will be stored in this folder.
30 changes: 19 additions & 11 deletions src/SCRIPTS/BF/PAGES/pwm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ local inc = { x = function(val) x = x + val return x end, y = function(val) y =
local labels = {}
local fields = {}

local gyroSampleRateKhz

if apiVersion >= 1.043 then
gyroSampleRateKhz = assert(loadScript("BOARD_INFO/"..mcuId..".lua"))().gyroSampleRateHz / 1000
end

local escProtocols = { [0] = "PWM", "OS125", "OS42", "MSHOT" }

if apiVersion >= 1.020 then
Expand All @@ -37,23 +43,24 @@ if apiVersion >= 1.043 then
escProtocols[#escProtocols + 1] = "DISABLED"
end

labels[#labels + 1] = { t = "System Config", x = x, y = inc.y(lineSpacing) }
if apiVersion >= 1.031 and apiVersion <= 1.040 then
fields[#fields + 1] = { t = "32kHz Sampling", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 1, vals = { 9 }, table = { [0] = "OFF", "ON" }, upd = function(self) self.updateRateTables(self) end }
fields[#fields + 1] = { t = "32kHz Sampling", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 1, vals = { 9 }, table = { [0] = "OFF", "ON" }, upd = function(self) self.updateRateTables(self) end }
end

if apiVersion >= 1.016 then
if apiVersion <= 1.042 then
fields[#fields + 1] = { t = "Gyro Update", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 1, max = 32, vals = { 1 }, table = {}, upd = function(self) self.updatePidRateTable(self) end, mult = -1 }
fields[#fields + 1] = { t = "PID Loop", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 1, max = 16, vals = { 2 }, table = {}, mult = -1 }
end
fields[#fields + 1] = { t = "Protocol", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = #escProtocols, vals = { 4 }, table = escProtocols }
fields[#fields + 1] = { t = "Unsynced PWM", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 1, vals = { 3 }, table = { [0] = "OFF", "ON" } }
fields[#fields + 1] = { t = "PWM Frequency", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 200, max = 32000, vals = { 5, 6 }, }
if apiVersion >= 1.043 then
fields[#fields + 1] = { t = "Gyro Update", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 1, max = 32, vals = { 1 }, table = {}, upd = function(self) self.updatePidRateTable(self) end, mult = -1, ro = true }
else
fields[#fields + 1] = { t = "Gyro Update", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 1, max = 32, vals = { 1 }, table = {}, upd = function(self) self.updatePidRateTable(self) end, mult = -1 }
end
fields[#fields + 1] = { t = "PID Loop", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 1, max = 16, vals = { 2 }, table = {}, mult = -1 }

labels[#labels + 1] = { t = "ESC/Motor", x = x, y = inc.y(lineSpacing) }
fields[#fields + 1] = { t = "Protocol", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = #escProtocols, vals = { 4 }, table = escProtocols }
if apiVersion >= 1.031 then
fields[#fields + 1] = { t = "Idle Throttle %", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 2000, vals = { 7, 8 }, scale = 100 }
fields[#fields + 1] = { t = "Idle Throttle %", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 2000, vals = { 7, 8 }, scale = 100 }
end
fields[#fields + 1] = { t = "Unsynced PWM", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 1, vals = { 3 }, table = { [0] = "OFF", "ON" } }
fields[#fields + 1] = { t = "Frequency", x = x + indent*2, y = inc.y(lineSpacing), sp = x + sp, min = 200, max = 32000, vals = { 5, 6 }, }

return {
read = 90, -- MSP_ADVANCED_CONFIG
Expand Down Expand Up @@ -81,6 +88,7 @@ return {
end,
calculateGyroRates = function(self, baseRate)
local idx = self.getGyroDenomFieldIndex(self)
baseRate = gyroSampleRateKhz or baseRate
for i=1, 32 do
self.gyroRates[i] = baseRate/i
self.fields[idx].table[i] = string.format("%.2f",baseRate/i)
Expand Down
137 changes: 137 additions & 0 deletions src/SCRIPTS/BF/board_info.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
local MSP_BOARD_INFO = 4

local boardInfoReceived = false

local boardIdentifier = ""
local hardwareRevision = 0
local boardType = 0
local targetCapabilities = 0
local targetName = ""
local boardName = ""
local manufacturerId = ""
local signature = {}
local mcuTypeId = 255
local configurationState = 0
local gyroSampleRateHz = 0
local configurationProblems = 0
local spiRegisteredDeviceCount = 0
local i2cRegisteredDeviceCount = 0

local lastRunTS = 0
local INTERVAL = 100

local function processMspReply(cmd, payload)
if cmd == MSP_BOARD_INFO then
local length
local i = 1
length = 4
for c = 1, 4 do
boardIdentifier = boardIdentifier..string.char(payload[i])
i = i + 1
end
for idx = 1, 2 do
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
hardwareRevision = bit32.bor(hardwareRevision, raw_val)
i = i + 1
end
if apiVersion >= 1.035 then
boardType = payload[i]
end
i = i + 1
if apiVersion >= 1.037 then
targetCapabilities = payload[i]
i = i + 1
length = payload[i]
i = i + 1
for c = 1, length do
targetName = targetName..string.char(payload[i])
i = i + 1
end
end
if apiVersion >= 1.039 then
length = payload[i]
i = i + 1
for c = 1, length do
boardName = boardName..string.char(payload[i])
i = i + 1
end
length = payload[i]
i = i + 1
for c = 1, length do
manufacturerId = manufacturerId..string.char(payload[i])
i = i + 1
end
length = 32
for c = 1, 32 do
signature[#signature + 1] = payload[i]
i = i + 1
end
end
i = i + 1
if apiVersion >= 1.041 then
mcuTypeId = payload[i]
end
i = i + 1
if apiVersion >= 1.042 then
configurationState = payload[i]
end
if apiVersion >= 1.043 then
for idx = 1, 2 do
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
gyroSampleRateHz = bit32.bor(gyroSampleRateHz, raw_val)
i = i + 1
end
for idx = 1, 4 do
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
configurationProblems = bit32.bor(configurationProblems, raw_val)
i = i + 1
end
end
if apiVersion >= 1.044 then
spiRegisteredDeviceCount = payload[i]
i = i + 1
i2cRegisteredDeviceCount = payload[i]
end
boardInfoReceived = true
end
end

local function getBoardInfo()
if lastRunTS + INTERVAL < getTime() then
lastRunTS = getTime()
if not boardInfoReceived then
protocol.mspRead(MSP_BOARD_INFO)
end
end
mspProcessTxQ()
processMspReply(mspPollReply())
if boardInfoReceived then
local f = io.open("BOARD_INFO/"..mcuId..".lua", 'w')
io.write(f, "return {", "\n")
io.write(f, " boardIdentifier = "..boardIdentifier..",", "\n")
io.write(f, " hardwareRevision = "..tostring(hardwareRevision)..",", "\n")
io.write(f, " boardType = "..tostring(boardType)..",", "\n")
io.write(f, " targetCapabilities = "..tostring(targetCapabilities)..",", "\n")
io.write(f, " targetName = "..targetName..",", "\n")
io.write(f, " boardName = "..boardName..",", "\n")
io.write(f, " manufacturerId = "..manufacturerId..",", "\n")
local signatureString = " signature = { "
for i = 1, #signature do
signatureString = signatureString..tostring(signature[i])..", "
end
signatureString = signatureString.."},"
io.write(f, signatureString, "\n")
io.write(f, " mcuTypeId = "..tostring(mcuTypeId)..",", "\n")
io.write(f, " configurationState = "..tostring(configurationState)..",", "\n")
io.write(f, " gyroSampleRateHz = "..tostring(gyroSampleRateHz)..",", "\n")
io.write(f, " configurationProblems = "..tostring(configurationProblems)..",", "\n")
io.write(f, " spiRegisteredDeviceCount = "..tostring(spiRegisteredDeviceCount)..",", "\n")
io.write(f, " i2cRegisteredDeviceCount = "..tostring(i2cRegisteredDeviceCount)..",", "\n")
io.write(f, "}", "\n")
io.close(f)
assert(loadScript("BOARD_INFO/"..mcuId..".lua", 'c'))
end
return boardInfoReceived
end

return { f = getBoardInfo, t = "Downloading board info" }
2 changes: 1 addition & 1 deletion src/SCRIPTS/BF/pages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if apiVersion >= 1.042 then
end

if apiVersion >= 1.016 then
PageFiles[#PageFiles + 1] = { title = "Gyro / Motor", script = "pwm.lua" }
PageFiles[#PageFiles + 1] = { title = "System / Motor", script = "pwm.lua" }
end

if apiVersion >= 1.016 then
Expand Down
25 changes: 20 additions & 5 deletions src/SCRIPTS/BF/ui_init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
local apiVersionReceived = false
local vtxTablesReceived = false
local mcuIdReceived = false
local getApiVersion, getVtxTables, getMCUId
local boardInfoReceived = false
local getApiVersion, getVtxTables, getMCUId, getBoardInfo
local returnTable = { f = nil, t = "" }

local function modelActive()
Expand All @@ -25,10 +26,16 @@ local function init()
mcuIdReceived = getMCUId.f()
if mcuIdReceived then
getMCUId = nil
local vtxTables = loadScript("/BF/VTX/"..mcuId..".lua")
if vtxTables and vtxTables() then
local f = loadScript("/BF/VTX/"..mcuId..".lua")
if f and f() then
vtxTablesReceived = true
vtxTables = nil
f = nil
end
collectgarbage()
f = loadScript("BOARD_INFO/"..mcuId..".lua")
if f and f() then
boardInfoReceived = true
f = nil
end
collectgarbage()
end
Expand All @@ -40,10 +47,18 @@ local function init()
getVtxTables = nil
collectgarbage()
end
elseif not boardInfoReceived then
getBoardInfo = getBoardInfo or assert(loadScript("board_info.lua"))()
returnTable.t = getBoardInfo.t
boardInfoReceived = getBoardInfo.f()
if boardInfoReceived then
getBoardInfo = nil
collectgarbage()
end
else
return true
end
return apiVersionReceived and vtxTablesReceived and mcuId
return apiVersionReceived and vtxTablesReceived and mcuId and boardInfoReceived
end

returnTable.f = init
Expand Down