Created
September 19, 2019 22:59
-
-
Save cjlawson02/a49996a6c938e34f1618f049dfc95f6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
import time | |
max_ah = 18 # Maximum Amp Hours of the batteries | |
dt = 0.01 # Time delta for graph points | |
res = 0.5 / 1000 # Internal Resistance | |
tol = 0.01 # Tolerance/Delta between the 2 battery voltages before stopping | |
# Ask user for state of charge percentage | |
batt1_soc = int(input('Battery 1 Charge %: ')) | |
batt2_soc = int(input('Battery 2 Charge %: ')) | |
# Convert SOC to decimal | |
batt1_soc /= 100 | |
batt2_soc /= 100 | |
# Return the voltage from the state of charge using a linearized equation | |
def calcVoltageFromSOC(soc): | |
return soc + 11.8 | |
# Return the voltage from a capacity | |
def calcVoltageFromCapacity(cap): | |
return calcVoltageFromSOC((cap / 3600) / max_ah) | |
# Return the current from 2 voltages using internal resistance | |
def calcCurrentFromVoltages(v1, v2): | |
return (v1 - v2) / (2 * res) | |
# Calculate the initial capacity from SOC | |
def calcInitialCapacityFromSOC(soc): | |
return (max_ah * soc) * 3600 | |
# Return the last value in a list | |
def lastIndex(indexlist): | |
try: | |
return indexlist[-1] | |
except IndexError: | |
return 0 | |
# Initialize initial values | |
batt1_voltage = calcVoltageFromSOC(batt1_soc) | |
batt2_voltage = calcVoltageFromSOC(batt2_soc) | |
batt1_capacity = calcInitialCapacityFromSOC(batt1_soc) | |
batt2_capacity = calcInitialCapacityFromSOC(batt2_soc) | |
currentList = [] | |
batt1_voltageList = [] | |
batt2_voltageList = [] | |
timeList = [] | |
done = False | |
# Main loop to calculate current and voltage | |
while (done == False): | |
currentList.append(calcCurrentFromVoltages(batt1_voltage, batt2_voltage)) | |
batt1_voltageList.append(batt1_voltage) | |
batt2_voltageList.append(batt2_voltage) | |
timeList.append(lastIndex(timeList) + dt) | |
if (batt1_voltage > batt2_voltage): | |
batt1_capacity -= calcCurrentFromVoltages(batt1_voltage, batt2_voltage) * dt | |
batt2_capacity += calcCurrentFromVoltages(batt1_voltage, batt2_voltage) * dt | |
else: | |
batt1_capacity += calcCurrentFromVoltages(batt1_voltage, batt2_voltage) * dt | |
batt2_capacity -= calcCurrentFromVoltages(batt1_voltage, batt2_voltage) * dt | |
batt1_voltage = calcVoltageFromCapacity(batt1_capacity) | |
batt2_voltage = calcVoltageFromCapacity(batt2_capacity) | |
if (abs(batt1_voltage - batt2_voltage) < tol): | |
done = True | |
# Current Plot | |
plt.subplot(2, 1, 1) | |
plt.title("Battery Current/Voltage over Time") | |
plt.plot(timeList, currentList, label='Battery Current') | |
plt.ylabel('Current (a)') | |
plt.legend() | |
# Voltage Plot | |
plt.subplot(2, 1, 2) | |
plt.plot(timeList, batt1_voltageList, label='Battery 1 Voltage') | |
plt.plot(timeList, batt2_voltageList, label='Battery 2 Voltage') | |
plt.xlabel('Time (s)') | |
plt.ylabel('Voltage (v)') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment