Skip to content

Commit

Permalink
Update int_res_est_replay.py
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
  • Loading branch information
ryanjAA authored Jul 2, 2024
1 parent f65653a commit b5e7546
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 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 @@ -31,8 +38,34 @@ def rls_update(theta, P, x, V, I, lam):
return theta_corr, P_corr, error, data_cov, 0, 0
return theta_temp, P_temp, error, data_cov, gamma[0], gamma[1]


def main(log_name, n_cells, full_cell, empty_cell, lam, filtered):
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'))
if (filtered):
I = getData(log, 'battery_status', 'current_filtered_a')
Expand Down Expand Up @@ -83,7 +116,7 @@ def main(log_name, n_cells, full_cell, empty_cell, lam, filtered):
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 @@ -172,10 +205,10 @@ def main(log_name, n_cells, full_cell, empty_cell, lam, filtered):
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')
parser.add_argument('-d', type = bool, required = False, default = False, help = 'Filter measurements')
parser.add_argument('-d', type = bool, required = False, default = False, help = 'Filter measurements')
args = parser.parse_args()
main(args.f, args.c, args.u, args.e, args.l, args.d)

0 comments on commit b5e7546

Please sign in to comment.