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.
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
Read as text
- GCS failsafe check on every loop then is failsafe off? FS_GCS_ENABLE = 0
- is failsafe off? FS_GCS_ENABLE = 0 then (yes) nothing happens, failsafe not in use
- is failsafe off? FS_GCS_ENABLE = 0 then (no) has a ground station ever been seen?
- has a ground station ever been seen? then (no) exits quietly, no warning, counter stays at zero, code that sends no heartbeat falls here
- has a ground station ever been seen? then (yes) time since last heartbeat > FS_GCS_TIMEOUT ?
- time since last heartbeat > FS_GCS_TIMEOUT ? then (no) waits, checks again on the next loop
- time since last heartbeat > FS_GCS_TIMEOUT ? then (yes) FAILSAFE the action in FS_GCS_ENABLE
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
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.
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:
param show FS_GCS_ENABLE FS_GCS_TIMEOUT MAV_GCS_SYSIDExample: 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?
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