Skip to content

Commit 4a581f3

Browse files
authored
Merge pull request #400 from klutvott123/pwm-page-improvements
2 parents 2102034 + 51d9267 commit 4a581f3

File tree

5 files changed

+178
-17
lines changed

5 files changed

+178
-17
lines changed

src/SCRIPTS/BF/BOARD_INFO/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Board info downloaded from the flight controller will be stored in this folder.

src/SCRIPTS/BF/PAGES/pwm.lua

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ local inc = { x = function(val) x = x + val return x end, y = function(val) y =
1616
local labels = {}
1717
local fields = {}
1818

19+
local gyroSampleRateKhz
20+
21+
if apiVersion >= 1.043 then
22+
gyroSampleRateKhz = assert(loadScript("BOARD_INFO/"..mcuId..".lua"))().gyroSampleRateHz / 1000
23+
end
24+
1925
local escProtocols = { [0] = "PWM", "OS125", "OS42", "MSHOT" }
2026

2127
if apiVersion >= 1.020 then
@@ -37,23 +43,24 @@ if apiVersion >= 1.043 then
3743
escProtocols[#escProtocols + 1] = "DISABLED"
3844
end
3945

46+
labels[#labels + 1] = { t = "System Config", x = x, y = inc.y(lineSpacing) }
4047
if apiVersion >= 1.031 and apiVersion <= 1.040 then
41-
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 }
48+
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 }
4249
end
43-
44-
if apiVersion >= 1.016 then
45-
if apiVersion <= 1.042 then
46-
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 }
47-
fields[#fields + 1] = { t = "PID Loop", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 1, max = 16, vals = { 2 }, table = {}, mult = -1 }
48-
end
49-
fields[#fields + 1] = { t = "Protocol", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = #escProtocols, vals = { 4 }, table = escProtocols }
50-
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" } }
51-
fields[#fields + 1] = { t = "PWM Frequency", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 200, max = 32000, vals = { 5, 6 }, }
50+
if apiVersion >= 1.043 then
51+
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 }
52+
else
53+
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 }
5254
end
55+
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 }
5356

57+
labels[#labels + 1] = { t = "ESC/Motor", x = x, y = inc.y(lineSpacing) }
58+
fields[#fields + 1] = { t = "Protocol", x = x + indent, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = #escProtocols, vals = { 4 }, table = escProtocols }
5459
if apiVersion >= 1.031 then
55-
fields[#fields + 1] = { t = "Idle Throttle %", x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 2000, vals = { 7, 8 }, scale = 100 }
60+
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 }
5661
end
62+
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" } }
63+
fields[#fields + 1] = { t = "Frequency", x = x + indent*2, y = inc.y(lineSpacing), sp = x + sp, min = 200, max = 32000, vals = { 5, 6 }, }
5764

5865
return {
5966
read = 90, -- MSP_ADVANCED_CONFIG
@@ -81,6 +88,7 @@ return {
8188
end,
8289
calculateGyroRates = function(self, baseRate)
8390
local idx = self.getGyroDenomFieldIndex(self)
91+
baseRate = gyroSampleRateKhz or baseRate
8492
for i=1, 32 do
8593
self.gyroRates[i] = baseRate/i
8694
self.fields[idx].table[i] = string.format("%.2f",baseRate/i)

src/SCRIPTS/BF/board_info.lua

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
local MSP_BOARD_INFO = 4
2+
3+
local boardInfoReceived = false
4+
5+
local boardIdentifier = ""
6+
local hardwareRevision = 0
7+
local boardType = 0
8+
local targetCapabilities = 0
9+
local targetName = ""
10+
local boardName = ""
11+
local manufacturerId = ""
12+
local signature = {}
13+
local mcuTypeId = 255
14+
local configurationState = 0
15+
local gyroSampleRateHz = 0
16+
local configurationProblems = 0
17+
local spiRegisteredDeviceCount = 0
18+
local i2cRegisteredDeviceCount = 0
19+
20+
local lastRunTS = 0
21+
local INTERVAL = 100
22+
23+
local function processMspReply(cmd, payload)
24+
if cmd == MSP_BOARD_INFO then
25+
local length
26+
local i = 1
27+
length = 4
28+
for c = 1, 4 do
29+
boardIdentifier = boardIdentifier..string.char(payload[i])
30+
i = i + 1
31+
end
32+
for idx = 1, 2 do
33+
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
34+
hardwareRevision = bit32.bor(hardwareRevision, raw_val)
35+
i = i + 1
36+
end
37+
if apiVersion >= 1.035 then
38+
boardType = payload[i]
39+
end
40+
i = i + 1
41+
if apiVersion >= 1.037 then
42+
targetCapabilities = payload[i]
43+
i = i + 1
44+
length = payload[i]
45+
i = i + 1
46+
for c = 1, length do
47+
targetName = targetName..string.char(payload[i])
48+
i = i + 1
49+
end
50+
end
51+
if apiVersion >= 1.039 then
52+
length = payload[i]
53+
i = i + 1
54+
for c = 1, length do
55+
boardName = boardName..string.char(payload[i])
56+
i = i + 1
57+
end
58+
length = payload[i]
59+
i = i + 1
60+
for c = 1, length do
61+
manufacturerId = manufacturerId..string.char(payload[i])
62+
i = i + 1
63+
end
64+
length = 32
65+
for c = 1, 32 do
66+
signature[#signature + 1] = payload[i]
67+
i = i + 1
68+
end
69+
end
70+
i = i + 1
71+
if apiVersion >= 1.041 then
72+
mcuTypeId = payload[i]
73+
end
74+
i = i + 1
75+
if apiVersion >= 1.042 then
76+
configurationState = payload[i]
77+
end
78+
if apiVersion >= 1.043 then
79+
for idx = 1, 2 do
80+
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
81+
gyroSampleRateHz = bit32.bor(gyroSampleRateHz, raw_val)
82+
i = i + 1
83+
end
84+
for idx = 1, 4 do
85+
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
86+
configurationProblems = bit32.bor(configurationProblems, raw_val)
87+
i = i + 1
88+
end
89+
end
90+
if apiVersion >= 1.044 then
91+
spiRegisteredDeviceCount = payload[i]
92+
i = i + 1
93+
i2cRegisteredDeviceCount = payload[i]
94+
end
95+
boardInfoReceived = true
96+
end
97+
end
98+
99+
local function getBoardInfo()
100+
if lastRunTS + INTERVAL < getTime() then
101+
lastRunTS = getTime()
102+
if not boardInfoReceived then
103+
protocol.mspRead(MSP_BOARD_INFO)
104+
end
105+
end
106+
mspProcessTxQ()
107+
processMspReply(mspPollReply())
108+
if boardInfoReceived then
109+
local f = io.open("BOARD_INFO/"..mcuId..".lua", 'w')
110+
io.write(f, "return {", "\n")
111+
io.write(f, " boardIdentifier = "..boardIdentifier..",", "\n")
112+
io.write(f, " hardwareRevision = "..tostring(hardwareRevision)..",", "\n")
113+
io.write(f, " boardType = "..tostring(boardType)..",", "\n")
114+
io.write(f, " targetCapabilities = "..tostring(targetCapabilities)..",", "\n")
115+
io.write(f, " targetName = "..targetName..",", "\n")
116+
io.write(f, " boardName = "..boardName..",", "\n")
117+
io.write(f, " manufacturerId = "..manufacturerId..",", "\n")
118+
local signatureString = " signature = { "
119+
for i = 1, #signature do
120+
signatureString = signatureString..tostring(signature[i])..", "
121+
end
122+
signatureString = signatureString.."},"
123+
io.write(f, signatureString, "\n")
124+
io.write(f, " mcuTypeId = "..tostring(mcuTypeId)..",", "\n")
125+
io.write(f, " configurationState = "..tostring(configurationState)..",", "\n")
126+
io.write(f, " gyroSampleRateHz = "..tostring(gyroSampleRateHz)..",", "\n")
127+
io.write(f, " configurationProblems = "..tostring(configurationProblems)..",", "\n")
128+
io.write(f, " spiRegisteredDeviceCount = "..tostring(spiRegisteredDeviceCount)..",", "\n")
129+
io.write(f, " i2cRegisteredDeviceCount = "..tostring(i2cRegisteredDeviceCount)..",", "\n")
130+
io.write(f, "}", "\n")
131+
io.close(f)
132+
assert(loadScript("BOARD_INFO/"..mcuId..".lua", 'c'))
133+
end
134+
return boardInfoReceived
135+
end
136+
137+
return { f = getBoardInfo, t = "Downloading board info" }

src/SCRIPTS/BF/pages.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if apiVersion >= 1.042 then
3333
end
3434

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

3939
if apiVersion >= 1.016 then

src/SCRIPTS/BF/ui_init.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
local apiVersionReceived = false
22
local vtxTablesReceived = false
33
local mcuIdReceived = false
4-
local getApiVersion, getVtxTables, getMCUId
4+
local boardInfoReceived = false
5+
local getApiVersion, getVtxTables, getMCUId, getBoardInfo
56
local returnTable = { f = nil, t = "" }
67

78
local function modelActive()
@@ -25,10 +26,16 @@ local function init()
2526
mcuIdReceived = getMCUId.f()
2627
if mcuIdReceived then
2728
getMCUId = nil
28-
local vtxTables = loadScript("/BF/VTX/"..mcuId..".lua")
29-
if vtxTables and vtxTables() then
29+
local f = loadScript("/BF/VTX/"..mcuId..".lua")
30+
if f and f() then
3031
vtxTablesReceived = true
31-
vtxTables = nil
32+
f = nil
33+
end
34+
collectgarbage()
35+
f = loadScript("BOARD_INFO/"..mcuId..".lua")
36+
if f and f() then
37+
boardInfoReceived = true
38+
f = nil
3239
end
3340
collectgarbage()
3441
end
@@ -40,10 +47,18 @@ local function init()
4047
getVtxTables = nil
4148
collectgarbage()
4249
end
50+
elseif not boardInfoReceived then
51+
getBoardInfo = getBoardInfo or assert(loadScript("board_info.lua"))()
52+
returnTable.t = getBoardInfo.t
53+
boardInfoReceived = getBoardInfo.f()
54+
if boardInfoReceived then
55+
getBoardInfo = nil
56+
collectgarbage()
57+
end
4358
else
4459
return true
4560
end
46-
return apiVersionReceived and vtxTablesReceived and mcuId
61+
return apiVersionReceived and vtxTablesReceived and mcuId and boardInfoReceived
4762
end
4863

4964
returnTable.f = init

0 commit comments

Comments
 (0)