ArduPilot ignores Python commands during battery failsafe
When the battery failsafe kicks in, ArduPilot switches the mode to RTL and the Python command in GUIDED is silently dropped. See how your code can watch the mode and know when to stop.
Your Python code sends velocity commands in GUIDED mode, and at some point the drone stops responding to them and starts heading home on its own. The loop is running, and the commands are going out.
If a yellow battery warning flashed by on the ground station, you've found where the problem is. Keep reading even if you never saw the warning, because your code can miss it.
The drone receives the command but doesn't execute it
When the battery voltage stays below the threshold long enough and an action is assigned to the battery failsafe, the autopilot changes the mode itself. Your code still thinks it's in GUIDED and keeps sending velocity commands.
The autopilot doesn't send a reject or error reply to these commands. A velocity target is processed only while the vehicle is in GUIDED mode; in any other mode the message is silently dropped.
What happens to your commands after the voltage drops below the threshold
Look at the dashed line in the figure. Once the mode switches to RTL, every command sent by the code that ignores the mode is wasted. The code that watches the mode stops at that moment and lets you know.
With the default setting you never see this
With the default setting, the autopilot only warns you when the battery runs low and doesn't change the mode. The problem shows up the day you change the failsafe action to a value like RTL or LAND.
The voltage must stay below the low-voltage threshold, 10.5 V by default on Copter, continuously for the default 10 seconds. A momentary voltage dip doesn't start a failsafe.
The code has to watch the mode itself
The fix is to check which mode the vehicle is in before sending a command. If the mode number in the HEARTBEAT message changes from GUIDED (4) to some other value, the code stops sending commands and lets you know.
import time
from pymavlink import mavutil
GUIDED = 4 # GUIDED mode number in ArduCopter
OTOPILOT = 1 # autopilot's MAVLink component ID
HIZ_MASKE = 0b0000111111000111 # only the velocity fields are valid
def guided_ucus(master, vn, ve, sure_sn):
"""Sends the velocity command only while the vehicle is still in GUIDED."""
mod = GUIDED
bitis = time.time() + sure_sn
while time.time() < bitis:
while True:
msg = master.recv_match(type=['HEARTBEAT', 'STATUSTEXT'], blocking=False)
if msg is None:
break
if msg.get_type() == 'STATUSTEXT':
print(Autopilot:, msg.text)
elif msg.get_srcSystem() == master.target_system:
mod = msg.custom_mode
if mod != GUIDED:
print('Mode changed, number', mod, '- stopping commands')
return False
master.mav.set_position_target_local_ned_send(
0, master.target_system, OTOPILOT,
mavutil.mavlink.MAV_FRAME_LOCAL_NED, HIZ_MASKE,
0, 0, 0, vn, ve, 0, 0, 0, 0, 0, 0)
time.sleep(0.1)
return TrueThe code makes its decision based on the mode number in HEARTBEAT. The battery warning also arrives when the action is zero, and it doesn't change the mode. The number inside HEARTBEAT is what shows the mode really changed.
Example: Your code flies the drone in GUIDED with velocity commands to a payload drop point. At the end of a long flight the voltage drops below the threshold, and if the failsafe action is RTL, the autopilot starts heading home. Code that ignores the mode keeps sending velocity commands toward the drop point. The code above sees in HEARTBEAT that the mode number is no longer 4, prints "Mode changed", and stops. The battery warning on the ground station then tells you why.
See it for yourself in simulation
The parameter names for the three settings above are: action BATT_FS_LOW_ACT (default 0, warning only), threshold BATT_LOW_VOLT, duration BATT_LOW_TIMER.
Testing the failsafe by actually draining a battery is both dangerous and impossible to repeat. In ArduPilot SITL you lower the voltage with a parameter and watch the same behavior safely.
Start SITL with -w and set BATT_FS_LOW_ACT to 2 (RTL). While your code is sending velocity commands in GUIDED, pull SIM_BATT_VOLTAGE down to 9.5 V.
About 10 seconds after the voltage drops from 12.6 V to 9.5 V, the mode switches from GUIDED to RTL. A loop that doesn't watch the mode keeps sending commands meanwhile, and everything it sends after the mode change is dropped.
In a real flight, when the voltage drops below the threshold depends on your battery and your payload.
Error Messages
Battery 1 is low 9.50V used 469 mAh
The voltage or remaining capacity crossed the threshold. The voltage and mAh values are your battery's state at that moment. If the action is 0, the mode doesn't change.
Battery Failsafe
The autopilot has handled the failsafe. If the action isn't 0, the mode has changed; check the mode number inside HEARTBEAT.
Battery Failsafe - Disarming
The failsafe kicked in while the drone was on the ground, and the autopilot shut down the motors. A drone in the air doesn't give this message.
If you want to learn flying with code from the start
We go step by step from connecting with pymavlink to an autonomous mission. Course: Drone Control with Python from Scratch.
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