From 4a508731026d397e28139241b819735dab1e3a71 Mon Sep 17 00:00:00 2001 From: schoonderbeek <schoonderbeek@astron.nl> Date: Fri, 12 Jan 2024 14:20:05 +0100 Subject: [PATCH] Update the apspu scripts to work with Henri's USB-I2C device and added script for Henri. --- I2C_serial_iss.py | 70 +++++++++++++++++++++++++++++------ apspu_lib.py | 94 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 134 insertions(+), 30 deletions(-) diff --git a/I2C_serial_iss.py b/I2C_serial_iss.py index 281d24c..2df0b1d 100644 --- a/I2C_serial_iss.py +++ b/I2C_serial_iss.py @@ -10,11 +10,16 @@ sys.path.insert(0, 'c:\python34\lib\site-packages') DEBUG = False port = 'COM3' iss = UsbIss() +i2c_speed = 100 + try: iss.open(port) - iss.setup_i2c(400, True, None, None) + iss.setup_i2c(i2c_speed, True, None, None) + iss.close() except: - print("already in use") + print("Error opening the USB-I2C interface. Exit program") + sys.exit() + print("USB-I2C was already in use") class I2C: @@ -23,32 +28,75 @@ class I2C: self.I2C_Address = address self.BUS_NR = 1 # not used for Laptop but to enable Pi multiple busses. + def open_i2c(self): + port_open = False + try_cnt = 0 + while (not port_open) & (try_cnt < 3): + try: + iss.open(port) + sleep(0.1) + iss.setup_i2c(i2c_speed, True, None, None) + port_open = True + except: + iss.close() + port_open = False + return port_open + def read_bytes(self, register, bytes_to_read=2): + if not self.open_i2c(): sys.exit() ret_value = '' - read_values = iss.i2c.read(self.I2C_Address, register, bytes_to_read) - for x in read_values: - ret_value += hex(x)[2:] - ret_ack = True + try: + read_values = iss.i2c.read(self.I2C_Address, register, bytes_to_read) + for x in read_values: + ret_value += hex(x)[2:] + ret_ack = True + except: + print(f"Error reading data from 0x{self.I2C_Address:x} register 0x{register:x} nof bytes {bytes_to_read}") + ret_ack = False + ret_value = "999" + sleep(1) ret_value = ret_value #.decode("utf-8") + iss.close() return ret_ack, ret_value def read_last_reg(self, bytes_to_read): + if not self.open_i2c(): sys.exit() ret_value = [] for cnt in range(bytes_to_read): ret_value += hex(iss.i2c.read_single(self.I2C_Address))[2:] ret_value = ret_value ret_ack = 1 + iss.close() return ret_ack, ret_value def write_bytes(self, register, data): - if len([data]) < 2: - data = [data] - iss.i2c.write(self.I2C_Address, register, data) - ret_ack = 1 + ret_ack = 0 + try_cnt = 0 + while (ret_ack == 0) & (try_cnt < 3): + try_cnt += 1 + if not self.open_i2c(): sys.exit() + if type(data) != list: + data = [data] + try: + iss.i2c.write(self.I2C_Address, register, data) + ret_ack = 1 + except: + print(f"Error writing {data} to register 0x{register:x} of 0x{self.I2C_Address:x} try number: {try_cnt}") + sleep(1) + ret_ack = 0 + iss.close() return ret_ack def write_pointer(self, register): - iss.i2c.write_single(self.I2C_Address, register) + if not self.open_i2c(): sys.exit() + try: + iss.i2c.write_single(self.I2C_Address, register) + ret_ack = 1 + except: + print("Error writing data") + sleep(1) + ret_ack = 0 + iss.close() return ret_ack diff --git a/apspu_lib.py b/apspu_lib.py index 7d4fa82..77a8417 100644 --- a/apspu_lib.py +++ b/apspu_lib.py @@ -73,6 +73,7 @@ class ApspuClass: # print("--------- \nProgram Pols\n---------") for pol in self.pols: + print(f"Set Pol {pol.name} to {VOUT_POLS[pol.name]:4.2f} V") pol.set_vout_pol(VOUT_POLS[pol.name]) vout = pol.read_vout_set() if not (0.9*VOUT_POLS[pol.name] < vout < 1.1*VOUT_POLS[pol.name]): @@ -272,7 +273,9 @@ class PolClass: # ret_ack = False if self.status: - ret_ack = self.pol_dev.write_bytes(LP_WRITE_PROTECT, [0]) + ret_ack = self.pol_dev.write_bytes(LP_WRITE_PROTECT, 0) + if not ret_ack: + print("Error set Write Protect") set_value = int(value * (2**11)) hex_set_value = hex(set_value) wr_value = (hex_set_value[4:6] + hex_set_value[2:4]) @@ -282,6 +285,8 @@ class PolClass: wr_data.append(int(hex_set_value[4:6], 16)) wr_data.append(int(hex_set_value[2:4], 16)) ret_ack = self.pol_dev.write_bytes(LP_VOUT_COMMAND, wr_data) + if not ret_ack: + print("Error write data to POL") return ret_ack def set_vout_ov_limit_pol(self, value): @@ -311,8 +316,8 @@ class PolClass: # return is always True # print(f"Store to NVM for POL {self.name}") - if False: - ret_ack = self.pol_dev.write_register(0x15) + if True: + ret_ack = self.pol_dev.write_pointer(0x15) sleep(1) else: self.pol_dev.close() @@ -361,10 +366,9 @@ class PolClass: except: ret_value.append(0) self.vout = calc_lin_3bytes(ret_value, [vout_mod]) - return True else: self.vout = 999 - return False + return self.vout def read_ov_limit(self): # @@ -418,6 +422,7 @@ class PolClass: self.iout = calc_lin_2bytes(ret_value) else: self.iout = 999 + return self.iout def read_temp(self): # @@ -431,6 +436,7 @@ class PolClass: self.temp = calc_lin_2bytes(ret_value) else: self.temp = 999 + return self.temp def read_all(self): # @@ -596,25 +602,75 @@ class FanmonitorClass: print(stri) +def temperature(pol=0): + apspu = ApspuClass() + measured_temperature = apspu.pols[pol].read_temp() + print(f"Temparature of POL {apspu.pols[pol].name} is {measured_temperature:4.2f} deg C") + return measured_temperature + +def voltage(pol=0): + apspu = ApspuClass() + measured_voltage = apspu.pols[pol].read_vout() + print(f"Output voltage of POL {apspu.pols[pol].name} is {measured_voltage:4.2f} V") + return measured_voltage + +def current(pol=0): + apspu = ApspuClass() + measured_current = apspu.pols[pol].read_iout() + print(f"Output current of POL {apspu.pols[pol].name} is {measured_current:4.2f} A") + return measured_current + +def apspu_off(): + apspu = ApspuClass() + apspu.apspu_on_off(False) + print(f"All outputs off") + +def apspu_on(): + apspu = ApspuClass() + apspu.apspu_on_off(True) + print(f"All outputs on") + +def fan_speed(fan_nr): + apspu = ApspuClass() + fan_speed = apspu.fans.read_fan(fan_nr) + print(f"Fan nr {fan_nr} has speed {fan_speed}") + return fan_speed + +def program_dc_dc_converters(): + apspu = ApspuClass() + apspu.set_pols() + print("Set DC/DC converters on APSPU") + def main(): # # Function to test the class, read all info and dump on the screen # + user = "Henri" apspu = ApspuClass() -# apspu.apspu_on_off(False) -# sleep(5) -# apspu.set_pols() -# apspu.apspu_on_off(True) -# sleep(10) -# apspu.read_all() -# apspu.print_status() -# apspu.check_apspu() -# apspu.apspu_on_off(False) -# sleep(10) - apspu.read_all() - apspu.print_status() -# apspu.apspu_on_off(True) -# apspu.eeprom.wr_rd_eeprom(value="APSPU-2", address=0) + if user == "Gijs": + apspu.apspu_on_off(False) + sleep(5) + apspu.set_pols() + apspu.apspu_on_off(True) + sleep(10) + apspu.read_all() + apspu.print_status() + apspu.check_apspu() + apspu.eeprom.wr_rd_eeprom(value="APSPU-2", address=0) + elif user == "Henri": + apspu_off() + program_dc_dc_converters() + apspu_on() + for pol_cnt in range(3): + temperature(pol_cnt) + voltage(pol_cnt) + current(pol_cnt) + for fan_cnt in range(3): + fan_speed(fan_cnt) + apspu_off() + + else: + print("Ok") if __name__ == "__main__": main() -- GitLab