Skip to content

Commit

Permalink
AP_Scripting: add bindings for fence
Browse files Browse the repository at this point in the history
  • Loading branch information
IamPete1 authored and tridge committed Nov 6, 2023
1 parent ff3925a commit ae2ab08
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libraries/AP_Scripting/docs/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3032,3 +3032,19 @@ function mavlink:send_chan(chan, msgid, message) end
-- Block a given MAV_CMD from being procceced by ArduPilot
---@param comand_id integer
function mavlink:block_command(comand_id) end

-- Geofence library
---@class fence
fence = {}

-- Returns the time at which the current breach started
---@return uint32_t_ud system_time milliseconds
function fence:get_breach_time() end

-- Returns the type bitmask of any breached fences
---@return integer fence_type bitmask
---| 1 # Maximim altitude
---| 2 # Circle
---| 4 # Polygon
---| 8 # Minimum altitude
function fence:get_breaches() end
39 changes: 39 additions & 0 deletions libraries/AP_Scripting/examples/FenceBreach.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- Example of checking and reporting on geo-fence breach

local breach_lookup = {
[1] = "Maximim altitude",
[2] = "Circle",
[4] = "Polygon",
[8] = "Minimum altitude"
}

-- Lookup brach type bitmask and return the names of any breached fences
local function get_breach_names(breaches)
local msg = ""
for bitmaks, name in pairs(breach_lookup) do
if (breaches & bitmaks) ~= 0 then
-- And + delimiter between types if more than one
if (string.len(msg) > 0) then
msg = msg .. " + "
end
msg = msg .. name
end
end

return msg
end

function update()

local breaches = fence:get_breaches()
if breaches ~= 0 then
-- Time passed since fence breach
local breach_time = millis() - fence:get_breach_time()

gcs:send_text(0, string.format("Breached: %s fence, outside for %0.2f seconds", get_breach_names(breaches), breach_time:tofloat() * 0.001))
end

return update, 1000
end

return update()
7 changes: 7 additions & 0 deletions libraries/AP_Scripting/generator/description/bindings.desc
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,10 @@ singleton mavlink manual register_rx_msgid lua_mavlink_register_rx_msgid 1
singleton mavlink manual send_chan lua_mavlink_send_chan 3
singleton mavlink manual receive_chan lua_mavlink_receive_chan 0
singleton mavlink manual block_command lua_mavlink_block_command 1

include AC_Fence/AC_Fence.h depends AP_FENCE_ENABLED
include AC_Fence/AC_Fence_config.h
singleton AC_Fence depends AP_FENCE_ENABLED
singleton AC_Fence rename fence
singleton AC_Fence method get_breaches uint8_t
singleton AC_Fence method get_breach_time uint32_t

0 comments on commit ae2ab08

Please sign in to comment.