Created
May 3, 2024 21:06
-
-
Save chaselambda/8364b0891dd128a6b723495ae4fd2c02 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
# Hello | |
# Yay! | |
# TODO maybe put units in variables | |
from collections import namedtuple | |
from collections import defaultdict | |
BatteryModule = namedtuple('BatteryModule', ['bank_id', 'string_id', 'module_id', 'voltage', 'temp']) | |
ModuleId = namedtuple('BatteryModule', ['bank_id', 'string_id', 'module_id']) | |
StringId | |
BankId | |
# VoltageSummary = namedtuple('VoltageSummary', ['overall_voltage', 'bank_voltages', 'string_voltages']) | |
# bank_voltages: map<BankId, string_voltage> | |
# string_voltages: map<StringId, string_voltage> | |
# module_voltages: map<ModuleId, string_voltage> | |
class BMSAlert: | |
temp_alerts = [] | |
voltage_alerts = [] | |
def __init__(self, temp_alerts, voltage_alerts): | |
self.temp_alerts = temp_alerts | |
self.voltage_alerts = voltage_alerts | |
class BMS: | |
banks = {} # Map of map of modules | |
max_voltage = 0 | |
min_voltage = 0 | |
max_temp = 0 | |
# TODO maybe update max/min voltage | |
def __init__(self, max_voltage, min_voltage, max_temp): | |
self.max_voltage = max_voltage | |
self.min_voltage = min_voltage | |
self.max_temp = max_temp | |
def get_overall_voltage(self): | |
bank_voltages = [] | |
for bank in self.banks.values(): | |
string_voltages = [] | |
for string in bank.values(): | |
string_voltage = sum([module.voltage for module in string.values()]) | |
string_voltages.append(string_voltage) | |
bank_voltage = sum(string_voltages) / len(string_voltages) | |
bank_voltages.append(bank_voltage) | |
overall_voltage = sum(bank_voltages) / len(bank_voltages) | |
return overall_voltage | |
def get_bank_voltages(self): | |
bank_voltages = {} | |
for bank_id, bank in self.banks.items(): | |
string_voltages = [] | |
for string in bank.values(): | |
string_voltage = sum([module.voltage for module in string.values()]) | |
string_voltages.append(string_voltage) | |
bank_voltage = sum(string_voltages) / len(string_voltages) | |
bank_voltages[bank_id] = bank_voltage | |
return bank_voltages | |
def update(self): | |
self.banks = {} | |
skip_first_line = True | |
with open('/Users/chase/Downloads/batteries.csv', 'r') as f: | |
for line in f.readlines(): | |
if skip_first_line: | |
skip_first_line = False | |
continue | |
(bank_id, string_id, module_id, voltage, temp) = line.split(',') | |
module = BatteryModule(int(bank_id), int(string_id), int(module_id), float(voltage), float(temp)) | |
if bank_id not in self.banks: | |
self.banks[bank_id] = {} | |
if string_id not in self.banks[bank_id]: | |
self.banks[bank_id][string_id] = {} | |
self.banks[bank_id][string_id][module_id] = module | |
def get_alerts(self): | |
temp_alerts = [] | |
voltage_alerts = [] | |
for bank in self.banks.values(): | |
for string in bank.values(): | |
for module in string.values(): | |
if module.voltage > self.max_voltage: | |
voltage_alerts.append(module) | |
if module.voltage < self.min_voltage: | |
voltage_alerts.append(module) | |
if module.temp > self.max_temp: | |
temp_alerts.append(module) | |
return BMSAlert(temp_alerts, voltage_alerts) | |
# TODO think about graphing | |
bms = BMS(14, 8, 40) | |
bms.update() | |
print('overall voltage', bms.get_overall_voltage()) | |
alerts = bms.get_alerts() | |
for alert in alerts.voltage_alerts: | |
print('voltage alert', alert) | |
for alert in alerts.temp_alerts: | |
print('temp alert', alert) | |
for bank_id, bank_voltage in bms.get_bank_voltages().items(): | |
print('bank_id: {}, volatage: {}'.format(bank_id, bank_voltage)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment