Skip to content

Commit 55c8eee

Browse files
committed
AP_Scripting: added BTAG_CUR_CYCLES
this can be used by another battery estimation lua script to adjust for max number of cycles across all batteries
1 parent 21a73eb commit 55c8eee

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

libraries/AP_Scripting/applets/BatteryTag.lua

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ local BTAG_ENABLE = bind_add_param('ENABLE', 1, 1)
4040
--]]
4141
local BTAG_MAX_CYCLES = bind_add_param('MAX_CYCLES', 2, 400)
4242

43+
--[[
44+
// @Param: BTAG_CUR_CYCLES
45+
// @DisplayName: current battery cycles
46+
// @Description: this is the highest value for battery cycles for all connected batteries
47+
// @Range: 0 10000
48+
// @User: Advanced
49+
--]]
50+
local BTAG_CUR_CYCLES = bind_add_param('CUR_CYCLES', 3, 0)
51+
4352
if BTAG_ENABLE:get() == 0 then
4453
return
4554
end
@@ -56,6 +65,14 @@ local auth_id = arming:get_aux_auth_id()
5665

5766
local highest_cycles = 0
5867

68+
local node_cycles = {}
69+
70+
local gcs_connect_time = nil
71+
local sent_report = false
72+
73+
-- report battery tags to GCS at 30s after first GCS connection
74+
local GCS_REPORT_TIME_S = 30
75+
5976
--[[
6077
check for BatteryTag messages
6178
--]]
@@ -69,7 +86,14 @@ local function check_batterytag()
6986
return
7087
end
7188

72-
highest_cycles = math.max(num_cycles, highest_cycles)
89+
if num_cycles > highest_cycles then
90+
highest_cycles = num_cycles
91+
BTAG_CUR_CYCLES:set_and_save(highest_cycles)
92+
end
93+
if not node_cycles[nodeid] then
94+
gcs:send_text(MAV_SEVERITY.INFO, string.format("BatteryTag: Node %d, Cycles %d", nodeid, num_cycles))
95+
end
96+
node_cycles[nodeid] = num_cycles
7397

7498
-- log battery information
7599
logger:write("BTAG",
@@ -116,10 +140,31 @@ local function check_globaltime()
116140
globaltime_handle:broadcast(payload7)
117141
end
118142

143+
--[[
144+
update GCS connection status
145+
--]]
146+
local function check_GCS()
147+
if not gcs_connect_time then
148+
local last_seen = gcs:last_seen()
149+
if last_seen ~= 0 then
150+
gcs_connect_time = last_seen
151+
end
152+
elseif not sent_report and #node_cycles and
153+
millis() - gcs_connect_time >= GCS_REPORT_TIME_S*1000
154+
then
155+
-- report battery tags at GCS_REPORT_TIME_S seconds
156+
sent_report = true
157+
for nodeid, cycles in pairs(node_cycles) do
158+
gcs:send_text(MAV_SEVERITY.INFO, string.format("BatteryTag: Node %d, Cycles %d", nodeid, cycles))
159+
end
160+
end
161+
end
162+
119163
local function update()
120164
if BTAG_ENABLE:get() ~= 0 then
121165
check_batterytag()
122166
check_globaltime()
167+
check_GCS()
123168
end
124169
return update, 200
125170
end

libraries/AP_Scripting/applets/BatteryTag.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ Allow for enable/disable of the script
1717

1818
Maximum number of battery cycles to allow arming
1919

20+
## BTAG_CUR_CYCLES
21+
22+
Current maximum of number of cycles from all active BatteryTag
23+
nodes. This can be used by other scripts to adjust battery percentage
24+
estimation at boot based on age of the battery.

0 commit comments

Comments
 (0)