mirror of
https://github.com/pascallanger/DIY-Multiprotocol-TX-Module.git
synced 2026-12-06 15:47:35 +00:00
* Futaba: T-FHSS Surface, SR and Air sub protocols New sub protocols of Futaba (21) next to SFHSS, on the CC2500: - TFHSS: T-FHSS Surface (R334SBS...). 384 kbps MSK, 3 ms frames, CH1-4 every frame and CH5-8 in turn, bind packets on channel 1, failsafe, telemetry. - TFH-SR: T-FHSS SR, 1.2 ms frames, no telemetry. - TF-Air: T-FHSS Air (R3008SB...). 128 kbps 2-FSK, 15 ms frames, 16 channels, link with the receiver's ID, telemetry. No failsafe frames (the receiver's own). From the R334SBS receiver and T18SZ transmitter firmware, checked against captures of a 4PM Plus (Surface, SR) and a T6K (Air) shared on issue #507. Telemetry (FUTABA_FW_TELEMETRY): the link frame (A1 = receiver voltage, RSSI = its link quality) and a new MULTI_TELEMETRY_FUTABA (0x13) frame with the S.BUS2 sensor slots as the receiver sends them, for the radio to decode (Multiprotocol.h). Tested against a CC2652/CC2755 receiver of the packets, not on real Futaba receivers yet. Size: +3.0 KB on the STM32 builds, Futaba alone on the ATmega328p 14.9 KB. * Futaba: T-FHSS on STM32 only T-FHSS adds 3.9 KB on the ATmega328p, and the CC2500 release build (all its CC2500 protocols) no longer fits. FUTABA_TFHSS (Validate.h) enables the T-FHSS sub protocols on STM32 boards only; on the ATmega Futaba is S-FHSS alone, as before (same build size as master). * Futaba: no T-FHSS on the 64KB STM32 The CC2500 64KB release build was 1 KB over with T-FHSS (+3.1 KB). FUTABA_TFHSS now needs a 128KB STM32 (not MCU_STM32F103C8, as the surface protocols in Validate.h). All release recipes build: 64KB as master (53200 bytes), 4-in-1 117940, T18 118856. * Futaba: T-FHSS bind hops the list and waits for the reply The R334SBS scans the 16-channel list in its link mode, answers a bind packet on that channel (type 24 / 25, keyed as its telemetry) and only stores the bind after packets with byte 1 bit 7. The 4PM Plus sets bit 7 once it has heard the reply and binds 9.1 s without one. So the bind now hops the list at 3 ms per packet (channel 1 every 48 ms, as captured), listens on the same channel, then sends 240 packets with bit 7; 9.6 s at most. * Futaba: T-FHSS binds at the normal power The bind (Surface, SR) and the Air link now use the radio's power setting (low / high, range), not CC2500_BIND_POWER. The R334SBS takes a bind packet only above about -79 dBm (its RSSI check); at bind power the packets were 41 dB below the servo frames. The 4PM Plus binds 33 dB down from a higher level, the T6K links at full power. Checked on the bench with a 4-in-1 answering as the receiver (Surface and Air). * Futaba: T-FHSS Air picks slot channels as the T18SZ Slots 0..5 and 8 carry their own channel or the alternate (ch11..16, ch10), chosen as the T18SZ does: a channel moved = more than 3 us from the value last sent, age = frames since sent. Own moved: the alternate if it moved too and is 2 frames old, or is 64 old. Own still: the alternate if it moved (unless own is 64 old), else the older one, own on a tie. Was: the halves in turn, so moving sticks went out every other frame. * Futaba: T-FHSS bind reply on the next list channel The R334SBS hops before it replies to a bind packet, as it does for its telemetry: the module now listens on the next list channel after each bind packet, not on the same one. A 4PM Plus bind followed over the list: no reply on the bind packet's channel, and byte 7 of the bind packet is the channel it is sent on (the receiver echoes it), not 01. Bench (4-in-1 answering as the receiver): normal and SR link. * Futaba: T-FHSS Surface keys with the receiver's ID The last 4 key bytes of T-FHSS Surface are the receiver's own ID (its serial number), not a number of the TX: the receiver keeps only the TX ID from the bind. The module sent its own, so the R334SBS bound but decoded no servo packet. The ID now comes from the receiver's bind reply and is stored per RX number (TFHSS_EEPROM_OFFSET 1527, 4 bytes per model, end 1783). Bench (4-in-1 answering as the receiver with its own ID): bind, servo packets OK, again after a restart without bind. * Futaba: T-FHSS Surface reads the reply before the next packet The reply comes 1.9 ms after the TX packet's start, the read was 2.5 ms after the callback: on the MPM v2 replies were taken up to 1.5 ms only. Now the next frame's packet is built, the reply read, then the packet sent. * Futaba: S.BUS2 sensors Lua tool S.BUS2 sensors don't tell their type on the bus: the radio shows each slot with data (telemetry type 19) as a raw sensor. The tool turns a slot into a typed sensor the radio decodes (SBS-01T/TE, RPM, voltage, current), with an ESCape32 preset. Uses model.setSensor() and model.deleteSensor() when the radio has them (EdgeTX/edgetx#8105), else setTelemetryValue() with "Discover new sensors" on. --------- Co-authored-by: Raphael Coeffic <1050031+raphaelcoeffic@users.noreply.github.com>
349 lines
11 KiB
Lua
349 lines
11 KiB
Lua
local toolName = "TNS|Futaba S.BUS2 sensors|TNE"
|
|
|
|
-- Sets up the S.BUS2 sensors of a Futaba receiver on a MULTI-Module (T-FHSS): EdgeTX's Futaba telemetry (radio/src/telemetry/futaba.h)
|
|
-- shows a slot with data that no sensor claims as a raw sensor "S<slot>"
|
|
-- (ID 0x1310, instance = the slot). S.BUS2 sensors don't tell their type
|
|
-- on the bus, so this tool creates the typed sensors: ID = the type,
|
|
-- instance = its first slot, subId = the value (types with several). Once
|
|
-- a typed sensor claims a slot, EdgeTX decodes it. With model.setSensor()
|
|
-- and model.deleteSensor() (EdgeTX/edgetx#8105): sensors are created directly, the
|
|
-- raw ones of the slots taken deleted, a slot's type can be changed.
|
|
-- Before: setTelemetryValue(), which needs "Discover new sensors" on (the
|
|
-- model's telemetry page), and old sensors are deleted on that page.
|
|
|
|
local RAW = 0x1310
|
|
local canSet = type(model.setSensor) == "function" and type(model.deleteSensor) == "function"
|
|
local colorLcd = type(lcd.RGB) == "function"
|
|
local SLOTS = 32
|
|
|
|
-- Types (futaba.h): ID, name, slots, values { name, unit, prec }; the name
|
|
-- gets the type's count (Tmp1, Tmp2...)
|
|
local TYPES = {
|
|
{ id = 0x1311, name = "Temp (SBS-01T)", slots = 1, values = { { "Tmp", UNIT_CELSIUS, 0 } } },
|
|
{ id = 0x1312, name = "Temp 125 (SBS-01TE)", slots = 1, values = { { "Tmp", UNIT_CELSIUS, 0 } } },
|
|
{ id = 0x1313, name = "RPM (SBS-01RM)", slots = 1, values = { { "RPM", UNIT_RPMS, 0 } } },
|
|
{ id = 0x1314, name = "Voltage (SBS-01V)", slots = 2,
|
|
values = { { "Vlt", UNIT_VOLTS, 1 }, { "Vlt", UNIT_VOLTS, 1 } } },
|
|
{ id = 0x1315, name = "Current (SBS-01C)", slots = 3,
|
|
values = { { "Cur", UNIT_AMPS, 2 }, { "Vcu", UNIT_VOLTS, 2 }, { "Cap", UNIT_MAH, 0 } } },
|
|
}
|
|
|
|
local function typeById(id)
|
|
for i = 1, #TYPES do
|
|
if TYPES[i].id == id then return i end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- Presets: { slot offset in the group, type index, names }; ESCape32: its
|
|
-- telemetry ID 1..4 = the group of 8 slots, slots 2..7 of it
|
|
local PRESETS = {
|
|
{ name = "ESCape32", groups = 4, sensors = {
|
|
{ 2, 1, { "TEsc" } }, { 3, 1, { "TMot" } }, { 4, 5, { "Curr", "VEsc", "Capa" } }, { 7, 3, { "RPM" } } } },
|
|
}
|
|
|
|
local claims = {} -- slot -> { type = i, first = slot, sensors = { index... } } (all slots of a typed sensor)
|
|
local raws = {} -- slot -> { name = raw sensor name, index = n }
|
|
local pending = {} -- slot -> type index chosen here, not created yet
|
|
local lines = {}
|
|
local sel, top, edit, blink = 1, 1, false, 0
|
|
local preset, presetGroup = 1, 1
|
|
local status = ""
|
|
|
|
local function limit(v, mn, mx)
|
|
if v < mn then return mn end
|
|
if v > mx then return mx end
|
|
return v
|
|
end
|
|
|
|
local function screenLines()
|
|
if colorLcd then return limit(math.floor(((LCD_H or 272) - 46) / 20), 8, 12) end
|
|
return 6
|
|
end
|
|
|
|
local function drawTitle(title)
|
|
if colorLcd and lcd.drawFilledRectangle then
|
|
lcd.drawFilledRectangle(0, 0, LCD_W, 30, COLOR_THEME_SECONDARY1)
|
|
lcd.drawText(3, 5, title, COLOR_THEME_PRIMARY2)
|
|
elseif lcd.drawScreenTitle then
|
|
lcd.drawScreenTitle(title, 0, 0)
|
|
else
|
|
lcd.drawText(0, 0, title, INVERS)
|
|
end
|
|
end
|
|
|
|
-- The model's sensors: the typed ones' slots, the raw ones
|
|
local function scan()
|
|
claims, raws = {}, {}
|
|
for i = 0, 59 do
|
|
local s = model.getSensor(i)
|
|
if s and s.type == 0 and s.id then
|
|
local t = typeById(s.id)
|
|
if t and s.instance > 0 then
|
|
local c = claims[s.instance]
|
|
if not (c and c.first == s.instance and c.type == t) then
|
|
c = { type = t, first = s.instance, sensors = {} }
|
|
for k = 0, TYPES[t].slots - 1 do claims[s.instance + k] = c end
|
|
end
|
|
c.sensors[#c.sensors + 1] = i
|
|
elseif s.id == RAW and s.instance > 0 then
|
|
raws[s.instance] = { name = s.name, index = i }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function rebuildLines()
|
|
lines = {}
|
|
for slot = 1, SLOTS - 1 do
|
|
if claims[slot] or raws[slot] or pending[slot] then
|
|
lines[#lines + 1] = { kind = "slot", slot = slot }
|
|
end
|
|
end
|
|
lines[#lines + 1] = { kind = "preset" }
|
|
lines[#lines + 1] = { kind = "create" }
|
|
sel = limit(sel, 1, #lines)
|
|
end
|
|
|
|
-- Slot covered by a pending multi-slot type starting before it
|
|
local function pendingCover(slot)
|
|
for s = slot - 1, math.max(1, slot - 2), -1 do
|
|
local t = pending[s]
|
|
if t and s + TYPES[t].slots > slot then return s end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function slotText(slot)
|
|
local t = pending[slot]
|
|
if t then return "* " .. TYPES[t].name end
|
|
if pending[slot] == false then return "* raw" end
|
|
local c = claims[slot]
|
|
if c then
|
|
return (c.first == slot and "" or " ") .. TYPES[c.type].name
|
|
end
|
|
local cov = pendingCover(slot)
|
|
if cov then return " (slot " .. cov .. ")" end
|
|
local v = raws[slot] and getValue(raws[slot].name)
|
|
if type(v) == "number" then return string.format("raw %04X", v) end
|
|
return "raw"
|
|
end
|
|
|
|
local function lineText(line)
|
|
if line.kind == "slot" then return "Slot " .. line.slot, slotText(line.slot) end
|
|
if line.kind == "preset" then
|
|
local p = PRESETS[preset]
|
|
return "Preset", p.name .. " ID " .. presetGroup
|
|
end
|
|
return "Create sensors", ">"
|
|
end
|
|
|
|
local function applyPreset()
|
|
local p = PRESETS[preset]
|
|
local base = 8 * (presetGroup - 1)
|
|
for _, s in ipairs(p.sensors) do
|
|
local slot = base + s[1]
|
|
if not claims[slot] or canSet then pending[slot] = s[2] end
|
|
end
|
|
status = "Preset set: Create sensors"
|
|
rebuildLines()
|
|
end
|
|
|
|
-- pending[slot]: a type index, or false = back to raw (its typed sensor
|
|
-- deleted); nil = unchanged
|
|
local function changeSlot(slot, dir)
|
|
local c = claims[slot]
|
|
if c and not canSet then
|
|
status = "Has a sensor: delete it first"
|
|
return
|
|
end
|
|
if c and c.first ~= slot then
|
|
status = "Part of slot " .. c.first
|
|
return
|
|
end
|
|
local cur = pending[slot]
|
|
if cur == nil then cur = c and c.type or 0 elseif cur == false then cur = 0 end
|
|
local t = cur + dir
|
|
if t < 0 then t = #TYPES end
|
|
if t > #TYPES then t = 0 end
|
|
if c and t == c.type then pending[slot] = nil
|
|
elseif t == 0 then pending[slot] = c and false or nil
|
|
else pending[slot] = t end
|
|
end
|
|
|
|
-- Names: the type's base + a count (the preset's own names if given)
|
|
local function nextName(base, used)
|
|
for n = 1, 9 do
|
|
local name = base .. n
|
|
if not used[name] then
|
|
used[name] = true
|
|
return name
|
|
end
|
|
end
|
|
return base
|
|
end
|
|
|
|
-- With model.setSensor(): a free sensor index
|
|
local function freeIndex()
|
|
for i = 0, 59 do
|
|
local s = model.getSensor(i)
|
|
if s and s.name == "" then return i end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function createSensor(id, sub, slot, unit, prec, name)
|
|
if not canSet then
|
|
return setTelemetryValue(id, sub, slot, 0, unit, prec, name)
|
|
end
|
|
local i = freeIndex()
|
|
return i ~= nil and model.setSensor(i, { name = name, id = id, subId = sub,
|
|
instance = slot, unit = unit, prec = prec })
|
|
end
|
|
|
|
-- With model.deleteSensor(): the old sensors of the slots a change takes
|
|
local function deleteOld()
|
|
if not canSet then return end
|
|
local del = {}
|
|
for slot, t in pairs(pending) do
|
|
local c = claims[slot]
|
|
if c and c.first == slot then
|
|
for _, i in ipairs(c.sensors) do del[i] = true end
|
|
end
|
|
local n = t and TYPES[t].slots or 1
|
|
for k = 0, n - 1 do
|
|
if t and raws[slot + k] then del[raws[slot + k].index] = true end
|
|
end
|
|
end
|
|
for i in pairs(del) do model.deleteSensor(i) end
|
|
end
|
|
|
|
local function createSensors()
|
|
local used, made, failed = {}, 0, 0
|
|
local changed = next(pending) ~= nil
|
|
deleteOld()
|
|
for i = 0, 59 do
|
|
local s = model.getSensor(i)
|
|
if s and s.name and s.name ~= "" then used[s.name] = true end
|
|
end
|
|
local names = {}
|
|
local p = PRESETS[preset]
|
|
for _, s in ipairs(p.sensors) do names[8 * (presetGroup - 1) + s[1]] = s[3] end
|
|
for slot = 1, SLOTS - 1 do
|
|
local t = pending[slot]
|
|
if t then
|
|
local ty = TYPES[t]
|
|
for sub, v in ipairs(ty.values) do
|
|
local name = names[slot] and names[slot][sub]
|
|
if name and not used[name] then used[name] = true else name = nextName(v[1], used) end
|
|
if createSensor(ty.id, sub - 1, slot, v[2], v[3], name) then
|
|
made = made + 1
|
|
else
|
|
failed = failed + 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
pending = {}
|
|
scan()
|
|
rebuildLines()
|
|
if failed > 0 then
|
|
status = canSet and "No free sensor" or "Turn on Discover new sensors"
|
|
elseif made > 0 then
|
|
status = made .. " sensors created"
|
|
elseif changed and canSet then
|
|
status = "Sensors deleted"
|
|
else
|
|
status = "Nothing to create"
|
|
end
|
|
end
|
|
|
|
local function handleEvent(event)
|
|
local line = lines[sel]
|
|
local nextEvent = edit and EVT_VIRTUAL_INC or EVT_VIRTUAL_NEXT
|
|
local prevEvent = edit and EVT_VIRTUAL_DEC or EVT_VIRTUAL_PREV
|
|
if event == nextEvent or event == prevEvent then
|
|
local dir = event == nextEvent and 1 or -1
|
|
if not edit then
|
|
sel = limit(sel + dir, 1, #lines)
|
|
elseif line.kind == "slot" then
|
|
changeSlot(line.slot, dir)
|
|
elseif line.kind == "preset" then
|
|
presetGroup = limit(presetGroup + dir, 1, PRESETS[preset].groups)
|
|
end
|
|
elseif event == EVT_VIRTUAL_ENTER then
|
|
if line.kind == "create" then
|
|
createSensors()
|
|
elseif line.kind == "preset" and edit then
|
|
edit = false
|
|
applyPreset()
|
|
else
|
|
edit = not edit
|
|
if not edit then rebuildLines() end
|
|
end
|
|
end
|
|
local count = screenLines()
|
|
if sel < top then top = sel end
|
|
if sel >= top + count then top = sel - count + 1 end
|
|
end
|
|
|
|
local function draw()
|
|
lcd.clear()
|
|
drawTitle("Futaba S.BUS2 sensors")
|
|
local font = colorLcd and 0 or SMLSIZE
|
|
local x = 2
|
|
local y = colorLcd and 34 or 9
|
|
local dy = colorLcd and 20 or 8
|
|
local valueX = colorLcd and 150 or 50
|
|
if status ~= "" then
|
|
lcd.drawText(LCD_W - 2, colorLcd and 5 or 0, status, (colorLcd and COLOR_THEME_PRIMARY2 or SMLSIZE + INVERS) + RIGHT)
|
|
end
|
|
if #lines <= 2 then
|
|
lcd.drawText(x, y + dy * (screenLines() - 1), "No slot data yet (raw S<n> sensors)", font)
|
|
end
|
|
for i = top, math.min(#lines, top + screenLines() - 1) do
|
|
local attr = font
|
|
if i == sel then
|
|
attr = attr + INVERS
|
|
if edit then
|
|
blink = (blink + 1) % 30
|
|
if blink > 15 then attr = font end
|
|
end
|
|
end
|
|
local label, value = lineText(lines[i])
|
|
local yy = y + (i - top) * dy
|
|
lcd.drawText(x, yy, label, attr)
|
|
lcd.drawText(valueX, yy, value, attr)
|
|
end
|
|
end
|
|
|
|
local scanTime = 0
|
|
|
|
local function init()
|
|
scan()
|
|
rebuildLines()
|
|
end
|
|
|
|
local function run(event)
|
|
if event == nil then
|
|
error("Cannot be run as a model script!")
|
|
return 2
|
|
elseif event == EVT_VIRTUAL_EXIT and not edit then
|
|
return 2
|
|
elseif event == EVT_VIRTUAL_EXIT then
|
|
edit = false
|
|
rebuildLines()
|
|
end
|
|
-- new raw slots show up as they are discovered
|
|
if not edit and getTime() > scanTime then
|
|
scanTime = getTime() + 100
|
|
scan()
|
|
rebuildLines()
|
|
end
|
|
handleEvent(event)
|
|
draw()
|
|
return 0
|
|
end
|
|
|
|
return { init = init, run = run }
|