diff --git a/ohmpi/hardware_components/pwr_dps5005.py b/ohmpi/hardware_components/pwr_dps5005.py new file mode 100644 index 0000000000000000000000000000000000000000..0ba8bfa86bbe9914bc04669909f5687ba00bf1ce --- /dev/null +++ b/ohmpi/hardware_components/pwr_dps5005.py @@ -0,0 +1,53 @@ +from ohmpi.hardware_components.abstract_hardware_components import PwrAbstract +from ohmpi.config import HARDWARE_CONFIG +import importlib +import numpy as np +import os + +ctl_name = HARDWARE_CONFIG['ctl'].pop('board_name', 'raspberry_pi_modbus') +ctl_module = importlib.import_module(f'ohmpi.hardware_components.{ctl_name}') + +class Pwr(PwrAbstract): + def __init__(self, **kwargs): + kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')}) + voltage = kwargs.pop('voltage', 12.) + super().__init__(**kwargs) + if self.ctl is None: + self.ctl = ctl_module.Ctl() + self.ctl.bus.configure_modbus() + self.voltage_adjustable = True + self._voltage = voltage + self._current_adjustable = False + self._current = np.nan + self._current_max = kwargs.pop('current_max', 100.) + + @property + def current(self): + return self._current + + @current.setter + def current(self, value, **kwargs): + self.exec_logger.debug(f'Current cannot be set on {self.board_name}') + + def turn_off(self): + self.ctl.bus.write_register(0x09, 1) + self.exec_logger.debug(f'{self.board_name} is off') + + def turn_on(self): + self.ctl.bus.write_register(0x09, 1) + self.exec_logger.debug(f'{self.board_name} is on') + + @property + def voltage(self): + return PwrAbstract.voltage.fget(self) + + @voltage.setter + def voltage(self, value): + self.ctl.bus.write_register(0x0000, value, 2) + + def battery_voltage(self): + self.ctl.bus.read_register(0x05, 2) + + @property + def current_max(self,value): + self.ctl.bus.write_register(0x0001, value * 10, 0) diff --git a/ohmpi/hardware_components/raspberry_pi_modbus.py b/ohmpi/hardware_components/raspberry_pi_modbus.py new file mode 100644 index 0000000000000000000000000000000000000000..7bfc0ddd561d525f17797e8399b28f5381a3f6fb --- /dev/null +++ b/ohmpi/hardware_components/raspberry_pi_modbus.py @@ -0,0 +1,33 @@ +from ohmpi.hardware_components import CtlAbstract +import board # noqa +import busio # noqa +import os +from ohmpi.utils import get_platform +from gpiozero import CPUTemperature # noqa +import minimalmodbus # noqa + + +class Ctl(CtlAbstract): + def __init__(self, **kwargs): + kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')}) + super().__init__(**kwargs) + self.bus = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1) # port name, address (decimal) + self.configure_modbus() # TODO: check if this works on init + platform, on_pi = get_platform() + assert on_pi + self.board_name = platform + self._cpu_temp_available = True + self.max_cpu_temp = 85. # °C + + @property + def _cpu_temp(self): + return CPUTemperature().temperature + + def configure_modbus(self, baudrate=9600, bitesize=8, timeout=1, debug=False, parity='N', + mode=minimalmodbus.MODE_RTU): + self.bus.serial.baudrate = baudrate # Baud rate 9600 as listed in doc + self.bus.serial.bytesize = bitesize # + self.bus.serial.timeout = timeout # greater than 0.5 for it to work + self.bus.debug = debug # + self.bus.serial.parity = parity # No parity + self.bus.mode = mode # RTU mode \ No newline at end of file