Drone Not Taking Off with ArduPilot

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!

for F4, you can only use custom build firmware, to include C++ scripting function. Otherwise you cannot use LUA scripting.

Scripting is not available in F4 based autopilots. Autopilots must have an SD card to store the script unless the user builds his own firmware and embeds it directly in the firmware.

2 Likes

I have an SD card in my flight controller. So can I save the script in the APM/SCRIPS folder and then test it?

I am a bit confused as to why you are overriding the throttle when there is a parameter that you can change to allow Auto mode to start a mission without needing throttle input.

Okay I didn’t know about that, but I also want to arm the drone when I switch in Auto Mode. Is there a parameter for that also?

No - but basically what I would do is simplify your Lua script by changing that parameter.

And then in the Lua all you do is Arm when you switch to Auto (although personally I think this is a bit scary/risky to add any sort of automatic arming that isn’t user-verified).

1 Like

Yeah yeah I simplified the code to only arm the drone when I switch to Auto mode.
Im considering this because in my project I want to arm the drone, not automatically, but by an external signal.
Because I dont have a device to send an external signal, I asked if there is another way to do that.

And so I was trying to see, if this can do my thing.

What device are you using to set it to Auto mode?

Im using the transmitter(flysky fs-i6).

I am a bit confused - if you can use the transmitter to put into Auto. Why not also use the transmitter to Arm?

You can even have the same switch both arm the aircraft and put it into Auto flight mode with the correct transmitter mixing - if you don’t want to flick one switch for arm and another for auto.

Alongside this bitmask set appropriately:

2 Likes

I’d go one step further, set the initial flight mode to Auto. So then rather than having the script to arm when you switch to auto, you go the other way and have it default in auto and just arm. No Lua required.

5 Likes

Okay guys. For real thank you both for the suggestions, im going to test them tommorow morning as Im not now in an enviroment to do it.

Thanks again for the help!!!

1 Like