Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Lua_scripts / pidDsm.lua
blob8d2b89bbd9509cdd1c15fbafb9e0b37e8f81c273
1 --
2 -- This telemetry script displays the Flight Log Gain
3 -- Parameters streamed from the Blade 150S Spektrum AR6335A
4 -- Flybarless Controller.
6 -- The script facilitates the setting of the FBL's
7 -- Gain Parameters including PID for both
8 -- cyclic and tail. It is similar to the Telemetry Based
9 -- Text Generator available on Spektrum transmitters.
11 -- Supporting similar Blade micros such as the Fusion 180
12 -- would possibly require minor modifications to this script.
14 -- This script reads telemetry data from the Spektrum
15 -- receiver and thus functionality relies on data being
16 -- captured by the OpenTX transmitter. A DSM
17 -- telemetry-ready module is required. Please see the
18 -- MULTI-Module project at https://www.multi-module.org/.
20 -- The only supported display is the Taranis'. It may work
21 -- with higher res screens.
25 -- Sensor names
26 local PSensor = "FdeA"
27 local ISensor = "FdeB"
28 local DSensor = "FdeL"
29 local RSensor = "FdeR"
30 local ActiveParamSensor = "Hold"
32 local tags = {"P", "I", "D"}
35 local function getPage(iParam)
36 -- get page from 0-based index
37 -- {0,1,2,3}: cyclic (1), {4,5,6,7}: tail (2)
38 local res = (math.floor(iParam/4)==0) and 1 or 2
39 return res
40 end
42 function round(v)
43 -- round float
44 local factor = 100
45 return math.floor(v * factor + 0.5) / factor
46 end
49 local function readValue(sensor)
50 -- read from sensor, round and return
51 local v = getValue(sensor)
52 v = round(v)
53 return v
54 end
56 local function readActiveParamValue(sensor)
57 -- read and return a validated active parameter value
58 local v = getValue(sensor)
59 if (v<1 or v>8) then
60 return nil
61 end
62 return v
63 end
65 local function readParameters()
66 -- read and return parameters
67 local p = readValue(PSensor)
68 local i = readValue(ISensor)
69 local d = readValue(DSensor)
70 local r = readValue(RSensor)
71 local a = readActiveParamValue(ActiveParamSensor)
72 return {p,i,d,r,a}
73 end
75 local function drawParameters()
76 -- draw labels and params on screen
77 local params = readParameters()
78 local activeParam = params[5]
80 -- if active gain does not validate then assume
81 -- Gain Adjustment Mode is disabled
82 if not activeParam then
83 lcd.clear()
84 lcd.drawText(20,30,"Please enter Gain Adjustment Mode")
85 return
86 end
88 local activePage = getPage(activeParam-1)
89 for iParam=0,7 do
90 -- highlight selected parameter
91 local attr = (activeParam==iParam+1) and 2 or 0
92 -- circular index (per page)
93 local perPageIndx = iParam % 4 + 1
94 -- check if displaying cyclic params.
95 local isCyclicPage = (getPage(iParam)==1)
96 -- set y draw coord
97 local y = perPageIndx*10+2
99 -- labels
100 local x = isCyclicPage and 6 or 120
101 -- labels are P,I,D for both pages except for last param
102 local val = iParam==3 and "Response" or
103 (iParam==7 and "Filtering" or tags[perPageIndx])
104 lcd.drawText (x, y, val, attr)
106 -- gains
107 -- set all params for non-active page to '--' rather than 'last value'
108 val = (getPage(iParam)==activePage) and params[perPageIndx] or '--'
109 x = isCyclicPage and 70 or 180
110 lcd.drawText (x, y, val, attr)
115 local function run_func(event)
116 -- TODO: calling clear() on every function call redrawing all labels is not ideal
117 lcd.clear()
118 lcd.drawText (8, 2, "Cyclic (0...200)")
119 lcd.drawText (114, 2, "Tail (0...200)")
120 drawParameters()
123 local function init_func() end
124 local function bg_func() end
127 return { run=run_func, background=bg_func, init=init_func }