diff --git a/libraries/AP_Scripting/applets/README.md b/libraries/AP_Scripting/applets/README.md new file mode 100644 index 0000000000..8a4df742a0 --- /dev/null +++ b/libraries/AP_Scripting/applets/README.md @@ -0,0 +1,5 @@ +# LUA Applets + +Unlike the code in the examples directory, these are complete applets that required no user editing of the file for use. Each one has an .md file describing its operation and any switches or parameters in ArduPilot that are required for operation. The user only has to copy the applet to APM/scripts directory on their SD card, in order to install. Of course, scripting must be enabled via SCR_ENABLE =1 and the autopilot be capable of running scripts. See the Wiki for Lua Scripting under Advanced Configuration. + +Each will have a Wiki entry mirroring the .md file'd contents with a link back to this directory. \ No newline at end of file diff --git a/libraries/AP_Scripting/applets/SmartAudio.lua b/libraries/AP_Scripting/applets/SmartAudio.lua new file mode 100644 index 0000000000..a5820d598e --- /dev/null +++ b/libraries/AP_Scripting/applets/SmartAudio.lua @@ -0,0 +1,95 @@ +-------------------------------------------------- +-------------------------------------------------- +--------- VTX LUA for SMARTAUDIO 2.0 ------------- +------------based on work by---------------------- +---------Craig Fitches 07/07/2020 ---------------- +-------------Mods by H. Wurzburg ----------------- +------------clean up by Peter Hall---------------- + +-----------------HARDWARE------------------ +-- tested on CUAVv5Nano and TX8111 VTX + +-- Prerequisites ---------------------------------- +-- 1. Only works in Ardupilot 4.1dev or later +-- 2. FC with 2MB cache for LUA Scripting +-- 3. Currently only works with SmartAudio 2.0 + +------------ Instructions ------------------------ +-- 1. Set an unused Serial port in Ardupilot to protocol 28 (scripting) and option 4 (half-duplex) +-- 2. Setup an rc channel's RXc_OPTION to 300 for changing power and SCR_USER1 parameter for initial power upon boot +---------and set to -1 for unchanged, 0 (PitMode),1,2,3, or 4 for power level (1 lowest,4 maximum) +-- 3. Attach the UART's TX for the Serial port chosen above to the VTX's SmartAudio input + +-- init local variables +local startup_pwr = param:get('SCR_USER1') +local scripting_rc = rc:find_channel_for_option(300) +local port = serial:find_serial(0) +local _current_power = -1 + +-- hexadecimal smart audio 2.0 commands +local power_commands = {} +power_commands[1] = { {0x00,0x00,0xAA,0x55,0x0B,0x01,0x01,0xF8,0x00}, "VTX Pit Mode" } +power_commands[2] = { {0x00,0x00,0xAA,0x55,0x05,0x01,0x00,0x6B,0x00}, "VTX PWR LOW" } -- SMARTAUDIO_V2_COMMAND_POWER_0 +power_commands[3] = { {0x00,0x00,0xAA,0x55,0x05,0x01,0x01,0xBE,0x00}, "VTX PWR MED" } -- SMARTAUDIO_V2_COMMAND_POWER_1 +power_commands[4] = { {0x00,0x00,0xAA,0x55,0x05,0x01,0x02,0x14,0x00}, "VTX PWR HIGH" } -- SMARTAUDIO_V2_COMMAND_POWER_2 +power_commands[5] = { {0x00,0x00,0xAA,0x55,0x05,0x01,0x03,0xC1,0x00}, "VTX PWR MAX" } -- SMARTAUDIO_V2_COMMAND_POWER_3 + + +-- return a power level from 1 to 5 as set with a switch +function get_power() + input = scripting_rc:norm_input() -- - 1 to 1 + input = (input + 1) * 2 -- 0 to 4 + return math.floor(input+0.5) + 1 -- integer 1 to 5 +end + +-- set the power in the range 1 to 5 +function setPower(power) + if power == _current_power then + return + end + updateSerial(power_commands[power][1]) + gcs:send_text(4, power_commands[power][2]) + _current_power = power +end + +-- write output to the serial port +function updateSerial(value) + for count = 1, #value do + port:write(value[count]) + end +end + +---- main update --- +function update() + setPower(get_power()) + return update, 500 +end + +-- initialization +function init() + -- check if setup properly + if not port then + gcs:send_text(0, "SmartAudio: No Scripting Serial Port") + return + end + if not scripting_rc then + gcs:send_text(0, "SmartAudio: No RC option for scripting") + return + end + + port:begin(4800) + + -- Set initial power after boot based on SCR_USER1 + if startup_pwr then -- make sure we found the param + if startup_pwr >= 0 and startup_pwr < 5 then + setPower(math.floor(startup_pwr) + 1) + + -- set the current power local to that requested by the rc in + -- this prevents instantly changing the power from the startup value + _current_power = get_power() + end + end + return update, 500 +end + +return init, 2000 --Wait 2 sec before initializing, gives time for RC in to come good in SITL, also gives a better chance to see errors diff --git a/libraries/AP_Scripting/applets/SmartAudio.md b/libraries/AP_Scripting/applets/SmartAudio.md new file mode 100644 index 0000000000..fbd6d80792 --- /dev/null +++ b/libraries/AP_Scripting/applets/SmartAudio.md @@ -0,0 +1,25 @@ +# SmartAudio Lua Script + +Allows the setting of Smart Audio 2.0 video transmitters (VTX) via RC transmitter and to set an initial power level on system boot. + +Four power levels can be set by the value of an RC channel: Pit Mode, Low, Medium, High, and Maximum. What these power levels actually produce is dependent on transmitter used. The new power level is activated upon the channel's changing to the new power selection. + +The power level upon boot can be selected to be unchanged, or set to one of those above values by setting parameter SCR_USER1 to -1,0,1,2,3 or 4. Unchanged is -1. 0-4 correspond to the above levels. + +The RC channel used to control the power level has its RCx_OPTION set to "300". + +Ground Control Station messages will confirm boot value and any changes made later. + +The VTX SmartAudio input is connected to any free autopilot UART TX line. That UART's corresponding SERIALx Port should have its SERIALx_PROTOCOL set to 28 and SERIALx_OPTIONS to 4 for half-duplex. + +Pit Mode is set by the RC channel being a RCx_MIN for that channel, and MAXIMUM is set at RCx_MAX for the channel. The other levels are linearly spread in the interval. If MIN is 1000 and MAX is 2000, the mapping would be: + +PWM | Power Level +-----------|------------ +1000 - 1124| Pit Mode | +1125 - 1374| LOW | +1375-1624| MED | +1625-1874| HIGH | +1875-2000| MAX | + +Note: Some SmartAudio 2.0 VTX cannot be placed in PitMode remotely, but they can exit it and have power changed with this script. SmartAudio 2.1 VTX have the abiity to be remotely commanded into PitMode