Softivation

Drone doesn't RTL when the Python code stops

Your drone doesn't RTL when your Python code stops, even though FAILSAFE looks enabled. The GCS failsafe counter doesn't run because the autopilot has never seen a ground station. The fix is here.

Furkan IŞIKAY 5 min read

While flying in GUIDED mode from a companion computer, your Python code stopped or the connection dropped. You expected the drone to return home, but it hung in place and not a single warning appeared on screen.

Everything looks fine when you check the parameters: FAILSAFE is enabled, the timeout is set. The real issue is when the autopilot counts that it has seen a ground station.

The autopilot doesn't count your code as a ground station

ArduCopter runs the GCS failsafe by looking at the time elapsed since the last heartbeat. This counter starts only if the autopilot has seen a heartbeat from its own ground station at least once.

If it has never seen one, the counter stays at zero and the failsafe check quietly ends right at the start. In the figure, look at the second question: your code falls exactly there.

In what order the autopilot checks when deciding on a GCS failsafe

In what order the autopilot checks when deciding on a GCS failsafe: 8 boxes, 7 linksyesnonoyesnoyesGCS failsafe checkon every loopis failsafe off?FS_GCS_ENABLE = 0nothing happensfailsafe not in useground station everbeen seen?exits quietlyno warning, counter at zerostays, code that sends noheartbeat falls heretime since lastheartbeat> FS_GCS_TIMEOUT ?waitschecks again on the nextloopFAILSAFEthe action in FS_GCS_ENABLE
Read as text
  1. GCS failsafe check on every loop then is failsafe off? FS_GCS_ENABLE = 0
  2. is failsafe off? FS_GCS_ENABLE = 0 then (yes) nothing happens, failsafe not in use
  3. is failsafe off? FS_GCS_ENABLE = 0 then (no) has a ground station ever been seen?
  4. has a ground station ever been seen? then (no) exits quietly, no warning, counter stays at zero, code that sends no heartbeat falls here
  5. has a ground station ever been seen? then (yes) time since last heartbeat > FS_GCS_TIMEOUT ?
  6. time since last heartbeat > FS_GCS_TIMEOUT ? then (no) waits, checks again on the next loop
  7. time since last heartbeat > FS_GCS_TIMEOUT ? then (yes) FAILSAFE the action in FS_GCS_ENABLE
Where to look: the second question. If the autopilot has never received a heartbeat from a ground station, the check ends here and the timeout question is never reached. Source: ArduCopter/events.cpp, failsafe_gcs_check().

Most Python code that connects with pymavlink sends commands but doesn't send a heartbeat. In the autopilot's eyes, such a program isn't a ground station. Whether the code is running or crashing, the failsafe counter never runs.

Same setting, two different endings

In both cases FS_GCS_ENABLE, FS_GCS_TIMEOUT, and MAV_GCS_SYSID are exactly the same. The only difference is whether the code sends a heartbeat once a second.

The autopilot's mode in the two cases when the code stops

With code that sends no heartbeat, the mode stays GUIDED. When code that sends a heartbeat every second stops, the mode switches to RTL after 5 seconds
Where to look: the yellow window at the bottom. The counter starts at the last heartbeat, and when FS_GCS_TIMEOUT expires the mode becomes RTL. On top, there is no such window because no heartbeat ever arrived.

Look at the top strip in the figure: if no heartbeat is ever sent, the drone hangs in GUIDED at 15 meters, and nothing changes even after half a minute. In the bottom strip a heartbeat is sent and then stops. The autopilot switches to RTL within about five seconds and starts to descend.

Add a heartbeat to your code

The fix is for the code to send a heartbeat with the right identity. When opening the connection, match the source_system value to your ground station number, then send a heartbeat once a second in a separate thread.

Python
from pymavlink import mavutil
import threading, time

master = mavutil.mavlink_connection('udpout:127.0.0.1:14550', source_system=255)
master.wait_heartbeat()

def kalp_at():
    while True:
        master.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS,
                                  mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
        time.sleep(1)

# A heartbeat once per second on a separate thread: while the code is alive
# the autopilot sees a GCS; when the code stops, the timer starts running.
threading.Thread(target=kalp_at, daemon=True).start()

In this state the autopilot counts your code as a ground station. If the code crashes, the heartbeat stops, FS_GCS_TIMEOUT expires, and the action you chose in FS_GCS_ENABLE kicks in.

Check three parameters once before flight. If MAV_GCS_SYSID doesn't cover your code's source_system value, the autopilot won't count your heartbeat as a ground station. You can check with this command in MAVProxy:

Output
param show FS_GCS_ENABLE FS_GCS_TIMEOUT MAV_GCS_SYSID

Example: Try this deliberately in the rehearsal before the competition. With the drone in GUIDED a few meters up, the transmitter in the pilot's hand, and no ground station connected other than your code, stop the code with Ctrl+C and time it with a stopwatch. If the drone starts heading home within about five seconds, the autopilot counts your code as a ground station. If it hangs where it is, either the heartbeat isn't being sent or MAV_GCS_SYSID and source_system don't match.

Error Messages and Their Meanings

A few lines you may see on screen on this topic, or never see:

GCS Failsafe

The autopilot couldn't see the ground station for FS_GCS_TIMEOUT and the failsafe started. If you see this line, the counter is running. If you never see it, the autopilot may never have counted a ground station.

GCS Failsafe Cleared

The connection came back and the failsafe condition cleared. The autopilot still stays locked in the failsafe mode and doesn't return to its previous mode on its own. To go back you have to change the mode manually.

PreArm: GCS failsafe on

You tried to ARM on the ground while a failsafe was active. First fix the connection and the heartbeat stream, then ARM.

Learn this topic end to end

In the Drone Control with Python from Scratch course we cover connecting with pymavlink and DroneKit, GUIDED missions, and autonomous control from a companion computer step by step.

Was this article helpful?

Free resourcePixhawk and ArduPilot error message glossaryThe 3 messages in this article and similar ones are in the glossary: what they mean, why they appear, what to check first.

Learn this topic from start to finish

Season plan: all six courses for 9 months, ₺790 a month. ₺390 a month for high school and university students, ₺351 the first month with your member code. You can stop in any month you like. Start the Season plan · I'm a student