Skip to content

Instantly share code, notes, and snippets.

@ghostface
Last active October 16, 2022 15:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ghostface/b7de909b24fc7ce4b4c75de515c0ae46 to your computer and use it in GitHub Desktop.
Save ghostface/b7de909b24fc7ce4b4c75de515c0ae46 to your computer and use it in GitHub Desktop.
VTX Power switching via transmitter switch (Opentx/Betaflight/Tramp/Smartaudio)
--
-- *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:
-- 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))
playFile("eng.wav")
else
mspSendRequest(MSP_VTX_SET_CONFIG,VTXconfig(TxPower, TxBand, TxChannel,1))
playFile("eng.wav")
end
if TxPower == 0 then
mspSendRequest(MSP_VTX_SET_CONFIG,VTXconfig(0, TxBand, TxChannel))
end
lastPower = TxPower
lastBand = TxBand
lastChannel = TxChannel
end
mspProcessTxQ()
return
end
return {input=inputs, run=run}
@Klaus-Michael
Copy link

Thanks a lot for adapting the Script.
Unfortunately I have some problems to get it working correctly.
Changing Band and Channel works fine.
Powerlevel however is giving me Problems. Values auf 3 and 4 are set correctly on the Unify HV. Values of 1 and 2 however do not change anything on the VTX. Is anybody else having similar problems?

Best Regards
Klaus

@JosefB87
Copy link

JosefB87 commented Sep 23, 2019

@ghostface The script is not compatible anymore with Open TX 2.2.4 due to new soundfile naming pattern. I updated the script so it works with Open TX 2.2.4:

https://gist.github.com/JosefB87/1c9bc523f4e722c4c7cd3b6f3a587f3e

@elvenpure
Copy link

Hi, I have just installed this and it works really well.
Just wondering however if it is possible to not be bound to a specific band or channel?
As I would like to be able to select my channel, then simply change the power if needed during flight.
Otherwise if I forget I have changed channels, I will flick the switch and just get static.
I don't know much about Lua scripts, so unsure if this is possible.
Thanks again.
Elf

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