Mixes Scripts

Don't use Lua Mixes Scripts for controlling any aspect of your model that could cause a crash if the script stops executing!

Purpose

Mixes Scripts take one or more values as inputs, do some processing in Lua code, and output one or more values. Each model can have several Mixes Scripts associated with it, and these scripts are run periodically. They behave similarly to standard EdgeTX mixers, but at the same time they provide a much more flexible and powerful tool.

Typical use cases:

  • replacement for complex mixes that are not critical to model function

  • complex processing of inputs and reaction to their current state and/or their history

  • filtering of telemetry values

Execution & Lifetime

Mixes script is loded when model is selected. Script executes until

  • it misbehaves (e.g. run-time error or low memory)

  • One-time Script is running. When One-time script finishes execution, Wigdet Script resumes execution.

Limitations

  • cannot update LCD screen or perform user input.

  • should not exceed allowed run-time/ number of instructions.

  • custom scripts are run with less priority than built-in mixes. Their execution period is around 30ms and is not guaranteed!

If the script output is used as a mixer source , and the script is killed for whatever reason, then the whole mixer line is disabled!

Mixes Scripts should be as short as possible, to avoid delays. It is also important to keep in mind that other loaded Telemetry and Function scripts can add to the response time, or worse: hang the system!

To enable Mixes Scripts, firmware must be compiled with the option LUA_MIXER=Y.

File Location

Mixes scripts are located on SD card in folder /SCRIPTS/MIXES/

File name length (without extension) must be 6 characters or less

Interface

Every Mixes Script must include a return statement at the end, defining its interface to EdgeTX. This statement returns a table with the following fields:

table itemtyperequired

run

function

yes

This function is called periodicaly when script is running. Parameters Variables matching those used in input table Return values Variables matching those used in output table\

table itemtyperequired

init

function

no

This function is called once when Mixes Script is loaded and executed for the first time

Parameters none Return values none\

table itemtyperequired

input

table

yes

The input table defines what values are available as input(s) to custom scripts.

There are two forms of input table entries

SOURCE

{ "<name>", SOURCE }

SOURCE inputs provide the current value of a selected OpenTX variable. The source must be selected by the user when the script is configured. Source can be any value that EdgeTX knows about (inputs, channels, telemetry values, switches, custom functions etc.). Note: the typical input range is -1024 thru +1024. Simply divide the input value by 10.24 to convert to a percentage from -100% to +100%.

VALUE

{ "<name>", VALUE, <min>, <max>, <default> }

VALUE inputs provide a constant value that is set by the user when the script is configured.

  • name - maximum length of 8 characters

  • min - minimum value of -128

  • max - maximum value of 127

  • default - must be within the valid range specified

  • Maximum of 6 inputs per script

table itemtyperequired

output

table

yes

The output table defines only name(s), as the actual values are returned by the script's run function.

{ "<name1>", "<name2>" }

Above names are only visible as source values on the radio screen when the script is running. If the model is edited in Companion, then the values show as LUA1a and LUA1b etc.

Examples

Example of <decribe what it does>.

local my_input =
  {
    { "Strength", SOURCE},           -- user selects source (typically slider or knob)
    { "Interval", VALUE, 0, 100, 0 } -- interval value, default = 0.
  }

local my_output = { "Val1", "Val2" }

local function my_init()
  -- Called once when the script is loaded
end

local function my_run(Strength, Interval) -- Must match input table
  local v1, v2
  -- Called periodically
  return v1, v2                        -- Must match output table
end

return { input = my_input, output = my_output, run = my_run, init = my_init }

Example where Lua mix script is used to drive ailerons in some clever way, but control falls back to the standard aileron mix if script is killed. Second mixer line replaces the first one when the script is alive:

CH1  [I4]Ail Weight(+100%)
  := LUA4b Weight(+100%)