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: 9283

Comment by Ellison Chan on April 28, 2012 at 4:43pm

Good job, Andreas.  I was just thinking my self of adding a altitude ceiling to the code, but you beat me to it.  I'm sure there's some work to be done yet, but once we get this working, it will be a great help for those who are trying to get certification from various governments.

3D Robotics
Comment by Chris Anderson on April 28, 2012 at 5:06pm

Great stuff! Can't wait to try this with a well-tuned quad.

Comment by Chris Huitema on April 28, 2012 at 6:16pm

this is one of the things im most scared of, especially after loosing hundreds of quads in simulators! cant wait to be able to rely on this

Comment by Dror Caspi on April 28, 2012 at 9:14pm

Excellent feature.  Your scenario of "how a quad gets lost" describes exactly what happened to me about a year ago - except it got lost so well I never found it again.

But wouldn't the existing RTL work in such cases?  You lose control, switch RTL on and let it come back to you?

Comment by Chris Huitema on April 28, 2012 at 10:31pm

I needed to upload this today before i went down to the park - its a very windy day in perth today 14knot winds gusting to 17knots (26km/h to 32km/h)

Loiter was working well till the human magnet of a quad started drawing people in from miles around, back to manual and keep it away from onlookers, and a little higher and oh crap the wind is strong up there! panic panic managed to catch it with a tree! and surprisingly little damage - 3 props smashed,1 motor ripped out the 2mm thick aluminium tube it was bolted to and 1 motor full of dirt

Youtube video coming up - 4min and 45sec of flight - 8min of trying to find it in the tree (From GPS position and video feed) and 2min to get it out of the tree (using the last prop to spin it free from a branch)

Comment by Chris Huitema on April 29, 2012 at 12:17am

This is why this is a great idea! I completely forgot about RTL once it started to get away. A geo-fence should always be active in my opinion

Comment by Dror Caspi on April 29, 2012 at 12:42am

In my mind RTL should be the panic switch (possibly CH7).  It should also be automatically activated when the battery is low.

Comment by Andreas M. Antonopoulos on April 29, 2012 at 1:23am

Agreed Dror, but this is the same thing. The center of the fence is the RTL. The only difference is that RTL requires human action and in-range radio. This is always-on and works without any human action. Think of this as RTL2.0 or auto-RTL ;-)

PS. In case it wasn't clear from the blog entry - I didn't write this awesome code - I simply ported it to ArduCopter, so props  (pun not intended, but appropriate) to Andrew Tridgell and any others (?) who did the original work. 

Comment by Chris Huitema on April 29, 2012 at 1:34am

Where does the Ch 7 come into it then?

Comment by Chris Huitema on April 29, 2012 at 1:36am

and does it use the PID's for loiter and RTL?


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