@@ -40,6 +40,15 @@ local BTAG_ENABLE = bind_add_param('ENABLE', 1, 1)
4040--]]
4141local 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+
4352if BTAG_ENABLE :get () == 0 then
4453 return
4554end
@@ -56,6 +65,14 @@ local auth_id = arming:get_aux_auth_id()
5665
5766local 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 )
117141end
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+
119163local function update ()
120164 if BTAG_ENABLE :get () ~= 0 then
121165 check_batterytag ()
122166 check_globaltime ()
167+ check_GCS ()
123168 end
124169 return update , 200
125170end
0 commit comments