From 6c4ab2a063d3ee6d459d558e0097b035ff7b3271 Mon Sep 17 00:00:00 2001 From: su530201 <olivier.kaufmann@umons.ac.be> Date: Sat, 15 Apr 2023 17:45:42 +0200 Subject: [PATCH] Updates _vab_pulse --- config.py | 45 ++++------------ config_default.py | 106 ++++++++++++++++++++++++++++++++++++ config_mb_2024_rev_0_0.py | 13 +---- config_ohmpi_card_3_15.py | 110 ++++++++++++++++++++++++++++++++++++++ measure.py | 10 ++-- test_ohmpi_card_3_15.py | 8 +++ utils.py | 13 ++++- 7 files changed, 252 insertions(+), 53 deletions(-) create mode 100644 config_default.py create mode 100644 config_ohmpi_card_3_15.py diff --git a/config.py b/config.py index 673d71bd..021481c4 100644 --- a/config.py +++ b/config.py @@ -14,54 +14,29 @@ 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 'settings': 'ohmpi_settings.json', # INSERT YOUR FAVORITE SETTINGS FILE HERE - # '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 = { +HARDWARE_CONFIG = { 'controller': {'model' : 'raspberry_pi_3' }, - 'tx' : {'model' : 'mb_2024_rev_0_0', + 'tx' : {'model' : 'ohmpi_card_3_15', 'mcp_board_address': 0x20, - 'Imax': 4800 / 50 / 2, # Maximum current - 'R_shunt': 2 # Shunt resistance in Ohms + 'current_max': 4800 / 50 / 2, # Maximum current + 'r_shunt': 2 # Shunt resistance in Ohms }, - 'rx' : {'model': 'mb_2024_rev_0_0', + 'rx' : {'model': 'ohmpi_card_3_15', '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', + 'mux': {'model' : 'ohmpi_i2c_mux64_v1.01', '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... - } -}""" - -HARDWARE_CONFIG = { - 'controller': {'model' : 'dummy_controller' - }, - 'tx' : {'model' : 'dummy_tx', - 'current_max': 4800 / 50 / 2, # Maximum current mA - 'r_shunt': 2, # Shunt resistance in Ohms - 'low_battery': 12. # Volts - }, - 'rx' : {'model': 'dummy_rx', - }, - 'mux': {'model' : 'dummy_mux', - 'max_elec': 64, - 'voltage_max' : 100, - 'current_max' : 3 + 'voltage_max': 100, + 'current_max': 3 } } + # SET THE LOGGING LEVELS, MQTT BROKERS AND MQTT OPTIONS ACCORDING TO YOUR NEEDS # Execution logging configuration EXEC_LOGGING_CONFIG = { diff --git a/config_default.py b/config_default.py new file mode 100644 index 00000000..df5f5927 --- /dev/null +++ b/config_default.py @@ -0,0 +1,106 @@ +import logging +from OhmPi.utils import get_platform + +from paho.mqtt.client import MQTTv31 + +_, on_pi = get_platform() +# DEFINE THE ID OF YOUR OhmPi +ohmpi_id = '0001' if on_pi else 'XXXX' +# DEFINE YOUR MQTT BROKER (DEFAULT: 'localhost') +mqtt_broker = 'localhost' if on_pi else 'NAME_YOUR_BROKER_WHEN_IN_SIMULATION_MODE_HERE' +# DEFINE THE SUFFIX TO ADD TO YOUR LOGS FILES +logging_suffix = '' + +# OhmPi configuration +OHMPI_CONFIG = { + 'id': ohmpi_id, # Unique identifier of the OhmPi board (string) + 'settings': 'ohmpi_settings.json', # INSERT YOUR FAVORITE SETTINGS FILE HERE +} + +HARDWARE_CONFIG = { + 'controller': {'model' : 'dummy_controller' + }, + 'tx' : {'model' : 'dummy_tx', + 'current_max': 4800 / 50 / 2, # Maximum current mA + 'r_shunt': 2, # Shunt resistance in Ohms + 'low_battery': 12. # Volts + }, + 'rx' : {'model': 'dummy_rx', + }, + 'mux': {'model' : 'dummy_mux', + 'max_elec': 64, + 'voltage_max' : 100, + 'current_max' : 3 + } +} +# SET THE LOGGING LEVELS, MQTT BROKERS AND MQTT OPTIONS ACCORDING TO YOUR NEEDS +# Execution logging configuration +EXEC_LOGGING_CONFIG = { + 'logging_level': logging.INFO, + 'log_file_logging_level': logging.DEBUG, + 'logging_to_console': True, + 'file_name': f'exec{logging_suffix}.log', + 'max_bytes': 262144, + 'backup_count': 30, + 'when': 'd', + 'interval': 1 +} + +# Data logging configuration +DATA_LOGGING_CONFIG = { + 'logging_level': logging.INFO, + 'logging_to_console': True, + 'file_name': f'data{logging_suffix}.log', + 'max_bytes': 16777216, + 'backup_count': 1024, + 'when': 'd', + 'interval': 1 +} + +# State of Health logging configuration (For a future release) +SOH_LOGGING_CONFIG = { + 'logging_level': logging.INFO, + 'logging_to_console': True, + 'file_name': f'soh{logging_suffix}.log', + 'max_bytes': 16777216, + 'backup_count': 1024, + 'when': 'd', + 'interval': 1 +} + +# MQTT logging configuration parameters +MQTT_LOGGING_CONFIG = { + 'hostname': mqtt_broker, + 'port': 1883, + 'qos': 2, + 'retain': False, + 'keepalive': 60, + 'will': None, + 'auth': {'username': 'mqtt_user', 'password': 'mqtt_password'}, + 'tls': None, + 'protocol': MQTTv31, + 'transport': 'tcp', + 'client_id': f'{OHMPI_CONFIG["id"]}', + 'exec_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/exec', + 'exec_logging_level': logging.DEBUG, + 'data_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/data', + 'data_logging_level': DATA_LOGGING_CONFIG['logging_level'], + 'soh_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/soh', + 'soh_logging_level': SOH_LOGGING_CONFIG['logging_level'] +} + +# MQTT control configuration parameters +MQTT_CONTROL_CONFIG = { + 'hostname': mqtt_broker, + 'port': 1883, + 'qos': 2, + 'retain': False, + 'keepalive': 60, + 'will': None, + 'auth': {'username': 'mqtt_user', 'password': 'mqtt_password'}, + 'tls': None, + 'protocol': MQTTv31, + 'transport': 'tcp', + 'client_id': f'{OHMPI_CONFIG["id"]}', + 'ctrl_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/ctrl' +} diff --git a/config_mb_2024_rev_0_0.py b/config_mb_2024_rev_0_0.py index ab77038c..90b0f0a4 100644 --- a/config_mb_2024_rev_0_0.py +++ b/config_mb_2024_rev_0_0.py @@ -14,17 +14,8 @@ 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 'settings': 'ohmpi_settings.json', # INSERT YOUR FAVORITE SETTINGS FILE HERE - # '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' @@ -32,7 +23,7 @@ HARDWARE_CONFIG = { 'tx' : {'model' : 'mb_2024_rev_0_0', 'mcp_board_address': 0x20, 'current_max': 4800 / 50 / 2, # Maximum current - 'R_shunt': 2 # Shunt resistance in Ohms + '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 diff --git a/config_ohmpi_card_3_15.py b/config_ohmpi_card_3_15.py new file mode 100644 index 00000000..021481c4 --- /dev/null +++ b/config_ohmpi_card_3_15.py @@ -0,0 +1,110 @@ +import logging +from OhmPi.utils import get_platform + +from paho.mqtt.client import MQTTv31 + +_, on_pi = get_platform() +# DEFINE THE ID OF YOUR OhmPi +ohmpi_id = '0001' if on_pi else 'XXXX' +# DEFINE YOUR MQTT BROKER (DEFAULT: 'localhost') +mqtt_broker = 'localhost' if on_pi else 'NAME_YOUR_BROKER_WHEN_IN_SIMULATION_MODE_HERE' +# DEFINE THE SUFFIX TO ADD TO YOUR LOGS FILES +logging_suffix = '' + +# OhmPi configuration +OHMPI_CONFIG = { + 'id': ohmpi_id, # Unique identifier of the OhmPi board (string) + 'settings': 'ohmpi_settings.json', # INSERT YOUR FAVORITE SETTINGS FILE HERE +} + +HARDWARE_CONFIG = { + 'controller': {'model' : 'raspberry_pi_3' + }, + 'tx' : {'model' : 'ohmpi_card_3_15', + 'mcp_board_address': 0x20, + 'current_max': 4800 / 50 / 2, # Maximum current + 'r_shunt': 2 # Shunt resistance in Ohms + }, + 'rx' : {'model': 'ohmpi_card_3_15', + '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' : 'ohmpi_i2c_mux64_v1.01', + 'max_elec': 64, + 'board_addresses': {'A': 0x73, 'B': 0x72, 'M': 0x71, 'N': 0x70}, # CHECK IF YOUR BOARDS HAVE THESE ADDRESSES + 'voltage_max': 100, + 'current_max': 3 + } +} + +# SET THE LOGGING LEVELS, MQTT BROKERS AND MQTT OPTIONS ACCORDING TO YOUR NEEDS +# Execution logging configuration +EXEC_LOGGING_CONFIG = { + 'logging_level': logging.INFO, + 'log_file_logging_level': logging.DEBUG, + 'logging_to_console': True, + 'file_name': f'exec{logging_suffix}.log', + 'max_bytes': 262144, + 'backup_count': 30, + 'when': 'd', + 'interval': 1 +} + +# Data logging configuration +DATA_LOGGING_CONFIG = { + 'logging_level': logging.INFO, + 'logging_to_console': True, + 'file_name': f'data{logging_suffix}.log', + 'max_bytes': 16777216, + 'backup_count': 1024, + 'when': 'd', + 'interval': 1 +} + +# State of Health logging configuration (For a future release) +SOH_LOGGING_CONFIG = { + 'logging_level': logging.INFO, + 'logging_to_console': True, + 'file_name': f'soh{logging_suffix}.log', + 'max_bytes': 16777216, + 'backup_count': 1024, + 'when': 'd', + 'interval': 1 +} + +# MQTT logging configuration parameters +MQTT_LOGGING_CONFIG = { + 'hostname': mqtt_broker, + 'port': 1883, + 'qos': 2, + 'retain': False, + 'keepalive': 60, + 'will': None, + 'auth': {'username': 'mqtt_user', 'password': 'mqtt_password'}, + 'tls': None, + 'protocol': MQTTv31, + 'transport': 'tcp', + 'client_id': f'{OHMPI_CONFIG["id"]}', + 'exec_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/exec', + 'exec_logging_level': logging.DEBUG, + 'data_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/data', + 'data_logging_level': DATA_LOGGING_CONFIG['logging_level'], + 'soh_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/soh', + 'soh_logging_level': SOH_LOGGING_CONFIG['logging_level'] +} + +# MQTT control configuration parameters +MQTT_CONTROL_CONFIG = { + 'hostname': mqtt_broker, + 'port': 1883, + 'qos': 2, + 'retain': False, + 'keepalive': 60, + 'will': None, + 'auth': {'username': 'mqtt_user', 'password': 'mqtt_password'}, + 'tls': None, + 'protocol': MQTTv31, + 'transport': 'tcp', + 'client_id': f'{OHMPI_CONFIG["id"]}', + 'ctrl_topic': f'ohmpi_{OHMPI_CONFIG["id"]}/ctrl' +} diff --git a/measure.py b/measure.py index 6013743a..bc3ef817 100644 --- a/measure.py +++ b/measure.py @@ -48,18 +48,18 @@ class OhmPiHardware: def _vab_pulse(self, vab, length, polarity=None): """ Gets VMN and IAB from a single voltage pulse """ - def inject(length): + def inject(duration): self.tx_sync.set() - self.tx.voltage_pulse(length=length) + self.tx.voltage_pulse(length=duration) self.tx_sync.clear() def read_values(): - readings = [] + _readings = [] self.tx_sync.wait() start_time = time.gmtime() while self.tx_sync.is_set(): - readings.append([time.gmtime() - start_time, self.tx.current, self.rx.voltage]) - return np.array(readings) + _readings.append([time.gmtime() - start_time, self.tx.current, self.rx.voltage]) + return np.array(_readings) if polarity is not None and polarity != self.tx.polarity: self.tx.polarity = polarity diff --git a/test_ohmpi_card_3_15.py b/test_ohmpi_card_3_15.py index bbceaad4..e4e10683 100644 --- a/test_ohmpi_card_3_15.py +++ b/test_ohmpi_card_3_15.py @@ -28,3 +28,11 @@ r = np.array(r) print(f'Mean resistance: {np.mean(r):.3f} Ohms') print(f'Resistance std: {np.std(r):.3f} Ohms') print(f'Dev. {100. * np.std(r)/np.mean(r):.1} %') + +from OhmPi.measure import OhmPiHardware +from utils import change_config + +change_config('config_ohmpi_card_3_15.py') +k = OhmPiHardware() +k._vab_pulse(vab=12, length=2., polarity=1) +change_config('config_default.py') \ No newline at end of file diff --git a/utils.py b/utils.py index 6f559632..86e740b9 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,6 @@ import io - +import os +import shutil def get_platform(): """Gets platform name and checks if it is a raspberry pi @@ -17,4 +18,12 @@ def get_platform(): on_pi = True except FileNotFoundError: pass - return platform, on_pi \ No newline at end of file + return platform, on_pi + +def change_config(config_file): + cwd = os.getcwd() + try: + shutil.copyfile(f'{cwd}/OhmPi/config.py', f'{cwd}/OhmPi/config_tmp.py') + shutil.copyfile(f'{cwd}/OhmPi/{config_file}', f'{cwd}/OhmPi/config.py') + except Exception as error: + print(f'Could not change config file to {cwd}/OhmPi/{config_file}:\n{error}') \ No newline at end of file -- GitLab