When working with geofences for a competition, I found myself in a problem of having several geofences with different actions that needed to be taken for each one. Additionally, drones needed to be light and fast, so they also didn’t have an onboard computer. To solve that, I decided to use Lua scripts, which would allow me to only have the controller equipped with an SD card in the Copter.
The geofence.lua
is based on the Winding Number algorithm. This algorithm was chosen because, despite being computationally heavier than others (Crossing Number algorithm, for example), at the scale we were at (polygons with few sides) it did not make a significant difference. The code also ran on the Pixhawk 6 controller itself and without memory or processing problems. To explain the logic behind the script we will use the article published by Dan Sunday
Basically, the code checks every N seconds if the drone is within the polygon delimited for each area and takes different actions for each situation. For example, in the competition we needed to land if we left a green zone and it was necessary to turn off the motors if we left the red zone.
We tested and used it in the competition and passed the safety check, showing that the drone took the appropriate actions when it was outside the competition area. Fortunately, after that, no fence was breached and no emergency action actually had to be taken!
Full explanation and code is here.