Skip to content

Commit

Permalink
AP_Scripting: lua bindings for scripted notches
Browse files Browse the repository at this point in the history
  • Loading branch information
andyp1per committed Nov 30, 2024
1 parent 07910ac commit 600851e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
33 changes: 32 additions & 1 deletion libraries/AP_Scripting/docs/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ local Parameter_ud = {}
---@return Parameter_ud
function Parameter() end

-- Set the defualt value of this parameter, if the parameter has not been configured by the user its value will be updated to the new defualt.
-- Set the default value of this parameter, if the parameter has not been configured by the user its value will be updated to the new defualt.
---@param value number
---@return boolean
function Parameter_ud:set_default(value) end
Expand Down Expand Up @@ -1917,6 +1917,37 @@ function ins:get_gyro(instance) end
---@return Vector3f_ud
function ins:get_accel(instance) end

-- desc
---@class AP_InertialSensor__HarmonicNotch_ud
local AP_InertialSensor__HarmonicNotch_ud = {}

-- Get a specific scripting enabled harmonic notch instance
---@param instance integer -- the 0-based index of the harmonic notch instance to return.
---@return AP_InertialSensor__HarmonicNotch_ud|nil
function ins:get_harmonic_notch(instance) end

-- get array field
---@param index integer
---@return number
function AP_InertialSensor__HarmonicNotch_ud:get_frequency(index) end

-- set array field
---@param index integer
---@param value number
function AP_InertialSensor__HarmonicNotch_ud:set_frequency(index, value) end

-- get field
---@return integer
function AP_InertialSensor__HarmonicNotch_ud:get_num_frequencies() end

-- set field
---@param value integer
function AP_InertialSensor__HarmonicNotch_ud:set_num_frequencies(value) end

-- desc
---@param value number
function AP_InertialSensor__HarmonicNotch_ud:update_frequency(value) end

-- desc
Motors_dynamic = {}

Expand Down
9 changes: 9 additions & 0 deletions libraries/AP_Scripting/generator/description/bindings.desc
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,15 @@ singleton AP_InertialSensor method calibrating boolean
singleton AP_InertialSensor method get_gyro Vector3f uint8_t'skip_check
singleton AP_InertialSensor method get_accel Vector3f uint8_t'skip_check
singleton AP_InertialSensor method gyros_consistent boolean uint8_t'skip_check
ap_object AP_InertialSensor::HarmonicNotch depends AP_INERTIALSENSOR_HARMONICNOTCH_ENABLED
singleton AP_InertialSensor method get_harmonic_notch AP_InertialSensor::HarmonicNotch uint8_t 0 HAL_INS_NUM_HARMONIC_NOTCH_FILTERS

ap_object AP_InertialSensor::HarmonicNotch method set_frequency void uint8_t 0 INS_MAX_NOTCHES float'skip_check
ap_object AP_InertialSensor::HarmonicNotch method get_frequency float uint8_t 0 INS_MAX_NOTCHES
ap_object AP_InertialSensor::HarmonicNotch method set_num_frequencies void uint8_t 0 INS_MAX_NOTCHES
ap_object AP_InertialSensor::HarmonicNotch method get_num_frequencies uint8_t
ap_object AP_InertialSensor::HarmonicNotch method update_freq_hz void float'skip_check
ap_object AP_InertialSensor::HarmonicNotch method update_freq_hz rename update_frequency

singleton CAN manual get_device lua_get_CAN_device 1 1
singleton CAN manual get_device2 lua_get_CAN_device2 1 1
Expand Down
45 changes: 45 additions & 0 deletions libraries/AP_Scripting/tests/static_notches.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- static_notches.lua: use notch scripting to set several static notches
--

local PARAM_TABLE_KEY = 35
local PARAM_TABLE_PREFIX = "SNOTCH"
local SNTCH_NUM_NOTCHES = 8
local SNTCH_NUM_HARMONIC_NOTCHES = 2

local notches = {}

param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, SNTCH_NUM_NOTCHES)

for idx = 1, SNTCH_NUM_NOTCHES, 1 do
param:add_param(PARAM_TABLE_KEY, idx, idx .. "_FREQ", 0)
notches[idx-1] = Parameter(PARAM_TABLE_PREFIX .. idx .. "_FREQ")
end


function update()
for n = 0, SNTCH_NUM_HARMONIC_NOTCHES-1, 1 do
local notch = ins:get_harmonic_notch(n)

if notch == nil then
goto continue
end
local nfreqs = 0
for idx = 0, SNTCH_NUM_NOTCHES-1, 1 do
if notches[idx] ~= nil and notches[idx]:get() ~= nil then
local f = notches[idx]:get()
if f ~= 0 then
if f ~= notch:get_frequency(idx) then
print("HNTC" .. n+1 .."(" .. idx + 1 .. ") set to " .. f .. " (was " .. notch:get_frequency(idx) .. ")")
notch:set_frequency(idx, f)
end
nfreqs = nfreqs + 1
end
end
end
notch:set_num_frequencies(nfreqs)
::continue::
end
return update, 500
end

return update()

0 comments on commit 600851e

Please sign in to comment.