diff --git a/libraries/AP_Scripting/docs/docs.lua b/libraries/AP_Scripting/docs/docs.lua index afd113d5f4c01..5fe0593f11139 100644 --- a/libraries/AP_Scripting/docs/docs.lua +++ b/libraries/AP_Scripting/docs/docs.lua @@ -3147,6 +3147,11 @@ function battery:current_amps(instance) end ---@return number -- resting voltage function battery:voltage_resting_estimate(instance) end +-- Returns the estimated internal battery resistance in Ohms +---@param instance integer -- battery instance +---@return number -- estimated internal resistance in Ohms +function battery:get_resistance(instance) end + -- Returns the voltage of the selected battery instance. ---@param instance integer -- battery instance ---@return number -- voltage diff --git a/libraries/AP_Scripting/examples/battery_internal_resistance_check.lua b/libraries/AP_Scripting/examples/battery_internal_resistance_check.lua new file mode 100644 index 0000000000000..63a811e2e2a57 --- /dev/null +++ b/libraries/AP_Scripting/examples/battery_internal_resistance_check.lua @@ -0,0 +1,35 @@ +--[[ + script implementing pre-arm check that internal resistance is sensible +--]] + +local MAX_RESISTANCE = 0.03 -- Ohms + +local auth_id = assert(arming:get_aux_auth_id()) + +local warning_last_sent_ms = uint32_t() -- time we last sent a warning message to the user +warning_interval_ms = 10000 + +function update() + local num_batts = battery:num_instances() + local ok = true + for i=0,num_batts do + local resistance = battery:get_resistance(i) + failed = resistance > MAX_RESISTANCE + if failed then + msg = string.format("Batt[%u] high internal resistance %.5f Ohms", i+1, resistance) + if millis() - warning_last_sent_ms > warning_interval_ms then + gcs:send_text(0, msg) + warning_last_sent_ms = millis() + end + arming:set_aux_auth_failed(auth_id, msg) + ok = false + end + end + + if ok then + arming:set_aux_auth_passed(auth_id) + end + return update, 500 +end + +return update() diff --git a/libraries/AP_Scripting/generator/description/bindings.desc b/libraries/AP_Scripting/generator/description/bindings.desc index 5528b04cf80b9..dbeca52779958 100644 --- a/libraries/AP_Scripting/generator/description/bindings.desc +++ b/libraries/AP_Scripting/generator/description/bindings.desc @@ -94,6 +94,7 @@ singleton AP_BattMonitor depends AP_BATTERY_ENABLED singleton AP_BattMonitor method num_instances uint8_t singleton AP_BattMonitor method healthy boolean uint8_t 0 ud->num_instances() singleton AP_BattMonitor method voltage float uint8_t 0 ud->num_instances() +singleton AP_BattMonitor method get_resistance float uint8_t 0 ud->num_instances() singleton AP_BattMonitor method voltage_resting_estimate float uint8_t 0 ud->num_instances() singleton AP_BattMonitor method current_amps boolean float'Null uint8_t 0 ud->num_instances() singleton AP_BattMonitor method consumed_mah boolean float'Null uint8_t 0 ud->num_instances()