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
- Patch - see geofence-arducopter.diff, patch on "latest" ardupilot-mega source (circa Apr 25).
A bit easier, you will still need to know how to upload code to the board
- Download and flash this ArduCopter.hex file
#define GEOFENCE_ENABLED ENABLED
#define FENCE_CHANNEL 7
#define RESET_SWITCH_CHAN 8
#define FENCE_ACTION FENCE_ACTION_GUIDED
#define FENCE_MINALT 20
#define FENCE_MAXALT 100 // meters
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.