Skip to content

Instantly share code, notes, and snippets.

@JosefB87
Forked from ghostface/vtx.lua
Last active January 15, 2021 22:16
Show Gist options
  • Save JosefB87/1c9bc523f4e722c4c7cd3b6f3a587f3e to your computer and use it in GitHub Desktop.
Save JosefB87/1c9bc523f4e722c4c7cd3b6f3a587f3e to your computer and use it in GitHub Desktop.
VTX Power switching via transmitter switch (Opentx/Betaflight/Tramp/Smartaudio)
-- *Fixed version of JosefB87 for OpenTX 2.2.4
--
-- *Fixed version of wkochFPV for BF 3.5
-- Original: https://github.com/betaflight/betaflight/issues/3094#issuecomment-302953603
-- Note: If you put it on a 3-way switch and go from first to third position fast it may not register correctly so better step through slowly
--
-- *Changes:
-- Changed played soundfiles using new soundfile naming pattern since Open TX 2.2.4
-- Corrected loading of dependencies
-- Shortened filename to make it compatible with latest opentx restrictions
-- Added a condition in the script for pit mode (vtx power 0 alone didn't work for me)
--
-- *How To (Copy pasted from comment)
-- 1. Install betaflight-tx-lua-scripts as usual
-- 2. Install my script vtx.lua in /SCRIPTS/MIXES
-- 3. In model menu of your taranis go to LUA-scripts page and enable VTXprog.lua and configure as follows:
-- 4. Enter your desired band: 1="A", 2="B", 3="E", 4="F", 5="R" (unify pro!)
-- 5. Enter your desired channel: 1,2,3…
-- 6. Select “global variable 1” (GV1, or another free global variable) as input for TxPower
-- 7.Now configure any switch to adjust GV1 to the desired power level: 0=pit mode, 1=25mW, 2=200mW … (use special functions menu, select ‘adjust GV1’ as action, enter desired value). Use e.g. 0 for your switch neutral position, 1 for switch up, 2 for switch down, if you would like to select 25mw or 200mw based on switch position.
--
-- Load MSP smartport library
SCRIPT_HOME = "/SCRIPTS/BF"
protocol = assert(loadScript(SCRIPT_HOME.."/protocols.lua"))()
radio = assert(loadScript(SCRIPT_HOME.."/radios.lua"))()
assert(loadScript(radio.preLoad))()
assert(loadScript(protocol.transport))()
assert(loadScript(SCRIPT_HOME.."/MSP/common.lua"))()
-- define inputs
local inputs = {
{ "TxPower", SOURCE}, -- store requested TxPower in global variable, e.g. GV1
{ "TxBand", VALUE, 1, 5, 4 },
{ "TxChannel", VALUE, 1, 8, 1 }
}
-- Variables to store parameters last programmed to vtx
local lastPower = 0
local lastBand = 0
local lastChannel = 0
local MSP_VTX_SET_CONFIG = 89
local firstrun = 1
-- Returns payload to send to fc
local function VTXconfig(TxPower, TxBand, TxChannel,TxPitMode)
local channel = (TxBand-1)*8 + TxChannel-1
return { bit32.band(channel,0xFF), bit32.rshift(channel,8), TxPower, TxPitMode } -- last 0 disables PIT mode
end
local function run(TxPower, TxBand, TxChannel)
-- ignore any settings on first run of the script, send only further changes to vtx
if firstrun == 1 then
lastPower = TxPower
lastBand = TxBand
lastChannel = TxChannel
firstrun = 0
end
if (lastPower ~= TxPower) or (lastBand ~= TxBand) or (lastChannel ~= TxChannel) then
if TxPower > 0 then
mspSendRequest(MSP_VTX_SET_CONFIG,VTXconfig(TxPower, TxBand, TxChannel,0))
if TxPower > lastPower then
playFile("gearup.wav")
else
playFile("geardn.wav")
end
else
mspSendRequest(MSP_VTX_SET_CONFIG,VTXconfig(TxPower, TxBand, TxChannel,1))
if TxPower > lastPower then
playFile("gearup.wav")
else
playFile("geardn.wav")
end
end
lastPower = TxPower
lastBand = TxBand
lastChannel = TxChannel
end
mspProcessTxQ()
return
end
return {input=inputs, run=run}
@elvenpure
Copy link

This worked for me really well on my qx7.
I have just got an xlite, and it doesn't seem to be working.
Also any idea how I can make it just change the power and not the band or channel?
thanks heaps.

@paveltar
Copy link

paveltar commented Jun 9, 2020

I have no LUA-scripts page in my model menu, can you please explain a little more in detail how to set it up?

@elvenpure
Copy link

you need to tick Lua in companion when flashing your radio

@elvenpure
Copy link

but I cant get this to work with latest opentx anyway. not getting any response to my above question

@paveltar
Copy link

paveltar commented Jun 9, 2020

you need to tick Lua in companion when flashing your radio

I'm using OpenTX 2.4 nightly from TBS for the crsfshot, is there a way to make it work with it?

@paveltar
Copy link

paveltar commented Jun 9, 2020

There is a luac enabled with this firmware, I can go to the radio menu -> sd card, and execute the script from there isn't it the same?

@4noxx
Copy link

4noxx commented Sep 18, 2020

try it with OpenTX 2-3-9. will not work.

vtx

@nsfilho
Copy link

nsfilho commented Nov 21, 2020

try it with OpenTX 2-3-9. will not work.

vtx

I have the same problem. I can't assign inputs for the scripts. Are anyone find a solution?

@elvenpure
Copy link

Yes! this is actually a feature of betaflight now.
you can find the cli commands to set this up without lua scripts.
It works much better
https://github.com/betaflight/betaflight/wiki/VTX-CLI-Settings
Check out this page.
Let me know if you have any issues.

@JosefB87
Copy link
Author

Yep, you can set it up using Betaflight and its a way better solution, LUA scripts you have to keep maintained with every new open TX release it seems. This was available in Betaflight all along BTW, but most didn't know about it. Heres a great tutorial from Bardwell: https://www.youtube.com/watch?v=a1_U2v0kAjg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment