Battery failsafe doesn't work in SITL (voltage never drops)
Battery failsafe not working in SITL? The voltage stays stuck at 12.6 V, and even when the warning appears, the drone waits in GUIDED. Fix three default settings and see the RTL.
In a mission rehearsal, you want to see the drone return to the takeoff point when the battery runs low. SITL is running, the drone is in the air, and in Mission Planner the battery is stuck at 12.6 V; minutes pass and it doesn't drop one bit.
When you lower the voltage by hand, a red Battery Failsafe message appears on screen, but the drone just hangs in place. It doesn't return home or land; it waits as if nothing had happened.
Three settings are behind these two cases, and all three are at their default values. You can tell which one you've hit from the message on screen.
Why the voltage never drops
The simulator's battery is defined by two settings: SIM_BATT_VOLTAGE sets the voltage, and SIM_BATT_CAP_AH sets the capacity. The capacity defaults to 0, and 0 means an unlimited battery.
With the capacity at 0, the simulator never drains the voltage. The description in the ArduPilot source code also says "if SIM_BATT_CAP_AH is 0, the voltage is constant". Seeing 12.6 V stay constant is the simulator's default behavior.
You have two ways to test the failsafe. You can pull the voltage straight below the threshold, or give the capacity a real value and wait for the battery to drain as you fly. The first takes seconds and is repeatable, so use it in rehearsal.
The message appears, but the drone doesn't return home
When the voltage drops below the threshold, the autopilot doesn't count it as a failsafe right away. The default threshold is 10.5 V, and the voltage has to stay below it for 10 seconds without interruption. BATT_LOW_TIMER sets this duration.
How many seconds the voltage stayed below the threshold
Look at the two bars in the figure. The drop that lasted 6.4 seconds didn't fill the timer, and nothing happened. When the voltage came back, the timer reset. The drop that lasted 10.2 seconds crossed the line, and the failsafe started.
BATT_FS_LOW_ACT chooses what the drone does when the timer fills. Its default is 0, meaning warning only. The autopilot prints the message and doesn't change the flight mode. That's why the drone hangs in GUIDED. 1 means LAND, 2 means RTL.
Nothing happens on the second try
On the first try you only got a warning, so you set the action to 2, raised the voltage and dropped it again, and this time not even the message appears. The autopilot triggers the low battery failsafe once and keeps that state latched. The voltage rising back up doesn't release the latch.
You can't ARM until the latch is released either: after the drone lands, PreArm warnings pile up in the message panel. For a clean test, restart SITL or send the autopilot a battery reset command. You can see the second option step by step in the lesson section.
Don't run the failsafe test with the drone on the ground, armed but not yet taken off. While the drone is on the ground, the battery failsafe stops the motors no matter which action is selected. To see the behavior in the air, take off first.
Set up the test correctly
While Mission Planner is connected to SITL on port 5760, a second program can't connect to the same port. The Python code below uses port 5762. Take off with the drone in GUIDED from Mission Planner, then run the code.
from pymavlink import mavutil
# While Mission Planner is on 5760, the code connects through the second port
m = mavutil.mavlink_connection('tcp:127.0.0.1:5762')
m.wait_heartbeat()
m.mav.request_data_stream_send(m.target_system, m.target_component,
mavutil.mavlink.MAV_DATA_STREAM_ALL, 2, 1)
def param_yaz(ad, deger):
m.mav.param_set_send(m.target_system, m.target_component, ad.encode(),
deger, mavutil.mavlink.MAV_PARAM_TYPE_REAL32)
while True:
p = m.recv_match(type='PARAM_VALUE', blocking=True, timeout=5)
if p is None:
raise SystemExit(f'no reply for {ad}')
if p.param_id == ad:
print(f'{ad} = {p.param_value:g}')
return
param_yaz('BATT_FS_LOW_ACT', 2) # 0: warning only, 2: RTL
param_yaz('SIM_BATT_VOLTAGE', 10.3) # below BATT_LOW_VOLT (10.5)
mod = None
failsafe_geldi = False
while not (failsafe_geldi and mod == 'RTL'):
msg = m.recv_match(type=['STATUSTEXT', 'HEARTBEAT'], blocking=True, timeout=10)
if msg is None:
break
if msg.get_type() == 'STATUSTEXT':
print('Mesaj:', msg.text)
if msg.text == 'Battery Failsafe':
failsafe_geldi = True
elif msg.get_srcComponent() == 1:
yeni = mavutil.mode_string_v10(msg)
if yeni != mod:
print('Mod:', yeni)
mod = yeniThe code first sets the action to RTL, then pulls the voltage down to 10.3 V and waits for the failsafe message and the mode change. On every parameter write, it prints the value read back from the autopilot. If a write doesn't stick, you see it right away.
What you'll see
If you run the code while the drone is in the air in GUIDED, about 10 seconds later you'll see this:
BATT_FS_LOW_ACT = 2
SIM_BATT_VOLTAGE = 10.3
Mod: GUIDED
Mod: RTL
Mesaj: Battery 1 is low 10.30V used 232 mAh
Mesaj: Battery FailsafeThe mAh number depends on how long you've flown up to that moment. If you delete the BATT_FS_LOW_ACT line and repeat the same test with the value 0, both messages still appear, but the mode stays GUIDED.
Error messages and their meanings
Battery 1 is low 10.30V used 303 mAh
The voltage stayed below BATT_LOW_VOLT for longer than BATT_LOW_TIMER. The numbers are the current voltage and the capacity used. The action that follows depends on BATT_FS_LOW_ACT.
Battery Failsafe
The failsafe triggered. If the mode didn't change, BATT_FS_LOW_ACT is at 0. Set it to 1 or 2, release the latch and try again.
PreArm: Battery 1 low voltage failsafe
The drone is on the ground and the voltage is currently below the threshold. This check has no timer; it looks at the instantaneous voltage. Raise SIM_BATT_VOLTAGE above the threshold.
PreArm: Battery failsafe
The failsafe triggered once on this flight and is latched. Even if the voltage recovers, ARM permission won't come. Restart SITL or send a battery reset command.
More in the simulation course
Where the latch lives in the source code, the in-flight retry command, the voltage curve of a battery draining by capacity, and the log signature are covered in the Let's run simulations section of the AI and Drone Simulation course, alongside the swarm and wind scenarios.
Virtual Machine Image for AI and Drone Simulation: a virtual machine that comes with ArduPilot SITL, ROS Noetic and Gazebo 11 installed.
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