LUA script for Thr Lock/Disarm/Disable, + Launch control on one 3 pos sw

openTx has introduced a range of new features, ideas and bling. It is fast becoming the firmware of choice for many users. openTx will run on ALL current hardware platforms, including the gruvin9x and sky9x boards. Work has already started to support the new FrSky X9D radio!
Post Reply
chadh4
Posts: 1
Joined: Mon Oct 12, 2020 2:51 am
Country: -

LUA script for Thr Lock/Disarm/Disable, + Launch control on one 3 pos sw

Post by chadh4 » Mon Oct 12, 2020 2:58 am

Looking to use one OpenTX 3-pos switch for Throttle Arm / Disarm / Launch? Maybe this script I wrote will help you, too.

I fly a mix of hand-launched planes and planes with gear, and wanted to have one three-position switch in OpenTX give me "Throttle Enable / Armed", "Throttle Disable / Disarmed", and "Launch" modes. I also wanted logic that would prevent changing Enabled to Disabled if the throttle stick was above idle (to prevent locking out the throttle by hitting the wrong switch during flight), and similar logic to prevent going from Disabled to Enabled unless the throttle stick is at idle (to prevent suddenly having the plane go high-throttle when the switch is flipped to Enable / Arm). The "Launch" mode permits transitioning from "Throttle Disable" to "Enable" with a 5 second audible countdown followed by a 0.5 second ramp-up to the throttle stick position.
thrmod_thrlockswitch.png
thrmod_thrlockswitch.png (136.69 KiB) Viewed 194 times
-- Combined Throttle Disable / Launch Control
-- ==========================================
-- u/chadh4, October 2020
--
--
-- Uses (1) designated 3-position switch ("SG"); change getValue in run() to designate alternate to control 
-- throttle disable (lockout), throttle enable (normal), and launch control with countdown for hand-launched aircraft.
--
-- Control switch (SG):
--  Up = Throttle Disable; disables throttle if switch is in this position AND throttle setting (input3) is at/near zero.
--  Mid = Throttle Enable; enables throttle if switch is in this position AND throttle setting (input3) is at/near zero.
--  Down = Launch; prepares to launch (5 second countdown) IF throttle is disabled AND throttle setting (input3) is greater than zero.
--
-- For disabling and enabling the throttle, the validation that the throttle is at/near zero prevents accidentally
-- disabling the throttle during normal flight, and avoids accidentally enabling the throttle if the switch is moved
-- while the throttle input is set.
--
-- To use the Throttle Disable/Lockout feature:
--  (1) Move the control switch Up (throttle disable).
--  (2) Move the throttle input to zero.
--  .... Script announces "Throttle disabled" and sets ThrX to 100%, for use in the Mixes to force the throttle output mix to zero.
--
-- To enable the Throttle:
--  (1) Move the control switch to the Middle position (throttle enable).
--  (2) Move the throttle input to zero.
--  .... Script announces "Throttle enabled" and sets ThrX to 0%, taking its value out of the Mixes.  The throttle mix should now respond normally to the input.
--
-- To use the launch feature:
--  (1) Move the control switch Up (throttle disable).
--  (2) Set the throttle input to zero.
--  .... Script announces "Throttle disabled" and sets an override mix on the throttle output to zero it and lock out the throttle input.
--  (3) Increase the throttle input to the desired launch amount.
--  (4) Move the control switch through the Middle position (throttle enable).
--  .... Script announces "Throttle disabled.  Set zero throttle to disable, or set switch to Launch."
--  (5) Move the control switch to the Down position (launch).
--  .... Script announces "Launching in 5 seconds at set throttle position."
--  .... Script counts down, beeping once per second.
--  .... Script announces "Go for launch!" and throttle ramps up to set position over 0.5 second.
--
-- During flight, the switch can be left in the Launch position or moved back to Enable.  If it's accidentally moved back to Disable,
-- it won't have any accidental effect on the throttle unless the throttle input is also moved to zero.
--
-- /// HOW TO ENABLE THE SCRIPT IN OPEN TX
-- This script file, thrmod.lua, placed in the SCRIPTS/MIXES folder on the Open TX SD Card.
-- Within the Model setup, CUSTOM SCRIPTS, define script LUA1:
--   Script: thrmod
--   Name:   thrmod
-- Script requires no Inputs (the control switch and the throttle input channel are hard-coded in the script, below)
-- Script defines two Outputs (defined via the local output = {} statement in the script's globals, below):
--   ThrX: Logical value to use the for the Throttle Mix -- value "100.0" when throttle disabled by the script; value "0.0" when throttle enabled
--   ThrM: Logical value indicating current Throttle Mode (2 = disabled, 1 = enabled, 3 = launching)
--
-- /// MIXES: THROTTLE CHANNEL
-- += Name(Motor) Source(ThrX) Weight(-300%) NoTrim Slow(u0:d0.5)
-- ... optionally set a Switch(6P1) in the Mix to emergency disable the Throttle script when flight mode select is something other than "1".
--
-- /// VOICE RESOURCE FILES (I generated mine using OpenTXspeaker)
-- thrdrd.wav = "Set zero throttle to disable."
-- thrdis.wav = "Throttle disabled."
-- launca.wav = "Launch aborted."
-- threrd.wav = "Throttle disabled.  Set zero throttle to Enable, or set switch to Launch."
-- threna.wav = "Throttle enabled."
-- launcr.wav = "Launch ready.  Set throttle to launch."
-- launcs.wav = "Launching in five seconds at throttle setting."
-- launch.wav = "Go for launch!"
--

local CountDownSecs
local ThrottleMode = 1
local lastrun = 0
local nextcounttime

local MODE_NORMAL = 1
local MODE_THRDISABLED = 2
local MODE_LAUNCHING = 3

local THROTTLEX_ON = 1024
local THROTTLEX_OFF = 0

local SW_UP = 1024
local SW_MID = 0
local SW_DN = -1024

local output = { "ThrX", "ThrM"}

local function init()
  ThrottleMode = MODE_NORMAL
  lastrun = 0
end

local function throttledUp()
  if getValue("input3") > -800 then
    return true
  end

  return false
end

local function run()
  local modesw = getValue("sg")  -- SG is our throttle mode switch
  
  if modesw == SW_UP and ThrottleMode == MODE_NORMAL and throttledUp() then
    if lastrun ~= 1 then
      playFile("thrdrd.wav")
      lastrun = 1
    end

  elseif modesw == SW_UP and ThrottleMode == MODE_NORMAL and not throttledUp() then
    ThrottleMode = MODE_THRDISABLED
    playTone(500, 100, 10, PLAY_NOW)
    playFile("thrdis.wav")
    lastrun = 0

  elseif modesw == SW_UP and ThrottleMode == MODE_LAUNCHING then
    ThrottleMode = MODE_THRDISABLED
    playTone(500, 100, 10, PLAY_NOW)
    playFile("launca.wav")
    lastrun = 0

  elseif modesw == SW_MID and ThrottleMode == MODE_THRDISABLED and throttledUp() then
    if lastrun ~= 3 then
      playFile("threrd.wav")
      lastrun = 3
    end

  elseif modesw == SW_MID and ThrottleMode == MODE_THRDISABLED and not throttledUp() then
    ThrottleMode = MODE_NORMAL
    playTone(500, 100, 10, PLAY_NOW)
    playFile("threna.wav")
    lastrun = 0

  elseif modesw == SW_MID and ThrottleMode == MODE_LAUNCHING then
    ThrottleMode = MODE_THRDISABLED
    playTone(500, 100, 10, PLAY_NOW)
    playFile("launca.wav")
    lastrun = 0
    
  elseif modesw == SW_DN and ThrottleMode == MODE_THRDISABLED and not throttledUp() then
    if lastrun ~= 8 then
      playFile("launcr.wav")
      lastrun = 8
    end

  elseif modesw == SW_DN and ThrottleMode == MODE_THRDISABLED and throttledUp() then
    ThrottleMode = MODE_LAUNCHING
    CountDownSecs = 5
    playTone(500, 100, 10, PLAY_NOW)
    playFile("launcs.wav")
    nextcounttime = getTime() + (100 * 4)   -- 100 x10ms ticks
    lastrun = 0

  elseif modesw == SW_DN and ThrottleMode == MODE_LAUNCHING and not throttledUp() then
    ThrottleMode = MODE_THRDISABLED
    playTone(500, 100, 10, PLAY_NOW)
    playFile("launca.wav")
    lastrun = 0

  elseif modesw == SW_DN and ThrottleMode == MODE_LAUNCHING and throttledUp() then
    if getTime() >= nextcounttime then
      playTone(1000, 100, 10, PLAY_NOW)
      CountDownSecs = CountDownSecs - 1
      nextcounttime = getTime() + (100)

      if CountDownSecs == 1 then
        playFile("launch.wav")
      
      elseif CountDownSecs == 0 then
        ThrottleMode = MODE_NORMAL
        
      end
    end 
  
  else
    lastrun = 0
   
  end

  if ThrottleMode == MODE_NORMAL then
    return THROTTLEX_OFF, (ThrottleMode * 10.24)
  else
    return THROTTLEX_ON, (ThrottleMode * 10.24)
  end
end

return { output=output, run=run, init=init }
thrmod_statemachine.png


Post Reply

Return to “openTx”