Geo-Fencing for ArduCopter - Keep your copter "fenced in"

ArduCopter now has experimental geo-fencing support, thanks to Andrew Tridgell's superb geo-fencing code for ArduPlane.


If you haven't heard of the geo-fencing features of ArduPlane, you are probably an ArduCopter user! Since December 2011, ArduPlane piltos have had the ability to build a virtual fence around an area and prohibit the aircraft from exit from the fence.

The details are in the GeoFencing wiki page.


So, now, this amazing feature is available for early-stage testing in ArduCopter. 


Why do I want geo-fencing?

All four of my quad crashes follow the same scenario: pilot error causes the quad to go a bit far. Attempts to bring it back make the situation worse. The quad gets caught by wind or moves too fast and starts getting away from the pilot and out -of-sight. Eventually, you have to force a crash by killing the motors while you can still see the quad.

Runaway Copter - no more

The images above show a software-in-the-loop simulation of Tridge's geo-fencing in action. The quad starts in the center of a large sports field, with a fence defined in a 50m radius around the center. A minimum altitude of 20m and a maximum altitude of 100m are set for the quad. 

In the simulation, the quad's throttle is turned up to 75% and left at that level. Then pitch is set to a very aggressive 20-deg forward angle. 

After liftoff, and once past the minimum altitude of the fence (20m), the fence is turned on by a switch on CH7 (preset in code).

The quad shoots straight up and right past the max altitude, because it has a lot of upward momentum. The fence triggers and switches the quad to guided mode to pull it back to the center (which is halfway altitude 100m-20m = 80m, and the point in the center of the fenced area). Because the throttle is still very high, the guided mode is able to stop it from climbing but can't quite pull it down. Easing off the throttle to about 45% makes the quad a bit more "tame". 

The quad then spends the next five hours screaming at 25m/s speeds towards the walls, while hovering just under the ceiling (at an average of 98m) and bouncing back and forth. The shape that is created makes the fence visible. 

Bottom line: Your quad cannot get away.

Get the code - Testing the geo-fence on ArduCopter (Experienced Coders)

As an experimental feature, you will need to be able to compile and upload code to the board without the Mission Planner (ie using Arduino IDE or command line). You will also need to (possibly) tweak some parameters to your liking.

Get the code using one of the following options:

  • GIT - git checkout -b geofence_arducopter --track origin/geofence_arducopter 
Compile and install!

Get the code - Testing the geo-fence on ArduCopter (for Adventurous Beginners)

A bit easier, you will still need to know how to upload code to the board

Use the geo-fencing in a flight

To test the geo-fencing for ArduCopter, it is easiest to use the default parameters. You can build a fence as with ArduPlane using Mission Planner to define the coordinates of the fence (any size closed polygon). 

The other default parameters are defined in APM_Config.h with this code at the end of the file:





#define FENCE_MINALT 20

#define FENCE_MAXALT 100 // meters

You can either tweak these in the code, or send them as MAVlink parameters I believe (not tested)

Automatic Fence

The new ArduCopter geo-fencing has one addition on the original ArduPlane code - the auto-fence! With automatic fencing, the code will create a 100m fence around your current location

If you have enabled geofencing (in the code) but have not defined the fence coordinates, the code will build a fence for you. You will need to have a GPS lock (reboot the board once you get a GPS lock) for this to work.

The FENCE_MAXALT is used to define the ceiling AND the diameter of the fence. So with FENCE_MAXALT at 100 meters, the default fence will be 100m wide, 100m tall. It is defined as an 18-sided polygon, with the center at the location you started (just like RTL, I believe) and the points defined at 20-degree intervals from 50m due north of your position, clockwise around your quad in a full circle (decaoctagon, more accurately). It looks like this:

You can tweak these parameters at startup, through Mission Planner or another MAVLink GCS. You can also use Mission Planner to define the geo-fencing points, just like in ArduPlane (have not tried this yet). See the ArduPlane geo-fencing instructions for more detail

Many many thanks to Andrew Tridgell for the superb geo-fencing idea and code.

Go forth, test and enjoy. Please give me feedback for the testing and development. 


Views: 9245

Comment by Bob Wilkinson on July 8, 2012 at 9:55pm

Did this make it into ArduCopter 2.6?

Comment by Jon C on July 17, 2012 at 3:06am

This would definitely help me avoid a repeat of this!

I see the latest version (1.1.98) of the APM planner now includes a Geofencing section along with a link to the comprehensive Wiki docs.  However when I try to activate it I get the message 'This feature is not enabled in your firmware' which I'm taking to mean this is still officially only an Arduplane option.

Any word on when this will make it into the official Arducopter firmware?

Thanks for all the hard work!


Comment by Justin on September 4, 2012 at 7:24pm
Hi, has Geofencing support made it into the ArduCopter code base or is the patch still required?
Comment by steve on February 6, 2013 at 1:12pm
Exactly the same question I was going to ask. :)


You need to be a member of DIY Drones to add comments!

Join DIY Drones

Season Two of the Trust Time Trial (T3) Contest 
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here

© 2020   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service