Skip to content

Commit

Permalink
Update int_res_est_replay.py (#23351)
Browse files Browse the repository at this point in the history
Pulls cell count, min voltage and max voltage from log file but still allows for over-rides. Also added debug info to tell user what what it found in the log and what it is using

Co-authored-by: chfriedrich98 <[email protected]>
  • Loading branch information
ryanjAA and chfriedrich98 authored Jul 5, 2024
1 parent a1f4363 commit fd8df2e
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/lib/battery/int_res_est_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def getData(log, topic_name, variable_name, instance=0):
def us2s(time_us):
return time_us * 1e-6

def getParam(log, param_name):
if param_name in log.initial_parameters:
return log.initial_parameters[param_name]
else:
print(f"Parameter {param_name} not found in log.")
return None

def rls_update(theta, P, x, V, I, lam):
gamma = P @ x / (lam + x.T @ P @ x)
error = V - x.T @ theta
Expand All @@ -33,6 +40,31 @@ def rls_update(theta, P, x, V, I, lam):

def main(log_name, n_cells, full_cell, empty_cell, lam):
log = ULog(log_name)

log_n_cells = getParam(log, 'BAT1_N_CELLS')
log_full_cell = getParam(log, 'BAT1_V_CHARGED')
log_empty_cell = getParam(log, 'BAT1_V_EMPTY')

# Debug information
print(f"Extracted from log - BAT1_N_CELLS: {log_n_cells}, BAT1_V_CHARGED: {log_full_cell}, BAT1_V_EMPTY: {log_empty_cell}")

# Use log parameters unless overridden
if n_cells is None:
n_cells = log_n_cells
else:
print(f"Using override for n_cells: {n_cells}")
if full_cell is None:
full_cell = log_full_cell
else:
print(f"Using override for full_cell: {full_cell}")
if empty_cell is None:
empty_cell = log_empty_cell
else:
print(f"Using override for empty_cell: {empty_cell}")

# Debug information for final parameter values
print(f"Using parameters - n_cells: {n_cells}, full_cell: {full_cell}, empty_cell: {empty_cell}")

timestamps = us2s(getData(log, 'battery_status', 'timestamp'))
I = getData(log, 'battery_status', 'current_a')
V = getData(log, 'battery_status', 'voltage_v')
Expand Down Expand Up @@ -79,7 +111,7 @@ def main(log_name, n_cells, full_cell, empty_cell, lam):
data_cov_hist[index] = data_cov
internal_resistance_stable[index] = max(R_est[index]/n_cells, 0.001)

## Plot data
# Plot data
print("Internal Resistance mean (per cell): ", np.mean(R_est) / n_cells)

# Summary plot
Expand Down Expand Up @@ -168,9 +200,9 @@ def main(log_name, n_cells, full_cell, empty_cell, lam):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Estimate battery parameters from ulog file.')
parser.add_argument('-f', type = str, required = True, help = 'Full path to ulog file')
parser.add_argument('-c', type = float, required = True, help = 'Number of cells in battery')
parser.add_argument('-u', type = float, required = False, default = 4.05, help = 'Full cell voltage')
parser.add_argument('-e', type = float, required = False, default = 3.6, help = 'Empty cell voltage')
parser.add_argument('-c', type = float, required = False, help = 'Number of cells in battery')
parser.add_argument('-u', type = float, required = False, default = None, help = 'Full cell voltage')
parser.add_argument('-e', type = float, required = False, default = None, help = 'Empty cell voltage')
parser.add_argument('-l', type = float, required = False, default = 0.99, help = 'Forgetting factor')
args = parser.parse_args()
main(args.f, args.c, args.u, args.e, args.l)

0 comments on commit fd8df2e

Please sign in to comment.