From 959fd9b01d7b5f472fb35eb20aaf064bcb754700 Mon Sep 17 00:00:00 2001 From: Bryce Bixler Date: Mon, 9 Oct 2023 18:43:17 +0000 Subject: [PATCH 1/8] minor bugfixes to the hwp-gripper agent --- socs/agents/hwp_gripper/agent.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/socs/agents/hwp_gripper/agent.py b/socs/agents/hwp_gripper/agent.py index 62eeaaad3..9e19c43b9 100644 --- a/socs/agents/hwp_gripper/agent.py +++ b/socs/agents/hwp_gripper/agent.py @@ -381,8 +381,8 @@ def shutdown(self, session, params=None): self.shutdown_mode = True return True, 'Shutdown completed' - def grip_hwp(self, session, params=None): - """grip_hwp() + def grip(self, session, params=None): + """grip() **Task** - Series of commands to automatically grip the HWP. This will return grippers to their home position, then move them each @@ -442,7 +442,8 @@ def run_and_append(func, *args, **kwargs): # Reset alarms. If the warm-limit is hit, the alarm will be triggered # and return_dict['result'] will be True - return_dict = run_and_append(self.client.reset, job='grip', check_shutdown=check_shutdown) + return_dict = run_and_append(self.client.reset, job='grip', + check_shutdown=check_shutdown) if return_dict['result']: # If the warm-limit is hit, move the actuator outwards bit @@ -676,9 +677,7 @@ def main(args=None): args=args) agent, runner = ocs_agent.init_site_agent(args) - gripper_agent = HWPGripperAgent(agent, mcu_ip=args.mcu_ip, - control_port=args.control_port, - supervisor_id=args.supervisor_id) + gripper_agent = HWPGripperAgent(agent, args) agent.register_task('init_connection', gripper_agent.init_connection, startup=True) agent.register_process('monitor_state', gripper_agent.monitor_state, From e8623ac716414b4108da62c87f943f8c3a93bf35 Mon Sep 17 00:00:00 2001 From: Bryce Bixler Date: Mon, 9 Oct 2023 19:33:41 +0000 Subject: [PATCH 2/8] Modified hwp-pid and hwp-pmx agents to handle connection interruptions --- socs/agents/hwp_pid/drivers/pid_controller.py | 30 +++--- socs/agents/hwp_pmx/drivers/PMX_ethernet.py | 92 +++++++++++-------- 2 files changed, 69 insertions(+), 53 deletions(-) diff --git a/socs/agents/hwp_pid/drivers/pid_controller.py b/socs/agents/hwp_pid/drivers/pid_controller.py index 283bae0c0..632d57bd8 100644 --- a/socs/agents/hwp_pid/drivers/pid_controller.py +++ b/socs/agents/hwp_pid/drivers/pid_controller.py @@ -22,11 +22,13 @@ class PID: def __init__(self, ip, port, verb=False): self.verb = verb + self.ip = ip + self.port = port self.hex_freq = '00000' self.direction = None self.target = 0 # Need to setup connection before setting direction - self.conn = self._establish_connection(ip, int(port)) + self.conn = self._establish_connection(self.ip, int(self.port)) self.set_direction('0') @staticmethod @@ -45,18 +47,15 @@ def _establish_connection(ip, port, timeout=5): """ conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + conn.settimeout(timeout) # unit tests might fail on first connection attempt - attempts = 3 - for attempt in range(attempts): + while True: try: conn.connect((ip, port)) break - except ConnectionRefusedError: + except (ConnectionRefusedError, OSError) as e: print(f"Failed to connect to device at {ip}:{port}") - print(f"Connection attempts remaining: {attempts-attempt-1}") - time.sleep(1) - conn.settimeout(timeout) - + time.sleep(5) return conn @staticmethod @@ -259,20 +258,21 @@ def send_message(self, msg): str: Respnose from the controller. """ - self.conn.sendall((msg + '\r\n').encode()) - time.sleep(0.5) # Don't send messages too quickly for attempt in range(2): try: + self.conn.sendall((msg + '\r\n').encode()) + time.sleep(0.5) # Don't send messages too quickly data = self.conn.recv(4096).decode().strip() - break - except socket.timeout: + return data + except (socket.timeout, OSError) as e: print("Caught timeout waiting for response from PID controller. " + "Trying again...") time.sleep(1) if attempt == 1: - raise RuntimeError( - 'Response from PID controller timed out.') - return data + print("Resetting connection") + self.conn.close() + self.conn = self._establish_connection(self.ip, int(self.port)) + return self.send_message(msg) def return_messages(self, msg): """Decode list of responses from PID controller and return useful diff --git a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py index ae92c1fba..fbab9d78d 100644 --- a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py +++ b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py @@ -1,5 +1,5 @@ import time -from socket import AF_INET, SOCK_STREAM, socket +import socket protection_status_key = [ 'Over voltage', @@ -21,26 +21,51 @@ class PMX: """ def __init__(self, ip, port): - self.sock = socket(AF_INET, SOCK_STREAM) - self.sock.connect((ip, port)) - self.sock.settimeout(5) - + self.ip = ip + self.port = port self.wait_time = 0.01 self.buffer_size = 128 + self.conn = self._establish_connection(self.ip, int(self.port)) + + def _establish_connection(self, ip, port, timeout=5): + conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + conn.settimeout(timeout) + while True: + try: + conn.connect((ip, port)) + break + except (ConnectionRefusedError, OSError) as e: + print(f"Failed to connect to device at {ip}:{port}") + time.sleep(5) + return conn def close(self): - self.sock.close() - - def read(self): - return self.sock.recv(self.buffer_size).decode('utf-8') + self.conn.close() + + def send_message(self, msg, read = True): + for attempt in range(2): + try: + self.conn.sendall(msg) + time.sleep(0.5) + if read: + data = self.conn.recv(self.buffer_size).decode('utf-8') + return data + return + except (socket.timeout, OSError) as e: + print("Caught timeout waiting for responce from PMX. Trying again...") + time.sleep(1) + if attempt == 1: + print("Resetting connection") + self.conn.close() + self.conn = self._establish_connection(self.ip, int(self.port)) + return self.send_message(msg, read = read) def wait(self): time.sleep(self.wait_time) def check_output(self): """ Return the output status """ - self.sock.sendall(b'output?\n') - val = int(self.read()) + val = int(self.send_message(b'output?\n')) msg = "Measured output state = " states = {0: 'OFF', 1: 'ON'} if val in states: @@ -51,8 +76,7 @@ def check_output(self): def check_error(self): """ Check oldest error from error queues. Error queues store up to 255 errors """ - self.sock.sendall(b':system:error?\n') - val = self.read() + val = self.send_message(b':system:error?\n') code, msg = val.split(',') code = int(code) msg = msg[1:-2] @@ -60,94 +84,87 @@ def check_error(self): def clear_alarm(self): """ Clear alarm """ - self.sock.sendall(b'output:protection:clear\n') + self.send_message(b'output:protection:clear\n', read = False) def turn_on(self): """ Turn the PMX on """ - self.sock.sendall(b'output 1\n') + self.send_message(b'output 1\n', read = False) self.wait() return self.check_output() def turn_off(self): """ Turn the PMX off """ - self.sock.sendall(b'output 0\n') + self.send_message(b'output 0\n', read = False) self.wait() return self.check_output() def check_current(self): """ Check the current setting """ - self.sock.sendall(b'curr?\n') - val = float(self.read()) + val = float(self.send_message(b'curr?\n')) msg = "Current setting = {:.3f} A".format(val) return msg, val def check_voltage(self): """ Check the voltage setting """ - self.sock.sendall(b'volt?\n') - val = float(self.read()) + val = float(self.send_message(b'volt?\n')) msg = "Voltage setting = {:.3f} V".format(val) return msg, val def meas_current(self): """ Measure the current """ - self.sock.sendall(b'meas:curr?\n') - val = float(self.read()) + val = float(self.send_message(b'meas:curr?\n')) msg = "Measured current = {:.3f} A".format(val) return msg, val def meas_voltage(self): """ Measure the voltage """ - self.sock.sendall(b'meas:volt?\n') - val = float(self.read()) + val = float(self.send_message(b'meas:volt?\n')) msg = "Measured voltage = {:.3f} V".format(val) return msg, val def set_current(self, curr): """ Set the current """ - self.sock.sendall(b'curr %a\n' % curr) + self.send_message(b'curr %a\n' % curr, read = False) self.wait() return self.check_current() def set_voltage(self, vol): """ Set the voltage """ - self.sock.sendall(b'volt %a\n' % vol) + self.send_message(b'volt %a\n' % vol, read = False) self.wait() return self.check_voltage() def check_source(self): """ Check the source of PMX """ - self.sock.sendall(b'volt:ext:sour?\n') - val = self.read() + val = self.send_message(b'volt:ext:sour?\n') msg = "Source: " + val return msg, val def use_external_voltage(self): """ Set PMX to use external voltage """ - self.sock.sendall(b'volt:ext:sour volt\n') + self.send_message(b'volt:ext:sour volt\n', read = False) self.wait() return self.check_source() def ign_external_voltage(self): """ Set PMX to ignore external voltage """ - self.sock.sendall(b'volt:ext:sour none\n') + self.send_message(b'volt:ext:sour none\n', read = False) self.wait() return self.check_source() def set_current_limit(self, curr_lim): """ Set the PMX current limit """ - self.sock.sendall(b'curr:prot %a\n' % curr_lim) + self.send_message(b'curr:prot %a\n' % curr_lim) self.wait() - self.sock.sendall(b'curr:prot?\n') - val = float(self.read()) + val = float(self.send_message(b'curr:prot?\n')) msg = "Current Limit: {:.3f} A".format(val) return msg def set_voltage_limit(self, vol_lim): """ Set the PMX voltage limit """ - self.sock.sendall(b'volt:prot %a\n' % vol_lim) + self.send_message(b'volt:prot %a\n' % vol_lim, read = False) self.wait() - self.sock.sendall(b'volt:prot?\n') - val = float(self.read()) + val = float(self.send_message(b'volt:prot?\n')) msg = "Voltage Limit: {:.3f} V".format(val) return msg @@ -156,8 +173,7 @@ def check_prot(self): Return: val (int): protection status code """ - self.sock.sendall(b'stat:ques?\n') - val = int(self.read()) + val = int(self.send_message(b'stat:ques?\n')) return val def get_prot_msg(self, val): From a50ca058198a598cf8bb156617bd12bd51f1c822 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 22:12:42 -0400 Subject: [PATCH 3/8] [pre-commit.ci] pre-commit autoupdate (#540) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/pre-commit-hooks: v4.4.0 → v4.5.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.4.0...v4.5.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 36a40a34e..c91a0afb3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-ast - id: check-yaml From fa26efa881e25498490bd64240281e71b4ea2ca9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 13:40:01 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- socs/agents/hwp_gripper/agent.py | 2 +- socs/agents/hwp_pmx/drivers/PMX_ethernet.py | 22 ++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/socs/agents/hwp_gripper/agent.py b/socs/agents/hwp_gripper/agent.py index 9e19c43b9..c0207ce65 100644 --- a/socs/agents/hwp_gripper/agent.py +++ b/socs/agents/hwp_gripper/agent.py @@ -442,7 +442,7 @@ def run_and_append(func, *args, **kwargs): # Reset alarms. If the warm-limit is hit, the alarm will be triggered # and return_dict['result'] will be True - return_dict = run_and_append(self.client.reset, job='grip', + return_dict = run_and_append(self.client.reset, job='grip', check_shutdown=check_shutdown) if return_dict['result']: diff --git a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py index fbab9d78d..8fe870b27 100644 --- a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py +++ b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py @@ -1,5 +1,5 @@ -import time import socket +import time protection_status_key = [ 'Over voltage', @@ -42,7 +42,7 @@ def _establish_connection(self, ip, port, timeout=5): def close(self): self.conn.close() - def send_message(self, msg, read = True): + def send_message(self, msg, read=True): for attempt in range(2): try: self.conn.sendall(msg) @@ -58,7 +58,7 @@ def send_message(self, msg, read = True): print("Resetting connection") self.conn.close() self.conn = self._establish_connection(self.ip, int(self.port)) - return self.send_message(msg, read = read) + return self.send_message(msg, read=read) def wait(self): time.sleep(self.wait_time) @@ -84,17 +84,17 @@ def check_error(self): def clear_alarm(self): """ Clear alarm """ - self.send_message(b'output:protection:clear\n', read = False) + self.send_message(b'output:protection:clear\n', read=False) def turn_on(self): """ Turn the PMX on """ - self.send_message(b'output 1\n', read = False) + self.send_message(b'output 1\n', read=False) self.wait() return self.check_output() def turn_off(self): """ Turn the PMX off """ - self.send_message(b'output 0\n', read = False) + self.send_message(b'output 0\n', read=False) self.wait() return self.check_output() @@ -124,13 +124,13 @@ def meas_voltage(self): def set_current(self, curr): """ Set the current """ - self.send_message(b'curr %a\n' % curr, read = False) + self.send_message(b'curr %a\n' % curr, read=False) self.wait() return self.check_current() def set_voltage(self, vol): """ Set the voltage """ - self.send_message(b'volt %a\n' % vol, read = False) + self.send_message(b'volt %a\n' % vol, read=False) self.wait() return self.check_voltage() @@ -142,13 +142,13 @@ def check_source(self): def use_external_voltage(self): """ Set PMX to use external voltage """ - self.send_message(b'volt:ext:sour volt\n', read = False) + self.send_message(b'volt:ext:sour volt\n', read=False) self.wait() return self.check_source() def ign_external_voltage(self): """ Set PMX to ignore external voltage """ - self.send_message(b'volt:ext:sour none\n', read = False) + self.send_message(b'volt:ext:sour none\n', read=False) self.wait() return self.check_source() @@ -162,7 +162,7 @@ def set_current_limit(self, curr_lim): def set_voltage_limit(self, vol_lim): """ Set the PMX voltage limit """ - self.send_message(b'volt:prot %a\n' % vol_lim, read = False) + self.send_message(b'volt:prot %a\n' % vol_lim, read=False) self.wait() val = float(self.send_message(b'volt:prot?\n')) msg = "Voltage Limit: {:.3f} V".format(val) From f939874db8b234313b9eee4ba74cd0737fbe479f Mon Sep 17 00:00:00 2001 From: Bryce Bixler Date: Fri, 17 Nov 2023 22:25:02 +0000 Subject: [PATCH 5/8] Changed connection protocal from an infinite loop into a set number of iterations. Added reconnection functionality into the agent acq process --- socs/agents/hwp_pid/agent.py | 18 ++++++---- socs/agents/hwp_pid/drivers/pid_controller.py | 5 ++- socs/agents/hwp_pmx/agent.py | 35 +++++++++++-------- socs/agents/hwp_pmx/drivers/PMX_ethernet.py | 5 ++- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/socs/agents/hwp_pid/agent.py b/socs/agents/hwp_pid/agent.py index 2c530f352..33e129077 100644 --- a/socs/agents/hwp_pid/agent.py +++ b/socs/agents/hwp_pid/agent.py @@ -295,13 +295,17 @@ def acq(self, session, params): data = {'timestamp': time.time(), 'block_name': 'HWPPID', 'data': {}} - current_freq = self.pid.get_freq() - target_freq = self.pid.get_target() - direction = self.pid.get_direction() - - data['data']['current_freq'] = current_freq - data['data']['target_freq'] = target_freq - data['data']['direction'] = direction + try: + current_freq = self.pid.get_freq() + target_freq = self.pid.get_target() + direction = self.pid.get_direction() + + data['data']['current_freq'] = current_freq + data['data']['target_freq'] = target_freq + data['data']['direction'] = direction + except: + time.sleep(1) + continue self.agent.publish_to_feed('hwppid', data) diff --git a/socs/agents/hwp_pid/drivers/pid_controller.py b/socs/agents/hwp_pid/drivers/pid_controller.py index 632d57bd8..cd3ac6361 100644 --- a/socs/agents/hwp_pid/drivers/pid_controller.py +++ b/socs/agents/hwp_pid/drivers/pid_controller.py @@ -49,13 +49,16 @@ def _establish_connection(ip, port, timeout=5): conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.settimeout(timeout) # unit tests might fail on first connection attempt - while True: + attempts = 3 + for attempt in range(attempts): try: conn.connect((ip, port)) break except (ConnectionRefusedError, OSError) as e: print(f"Failed to connect to device at {ip}:{port}") time.sleep(5) + else: + raise RuntimeError('Could not connect to PID controller') return conn @staticmethod diff --git a/socs/agents/hwp_pmx/agent.py b/socs/agents/hwp_pmx/agent.py index 3694dbb4c..3ed911241 100644 --- a/socs/agents/hwp_pmx/agent.py +++ b/socs/agents/hwp_pmx/agent.py @@ -281,26 +281,31 @@ def acq(self, session, params): 'block_name': 'hwppmx', 'data': {} } - msg, curr = self.dev.meas_current() - data['data']['current'] = curr - msg, volt = self.dev.meas_voltage() - data['data']['voltage'] = volt + try: + msg, curr = self.dev.meas_current() + data['data']['current'] = curr - msg, code = self.dev.check_error() - data['data']['err_code'] = code - data['data']['err_msg'] = msg + msg, volt = self.dev.meas_voltage() + data['data']['voltage'] = volt - prot_code = self.dev.check_prot() - if prot_code != 0: - self.prot = prot_code + msg, code = self.dev.check_error() + data['data']['err_code'] = code + data['data']['err_msg'] = msg - prot_msg = self.dev.get_prot_msg(self.prot) - data['data']['prot_code'] = self.prot - data['data']['prot_msg'] = prot_msg + prot_code = self.dev.check_prot() + if prot_code != 0: + self.prot = prot_code - msg, src = self.dev.check_source() - data['data']['source'] = src + prot_msg = self.dev.get_prot_msg(self.prot) + data['data']['prot_code'] = self.prot + data['data']['prot_msg'] = prot_msg + + msg, src = self.dev.check_source() + data['data']['source'] = src + except: + time.sleep(sleep_time) + continue self.agent.publish_to_feed('hwppmx', data) session.data = {'curr': curr, diff --git a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py index 8fe870b27..62925293a 100644 --- a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py +++ b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py @@ -30,13 +30,16 @@ def __init__(self, ip, port): def _establish_connection(self, ip, port, timeout=5): conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.settimeout(timeout) - while True: + attempts = 3 + for attempt in range(attempts): try: conn.connect((ip, port)) break except (ConnectionRefusedError, OSError) as e: print(f"Failed to connect to device at {ip}:{port}") time.sleep(5) + else: + raise RuntimeError('Could not connect to PID controller') return conn def close(self): From f7cc7e4f119ed9542db8d5f401075e3769c02b7c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 17 Nov 2023 22:27:02 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- socs/agents/hwp_pid/agent.py | 2 +- socs/agents/hwp_pmx/agent.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/socs/agents/hwp_pid/agent.py b/socs/agents/hwp_pid/agent.py index 33e129077..b8a871a61 100644 --- a/socs/agents/hwp_pid/agent.py +++ b/socs/agents/hwp_pid/agent.py @@ -303,7 +303,7 @@ def acq(self, session, params): data['data']['current_freq'] = current_freq data['data']['target_freq'] = target_freq data['data']['direction'] = direction - except: + except BaseException: time.sleep(1) continue diff --git a/socs/agents/hwp_pmx/agent.py b/socs/agents/hwp_pmx/agent.py index 3ed911241..76b41d9ff 100644 --- a/socs/agents/hwp_pmx/agent.py +++ b/socs/agents/hwp_pmx/agent.py @@ -303,7 +303,7 @@ def acq(self, session, params): msg, src = self.dev.check_source() data['data']['source'] = src - except: + except BaseException: time.sleep(sleep_time) continue From 0735a5982f1684cf648797ebf6a39b2f20c617bc Mon Sep 17 00:00:00 2001 From: Bryce Bixler Date: Mon, 20 Nov 2023 22:49:57 +0000 Subject: [PATCH 7/8] Fixes to pre-commit failures --- socs/agents/hwp_pid/drivers/pid_controller.py | 7 +++---- socs/agents/hwp_pmx/drivers/PMX_ethernet.py | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/socs/agents/hwp_pid/drivers/pid_controller.py b/socs/agents/hwp_pid/drivers/pid_controller.py index cd3ac6361..60d7c874a 100644 --- a/socs/agents/hwp_pid/drivers/pid_controller.py +++ b/socs/agents/hwp_pid/drivers/pid_controller.py @@ -32,7 +32,7 @@ def __init__(self, ip, port, verb=False): self.set_direction('0') @staticmethod - def _establish_connection(ip, port, timeout=5): + def _establish_connection(ip, port, timeout=2): """Connect to PID controller. Args: @@ -54,9 +54,8 @@ def _establish_connection(ip, port, timeout=5): try: conn.connect((ip, port)) break - except (ConnectionRefusedError, OSError) as e: + except (ConnectionRefusedError, OSError): print(f"Failed to connect to device at {ip}:{port}") - time.sleep(5) else: raise RuntimeError('Could not connect to PID controller') return conn @@ -267,7 +266,7 @@ def send_message(self, msg): time.sleep(0.5) # Don't send messages too quickly data = self.conn.recv(4096).decode().strip() return data - except (socket.timeout, OSError) as e: + except (socket.timeout, OSError): print("Caught timeout waiting for response from PID controller. " + "Trying again...") time.sleep(1) diff --git a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py index 62925293a..e4df3c523 100644 --- a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py +++ b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py @@ -27,7 +27,7 @@ def __init__(self, ip, port): self.buffer_size = 128 self.conn = self._establish_connection(self.ip, int(self.port)) - def _establish_connection(self, ip, port, timeout=5): + def _establish_connection(self, ip, port, timeout=2): conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.settimeout(timeout) attempts = 3 @@ -35,9 +35,8 @@ def _establish_connection(self, ip, port, timeout=5): try: conn.connect((ip, port)) break - except (ConnectionRefusedError, OSError) as e: + except (ConnectionRefusedError, OSError): print(f"Failed to connect to device at {ip}:{port}") - time.sleep(5) else: raise RuntimeError('Could not connect to PID controller') return conn @@ -54,7 +53,7 @@ def send_message(self, msg, read=True): data = self.conn.recv(self.buffer_size).decode('utf-8') return data return - except (socket.timeout, OSError) as e: + except (socket.timeout, OSError): print("Caught timeout waiting for responce from PMX. Trying again...") time.sleep(1) if attempt == 1: From a7f009fc19d32fb78e544bac1ed6f86465b1dd2d Mon Sep 17 00:00:00 2001 From: Bryce Bixler Date: Tue, 21 Nov 2023 21:42:37 +0000 Subject: [PATCH 8/8] Added read=False to set_current_limit meathod --- socs/agents/hwp_pmx/drivers/PMX_ethernet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py index e4df3c523..872719c31 100644 --- a/socs/agents/hwp_pmx/drivers/PMX_ethernet.py +++ b/socs/agents/hwp_pmx/drivers/PMX_ethernet.py @@ -156,7 +156,7 @@ def ign_external_voltage(self): def set_current_limit(self, curr_lim): """ Set the PMX current limit """ - self.send_message(b'curr:prot %a\n' % curr_lim) + self.send_message(b'curr:prot %a\n' % curr_lim, read=False) self.wait() val = float(self.send_message(b'curr:prot?\n')) msg = "Current Limit: {:.3f} A".format(val)