Softivation

A geofence breach doesn't trigger RTL in SITL

The geofence is defined in SITL, but no breach happens and RTL_AND_LAND doesn't trigger. FENCE_TYPE isn't enough: while FENCE_ENABLE is off, the autopilot doesn't check the boundary at all.

Furkan IŞIKAY 4 min read

In SITL you deliberately fly the drone 400 meters away from home. The geofence appears defined on screen, but nothing happens: the mode doesn't change and no warning comes.

The autopilot behaves as if there were no fence at all. Usually the problem isn't the fence's shape but whether it's enabled.

The fence may be defined but disabled

In ArduPilot the fence has two separate settings. One selects which boundary types are defined: altitude limit, circular boundary, polygon boundary. The other is the master switch that turns all of them on and off at once.

The master switch is off by default. Even if boundary types appear defined, no breach counts while the switch is off, because the autopilot never runs the breach check.

With the switch off and on

The two panels in the figure show the same situation: the drone is 400 meters from home, and the fence is a 150-meter circle. On the left the switch is off, on the right it's on.

Same fence, same drone position; the only thing that changes is the master switch

Left, FENCE_ENABLE 0: a 150-meter circle is defined, the drone is at 400 meters, and the mode stays GUIDED. Right, FENCE_ENABLE 1: at the same position the autopilot switches to LAND mode.
Where to look: the drone's position and the fence are the same in both panels. The dashed circle on the left is a fence that is defined but disabled. On the right, once the switch is on, the same position counts as a breach.

If you fly the drone to 400 meters with the switch off, no breach message comes and the mode stays in GUIDED. If you turn the switch on at the same position, the autopilot switches to LAND mode within a few seconds and reports the breach with a MAVLink message. Seeing LAND instead of RTL is normal: if you're more than 100 meters outside the fence, the autopilot goes straight to LAND without trying RTL. At 400 meters, that's the case.

Setting names and defaults

FENCE_TYPE selects the boundary types; the master switch is FENCE_ENABLE. The definition in AC_Fence.cpp is clear: FENCE_ENABLE's default value is 0, meaning off.

On Copter, FENCE_TYPE carries three bits at once by default: ALT_MAX(1) + CIRCLE(2) + POLYGON(4) = 7. Seeing this 7 on screen doesn't mean the fence is enabled.

Example: You set the fence radius to 150 meters to match the flight area, the parameter list shows FENCE_TYPE 7, and you think the fence is ready. When the drone leaves the area nothing happens, because the master switch is still at 0. In the list, look at the FENCE_ENABLE row instead of FENCE_TYPE. If it isn't 1, there is no fence.

Solution

Write the four parameters over MAVLink with PARAM_SET and confirm each one with the returned PARAM_VALUE. The code below writes FENCE_TYPE=2 (circle only), FENCE_RADIUS=150, and FENCE_ACTION=1 (RTL_AND_LAND), and sets FENCE_ENABLE to 1 last.

Writing FENCE_ENABLE last matters. With the type and action already defined, the breach check kicks in the moment you turn the switch on. Run the code while SITL is running.

Python
from pymavlink import mavutil

m = mavutil.mavlink_connection('tcp:127.0.0.1:5760')
m.wait_heartbeat()

for name, val in [('FENCE_TYPE', 2), ('FENCE_RADIUS', 150),
                   ('FENCE_ACTION', 1), ('FENCE_ENABLE', 1)]:
    m.mav.param_set_send(m.target_system, m.target_component,
                          name.encode(), float(val),
                          mavutil.mavlink.MAV_PARAM_TYPE_REAL32)
    print(m.recv_match(type='PARAM_VALUE', blocking=True, timeout=5))

For each line you should see a PARAM_VALUE carrying the name and value of the parameter you wrote. If the drone is outside the fence at that moment, the mode change follows right after.

Error Messages

"PreArm: Vehicle breaching Circle fence"

This exact message arrives while FENCE_ENABLE is on and the drone is outside the boundary. Its source is the breach check in AC_Fence.cpp: hal.util->snprintf(failure_msg, failure_msg_len, "Vehicle breaching %s", ...).

More in the course

In the AI and Drone Simulation Course we go step by step through this logic, all the way to the polygon upload step, log signatures, and field differences.

AI and Drone Simulation Virtual Machine: comes with ArduPilot SITL, ROS Noetic, and Gazebo 11 installed. You start simulating without any setup hassle.

Was this article helpful?

Free resourceArduPilot SITL setup guideSet up the simulation on your own computer: Ubuntu, Windows WSL, or a virtual machine, start to finish.