diff --git a/config.py b/config.py index c1fe151c93fa92059622716e82c9fccfc6f611fa..94ee759854a8d52d8206340b6f43b7880e9746d3 100644 --- a/config.py +++ b/config.py @@ -14,18 +14,42 @@ logging_suffix = '' # OhmPi configuration OHMPI_CONFIG = { 'id': ohmpi_id, # Unique identifier of the OhmPi board (string) - 'R_shunt': 2, # Shunt resistance in Ohms - 'Imax': 4800 / 50 / 2, # Maximum current - 'coef_p2': 2.50, # slope for current conversion for ADS.P2, measurement in V/V - 'nb_samples': 20, # Max value 10 # was named integer before... - 'version': 2, # Is this still needed? - 'max_elec': 64, - 'board_addresses': {'A': 0x73, 'B': 0x72, 'M': 0x71, 'N': 0x70}, # CHECK IF YOUR BOARDS HAVE THESE ADDRESSES + # 'R_shunt': 2, # Shunt resistance in Ohms + # 'Imax': 4800 / 50 / 2, # Maximum current + # 'coef_p2': 2.50, # slope for current conversion for ADS.P2, measurement in V/V + # 'nb_samples': 20, # Max value 10 # was named integer before... + # 'version': 2, # Is this still needed? + # 'max_elec': 64, + # 'board_addresses': {'A': 0x73, 'B': 0x72, 'M': 0x71, 'N': 0x70}, # CHECK IF YOUR BOARDS HAVE THESE ADDRESSES 'settings': 'ohmpi_settings.json', # INSERT YOUR FAVORITE SETTINGS FILE HERE - 'board_version': 'mb.2023.0.0',#,'22.10', - 'mcp_board_address': 0x20 + # 'board_version': 'mb.2023.0.0',#,'22.10', + # 'mcp_board_address': 0x20 } # TODO: add a dictionary with INA models and associated gain values +HARDWARE_CONFIG = { + {'Controller': {'model' : 'raspberry_pi_3' + } + }, + {'TX' : {'model' : 'mb_2024_rev_0_0', + 'mcp_board_address': 0x20, + 'Imax': 4800 / 50 / 2, # Maximum current + 'R_shunt': 2 # Shunt resistance in Ohms + } + }, + {'RX' : {'model': 'mb_2024_rev_0_0', + 'coef_p2': 2.50, # slope for current conversion for ADS.P2, measurement in V/V + 'nb_samples': 20, # Max value 10 # was named integer before... + } + }, + {'MUX': {'model' : 'mux_2021', + 'max_elec': 64, + 'board_addresses': {'A': 0x73, 'B': 0x72, 'M': 0x71, 'N': 0x70}, # CHECK IF YOUR BOARDS HAVE THESE ADDRESSES + 'coef_p2': 2.50, # slope for current conversion for ADS.P2, measurement in V/V + 'nb_samples': 20 # Max value 10 # was named integer before... + } + } +} + # SET THE LOGGING LEVELS, MQTT BROKERS AND MQTT OPTIONS ACCORDING TO YOUR NEEDS # Execution logging configuration EXEC_LOGGING_CONFIG = { diff --git a/hardware/hardware.py b/hardware/hardware.py new file mode 100644 index 0000000000000000000000000000000000000000..64f305d009fc83da45117628200bf3258ea291cd --- /dev/null +++ b/hardware/hardware.py @@ -0,0 +1,65 @@ +from abc import ABC +import os +class ControllerAbstract(ABC): + def __init__(self, **kwargs): + self.bus = None + +class MuxAbstract(ABC): + pass + +class TxAbstract(ABC): + def __init__(self, **kwargs): + polarity = kwargs.pop('polarity', 1) + inj_time = kwargs.pop('inj_time', 1.) + exec_logger = kwargs.pop('exec_logger', None) + soh_logger = kwargs.pop('soh_logger', None) + self._polarity = None + self._inj_time = None + self.polarity = polarity + self.inj_time = inj_time + board_name = os.path.basename(__file__) + self.exec_logger.debug(f'TX {board_name} Initialized.') + + @property + def inj_time(self): + return self._inj_time + + @inj_time.setter + def inj_time(self, value): + assert isinstance(value, float) + self._inj_time = value + + @property + def polarity(self): + return self._polarity + + @polarity.setter + def polarity(self, value): + assert value in [-1,1] + self._polarity = value + # add actions to set the polarity (switch relays) + + def turn_off(self): + # add actions to turn the DPS off + pass + + def turn_on(self): + # add actions to turn the DPS on + pass + + @property + def voltage(self): + # add actions to read the DPS voltage and return it + return None + + @voltage.setter + def voltage(self, value, **kwargs): + # add actions to set the DPS voltage + pass + + def current(self, **kwargs): + pass + + +class RxAbstract(ABC): + pass diff --git a/hardware/mb_2024_rev_0_0.py b/hardware/mb_2024_rev_0_0.py new file mode 100644 index 0000000000000000000000000000000000000000..478e72111b8eb6d0a41bb3d797d8386f36488d95 --- /dev/null +++ b/hardware/mb_2024_rev_0_0.py @@ -0,0 +1,67 @@ +import importlib +from ..config import OHMPI_CONFIG +import adafruit_ads1x15.ads1115 as ads # noqa +from adafruit_ads1x15.analog_in import AnalogIn # noqa +from adafruit_mcp230xx.mcp23008 import MCP23008 # noqa +from digitalio import Direction # noqa +import minimalmodbus # noqa +import time +from hardware import TxAbstract, RxAbstract +controller_module = importlib.import_module(f'{OHMPI_CONFIG["hardware"]["controller"]["model"]}') +TX_CONFIG = OHMPI_CONFIG['hardware']['TX'] + +class TX(TxAbstract): + def __init__(self, **kwargs): + self.controller = kwargs.pop('controller', controller_module.Controller()) + super().__init__(**kwargs) + + # I2C connexion to MCP23008, for current injection + self.mcp_board = MCP23008(self.controller.bus, address=TX_CONFIG['mcp_board_address']) + self.pin4 = self.mcp_board.get_pin(4) # Ohmpi_run + self.pin4.direction = Direction.OUTPUT + self.pin4.value = True + + # ADS1115 for current measurement (AB) + self.ads_current = ads.ADS1115(self.controller.bus, gain=2/3, data_rate=860, address=0x48) + + # DPH 5005 Digital Power Supply + self.pin2 = self.mcp_board.get_pin(2) # dps + + self.pin2.direction = Direction.OUTPUT + self.pin2.value = True + self.pin3 = self.mcp_board.get_pin(3) # dps - + self.pin3.direction = Direction.OUTPUT + self.pin3.value = True + time.sleep(4) + self.DPS = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1) # port name, address (decimal) + self.DPS.serial.baudrate = 9600 # Baud rate 9600 as listed in doc + self.DPS.serial.bytesize = 8 # + self.DPS.serial.timeout = 1 # greater than 0.5 for it to work + self.DPS.debug = False # + self.DPS.serial.parity = 'N' # No parity + self.DPS.mode = minimalmodbus.MODE_RTU # RTU mode + self.DPS.write_register(0x0001, 1000, 0) # max current allowed (100 mA for relays) + # (last number) 0 is for mA, 3 is for A + + # self.soh_logger.debug(f'Battery voltage: {self.DPS.read_register(0x05,2 ):.3f}') TODO: SOH logger + print(self.DPS.read_register(0x05, 2)) + self.switch_dps('off') + + def turn_on(self): + self.pin2.value = True + self.pin3.value = True + self.exec_logger.debug(f'Switching DPS on') + time.sleep(4) + + def turn_off(self): + self.pin2.value = False + self.pin3.value = False + self.exec_logger.debug(f'Switching DPS off') + + +class RX(RxAbstract): + def __init__(self, **kwargs): + self.controller = kwargs.pop('controller', controller_module.Controller()) + super().__init__(**kwargs) + + # ADS1115 for voltage measurement (MN) + self.ads_voltage = ads.ADS1115(self.controller.bus, gain=2/3, data_rate=860, address=0x49) \ No newline at end of file diff --git a/hardware/raspberry_pi_3.py b/hardware/raspberry_pi_3.py new file mode 100644 index 0000000000000000000000000000000000000000..0f1d73703ff1437b31bd02e79019bbc9fb3d6c56 --- /dev/null +++ b/hardware/raspberry_pi_3.py @@ -0,0 +1,8 @@ +from hardware import ControllerAbstract +import board # noqa +import busio # noqa + +class Controller(ControllerAbstract): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.bus = busio.I2C(board.SCL, board.SDA) # noqa \ No newline at end of file diff --git a/mb.2024.0.0/mb.2024.0.0-backups/mb.2024.0.0-2023-02-24_151404.zip b/mb.2024.0.0/mb.2024.0.0-backups/mb.2024.0.0-2023-02-24_151404.zip deleted file mode 100644 index 6c3ce9e4f07ad6315c65606fbc3c31c90105b6a0..0000000000000000000000000000000000000000 Binary files a/mb.2024.0.0/mb.2024.0.0-backups/mb.2024.0.0-2023-02-24_151404.zip and /dev/null differ