Hello again,
I’m currently working on creating the .lua script in order to automatically arm my drone and initiate an auto flight mode using a switch on my transmitter. Here are the details:
- Transmitter: I’m using a Flysky FS-i6.
- Switch: I have a three-position switch (swC) assigned to channel 6.
- Configuration: In position 3 of swC (the “Auto Mode” position), I want the drone to automatically arm and start its auto flight mode. This should happen without requiring me to manually arm the drone or apply a minimum throttle input.
To achieve this, I’ve written the following Lua script:
--[[
This script performs the following:
1) Checks the PWM value of channel 6 from the transmitter (RC).
2) If the PWM value of channel 6 exceeds the defined threshold (i.e., the third position - Auto mode),
then:
- It sets the flight mode to "AUTO" (if it isn't already).
- It arms the drone.
- It activates a throttle override (usually on channel 3) with the minimum throttle value for a short period,
so that automatic flight can start.
Note: Adjust the threshold, minimum throttle PWM, and override duration according to your requirements.
--]]
-- Parameter settings
local AUTO_MODE_THRESHOLD = 1800 -- PWM threshold for channel 6 to activate Auto mode (e.g., 1800)
local MIN_THROTTLE_PWM = 1100 -- PWM value for minimum throttle (adjust based on your system)
local OVERRIDE_DURATION = 2000 -- Duration in ms for the throttle override (e.g., 2 seconds)
-- State variables
local throttle_override_active = false
local throttle_override_start = 0
function update()
-- Check if channel 6 is available (RC check)
if rc:available() < 6 then
return update, 1000 -- If not available, wait 1 second
end
-- Read the PWM value from channel 6
local ch6 = rc:get_pwm(6)
-- Check if the switch is in the third position (Auto mode)
if ch6 and ch6 > AUTO_MODE_THRESHOLD then
-- If the drone is not yet armed:
if not vehicle:is_armed() then
-- Set the flight mode to "AUTO" (if it isn't already)
if vehicle:get_mode() ~= "AUTO" then
vehicle:set_mode("AUTO")
end
-- Execute arm command
vehicle:arm()
-- Activate throttle override to provide minimum throttle for a short period
if not throttle_override_active then
local overrides = {}
overrides[3] = MIN_THROTTLE_PWM -- Assuming channel 3 is the throttle
rc:set_channel_overrides(overrides)
throttle_override_start = systime_ms()
throttle_override_active = true
end
end
else
-- If channel 6 is not above the threshold (i.e., switch is not in the third position),
-- clear any throttle override.
if throttle_override_active then
rc:clear_channel_overrides()
throttle_override_active = false
end
end
-- Remove the throttle override after the specified duration has passed
if throttle_override_active and (systime_ms() - throttle_override_start > OVERRIDE_DURATION) then
rc:clear_channel_overrides()
throttle_override_active = false
end
-- Re-run the update function every 100 ms
return update, 100
end
-- Start the script
return update()
Questions:
Script Feedback:
Does this approach look solid?
Are there any improvements or potential pitfalls I should be aware of before testing in a real flight environment?
emphasized text
Lua Scripting on F4:
I noticed in the Ardupilot documentation that “Scripting is not available in F4 based autopilots.”
My flight controller is a SpeedyBee F405 v4. Can I run Lua scripts on this hardware? Has anyone tested Lua scripting on an F405-based board?
I appreciate any insights, suggestions, or experiences you can share regarding this setup.
Thanks in advance!