Commit 30b7eace authored by Olivier Kaufmann's avatar Olivier Kaufmann
Browse files

Adds first attempts to refactor the code

No related merge requests found
Showing with 173 additions and 9 deletions
+173 -9
......@@ -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 = {
......
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
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
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
File deleted
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment