@@ -57,6 +57,25 @@ def close_log(mlog: mavutil.mavfile) -> None:
5757 mlog .close ()
5858
5959
60+ class BatteryData : # pylint: disable=too-many-instance-attributes,too-few-public-methods
61+ """Stores battery telemetry data extracted from BAT log messages."""
62+
63+ # BCL will be added later during the Phy Validation
64+
65+ def __init__ (self ) -> None :
66+ self .time_us : list [int ] = []
67+ self .volt : list [float ] = []
68+ self .volt_r : list [float ] = []
69+ self .curr : list [float ] = []
70+ self .curr_tot : list [float ] = []
71+ self .enrg_tot : list [float ] = []
72+ self .temp : list [float ] = []
73+ self .res : list [float ] = []
74+ self .rem_pct : list [int ] = []
75+ self .health : list [int ] = []
76+ self .state_health : list [int ] = []
77+
78+
6079class LogData : # pylint: disable=too-few-public-methods
6180 """Contains all data extracted from an ArduPilot .bin log."""
6281
@@ -66,6 +85,8 @@ def __init__(self) -> None:
6685 self .current_params : dict [str , float ] = {}
6786 self .firmware_info : tuple [str , int , int , int ] | None = None
6887 self .frame_type : int | None = None
88+ # There could be multiple batteries in the vehicle, so create a separate dict for them.
89+ self .batteries : dict [int , BatteryData ] = {}
6990
7091
7192class LogReader : # pylint: disable=too-few-public-methods
@@ -108,6 +129,11 @@ def extract_log(self) -> LogData:
108129 # Fallback to MSG if version is not available.
109130 elif msg_type == "MSG" :
110131 firmware_from_msg = process_msg_version_fallback (msg , firmware_from_msg )
132+
133+ # Extract the BAT messages and store them in BatteryData
134+ elif msg_type == "BAT" :
135+ process_bat (msg , log_data )
136+
111137 if firmware_from_ver is not None :
112138 log_data .firmware_info = firmware_from_ver
113139 else :
@@ -121,6 +147,34 @@ def extract_log(self) -> LogData:
121147 return log_data
122148
123149
150+ def process_bat (msg : mavutil .mavfile , log_data : LogData ) -> None :
151+ """
152+ Extract battery telemetry from a BAT DataFlash log entry.
153+
154+ Args:
155+ msg: A BAT log entry parsed from an ArduPilot .bin file.
156+ log_data: The LogData instance to write battery data into.
157+
158+ """
159+ # If there are multiple battery instances store them separately
160+ inst = int (msg .Inst )
161+ if inst not in log_data .batteries :
162+ log_data .batteries [inst ] = BatteryData ()
163+ battery = log_data .batteries [inst ]
164+
165+ battery .time_us .append (int (msg .TimeUS ))
166+ battery .volt .append (float (msg .Volt ))
167+ battery .volt_r .append (float (msg .VoltR ))
168+ battery .curr .append (float (msg .Curr ))
169+ battery .curr_tot .append (float (msg .CurrTot ))
170+ battery .enrg_tot .append (float (msg .EnrgTot ))
171+ battery .temp .append (float (msg .Temp ))
172+ battery .res .append (float (msg .Res ))
173+ battery .rem_pct .append (int (msg .RemPct ))
174+ battery .health .append (int (msg .H ))
175+ battery .state_health .append (int (msg .SH ))
176+
177+
124178def process_param (msg : mavutil .mavfile , log_data : LogData ) -> None :
125179 """
126180 Validate and store a single PARM message into log_data's parameter dicts.
0 commit comments